Best way to take screenshots of tests in Selenium 2?

后端 未结 13 2067
别跟我提以往
别跟我提以往 2020-11-28 04:29

I need a way to take screenshots of my functional tests. Right now I\'m using Selenium 2 with C# bindings. I pretty much want to take a screenshot at the end of the test to

相关标签:
13条回答
  • 2020-11-28 04:57
    public void TakeScreenshot(string saveLocation) {
            var location = GetPath() + _name + "\\" + saveLocation + ".png";
            var ssdriver = _driver as ITakesScreenshot;
            var screenshot = ssdriver.GetScreenshot();
            screenshot.SaveAsFile(location, ImageFormat.Png);
        }
    

    This code will help you to take screen shot

    0 讨论(0)
  • 2020-11-28 04:58

    To do screenshots in Selenium 2 you need to do the following

    driver = new FireFoxDriver(); // Should work in other Browser Drivers
    driver.Navigate().GoToUrl("http://www.theautomatedtester.co.uk");
    Screenshot ss = ((ITakesScreenshot) driver).GetScreenshot();
    
    //Use it as you want now
    string screenshot = ss.AsBase64EncodedString;
    byte[] screenshotAsByteArray = ss.AsByteArray;
    ss.SaveAsFile("filename", ImageFormat.Png); //use any of the built in image formating
    ss.ToString();//same as string screenshot = ss.AsBase64EncodedString;
    

    That code should work, as I quickly tested it in IronPython Repl. See the IronPython code below

    import clr
    clr.AddReference("WebDriver.Common.dll")
    clr.AddReference("WebDriver.Firefox.dll")
    from OpenQA.Selenium import *
    from OpenQA.Selenium.Firefox import *
    driver = FirefoxDriver()
    driver.Navigate().GoToUrl("http://www.theautomatedtester.co.uk")
    s = driver.GetScreenshot()
    s.AsBaseEncodedString
    # HUGE string appears in the REPL
    
    0 讨论(0)
  • 2020-11-28 05:00

    JAVA

    protected void fullPageScreenshot(String testname) {
                String timeStamp = new SimpleDateFormat("dd_MM_yyyy_HH_mm_ss").format(Calendar.getInstance().getTime());
                String imageName = testname + "-" + timeStamp + ".png";
                Screenshot screenshot = new AShot().shootingStrategy(ShootingStrategies.viewportPasting(2000))
                        .takeScreenshot(DriverManager.getDriver());
                try {
                    ImageIO.write(screenshot.getImage(), "PNG", new File("./FullPage_Screenshots/" + imageName));
                } catch (Exception e) {
                    System.out.println("Capturing FullPage Screenshot failed");
                }
            }
    

    use Ashot library to take fullpage screenshots - even where pages needs to be scrolled https://mvnrepository.com/artifact/ru.yandex.qatools.ashot/ashot/1.5.4

    0 讨论(0)
  • 2020-11-28 05:05

    Use System.Drawing.Imaging reference. Following code can be used for taking screenshot.

    IWebDriver driver = new FirefoxDriver();
    ITakesScreenshot screenshotDriver = driver as ITakesScreenshot;
    Screenshot screenshot = screenshotDriver.GetScreenshot();
    String fp = "D:\\" + "snapshot" + "_"+ DateTime.Now.ToString("dd_MMMM_hh_mm_ss_tt") + ".png";
    screenshot.SaveAsFile(fp, ImageFormat.Png);
    

    Notes: Timestamp has two advantages: 1) You'll get to know the perfect DateTime when screenshot is taken. 2) SaveAsFile function overwrites the existing file. So, DateTime can help for different file creation.

    0 讨论(0)
  • 2020-11-28 05:07

    Define this in global code :

    var counter = DateTime.Now.Ticks.ToString();
    
    ((ITakesScreenshot)driver).GetScreenshot().SaveAsFile((snap +counter + ".jpg").ToString(), OpenQA.Selenium.ScreenshotImageFormat.Jpeg);
    test.Log(LogStatus.Fail, "Snapshot below: " + test.AddScreenCapture(snap + counter + ".jpg"));
    
    0 讨论(0)
  • 2020-11-28 05:13

    Using selenium there were two calls I was familiar with: captureEntirePageScreenshot and captureScreenshot. You might want to look into those calls to see if they'll accomplish what you're after.

    0 讨论(0)
提交回复
热议问题