Cross Browser Automation using Gauge
Web Automation — Cross Browser
Introduction
When you are developing a web based application, you will need to test it in multiple browsers and platforms. So if you are going to test all the functionalities in each browser manually, you may need a huge amount of time to complete those tests. Further, it may not gain any ROI (Return on invest).
To overcome that kind of situations, you will need to automate those tests with a customized driver class where all the drivers of the required browsers implemented.
Here I will guide you how to do that using Gauge framework.
I am not using any kind of geckodriver.exe to run the tests. It is not recommended to have .exe files on the project code base. Also it will not work in Ubuntu and Mac OS.
I am using Bonigarcia WebDriver Manager to implement the driver class.
Implementation
- First you need to add the below dependencies to your pom.xml
<dependencies>
<dependency>
<groupId>com.thoughtworks.gauge</groupId>
<artifactId>gauge-java</artifactId>
<version>0.6.8</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>3.12.0</version>
</dependency>
<dependency>
<groupId>io.github.bonigarcia</groupId>
<artifactId>webdrivermanager</artifactId>
<version>2.2.0</version>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>22.0</version>
</dependency>
<dependency>
<groupId>org.reflections</groupId>
<artifactId>reflections</artifactId>
<version>0.9.10</version>
</dependency>
</dependencies>
2. Then you need to add an environment property to the default.properties class inside the env/default folder.
# Browser
browser = chrome
3. Below is the DriverFactory class I have implemented.
package com.maxsoft.webautomation.util.driver;
import io.github.bonigarcia.wdm.ChromeDriverManager;
import io.github.bonigarcia.wdm.EdgeDriverManager;
import io.github.bonigarcia.wdm.FirefoxDriverManager;
import io.github.bonigarcia.wdm.InternetExplorerDriverManager;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.edge.EdgeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.openqa.selenium.safari.SafariDriver;
/**
* Project Name : Web-Cross-Browser-Automation-Demo
* Developer : Osanda Deshan
* Version : 1.0.0
* Date : 8/25/2018
* Time : 2:37 PM
* Description :
**/
public class DriverFactory {
private static final String BROWSER = System.getenv("browser");
private static final String CHROME = "chrome";
private static final String FIREFOX = "firefox";
private static final String IE = "ie";
private static final String EDGE = "edge";
private static final String SAFARI = "safari";
private static WebDriver driver;
// Get a new WebDriver Instance.
// There are various implementations for this depending on browser. The required BROWSER can be set as an environment variable.
// Refer http://getgauge.io/documentation/user/current/managing_environments/README.html
public static WebDriver getDriver() {
if (BROWSER == null) {
ChromeDriverManager.getInstance().setup();
return new ChromeDriver();
}
switch (BROWSER.toLowerCase())
{
case CHROME:
ChromeDriverManager.getInstance().setup();
return new ChromeDriver();
case FIREFOX:
FirefoxDriverManager.getInstance().setup();
return new FirefoxDriver();
case IE:
InternetExplorerDriverManager.getInstance().setup();
return new InternetExplorerDriver();
case EDGE:
EdgeDriverManager.getInstance().setup();
return new EdgeDriver();
case SAFARI:
driver = new SafariDriver();
return new SafariDriver();
default:
ChromeDriverManager.getInstance().setup();
return new ChromeDriver();
}
}
}
4. Next, implement the Driver class which uses DriverFactory methods inside execution hooks.
package com.maxsoft.webautomation.util.driver;
import com.thoughtworks.gauge.AfterSuite;
import com.thoughtworks.gauge.BeforeSuite;
import org.openqa.selenium.WebDriver;
/**
* Project Name : Web-Cross-Browser-Automation-Demo
* Developer : Osanda Deshan
* Version : 1.0.0
* Date : 8/25/2018
* Time : 2:37 PM
* Description :
**/
public class Driver {
// Holds the WebDriver instance
public static WebDriver driver;
// Initialize a driver instance of required browser
// Since this does not have a significance in the application's business domain, the BeforeSuite hook is used to instantiate the driver
@BeforeSuite
public void initializeDriver(){
driver = DriverFactory.getDriver();
driver.manage().window().maximize();
}
// Close the driver instance
@AfterSuite
public void closeDriver(){
driver.quit();
}
}
5. Here is a sample repository for you to get started.
Configurations
You may need to do below configurations if you need to use Internet Explorer 11 on Windows.
- Open Registry Editor (Windows Key + R → Type regedit → Press Enter).
- For 32-bit Windows installations, the key you must examine in the registry editor is
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Internet Explorer\Main\FeatureControl\FEATURE_BFCACHE - For 64-bit Windows installations, the key is
HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\Internet Explorer\Main\FeatureControl\FEATURE_BFCACHE - Please note that the FEATURE_BFCACHE subkey may or may not be present, and should be created if it is not present.
- Inside this key, create a DWORD value named iexplore.exe with the value of 0.
- Go to Internet Options on Internet Explorer.
7. Go to Security tab.
8. Check Enable Protected Mode for all zones (Internet, Local intranet, Trusted sites and Restricted sites).
9. Click on Apply button.
10. Click on OK button.
I have tested this on Windows 10 Core i7 computer using the following browsers.
Execution
You can run the tests by executing the following command on the command prompt/terminal.mvn test-compile gauge:execute -DspecsDir="<path_to_spec/s>" -Denv="<env>"
Ex: mvn test-compile gauge:execute -DspecsDir="specs/Login.spec" -Denv="QA"
Execution results will be saved inside reports folder. There will be mainly 3 types of reports available.
- HTML Report
- XML Report
- JSON Report
Happy Automation !!!