Best way to take screenshots of tests in Selenium 2?

后端 未结 13 2144
别跟我提以往
别跟我提以往 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: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
    

提交回复
热议问题