Sunday, January 17, 2016

Selenium code to work with different browsers

In this article on Selenium codes, we will explain how to create a function to run test on different browsers using selenium webdriver in Java.

In each of the examples explained in selenium code series, we will define a problem statement, Pre-condition and then the solution for the problem:


Let us start with the first problem :


Problem Statement: We need to create a generic function to create an instance of webdriver in different browsers and perform following actions:


1. Launch the application in one of the browser from IE, Firefox and chrome.

2. Navigate to the web application.

3. verify the title for the Page.


Pre-condition :

1. The jar files for selenium should be downloaded and added as external file in the build path for the project.

2. Server exe files for chrome and internet explorer and downloaded.


Below code explains how to create a method using selenium webdriver for launching different browsers.

import java.io.File;
import org.openqa.selenium.*;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;


public class Testingbrowser {
 
//Define the Webdriver Object driver. Defining this globally can help this driver to be used in the methods in the class
   WebDriver driver;

//This is the main method where we will call other methods in the test.   
 public static void main(String[] args) throws InterruptedException
 { 
  //There are two part in the flow for which we created the required methods 
  Testingbrowser Tb = new Testingbrowser();
  //provide the name of browser to run test in
  Tb.LaunchBrowser("chrome");
  //Call the functon to launch the application and validating the title of application
  Tb.LaunchAppandValidate("https://seleniumbites.blogspot.com", "Selenium WebDriver Automation concepts");
 }
 
 
//Function to launch required browser 
 public void LaunchBrowser (String strBrowser)
 {
//Using an if else loop to launch the required browser, we will provide value from ie, chrome, and firefox.
// We can also use enum to provide the browser name in the test.
  if (strBrowser.trim().toLowerCase().contentEquals("ie"))
  {
//Provide the file path for IE Server
   File file = new File("D:\\Selenium_Automation\\IEDriverServer.exe");
   System.setProperty("webdriver.ie.driver", file.getAbsolutePath()); 
   driver = new  InternetExplorerDriver();
  }

  else if (strBrowser.trim().toLowerCase().contentEquals("firefox"))
  {
   driver = new  FirefoxDriver();
  }
  else if (strBrowser.trim().toLowerCase().contentEquals("chrome"))
  {
//Provide the file path for Chrome drive
       System.setProperty("webdriver.chrome.driver", "D:\\Selenium_Automation\\chromedriver.exe");
       DesiredCapabilities capabilities = DesiredCapabilities.chrome();
       ChromeOptions options = new ChromeOptions();
       options.addArguments("test-type");
//Provide location where chrome is currently installed.  
       String Chrome_Path = "C:\\Program Files\\Google\\Chrome\\Application\\chrome.exe";
       capabilities.setCapability("chrome.binary",Chrome_Path);
       capabilities.setCapability(ChromeOptions.CAPABILITY, options);
       driver = new ChromeDriver(capabilities);
  }
  else
  {
//In case browser name in not from the list, print the below message
     System.out.println("Please select a browser from IE/Firefox or chrome");
  }

 }
 
//Function to perform required action on the application 
 public void LaunchAppandValidate(String url, String strTextinTitle)
 {
  try
  {
//open the required url 
         driver.get(url);
//once the url is launched, verify the title contains the required text.
//Note : In case we require exact match , we will use contentequals instead of contains
   if (driver.getTitle().contains(strTextinTitle))
   {
         System.out.println("Title of the Page contains text :" + strTextinTitle );  
   }
   else
   {
         System.out.println("Title of the Page does not contains text :" + strTextinTitle );  
   }
  }
  catch(Exception e)
  {
        System.out.println(e.getClass());
  }
 }
 
}

No comments:

Post a Comment