How to take the screenshots of Javascript alert

Hi Andrej, Mate Mrse,Kazurayam, Marek Melocik, RUSS … How to take the screenshots of alert Javascript popup so that it should print on the generated katlon html report. If anyone could help then i will be very thankful. Its really urgent.

WebUI.openBrowser('')
'Use Javascript to make an alert'WebUI.executeJavaScript('alert("I am the Javascript ... Katalon can you take screenshot of me and place it in HTML Reports?")', [])
WebUI.delay(2)
WebUI.takeScreenshot()

I am getting the below error ...07-22-2018 08:16:29 PM - [WARNING] - Cannot take screenshot07-22-2018 08:16:29 PM - [FAILED] - Cannot take screenshot (Root cause: org.openqa.selenium.UnhandledAlertException: unexpected alert open: {Alert text : I am the Javascript ... Katalon can you take screenshot of me and place it in HTML Reports?}  (Session info: chrome=67.0.3396.99)  (Driver info: chromedriver=2.35.528161 (5b82f2d2aae0ca24b877009200ced9065a772e73),platform=Windows NT 10.0.15063 x86_64) (WARNING: The server did not provide any stacktrace information)Command duration or timeout: 0 milliseconds: null

hi disover,
detail on popus and selenium diver is elaborated here:
https://huddle.eurostarsoftwaretesting.com/how-to-selenium-popups-with-selenium/
from that metrial i quote:

JavaScript popups are not part of the website that is being rendered, they are instead rendered by the browser. As a result they don’t show up in screenshots taken by selenium.

To all intents and purposes it looks like we have found a bug with the website implementation

You could simulate taking a screenshot using the robot class:

import java.awt.Robot as Robot
import java.awt.event.KeyEvent as KeyEvent
Robot rb = new Robot();
rb.keyPress(KeyEvent.VK_WINDOWS);
rb.keyPress(KeyEvent.VK_PRINTSCREEN);
rb.keyRelease(KeyEvent.VK_PRINTSCREEN);
rb.keyRelease(KeyEvent.VK_WINDOWS);
2 Likes

Thanks Andrej and Mate for your reply.

Hi Mate … How to place the screenshot taken via Robot class in HTML Report?

Kindly help …

Did you try something like this?

WebUI.waitForAlert(10)
WebUI.takeScreenshot()
WebUI.acceptAlert()

Your problem might be that the call to JavaScript returns immediately (before the alert appears) and the code running in the webdriver context doesn’t know it needs to wait or even handle it. You have basically confounded the cooperation mechanism between the two execution contexts (webdriver and browser).

As for your other question, there is no API I’m aware of that lets you interface directly to an executing Test Report (other than the obvious commenting and result(s) mechanisms). But I could be wrong - I frequently am B)

i did small test:
wait for alert, switch to it’s window and take screen shot (in FF it’s possible) but image is blank

and btw. Russ, it was not your turn, you should wait for kazurayam and Marek Melocik to go first :wink:

Andrej: Yes, I saw that. But my name was in ALL CAPS so I figured I must be much more important B)

The reality is: I’m a novice with thirty+ years of humbling mistakes and errors behind me :’(

3 Likes

let him who is without mistake cast the first nullPointerException
o:)

1 Like

LOL!!!

Russ Thomas said:

Did you try something like this?

WebUI.waitForAlert(10)

WebUI.takeScreenshot()
WebUI.acceptAlert()


  
Your problem might be that the call to JavaScript returns immediately (before the alert appears) and the code running in the webdriver context doesn't know it needs to wait or even handle it.  You have basically confounded the cooperation mechanism between the two execution contexts (webdriver and browser).  
  
As for your other question, there is no API I'm aware of that lets you interface directly to an executing Test Report (other than the obvious commenting and result(s) mechanisms).  But I could be wrong - I frequently am B)  
  
  

  

Hi Russ, Your suggestion didn’t work out. :frowning: Still facing the same issue.

Hi Guys … Got a dirty work around to take screenshot of Alert message using KS… at the same time be a part of HTML report. Execute the below code to see it, there is no Object Repo involved … so copy paste and execute it.

import java.awt.Rectangle as Rectangle
import java.awt.Robot as Robot
import java.awt.Toolkit as Toolkit
import java.awt.image.BufferedImage as BufferedImage
import javax.imageio.ImageIO as ImageIO
import com.kms.katalon.core.webui.keyword.WebUiBuiltInKeywords as WebUI

‘Run the Test from TestSuite’
WebUI.openBrowser(‘http://www.google.com’)
WebUI.maximizeWindow()

String path = WebUI.takeScreenshot()

‘This line will print the path and name of the screenshot file.’
System.err.println(path)

WebUI.delay(2)

‘Use Javascript to make an alert’
WebUI.executeJavaScript(‘alert(\‘This is an alert … :slight_smile: \’)’, null)

‘Capture the alert’
BufferedImage image = new Robot().createScreenCapture(new Rectangle(Toolkit.getDefaultToolkit().getScreenSize()))

ImageIO.write(image, ‘png’, new File(path))

WebUI.delay(2)

Alert Screenshot.jpg

6 Likes

Thanks a lot discover.selenium. Finally I got a solution for this.

Thanks discover.selenium … This works for me. :smiley:

Hi Katalon Team … Kindly include the screenshot feature of Javascript alert in html report in future release without any work around :wink: .

Once again thanks to Andrej, Mate Mrse and RUSS for your prompt reply … :slight_smile:

2 Likes

Finally working.
Thanks, discover.selenium…