Cross Browser Testing using Selenium WebDriver

Cross Browser Testing using Selenium WebDriver to run Test Scripts in different browsers like Google Chrome,Firefox ,Internet Explorer etc at a same time then we have to use TestNG Framework for Automating Cross Browser Testing using Selenium WebDriver.

What is Cross Browser Testing?

Cross Browser Testing means testing the application under test(AUT) with different browsers in order to verify all functionalities are working same in all browsers.

Problems if not done Cross Browser Testing

1.Main defect is Design issue.
2.Some functionalities not supports javascript because of browser
3.Some functionalities will not work in some browsers because of configurations.
4.Some time facing issues while uploading/Downloading file formats.
5.Faacing the problems with additional plugins like displaying grid details in applications.
6.Some functionalities will not work changing the compatibility mode in browsers.

ALSO READ  How to Add Screenshots to TestNG Report

In order to over come above some problems we have to perform Cross Browser Testing for applications under test(AUT).

Also Read Run Selenium Script using runnable JAR

Pre-Requisites

1.testng.xml
2.Different browsers drivers(IE Driver,Chrome Driver etc).
3.Test Script class for test cases.
4.Declaring the browsers in a class.
5.@Parameters annotation which Describes how to pass parameters to a @Test method,where we are writing our Test Cases  i.e Test Scripts.

Example for Cross Browser Testing

Prepare textng.xml file to set up parameters for different browsers,follow same code as below.

<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Cross Browser Testing" preserve-order="true" verbose="2"
parallel="tests">
  
<parameter name="Weburl" value="http://www.google.com"></parameter>
  
<test name="Internet Explorer">
<parameter name="WebBrowser" value="ie"></parameter>
<classes>
<class name="com.wiki.wiki.Cross" />
</classes>
</test>
<test name="Google Chrome">
<parameter name="WebBrowser" value="chrome"></parameter>
<classes>
<class name="com.wiki.wikishown.Cross" />
</classes>
</test>
<test name="Firefox">
<parameter name="WebBrowser" value="firefox"></parameter>
<classes>
<class name="com.wiki.wikishown.Cross" />
</classes>
</test> 
</suite>

Now create two classes one class is for driver initialization and Browser setting and second clas is for Test Scripts under @Test annotaition’s,follow below classesCreate SelDriver.java class to initialize the driver and browsers set up as below.

ALSO READ  Verify element is enabled in Selenium WebDriver

Also Read Send Extent Reports in Email with Screenshots

package gmail.automation;

import java.util.concurrent.TimeUnit;
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.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Parameters;

public class SelDriver {
public static WebDriver driver;
static String baseUrl;

@BeforeTest@Parameters("WebBrowser")
public void setup(String WebBrowser){
//Adding verfication point using IF Condition
if(WebBrowser.equalsIgnoreCase("firefox")){
driver = new FirefoxDriver(); 
}else if(WebBrowser.equalsIgnoreCase("ie"))
{ 
//For ie browser we have to use System.SetProperty 
System.setProperty("webdriver.ie.driver", "F:\Java_Applications\Zip Files\IEDriverServer.exe"); 
driver = new InternetExplorerDriver(); 
}else if(WebBrowser.equalsIgnoreCase("chrome")){ 
//For Google chrome browser we have to use System.setProperty and Chrome Driver 
System.setProperty("webdriver.chrome.driver", "F:\Java_Applications\Zip Files\chromedriver.exe"); 
driver = new ChromeDriver(); 
}
//Maximize the windows of all browsers
driver.manage().window().maximize();
}

@AfterTest
public void tearDown() {
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
}
}
}
}
}

Now i am using Wikishown class and using inheritance wit the help of extends keyword as below.

ALSO READ  Check if a checkbox is checked in selenium webdriver

Also Read Run Selenium Webdriver Scripts using Command Line

 

import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.testng.annotations.Parameters;
import org.testng.annotations.Test;

public class Cross extends SelDriver {

@Test(priority=1)@Parameters("Weburl")
public void testMethod(String Weburl){

//Navigate to URL and Weburl is taking from testng.xml file
driver.navigate().to(Weburl);
//wait until page loads
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
driver.findElement(By.liknText("Manual Testing")).click();
driver.manage().timeouts().implicitlyWait(40, TimeUnit.SECONDS);
driver.findElement(By.linkText("Selenium")).click();
}
}
}
}

Above in two classes i have used @Parameters annotation to call parameter name from xml file.Now run xml file as run as — TestNG Suite then it will run all @Test methods in a class and displays test execution results in Console as well as Test NG icon.

Rajesh K

Leave a Reply

Your email address will not be published. Required fields are marked *