Issue with taking screenshot while the jscript dialog box is opened

1,071 views
Skip to first unread message

nimesh prajapati

unread,
Jul 6, 2012, 11:51:32 PM7/6/12
to webd...@googlegroups.com

I'm getting org.openqa.selenium.UnhandledAlertException while taking up the screenshot

Expected: When the alert box is being opened-take the screenshot
Actual: Getting unhandled exception-Modal dialog present

Webdriver: 2.23
Using the below code
                                                        wd.switchTo().activeElement();
Alert RentalSavedAlert=wd.switchTo().alert();
//String msg= RentalSavedAlert.getText();
File scrFile = ((TakesScreenshot)wd).getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(scrFile, new File("C:\\screenshot.jpeg"));
RentalSavedAlert.accept();

Is everything okay?

Krishnan Mahadevan

unread,
Jul 7, 2012, 12:52:38 AM7/7/12
to webd...@googlegroups.com
Without processing the alert you cannot take screenshot. So please process the alert by accepting or dismissing it and retry. 
--
You received this message because you are subscribed to the Google Groups "webdriver" group.
To view this discussion on the web visit https://groups.google.com/d/msg/webdriver/-/t-yb6hqZTCwJ.
To post to this group, send email to webd...@googlegroups.com.
To unsubscribe from this group, send email to webdriver+...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/webdriver?hl=en.


--
Thanks & Regards
Krishnan Mahadevan

"All the desirable things in life are either illegal, expensive, fattening or in love with someone else!"
My Scribblings @ http://wakened-cognition.blogspot.com/

Bill Ross

unread,
Jul 7, 2012, 1:12:43 AM7/7/12
to webd...@googlegroups.com
I wonder what would make it impossible to take a screenshot of an alert?

Krishnan Mahadevan <krishnan.ma...@gmail.com> wrote:

> Without processing the alert you cannot take screenshot. So please process
> the alert by accepting or dismissing it and retry.
>
> On Saturday, July 7, 2012, nimesh prajapati wrote:
>
> >
> > I'm getting org.openqa.selenium.**UnhandledAlertException while taking up
> > the screenshot
> >
> > Expected: When the alert box is being opened-take the screenshot
> > Actual: Getting unhandled exception-Modal dialog present
> >
> > Webdriver: 2.23
> > Using the below code
> >
> > wd.switchTo().activeElement();
> > Alert RentalSavedAlert=wd.switchTo()**.alert();
> > //String msg= RentalSavedAlert.getText();
> > File scrFile = ((TakesScreenshot)wd).**getScreenshotAs(OutputType.**FILE);
> > FileUtils.copyFile(scrFile, new File("C:\\screenshot.jpeg"));
> > RentalSavedAlert.accept();
> >
> > Is everything okay?
> >
> > --
> > You received this message because you are subscribed to the Google Groups
> > "webdriver" group.
> > To view this discussion on the web visit
> > https://groups.google.com/d/msg/webdriver/-/t-yb6hqZTCwJ.
> > To post to this group, send email to webd...@googlegroups.com<javascript:_e({}, 'cvml', 'webd...@googlegroups.com');>
> > .
> > To unsubscribe from this group, send email to
> > webdriver+...@googlegroups.com <javascript:_e({}, 'cvml',
> > 'webdriver%2Bunsu...@googlegroups.com');>.
> > For more options, visit this group at
> > http://groups.google.com/group/webdriver?hl=en.
> >
>
>
> --
> Thanks & Regards
> Krishnan Mahadevan
>
> "All the desirable things in life are either illegal, expensive, fattening
> or in love with someone else!"
> My Scribblings @ http://wakened-cognition.blogspot.com/
>
> --
> You received this message because you are subscribed to the Google Groups "webdriver" group.

Krishnan Mahadevan

unread,
Jul 7, 2012, 1:20:59 AM7/7/12
to webd...@googlegroups.com
Bill,
I would let someone from the core group answer that since I dont know the internals. 

Here is what I think how it works. Screenshot capturing in webdriver is not by doing a printscreen but by working on the full html page on the browser. When the alert pops up it sits between the webdriver and the webpage on the browser which is why webdriver complains about an unhandledalert exception. 

Mark Collin

unread,
Jul 9, 2012, 5:45:44 AM7/9/12
to webd...@googlegroups.com

Sounds like it is worth raising a bug on this one if it’s reproducible and you can provide a minimised script. 

Manoj

unread,
Jul 9, 2012, 1:20:28 PM7/9/12
to webd...@googlegroups.com
Hi Prajapati,

Try this code

 Robot objRobot = null;
        try
        {
           objRobot = new Robot();
        } catch(Exception ex)
        {

}

Dimension screenDim =  Toolkit.getDefaultToolkit().getScreenSize(); 

BufferedImage objBufferedImage =  objRobot.createScreenCapture(new Rectangle(0, 0, (int)screenDim.getWidth(), (int)screenDim.getHeight()));

int areaToExportWidth = 1024;
int areaToExportHeight = 768;

//Create the image 
BufferedImage exportImage =objRobot.createScreenCapture(new Rectangle(0, 0, (int)screenDim.getWidth(), (int)screenDim.getHeight()));

//Get graphics - Get the layer we can actually draw on
Graphics2D imageGraphics = (Graphics2D) exportImage.getGraphics();



//Cleanup after ourselves
imageGraphics.dispose();

//Setup to write the BufferedImage to a file
String pathToFile = "dir";
File outputDirectory = new File(pathToFile);
File outputFile = new File("F:\\MyImage.png");

//Here we make sure the directory exists.
/*
* Returns TRUE if:
*  The directory is MISSING
*  and/or the directory IS NOT a directory
*/
if(!outputDirectory.exists() || !outputDirectory.isDirectory()){
outputDirectory.mkdirs(); //Make the directory
} // Else do nothing

//Write the file
try { //Attempt the write
ImageIO.write(exportImage, "png", outputFile);
} catch (IOException e) { //For some reason it failed so...
e.printStackTrace();//... why did it fail?
driver.switchTo().alert().accept() ;
}



Regards
Manoj

darrell

unread,
Jul 10, 2012, 8:12:13 PM7/10/12
to webdriver
I was just going to post a link to my article
http://darrellgrainger.blogspot.ca/2011/02/generating-screen-capture-on-exception.html.
It has similar code using Robot to capture the entire screen and save
it to a file.

It should be noted that Robot works locally. If you are using
RemoteWebDriver to drive the browser on a different computer, this
code will not work. For situations like that I use some remote
execution capable from the operating system and run a stand alone
program which captures the screen to a file (basic turn this code into
a stand alone application).
Reply all
Reply to author
Forward
0 new messages