How to take screenshot in selenium webdriver using c#

Jan 18, 2021, 1323 Views, 2

Contents

  • Take Screenshot in Selenium
  • Code to capture screenshot in C#
  • Code Explanation
  • Output
  • Summary
  • 1.Take Screenshot in Selenium

    Selenium webdriver testing tool is designed specifically to automate the cross-browser testing process which henceforth facilitates testing for the frontend engineer in terms of UI/UX. Selenium helps in generating scripts, where we first locate an element using ID or classes and then capture the experience of waits and javascript using the screenshot facility. To not overcomplicate the blog we will focus only on capturing screenshots here and talk about waits and prompts in the coming series. Here, we are using visual studio 2017, dot net MVC core 2.2 project,s and chromedriver for the testing process. Also, do check out the previous blogs where we installed Selenium Webdriver using C#, learned various ways to locate an element on the webpage, and now let us get rolling to capture images. 

    2.Code to capture screenshot in C#

    using System;
    using System.Collections.Generic;
    using System.Diagnostics;
    using System.Linq;
    using System.Threading.Tasks;
    using Microsoft.AspNetCore.Mvc;
    using WebApplication1.Models;
    using OpenQA.Selenium;
    using OpenQA.Selenium.Chrome;
    using OpenQA.Selenium.Remote;
    using OpenQA.Selenium.Support.UI;

    namespace WebApplication1.Controllers
    {
        public class HomeController : Controller
        {
            public IActionResult Index()
            {

                ChromeOptions options = new ChromeOptions();
                options.BinaryLocation = "C:\\Program Files\\Google\\Chrome\\Application\\chrome.exe";


                DesiredCapabilities capabilities = new DesiredCapabilities();
                capabilities.SetCapability(ChromeOptions.Capability, options);
                ChromeDriver driver = new ChromeDriver("C:\\WebDriver\\bin", options);
                driver.Navigate().GoToUrl(@"https://engineerdiaries.com/");

                IWebElement element = driver.FindElement(By.CssSelector("img"));
                Screenshot screenshot = (element as ITakesScreenshot).GetScreenshot();
                screenshot.SaveAsFile("screenshotImg.png", ScreenshotImageFormat.Png);
                return View();

            }
        }
    }

    3.Code Explanation

    1. options is used to located google chrome because the system was earlier not able to locate chrome. So, this step is optional if your code too raises exception
    2. chromeDriver is used to initialize location of driver in the system. Our chromedriver is installed on C:/chromedriver/bin
    3. driver.Navigate() to go to URL to test the webpage
    4.IWebElement is the element whose screenshot we are taking. Here it will take capture the image of first <img> element in our case is the logo of our website
    5. GetScreenshot() will capture the image and store it  with name 'screenshotImg.png' in the code folder

    4.Output

    As in our code, it will capture the image of the first <img> element which is the logo of the site as shown in the image. You can also see that it is saved in the folder containing code with screenshotImg.png name. Now you can use the images to compare various elements, their output and compare UI/UX. Also here we have used one element, you can use a list and generate multiple images similarly.

    How to take screenshot in selenium webdriver using c#

    How to take screenshot in selenium webdriver using c#

    How to take screenshot in selenium webdriver using c#

    5.Summary

    You learnt to take screenshots in selenium webdriver using c#. Which further could be translated into writing scripts for multiple browsers and hence, comparing the output validating the code to merge. One can make use of waits and test prompts output similarly and not restricting to just elements. Do ask in the comment section for any further queries.

    Comments Section:

    Admin

    Feb 19, 2021

    Definitely. Coming soon. Thank you for your feedback:)

    Manoj Jagdale

    Feb 18, 2021

    Explain in Java also