Wednesday 27 November 2013

Executing same selenium script with multiple browsers only or cross browser testing - Opening Browsers Parallel

Purpose: Executing same selenium script with multiple browsers only or cross browser testing - Opening Browsers Parallel

In my previous post I provided one way of executing same script with different browsers. Now I am going to show how to run the same script parallel(opening all the browsers at time)

Only the change is very small but I wontedly keeping the post separately to avoid the confusion.

The parallel attribute on the <suite> tag can take one of following values:

<suite name="My suite" parallel="methods">

parallel="methods": TestNG will run all your test methods in separate threads. Dependent methods will also run in separate threads but they will respect the order that you specified.

<suite name="My suite" parallel="tests">

parallel="tests": TestNG will run all the methods in the same <test> tag in the same thread, but each <test> tag will be in a separate thread. This allows you to group all your classes that are not thread safe in the same <test> and guarantee they will all run in the same thread while taking advantage of TestNG using as many threads as possible to run your tests.

<suite name="My suite" parallel="classes">

parallel="classes": TestNG will run all the methods in the same class in the same thread, but each class will be run in a separate thread.

<suite name="My suite" parallel="instances">

parallel="instances": TestNG will run all the methods in the same instance in the same thread, but two methods on two different instances will be running in different threads.


From the above parameters: we will use parallel="tests" to achieve our goal.

Step 1: We need to write our case with small change by passing a parameter.

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Parameters;
import org.testng.annotations.Test;
public class MultiBrowser {
 private WebDriver driver;

 @Parameters("browser")
 @BeforeMethod
 public void setup(String browser)
 {
  if(browser.equalsIgnoreCase("firefox"))
  {
   driver = new FirefoxDriver();
  }
  else if(browser.equalsIgnoreCase("iexplorer"))
  {
// Update the driver path with your location
   System.setProperty("webdriver.ie.driver", "Drivers\\IEDriverServer.exe");
   driver = new InternetExplorerDriver();
  }
  else if(browser.equalsIgnoreCase("chrome"))
  {
// Update the driver path with your location
   System.setProperty("webdriver.chrome.driver", "Drivers\\chromedriver.exe");
   driver = new ChromeDriver();
  }
  driver.manage().window().maximize();
 }

 @AfterMethod
 public void tearDown()
 {
 driver.quit();
 }

 @Test
 public void testMultiBrowser() throws InterruptedException
 {
  driver.get("http://www.google.com");
  Thread.sleep(3000);
 }
}

2. Now we need to create TestNG.xml and write the following code:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name="MultiBrowser">
       <test name="TestFirefox" verbose="10">
              <parameter name="browser" value="firefox" />
              <classes>
                     <class name="MultiBrowser" />
              </classes>
       </test>
       <test name="ChromeTest">
              <parameter name="browser" value="chrome" />
              <classes>
                     <class name="MultiBrowser" />
              </classes>
       </test>
       <test name="IETest">
              <parameter name="browser" value="iexplorer" />
              <classes>
                     <class name="MultiBrowser" />
              </classes>
       </test>
</suite>

3. Now run the code from TestNG.xml

You can observer all the three browsers will open and perform the task at a time.






 

Friday 22 November 2013

Executing same selenium script with multiple browsers onfly or cross browser testing

Purpose: Executing same selenium script with multiple browsers

Testing in multiple browsers or Browser compatibility Testing: these are the word we always bother while automating our Test Cases. Following code will help how to execute the same script with multiple browsers.

Step 1: We need to write our case with small change by passing a parameter.

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Parameters;
import org.testng.annotations.Test;
public class MultiBrowser {
 private WebDriver driver;

 @Parameters("browser")
 @BeforeMethod
 public void setup(String browser)
 {
  if(browser.equalsIgnoreCase("firefox"))
  {
   driver = new FirefoxDriver();
  }
  else if(browser.equalsIgnoreCase("iexplorer"))
  {
// Update the driver path with your location
   System.setProperty("webdriver.ie.driver", "Drivers\\IEDriverServer.exe");
   driver = new InternetExplorerDriver();
  }
  else if(browser.equalsIgnoreCase("chrome"))
  {
// Update the driver path with your location
   System.setProperty("webdriver.chrome.driver", "Drivers\\chromedriver.exe");
   driver = new ChromeDriver();
  }
  driver.manage().window().maximize();
 }

 @AfterMethod
 public void tearDown()
 {
 driver.quit();
 }

 @Test
 public void testMultiBrowser() throws InterruptedException
 {
  driver.get("http://www.google.com");
  Thread.sleep(3000);
 }
}

2. Now we need to create TestNG.xml and write the following code:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name="MultiBrowser">
       <test name="TestFirefox" verbose="10">
              <parameter name="browser" value="firefox" />
              <classes>
                     <class name="MultiBrowser" />
              </classes>
       </test>
       <test name="ChromeTest">
              <parameter name="browser" value="chrome" />
              <classes>
                     <class name="MultiBrowser" />
              </classes>
       </test>
       <test name="IETest">
              <parameter name="browser" value="iexplorer" />
              <classes>
                     <class name="MultiBrowser" />
              </classes>
       </test>
</suite>

3. Now run the code from TestNG.xml

You can observer all the three browsers will open and perform the task one another.

Sunday 17 November 2013

Prioritizing Selenium Test cases using TestNG annotations

Purpose: Prioritizing Selenium Test cases using TestNG annotations

Some times we may need to priorities our test cases based on some requirements. Following code will help you all.

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
public class PriorityParam {
 public WebDriver driver;
 public String baseURL;

 @BeforeMethod
 public void setup()
 {
  driver = new FirefoxDriver();
  driver.manage().window().maximize();
  baseURL = "http://mail.google.com";
 }

 @AfterMethod
 public void tearDown()
 {
  driver.quit();
 }

 @Test(priority=3)
 public void testPriorityParam() throws InterruptedException
 {
  driver.get(baseURL);
  Thread.sleep(3000);
  System.out.println("Priority 3");
 }

 @Test(priority=2)
 public void testPriorityParam1() throws InterruptedException
 {
  driver.get(baseURL);
  Thread.sleep(3000);
  System.out.println("Priority 2");
 }

 @Test(priority=4)
 public void testPriorityParam2() throws InterruptedException
 {
  driver.get(baseURL);
  Thread.sleep(3000);
  System.out.println("Priority 4");
 }

Thursday 14 November 2013

Different Operations on Combo Box / Dropdown / Select Option using Selenium Web Driver

Purpose: Different Operations on Combo Box / Dropdown using Selenium Web Driver

We always encounter Combo Box / Dropdown / Select options and following example will help you how to perform different operations.

import java.util.ArrayList;
import java.util.List;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.Select;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

public class SelectOption {
 public WebDriver driver;
 public String baseURL;

 @BeforeTest
 public void setup()
 {
  driver = new FirefoxDriver();
  driver.manage().window().maximize();
  baseURL = "http://hscripts.com/tutorials/html/form-combobox.php";
 }

 @AfterTest
 public void tearDown()
 {
  driver.quit();
 }

 @Test
 public void testSelectOption() throws InterruptedException
 {
  driver.get(baseURL);
 
  WebElement select = driver.findElement(By.xpath(".//form[1]/select"));
  WebElement multiSelect = driver.findElement(By.xpath(".//form[3]/select"));
  List<WebElement> selectedValues = new ArrayList<>();
 
  Select dropdown = new Select(select);
  dropdown.deselectAll();
  dropdown.selectByValue("two");
  selectedValues = dropdown.getAllSelectedOptions();
  for(WebElement value: selectedValues)
  {
   System.out.println("Selected Value: "+value.getText());
  }
  Thread.sleep(3000);
 
  dropdown = new Select(multiSelect);
  dropdown.deselectAll();
  dropdown.selectByValue("three");
  dropdown.selectByValue("four");
  selectedValues = dropdown.getAllSelectedOptions();
  for(WebElement value: selectedValues)
  {
   System.out.println("Selected Value: "+value.getText());
  }
  Thread.sleep(3000);
  dropdown.deselectAll();
  Thread.sleep(3000);
 }
}