Best way to take screenshots of tests in Selenium 2?

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

    Just use the extension method TakeScreenshot() in one line of code.

    IWebDriver driver = new InternetExplorerDriver();
    driver.Navigate().GoToUrl("Your_Homepage_Url");
    
    driver.TakeScreenshot().SaveAsFile("file_name_string", ImageFormat.Jpeg);
    
    0 讨论(0)
  • 2020-11-28 04:50
    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");
    
    0 讨论(0)
  • 2020-11-28 04:51
            ScreenCaptureJob scj;
            scj = new ScreenCaptureJob();
            // Specify the path & file name in which you want to save         
            scj.OutputScreenCaptureFileName = @"C:\Users\jpavankumar\Desktop\Screencaptuere\ScreenRecording4.wmv";
            // Start the Screen Capture Job
            scj.Start(); scj.Stop();
    

    Try this code out here ... hope it will be useful to you .... !

    0 讨论(0)
  • 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;
            }
        }
    }
    
    0 讨论(0)
  • 2020-11-28 04:53

    Best way to take screenshot and store in the file location in a generic way in python :

    def screenShots(self):
            fileName= NewFile + "." + str(round(time.time() * 1000)) + ".png"
            screenshotDirectory = "../screenshot/"  #Move to that directory where you want ot store the screenshot
            relativeFileName = screenshotDirectory + fileName
            currentDirectory = os.path.dirname(__file__)
            destinationFile = os.path.join(currentDirectory,relativeFileName)
            destinationDirectory = os.path.join(currentDirectory,screenshotDirectory)
    
            try:
                if not os.path.exists(destinationDirectory):
                    os.makedirs(destinationDirectory)
                self.driver.save_screenshot(destinationFile)
    
                self.log.info("Screenshot saved to directory" + destinationFile)
    
            except:
                self.log.error("Exception Occured")
                print_stack()
    
    0 讨论(0)
  • 2020-11-28 04:54

    I don't know if it matters, but I ended up having to cast the driver when i was writing in c#.

    something like:

    Screenshot ss = ((ITakesScreenshot)driver).GetScreenshot();
    
    0 讨论(0)
提交回复
热议问题