Best way to take screenshots of tests in Selenium 2?

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

    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;
            }
        }
    }
    

提交回复
热议问题