[Selenium+Java] Cross Browser Testing using Selenium WebDriver

來源:互聯網
上載者:User

標籤:programs   AC   http   stat   boa   tin   pack   Suite   ebe   

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

What is Cross Browser Testing?

Cross Browser Testing is a type of functional test to check that your web application works as expected in different browsers.

Why do we need Cross Browser Testing?

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

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

 

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

This motive can be 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 Selenium WebDriver.

Your testing.xml will look like that,

This testing.xml will map with the Test Case which will look like that

Here because the testing.xml has two Test tags (‘ChromeTest‘,‘FirefoxTest‘),this test case will execute two times for 2 different browsers.

First Test ‘ChromeTest‘ will pass the value of parameter ‘browser‘ as ‘chrome‘ so ChromeDriver will be executed. This test case will run on Chrome browser.

Second Test ‘FirefoxTest‘ will pass the value of parameter ‘browser‘ as ‘Firefox‘ so FirefoxDriver will be executed. This test case will 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 instanceSystem.setProperty("webdriver.firefox.marionette", ".\\geckodriver.exe");driver = new FirefoxDriver();}//Check if parameter passed as ‘chrome‘else if(browser.equalsIgnoreCase("chrome")){//set path to chromedriver.exeSystem.setProperty("webdriver.chrome.driver",".\\chromedriver.exe");//create chrome instancedriver = new ChromeDriver();}//Check if parameter passed as ‘Edge‘else if(browser.equalsIgnoreCase("Edge")){//set path to Edge.exeSystem.setProperty("webdriver.edge.driver",".\\MicrosoftWebDriver.exe");//create Edge instancedriver = new EdgeDriver();}else{//If no browser passed throw exceptionthrow new Exception("Browser is not correct");}driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);}@Testpublic void testParameterWithXML() throws InterruptedException{driver.get("http://demo.guru99.com/V4/");//Find user nameWebElement userName = driver.findElement(By.name("uid"));//Fill user nameuserName.sendKeys("guru99");//Find passwordWebElement password = driver.findElement(By.name("password"));//Fill passwordpassword.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 is a technique to test web application with different web browsers.
  2. Selenium can support different type of browsers for automation.
  3. Selenium can be 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 was built & tested on selenium 3.0.1, Chrome 56.0.2924.87 , Firefox 47.0.2 & Microsoft Edge 14.14393. If the programs give an error, please update the driver

Download the Selenium Project Files for the Demo in this Tutorial

[Selenium+Java] Cross Browser Testing using Selenium WebDriver

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.