Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Capture screenshot of alert

I have used the below code to take screenshot of the application after pop up appears.

Alert alert = driver.switchTo().alert();
File scrFile= ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(scrFile, new File("G:\\Screens\\sc1.jpg"));
String alertMsg = alert.getText();
System.out.println(alertMsg);
alert.accept();

But it is throwing this exception

Exception in thread "main" org.openqa.selenium.UnhandledAlertException:Modal dialog present: Assessment Name already Exist.

But the code works fine if I remove the screenshot procedures.

like image 833
Dinu Avatar asked Feb 10 '14 06:02

Dinu


2 Answers

You have to handle the alert before you take a screenshot... you can read about it more here https://code.google.com/p/selenium/issues/detail?id=4412

like image 74
Praveen Kumar Avatar answered Nov 20 '22 17:11

Praveen Kumar


You can always get the screenshot of entire page using Robot. I just tried it out, this code is working:

WebDriver driver;
@Before
public void init() throws Exception {
    driver = new FirefoxDriver();
    driver.get("http://www.tizag.com/javascriptT/javascriptalert.php");
}

@Test
public void bla() throws AWTException, IOException {
    WebElement element = driver.findElement(By.xpath("//input[@type=\"button\"]"));
    // Trigger the alert
    element.click();
    BufferedImage image = new Robot().createScreenCapture(new Rectangle(Toolkit.getDefaultToolkit().getScreenSize()));
    ImageIO.write(image, "png", new File("c:\\localdev\\bla.png"));
    driver.switchTo().alert().accept();
}
like image 40
Erki M. Avatar answered Nov 20 '22 17:11

Erki M.