The term cross-browser testing can be applied to both multi-browser testing and compatibility testing. Testing the web application with multiple web browsers is defined as cross-browser testing. A lack of cross-browser testing results in layout and functionality issues. Manually testing an application needs a lot of human effort and time to finish a complex job, but automated tests are carried out to avoid such issues. In general, most of the cross-browser issues are generated while rendering web page elements, which results in a functional and UI-based mess. Selenium WebDriver provides excellent support for automating test cases with the most popular browsers, using their own drivers. Selenium cross-browser tests can also be automated on the cloud using web application tools, such as SauceLabs, BrowserStack, and TestingBot.
In this chapter, we will cover the following topics:
Selenium WebDriver handles browser compatibility tests on almost every popular browser, including Chrome, Firefox, Internet Explorer, Safari, and Opera. In general, every browser's JavaScript engine differs from the others, and each browser interprets the HTML tags differently. The WebDriver API drives the web browser as the real user would drive it. By default, FirefoxDriver comes with the selenium-server-standalone.jar
library added, but for Chrome, IE, and Opera, there are libraries that need to be added or instantiated externally.
Let's see how we can instantiate each of the following browsers through its own driver:
selenium-server-standalone
library is bundled with FirefoxDriver to initialize and run tests in a Firefox browser. FirefoxDriver is added to the Firefox profile as a file extension on starting a new instance of FirefoxDriver. Please check the Firefox versions and its suitable drivers at http://selenium.googlecode.com/git/java/CHANGELOG.The following is the code snippet to kick start Mozilla Firefox:
WebDriver driver = new FirefoxDriver();
System.setProperty("webdriver.chrome.driver","C:\chromedriver.exe"); WebDriver driver = new ChromeDriver();
To download ChromeDriver, refer to http://chromedriver.storage.googleapis.com/index.html.
IEDriverServer
is an executable file that uses the WebDriver wire protocol to control the IE browser in Windows. Currently, IEDriverServer supports the IE versions 6, 7, 8, 9, and 10. The following code snippet helps you to instantiate IEDriverServer:System.setProperty("webdriver.ie.driver","C:\IEDriverServer.exe"); DesiredCapabilities dc = DesiredCapabilities.internetExplorer(); dc.setCapability(InternetExplorerDriver.INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS, true); WebDriver driver = new InternetExplorerDriver(dc);
To download IEDriverServer, refer to http://selenium-release.storage.googleapis.com/index.html.
The following code snippet helps you to instantiate SafariDriver:
WebDriver driver = new SafariDriver();
The following code snippet helps you to instantiate OperaChromiumDriver:
DesiredCapabilities capabilities = new DesiredCapabilities(); capabilities.setCapability("opera.binary", "C://Program Files (x86)//Opera//opera.exe"); capabilities.setCapability("opera.log.level", "CONFIG"); WebDriver driver = new OperaDriver(capabilities);
To download OperaChromiumDriver, refer to https://github.com/operasoftware/operachromiumdriver/releases.
TestNG (Next Generation) is one of the most widely used unit-testing frameworks implemented for Java. It runs Selenium-based browser compatibility tests with the most popular browsers. The Eclipse IDE users must ensure that the TestNG plugin is integrated with the IDE manually. However, the TestNG plugin is bundled with IntelliJ IDEA as default. The testng.xml
file is a TestNG build file to control test execution; the XML file can run through Maven tests using POM.xml
with the help of the following code snippet:
<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <version>2.12.2</version> <configuration> <suiteXmlFiles> <suiteXmlFile>testng.xml</suiteXmlFile> </suiteXmlFiles> </configuration> </plugin>
To create a testng.xml
file, right-click on the project folder in the Eclipse IDE, navigate to TestNG | Convert to TestNG, and click on Convert to TestNG, as shown in the following screenshot:
The testng.xml
file manages the entire test. It acts as a mini data source by passing the parameters directly into the test methods. The location of the testng.xml
file is shown in the following screenshot:
As an example, create a Selenium project (for example, Selenium Essentials
) along with the testng.xml
file, as shown in the previous screenshot. Modify the testng.xml
file with the following tags:
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd"> <suite name="Suite" verbose="3" parallel="tests" thread-count="5"> <test name="Test on Firefox"> <parameter name="browser" value="Firefox" /> <classes> <class name="package.classname" /> </classes> </test> <test name="Test on Chrome"> <parameter name="browser" value="Chrome" /> <classes> <class name="package.classname" /> </classes> </test> <test name="Test on InternetExplorer"> <parameter name="browser" value="InternetExplorer" /> <classes> <class name="package.classname" /> </classes> </test> <test name="Test on Safari"> <parameter name="browser" value="Safari" /> <classes> <class name="package.classname" /> </classes> </test> <test name="Test on Opera"> <parameter name="browser" value="Opera" /> <classes> <class name="package.classname" /> </classes> </test> </suite> <!-- Suite -->
Download all the external drivers except FirefoxDriver and SafariDriver, extract the zipped folders, and locate the external drivers as mentioned in the preceding snippets for each browser.
Refer to Chapter 1, The Selenium IDE, to learn how to set the path on different platforms.
The following Java snippet will explain how you can get parameters directly from the testng.xml
file and how you can run cross-browser tests as a whole:
@BeforeTest @Parameters({"browser"}) public void setUp(String browser) throws MalformedURLException { if (browser.equalsIgnoreCase("Firefox")) { System.out.println("Running Firefox"); driver = new FirefoxDriver(); } else if (browser.equalsIgnoreCase("chrome")) { System.out.println("Running Chrome"); System.setProperty("webdriver.chrome.driver", "C:\chromedriver.exe"); driver = new ChromeDriver(); } else if (browser.equalsIgnoreCase("InternetExplorer")) { System.out.println("Running Internet Explorer"); System.setProperty("webdriver.ie.driver", "C:\IEDriverServer.exe"); DesiredCapabilities dc = DesiredCapabilities.internetExplorer(); dc.setCapability(InternetExplorerDriver.INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS, true); //If IE fail to work, please remove this line and remove enable protected mode for all the 4 zones from Internet options driver = new InternetExplorerDriver(dc); } else if (browser.equalsIgnoreCase("safari")) { System.out.println("Running Safari"); driver = new SafariDriver(); } else if (browser.equalsIgnoreCase("opera")) { System.out.println("Running Opera"); // driver = new OperaDriver(); --Use this if the location is set properly-- DesiredCapabilities capabilities = new DesiredCapabilities(); capabilities.setCapability("opera.binary", "C://Program Files(x86)//Opera//opera.exe"); capabilities.setCapability("opera.log.level", "CONFIG"); driver = new OperaDriver(capabilities); } }
SafariDriver is not yet stable. A few of the major issues in SafariDriver are as follows: