[Selenium+Java] Cross Browser Testing using Selenium WebDriver

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 BrowserTestingof 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 integrateTestngframework with Selenium WebDriver.

Your testing.xml will look like that,

This testing.xml will map with theTest Casewhich 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 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 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

原文地址:https://www.cnblogs.com/alicegu2009/p/9082453.html

时间: 2024-08-06 07:17:23

[Selenium+Java] Cross Browser Testing using Selenium WebDriver的相关文章

[Selenium+Java] Handling AJAX Call in Selenium Webdriver

Original URL: https://www.guru99.com/handling-ajax-call-selenium-webdriver.html Handling AJAX Call in Selenium Webdriver Ajax is a technique used for creating fast and dynamic web pages. This technique is asynchronous and uses a combination of Javasc

软件测试之Selenium Java WebDriver

编写Selenium Java WebDriver程序,测试inputgit.csv表格中的学号和git地址的对应关系 package selenium2; import java.util.regex.Pattern; import java.util.concurrent.TimeUnit; import org.junit.*; import org.junit.runner.RunWith; import org.junit.runners.Parameterized; import o

Selenium Java WebDriver 使用

一. Firefox安装Selenium插件 在FireFox的菜单中的附加组件中搜索Selenium IDE 然后安装 二. 使用Selenium IDE录制脚本/导出脚本 点击图中标志打开Selenium IDE 红色按钮按下表示正在录制,这时候只用将界面切换到Firefox,网址中输入www.baidu.com,然后再搜索框中输入文字,点击搜索,所有的控件的访问都会被记录下来,然后切换回seleniumIDE就可以看到已经录制完毕 然后在图中红色选中的区域可以调整重新执行的速度,蓝色选中区

[Selenium+Java] Selenium with HTMLUnit Driver &amp; PhantomJS

Original URL: https://www.guru99.com/selenium-with-htmlunit-driver-phantomjs.html HTMLUnitDriver & PhantomJS for Selenium Headless Testing Selenium Web driver is a web automation tool which enables you to run the tests against different browsers. The

Functional testing - python, selenium and django

Functional testing  - python selenium django - Source Code : from selenium import webdriverfrom selenium.webdriver.common.by import By from django.test import LiveServerTestCase class Browser():  #(Run more than one testcases on one browser) browser

selenium + java + testNG 自动化环境搭建

kSelenium终极自动化测试环境搭建(一)Selenium+Eclipse+Junit+TestNG 第一步 安装JDK JDk1.7. 下载地址:http://www.oracle.com/technetwork/java/javase/downloads/jdk7-downloads-1880260.html 一路猛击'下一步',OK.安装完成后配置环境变量: JAVA_HOME = E:\Java\Java\jdk1.7.0_15 PATH = %JAVA_HOME%\bin CLAS

selenium+java的常使用的一些操作

常用操作均在下面的有测试用例有体现. 1 package CRM; 2 3 import static org.testng.Assert.assertEquals; 4 5 import java.awt.Checkbox; 6 import java.sql.Driver; 7 import java.util.Iterator; 8 import java.util.List; 9 import java.util.Set; 10 import java.util.concurrent.D

自动化测试【Maven+Eclipse+Selenium+Java环境搭建和测试】

一.下载必要的文件 1.eclipse Eclipse官网 2.jdk jdk官网 3.selenium IDE.Selenium Server.Selenium Client Drivers(Java)等等 Selenium下载地址  备注:需要代理服务器才能下载 我使用的是太太猫 4.maven安装.配置等 二.安装    1.Eclipse解压缩就可以用了    2.jdk安装.配置变量等    3.Selenium相关的安装    4.maven 最新版本的Eclipse已经自带mave

selenium第一课(selenium+java+testNG+maven)

selenium介绍和环境搭建 一.简单介绍 1.selenium:Selenium是一个用于Web应用程序测试的工具.Selenium测试直接运行在浏览器中,就像真正的用户在操作一样.支持的浏览器包括IE.Mozilla Firefox.Chrome等.支持自动录制动作和自动生成,Net.Java.Python等不同语言的测试脚本.Selenium 测试脚本可以在 Windows.Linux 和 Macintosh等多种平台上运行. 2.TestNG:TestNG是一个测试框架,其灵感来自JU