Monday, March 25, 2013

Handling a Java Script pop up dialog in Selenium WebDriver c#

A modal dialog box can be handled and suppressed by using IAlert class. Follow the code snippet below which closes a Java Script alert displayed.
IWebDriver driver = new FirefoxDriver();
driver.Navigate().GoToUrl("http://www.tizag.com/javascriptT/javascriptalert.php");
driver.FindElement(By.XPath("//div/form/input[@value='Confirmation Alert']")).Click();
IAlert alert = driver.SwitchTo().Alert();
Console.WriteLine(alert.Text);
alert.Accept();
The second method to suppress a modal dialog's is to use SendKeys class of name space System.Windows.Forms and send a keyboard  Enter using SendKeys.SendWait("{Enter}")
IWebDriver driver = new FirefoxDriver();
driver.Navigate().GoToUrl("http://www.tizag.com/javascriptT/javascriptalert.php");
driver.FindElement(By.XPath("//div/form/input[@value='Confirmation Alert']")).Click();
SendKeys.SendWait("{Enter}");

Sunday, March 24, 2013

Select options in a DropDown List without using "SelectElement" class - Selenium WebDriver C#

Here is an example to better illustrate how to get all the items in a Drop down list and to select an item from the drop down list.
A sample Html code for drop down list
<select>
  <option>Milk</option>
  <option>Coffee</option>
  <option>Tea</option>
</select>
Code below gets all the items from the drop down list above and selects item 'Coffee'.Logic of the code is as follows
Step 1. Create an interface of the web element tag

Step 2. Create an IList with all the child elements of web element tag Step 3. Select the Drop List item "Coffee"
using System;
using System.Collections.Generic;
using OpenQA.Selenium;
using OpenQA.Selenium.Firefox;
using OpenQA.Selenium.Support.UI;

namespace SeleniumTests
{
    class DropDownListSelection
    {
        static void Main(string[] args)
        {
            IWebDriver driver = new FirefoxDriver(); 
            driver.Navigate().GoToUrl("http://DropDownList.html");
            IWebElement element = driver.FindElement(By.XPath("//Select"));
            IList<IWebElement> AllDropDownList =    element.FindElements(By.XPath("//option"));
            int DpListCount = AllDropDownList.Count;
            for (int i = 0; i < DpListCount; i++)
            {
                if (AllDropDownList[i].Text == "Coffee")
                 {
                    AllDropDownList[i].Click();
                 }
            }
            Console.WriteLine(DpListCount);
            Console.ReadLine();
        }
    }
}

Generating Test Results using Selenium WebDriver C#

Its up to the discretion of the user what to do with the Selenium Webdriver automation and how to report the test results. Selenium Webdriver will give you the power to control your web browser and to automate your web application tests.
Same as how you have to program in any other automation tool the conditions for checking your pass or fail criteria for any tests, in Selenium also it has to be programmed.It is totally up to the programmer how to report their results and the template to be followed. You will have to write your own code to format and store the test results.

Retrieving data from an HTML Table Selenium WebDriver C#

Follow the below C# code to retrieve data from a table. To demonstrate this I have used a table with id="accounts" in "http://developer.yahoo.com/yui/examples/datasource/datasource_table_to_array.html".


<table id="accounts"> 
    <caption>Table in markup with data in it</caption>
    <thead> 
           <tr> 
                <th>Due Date</th> 
                <th>Account Number</th> 
                <th>Quantity</th> 
                <th>Amount Due</th> 
           </tr> 
    </thead> 
    <tbody> 
           <tr> 
                <td>1/23/1999</td> 
                <td>29e8548592d8c82</td> 
                <td>12</td> 
                <td>$150.00</td> 
           </tr> 
           <tr> 
                <td>5/19/1999</td> 
                <td>83849</td> 
                <td>8</td> 
                <td>$60.00</td> 
           </tr> 
            ... 
   </tbody> 
</table> 
Details of the code.
  1. Created a Web Element for of the By.XPath("//table[@id='accounts']/thead/tr")
  2. Created an IList with collection of all the tag web elements under the /thead/trBy.XPath("//table[@id='accounts']/thead/tr/th")
  3. Access the contents of the table head using a loop.
  4. Created a Web Element for of the By.XPath("//table[@id='accounts']/tbody/tr")
  5. Created a IList with collection of all the tag web elements under the /tbody/tr By.XPath("//table[@id='accounts']/tbody/tr/th")
  6. Access the contents of the table body using a loop.
-
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using OpenQA.Selenium;
using OpenQA.Selenium.Firefox;
using OpenQA.Selenium.Support.UI;

namespace BusinessCreation
{
    class ExtractDataFromATable
    {
        static void Main(string[] args)
        {
            IWebDriver driver = new FirefoxDriver();
            driver.Navigate().GoToUrl("http://developer.yahoo.com/yui/examples/datasource/datasource_table_to_array.html");
          if(driver.FindElement(By.XPath("//table[@id='accounts']/thead/tr/th")).Displayed)
            {
                IWebElement webElementHead =  driver.FindElement(By.XPath("//table[@id='accounts']/thead/tr"));
                IList<IWebElement> ElementCollectionHead = webElementHead.FindElements(By.XPath("//table[@id='accounts']/thead/tr/th"));
                foreach (IWebElement item in ElementCollectionHead)
                {
                    Console.WriteLine(item.Text);
                }
            }
            if (driver.FindElement(By.XPath("//table[@id='accounts']/tbody/tr")).Displayed)
            {
                IWebElement webElementBody = driver.FindElement(By.XPath("//table[@id='accounts']/tbody/tr"));
                IList<IWebElement> ElementCollectionBody = webElementBody.FindElements(By.XPath("//table[@id='accounts']/tbody/tr"));
                foreach (IWebElement item in ElementCollectionBody)
                {
                    string []arr= new string[4];
                    arr = item.Text.Split(' ');
                    for (int i = 0; i < arr.Length; i++)
                    {
                        Console.WriteLine(arr[i]);
                    }               
                }
            }
            Console.ReadLine();
        }
    }
}