Checkout our demo site to practice selenium https://magento.softwaretestingboard.com/

0 like 0 dislike
4.9k views
by The go-to Tester (158 points)
edited by

Sometimes my tests falls with error: OpenQA.Selenium.UnhandledAlertException : unexpected alert open: {Alert text : If you leave the page, unsaved data will be lost.}

I'm trying to reproduce error, but I see nothing. Alert present only when tests run automaticly. Is there a way in Selenium to take screenshot of browser with alert present?

1 Answer

0 like 0 dislike
by The go-to Tester (181 points)
selected by
 
Best answer

Selenium will not be able to take screenshot of an unhandelled Alert. If you want screenshot for debugging, you can try Robot to capture screenshot.

BufferedImage image = new Robot().createScreenCapture(new Rectangle(Toolkit.getDefaultToolkit().getScreenSize()));
ImageIO.write(image, "png", new File("path\\to\\screenshot.png"));
 
But problem with above code is that, you will not be able to use on remote machine and you can not execute tests in background.
 
You start your tests, keep your browser on top of all other windows and once exception is caught, you can use above code to capture screenshot.
 
Let me know your views in the comment.
 
by The go-to Tester (158 points)
Is it possible to grab a screenshot using the .NET Framework?
by The go-to Tester (181 points)
You can create an new Bitmap object and draw that into Graphics.CopyfromScreen method.

eg.

using (Bitmap bmpScreenCapture = new Bitmap(Screen.PrimaryScreen.Bounds.Width,
                                            Screen.PrimaryScreen.Bounds.Height))
{
    using (Graphics g = Graphics.FromImage(bmpScreenCapture))
    {
        g.CopyFromScreen(Screen.PrimaryScreen.Bounds.X,
                         Screen.PrimaryScreen.Bounds.Y,
                         0, 0,
                         bmpScreenCapture.Size,
                         CopyPixelOperation.SourceCopy);
    }
}


This site is for software testing professionals, where you can ask all your questions and get answers from 1300+ masters of the profession. Click here to submit yours now!

1.4k questions

1.6k answers

866 comments

1.9k users

...