Archive

Archive for March, 2014

Taking a Screenshot using Selenium WebDriver in C#

March 18, 2014 5 comments

It is always good to capture the screenshot of the test results if you are running the tests overnight. It is very easy and you may explicitly set the areas were you want to take the screenshot during the execution of automated tests and specify the save location.

SOFTWARE REQUIREMENTS:

  • Windows 7 as OS
  • Microsoft Visual Studio 2010 as IDE
  • NUnit as Unit Testing Tool
  • Selenium as a Test Automation Tool

Let me consider the test:

Given I am on the Google home page
When I search for text selenium
Then I should see the search results

I would be interested to take the snapshot in 2 locations:

  • When the system inputs the search text in the search field
  • Displaying of search results screen
TEST EXECUTION INSTRUCTIONS:
  • Refer my previous blog on Set-up instructions.
  • Copy and paste the code snippet below into your Visual Studio 2010.
  • Run the code using NUnit.
  • Visit the image save location to view the screenshots (ex: C:\Users\anoops\Desktop\Screenshots in my case)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NUnit.Framework;
using OpenQA.Selenium;
using OpenQA.Selenium.Firefox;
using OpenQA.Selenium.Support.UI;
using System.Drawing.Imaging;

namespace TestAutomation
{
 [TestFixture]
 public class Driver
 {
   IWebDriver driver;

   public void TakeScreenshot(IWebDriver driver, string saveLocation)
   {
   ITakesScreenshot ssdriver = driver as ITakesScreenshot;
   Screenshot screenshot = ssdriver.GetScreenshot();
   screenshot.SaveAsFile(saveLocation, ImageFormat.Png);
   }

   [SetUp]
   public void Setup()
   {
   // Create a new instance of the Firefox driver
   driver = new FirefoxDriver();
   }

   [TearDown]
   public void Teardown()
   {
   driver.Quit();
   }

   [Test]
   public void GoogleSearch()
   {
   //Navigate to the site
   driver.Navigate().GoToUrl("http://www.google.com.au");
   // Find the text input element by its name
   IWebElement query = driver.FindElement(By.Name("q"));
   // Enter something to search for
   query.SendKeys("Selenium");
   //Take screenshot during the input of search text
   TakeScreenshot(driver, @"C:\Users\anoops\Desktop\Screenshots\SearchInput.png");
   // Now submit the form
   query.Submit();
   // Google's search is rendered dynamically with JavaScript.
   // Wait for the page to load, timeout after 10 seconds
   WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(5));
   wait.Until((d) => { return d.Title.StartsWith("selenium"); });
   //Check that the Title is what we are expecting
   Assert.AreEqual("selenium - Google Search", driver.Title);
   //Take screenshot after the dispay of search results
   TakeScreenshot(driver, @"C:\Users\anoops\Desktop\Screenshots\SearchResults.png");
  }
 }
}

Data Driven Testing (DDT) using C#, Selenium WebDriver and NUnit

March 10, 2014 6 comments

Data Driven Testing (DDT) is to verify many different test cases by driving the test from an external data source instead of using the same hard-coded values each time the test runs. This way, you can test how the application handles various input without having a lot of similar tests that only have different data sets.

SOFTWARE REQUIREMENTS:

  • Windows 7 as OS
  • Microsoft Visual Studio 2010 as IDE
  • NUnit as Unit Testing Tool
  • Selenium as a Test Automation Tool
  • Microsoft Excel as External Data Source

SET-UP INSTRUCTIONS:

Refer my previous blog on set-up instructions.

TEST SCENARIO:

As an end user,
I would like to visit the google search page 
And then I would like to search an item so that
I can view the search results

TEST EXECUTION INSTRUCTIONS:

  • Create a sample spreadsheet using MS Excel and add some search text that needs to be searched.
  • Copy and paste the code snippet below into your Visual Studio 2010.
  • Add the reference to Microsoft.Office.Interop.Excel using .Net tab in the ‘Add Reference’ window.
  • Update the location of the spreadsheet in the code (i.e., in my case @”C:\Users\anoops\Desktop\GoogleTestData.xls”)
  • Run the tests using NUnit.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NUnit.Framework;
using OpenQA.Selenium;
using OpenQA.Selenium.Firefox;
using OpenQA.Selenium.Support.UI;
using Excel = Microsoft.Office.Interop.Excel;

namespace TestAutomation
{
 [TestFixture]
 public class Driver
 {
  IWebDriver driver;

  [SetUp]
  public void Setup()
  {
  // Create a new instance of the Firefox driver
  driver = new FirefoxDriver();
  }

  [TearDown]
  public void Teardown()
  {
  driver.Quit();
  }

  [Test]
  public void GoogleSearch()
  {
   Excel.Application xlApp;
   Excel.Workbook xlWorkBook;
   Excel.Worksheet xlWorkSheet;
   Excel.Range range;

   string str;
   int rCnt = 0;
   int cCnt = 0;

   xlApp = new Excel.Application();
   //Opening Excel file
   xlWorkBook = xlApp.Workbooks.Open(@"C:\Users\anoops\Desktop\GoogleTestData.xls", 0, true, 5, "", "", true, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "\t", false, false, 0, true, 1, 0);
   xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);

   //Gives the used cells in the sheet
   range = xlWorkSheet.UsedRange;

   for (rCnt = 1; rCnt <= range.Rows.Count; rCnt++)
   {
    for (cCnt = 1; cCnt <= range.Columns.Count; cCnt++)
     {
      //Get the string from the sheet
      str = (string)(range.Cells[rCnt, cCnt] as Excel.Range).Value2;
      driver.Navigate().GoToUrl("http://www.google.com.au");
      // Find the text input element by its name
      IWebElement query = driver.FindElement(By.Name("q"));
      // Convert the search string to lower case
      string lowerstr = str.ToLower();
      // Input the search string
      query.SendKeys(lowerstr);
      // Submit the form
      query.Submit();

      // Google's search is rendered dynamically with JavaScript.
      // Wait for the page to load, timeout after 5 seconds
      WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(5));
      IWebElement title = wait.Until<IWebElement>((d) =>
        {
        return d.FindElement(By.ClassName("ab_button"));
        });

      //Check that the Title is what we are expecting
      Assert.True(driver.Title.ToLower().StartsWith(lowerstr));
      //Assert.True(driver.Title.Contains(lowerstr));
      //Console.WriteLine("Page Title is: " + driver.Title);
       }
      }
   xlWorkBook.Close(true, null, null);
   xlApp.Quit();

  }
 }
}
%d bloggers like this: