[Selenium+java] Cross Browser testing using Selenium Webdriver

Source: Internet
Author: User
Tags throw exception testng guru99 microsoft edge



Original url:https://www.guru99.com/cross-browser-testing-using-selenium.html


What is the cross Browser testing?


Cross Browser testing are a type of functional test to check this your Web application works as expected in differ ENT browsers.





Why does we need cross Browser testing?


web-based applications is totally different from Windows applications. A Web application can opened in any browser by the end user. For example, some people prefer to open http://twitter.comin Firefox browser, while other ' s can is using Chrome Browser or IE.



In the diagram below you can observe this in IE, the login box of Twitter was not showing curve at all corners, BU T we is able to see it in Chrome browser.









So we need to ensure that the Web application would work as expected in all popular browsers so this more people can access It and use it.



This motive can is fulfilled with cross Browser testing of the product.



Reason Cross Browser Issues


    1. Font size mismatch in different browsers.
    2. JavaScript implementation can be different.
    3. css,html validation difference can be there.
    4. Some Browser still not supporting HTML5.
    5. Page alignment and div size.
    6. Image orientation.
    7. Browser incompatibility with OS. ETC.
How to perform cross Browser testing


If we are using Selenium webdriver, we can automate test cases using Internet Explorer, FireFox, Chrome, Safari browsers.



To execute test cases with different browsers in the same machine at the same time we can integratetestng framework with S Elenium Webdriver.



Your Testing.xml would look like that,






This testing.xml would map with the Test case which would look like that






Here because the Testing.xml have both Test tags (' chrometest ', ' firefoxtest '), this Test case would execute with the Times for 2 di Fferent browsers.



First Test ' chrometest ' would pass the value of parameter ' browser ' as ' chrome ' so chromedriver would be executed. This test case would run on Chrome browser.



Second Test ' firefoxtest ' would pass the value of parameter ' browser ' as ' Firefox ' so firefoxdriver would be executed. This test case would run on FireFox browser.



Complete Code:



Guru99crossbrowserscript.java


package parallelTest;

import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.edge.EdgeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Parameters;
import org.testng.annotations.Test;

public class CrossBrowserScript {

	WebDriver driver;

	/**
	 * This function will execute before each Test tag in testng.xml
	 * @param browser
	 * @throws Exception
	 */
	@BeforeTest
	@Parameters("browser")
	public void setup(String browser) throws Exception{
		//Check if parameter passed from TestNG is ‘firefox‘
		if(browser.equalsIgnoreCase("firefox")){
		//create firefox instance
			System.setProperty("webdriver.firefox.marionette", ".\\geckodriver.exe");
			driver = new FirefoxDriver();
		}
		//Check if parameter passed as ‘chrome‘
		else if(browser.equalsIgnoreCase("chrome")){
			//set path to chromedriver.exe
			System.setProperty("webdriver.chrome.driver",".\\chromedriver.exe");
			//create chrome instance
			driver = new ChromeDriver();
		}
		//Check if parameter passed as ‘Edge‘
				else if(browser.equalsIgnoreCase("Edge")){
					//set path to Edge.exe
					System.setProperty("webdriver.edge.driver",".\\MicrosoftWebDriver.exe");
					//create Edge instance
					driver = new EdgeDriver();
				}
		else{
			//If no browser passed throw exception
			throw new Exception("Browser is not correct");
		}
		driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
	}
	
	@Test
	public void testParameterWithXML() throws InterruptedException{
		driver.get("http://demo.guru99.com/V4/");
		//Find user name
		WebElement userName = driver.findElement(By.name("uid"));
		//Fill user name
		userName.sendKeys("guru99");
		//Find password
		WebElement password = driver.findElement(By.name("password"));
		//Fill password
		password.sendKeys("guru99");
	}
}


Testing.xml


<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">

<suite name="TestSuite" thread-count="2" parallel="tests" >

<test name="ChromeTest">

<parameter name="browser" value="Chrome" />

<classes>

<class name="parallelTest.CrossBrowserScript">

</class>

</classes>

</test>

<test name="FirefoxTest">

<parameter name="browser" value="Firefox" />

<classes>

<class name="parallelTest.CrossBrowserScript">

</class>

</classes>

</test>

<test name="EdgeTest">

<parameter name="browser" value="Edge" />

<classes>

<class name="parallelTest.CrossBrowserScript">

</class>

</classes>

</test>

</suite>


Note: To run the test, right click on the testing.xml, Select Run as, and click TestNG






Summary


    1. Cross browser testing are a technique to test Web application with different Web browsers.
    2. Selenium can support different type of browsers for automation.
    3. Selenium can integrated with TestNG to perform Multi Browser testing.
    4. From parameters in Testing.xml we can pass browser name, and in a test case, we can create webdriver reference accordingly .


Note: The given program is built & tested on Selenium 3.0.1, Chrome 56.0.2924.87, Firefox 47.0.2 & Microsoft Edge 14.1 4393. If the programs give an error, please update the driver



Download the Selenium Project Files for the Demo on this Tutorial



[Selenium+java] Cross Browser testing using Selenium Webdriver


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.