Selenium 2 WebDriver Advanced
  • Preface
  • 1. API Testing Using Selenium WebDriver
    • 1.1 Code Snippet for API testing
    • 1.2 SoapUI
      • 1.2.1 Add selenium jars to soapUI
      • 1.2.2 Run selenium tests
      • 1.2.3 Data Driven Testing using SoapUI
  • 2. Advanced Profiles for Browsers
    • 2.1 Profile for Firefox Browser
    • 2.2 Profile for Chrome Browser
    • 2.3 Profile for IE Browser
    • 2.4 Profile for Safari Browser
    • 2.5 Profile for Opera Browser
  • 3. TestNG
    • 3.1 Sample TestNG class
    • 3.2 Parallel execution
    • 3.3 Execution of tests from batch file
  • 4. ExtentReports
    • 4.1 Basics
    • 4.2 Sample Extent Reports
  • 5. Advanced tricks
    • 5.1 Keystroke handling
    • 5.2 Screenshot capture
    • 5.3 Get HTML Source of WebElement
Powered by GitBook
On this page

Was this helpful?

  1. 5. Advanced tricks

5.2 Screenshot capture

This situation generally arises whenever there is a failure in the application. This will help developers know the areas of changes which has caused the issue.

To apprehend the problem we are using the Interface ITakesScreenshot method to take a full-screen screenshot of the browser in required format.

In .NET

public void TakeFullScreenshot(IWebDriver driver, String filename)
{
    Screenshot screenshot = ((ITakesScreenshot)driver).GetScreenshot();
    screenshot.SaveAsFile(filename, ImageFormat.Png);
}

Sometimes you may need to take a screenshot of a single element.

public void TakeScreenshotOfElement(IWebDriver driver, By by, string fileName)
{
 // 1. Make screenshot of all screen
 var screenshotDriver = driver as ITakesScreenshot;
 Screenshot screenshot = screenshotDriver.GetScreenshot();
 var bmpScreen = new Bitmap(new MemoryStream(screenshot.AsByteArray));

 // 2. Get screenshot of specific element
 IWebElement element = driver.FindElement(by);
 var cropArea = new Rectangle(element.Location, element.Size);
 var bitmap = bmpScreen.Clone(cropArea, bmpScreen.PixelFormat);
 bitmap.Save(fileName);
}

First we make a full-screen screenshot then we locate the specified element by its location and size attributes. After that, the found rectangle chunk is saved as a bitmap.

Here is how you use both methods in tests.

@AfterMethod
public void WebDriverAdvancedUsage_TakingFullScrenenScreenshot()
{
 this.driver.Navigate().GoToUrl(@"https://saikiranpro.blogspot.in/");
 this.WaitUntilLoaded();
 string tempFilePath = Path.GetTempFileName().Replace(".tmp", ".png");
 this.TakeFullScreenshot(this.driver, tempFilePath);
}

@AfterMethod
public void WebDriverAdvancedUsage_TakingElementScreenshot()
{
 this.driver.Navigate().GoToUrl(@"https://saikiranpro.blogspot.in/");
 this.WaitUntilLoaded();
 string tempFilePath = Path.GetTempFileName().Replace(".tmp", ".png");
 this.TakeScreenshotOfElement(this.driver,By.XPath("//h3[text()='About Me ']"), tempFilePath);
}

We get a temp file name through the special "Path" .NET class. By default temp files are generated with ".tmp" extension because of that we replace it with ".png".

Previous5.1 Keystroke handlingNext5.3 Get HTML Source of WebElement

Last updated 5 years ago

Was this helpful?