Best way to take screenshots of tests in Selenium

2019-01-07 06:24发布

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 make sure the desired page is displayed. Are there any particular tools you guys know of that I can incorporate into my C# code that will trigger a screenshot? I couldn't find a built-in Selenium 2 solution (without looking it over).

12条回答
女痞
2楼-- · 2019-01-07 07:01
driver.Url = "https://www.amazon.in/";
//Store image in bin folder
((ITakesScreenshot)driver).GetScreenshot().SaveAsFile("CurrentPage.png"); 
//Store image in D drive        
((ITakesScreenshot)driver).GetScreenshot().SaveAsFile(@"D:\CurrentPage.png");
查看更多
虎瘦雄心在
3楼-- · 2019-01-07 07:02

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
查看更多
叛逆
4楼-- · 2019-01-07 07:05

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.

查看更多
别忘想泡老子
5楼-- · 2019-01-07 07:08
var driver = new InternetExplorerDriver();
driver.Navigate().GoToUrl("http://www.google.com");
var ss = driver.GetScreenshot();   
ss.SaveAsFile("ss.png", System.Drawing.Imaging.ImageFormat.Png);
查看更多
男人必须洒脱
6楼-- · 2019-01-07 07:08
  1. Add a reference of System.Drawing in your solution/project.
  2. Use System.Drawing.Imaging namespace in your test.

Here I am capturing the screen shot of Facebook Home page.

using System;
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using OpenQA.Selenium.Support.UI;
using NUnit.Framework;
using System.IO;
using System.Collections;
using System.Drawing.Imaging;

namespace FacebookRegistrationUsingC_Sharp
{
    [TestFixture]
    public class ScreenShot
    {
        IWebDriver driver = null;
        IWebElement element = null;

        [SetUp]
        public void SetUp()
        {
            driver = new ChromeDriver("G:\\Selenium_Csharp\\Jar\\chromedriver_win32");           
            driver.Navigate().GoToUrl("https://www.Facebook.com");
            driver.Manage().Window.Maximize();

        }
        [Test]
        public void TestScreenShot()
        {           

            Screenshot ss = ((ITakesScreenshot)driver).GetScreenshot();
            ss.SaveAsFile("e:\\pande", System.Drawing.Imaging.ImageFormat.Jpeg);
        }

        [TearDown]
        public void TearDown()
        {
            driver = null;
            element = null;
        }
    }
}
查看更多
放荡不羁爱自由
7楼-- · 2019-01-07 07:09

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

查看更多
登录 后发表回答