Thursday, 4 June 2015

Taking Screen Shot of a page By using selenium Webdriver With NUnit and C#.Net in Visual Studio-2012.

Taking Screen Shot of a page  By using selenium Webdriver With NUnit and C#.Net in Visual Studio-2012.

  1. create / select a Unit Test project from existing template under test option.
  2. Remove the name space - using Microsoft.VisualStudio.TestTools.UnitTesting; from your class/test.
  3. Add the  name space in your test/class  using NUnit.Framework;
  4. Add the  name space in your test/class  using OpenQA.Selenium;
  5. Add the  name space in your test/class  using OpenQA.Selenium.Chrome;
  6. Add the  name space in your test/class  using OpenQA.Selenium.Support.UI;
  7. Add the reference of System.Drawing by right click on References of your project.
  8. Add the name space in your test/class  using OpenQA.Selenium;s
  
Below is the code to take 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:\\AvinashPande.jpeg", System.Drawing.Imaging.ImageFormat.Jpeg);
        }

        [TearDown]
        public void TearDown()
        {
           
driver = null;
            element = null;
        }
    }
}


Here the chance to get an error: A generic error occurred in GDI+.



following Precaution to needs to be taken:
  1. You should not store the file on a drive on which operating system has been already installed.
  2. you should give the name other than the file which has been present already in that folder in which current file in storing .  


See Also Below Links:



















 
click

No comments:

Post a Comment