Cross Browser Testing using Selenium WebDriver_Selenium online Training

In this post I am going to explain how can we do Cross Browser Testing using Selenium.

If we are using Selenium WebDriver,In order to execute test cases using different browsers like Firefox, Chrome, Opera ,Internet Explorer we have to use TestNG framework.

 Test.xml :
In TestNG, if we want execute same test case with different browsers we have to use Test.xml file.

Sample Code:
<suite name="CrossBrowserTesting" preserve-order="true" parallel="false" >
   
<test name="Smoketest" preserve-order="true">

<parameter name="browserName" value="firefox"></parameter>
<!-- 
<parameter name="browserName" value="ie"></parameter>
<parameter name="browserName" value="chrome"></parameter>
<parameter name="browserName" value="opera"></parameter>
 -->
<classes>

<class name="Configuration.HMSLogin"></class> 
  
</classes>

</test>

</suite>

BrowserType.java

We have to pass "browserName" parameters from Test.xml and it will map with the BrowserType.java using @Parameters  annotation.
And the BrowserType.java will look like below
Sample Code:
import java.io.IOException;
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.openqa.selenium.opera.OperaDriver;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Parameters;

public class BrowserType {

public static WebDriver driver;
@BeforeClass
@Parameters("browserName")
public void setup(String browserName) throws IOException, InterruptedException{

//open Firefox Browser 
//open the browser based on parameter passed in .xml file.
if(browserName.equalsIgnoreCase("firefox")){

//if you are using selenium 2.0
driver = new FirefoxDriver();

//if you are using selenium 3.0
//System.setProperty("webdriver.gecko.driver", "path/to/geckodriver.exe");
//driver = new FirefoxDriver();
}
else if(browserName.equalsIgnoreCase("chrome")){

//open chrome browser
System.setProperty("webdriver.chrome.driver", "E:/Selenium Training/Driver Files/chromedriver.exe");
driver= new ChromeDriver();
}

else if(browserName.equalsIgnoreCase("ie")){
//open Internet Explorer Browser
System.setProperty("webdriver.ie.driver", "E:/Selenium Training/Driver Files/IEDriverServer.exe");
driver= new InternetExplorerDriver();

}
else if(browserName.equalsIgnoreCase("opera")){
//open opera Browser
System.setProperty("webdriver.opera.driver", "E:/Selenium Training/Driver Files/operadriver.exe");
driver= new OperaDriver();

}
//maximizing window
driver.manage().window().maximize();

//Navigating URL
driver.get("http://selenium4testing.com/hms/index.php?");

}
}

Testcase:
We have to extends BrowserType class to each test-suite and it will look like below
Sample Testcase code:
import org.openqa.selenium.By;
import org.testng.annotations.Test;

public class HMSLogin extends BrowserType {
@Test(priority=1)
public void Login() {

driver.findElement(By.xpath("//input[@name='username']")).clear();
driver.findElement(By.xpath("//input[@name='username']")).sendKeys("admin");

driver.findElement(By.xpath("//input[@name='password']")).clear();
driver.findElement(By.xpath("//input[@name='password']")).sendKeys("admin");

driver.findElement(By.xpath("//input[@name='submit']")).click();

}

}


finally execute the Test-suite by using Test.xml file.