Introduction
Gauge is a free and open-source framework mainly gaining attention as a cross-browser test framework. It is a new Behavior Driven Development based test framework released by Selenium, ThoughtWorks. It supports all major programming languages and also provides an HTML report plug-in which generates a very beautiful report with clear instructions of failures and success test cases. The gauge can easily be integrated with different IDE’s like IntelliJ, Visual Studio, etc, and with CI servers for continuous functional testing.
To get started with Gauge, let’s try running a sample Gauge example from Command Line itself.
Download the latest release of Gauge compatible with your operating system. To verify the version of Gauge installed, run command: “gauge --version”. Gauge framework provides the list of supported templates which can be seen by running the command: “gauge init --templates”.
In this article, we would be using a Java template to understand the gauge framework. To initialise the sample Java project, run command: “gauge init java”.
You can install the HTML report plug-in using the command: “gauge install html-report” for generating HTML reports. In the Gauge framework we run our specs file which is similar to the feature file of the BDD framework. Use the command: “gauge run specs” to run your project spec file. For each specification, we have its implementation for which we create a StepImplementation Java class file.
You might have questions in mind about how these specifications and implementations work together? Let’s have a look at these terms with examples.
Specification
As the feature file in the BDD framework is written in the Gherkin language, similarly, in Gauge we have a spec file written using Markdown syntax. The components of specifications are:
- Specification heading
- Scenario
- Tags
- Step
- Parameters
- Comments
Example of a spec file:
# User Authentication
Testing of a new login page on production
## Login Page Test
Tags: SmokeTest
* Enter email as "thatperfectview@gmail.com"
* Enter password as "ramit1995"
* Click Login button
In this spec file, “User Authentication” is the specification heading which can be written using one of the following syntax:
# User Authentication
OR
User Authentication
===============
In spec file, “Login Page Test” is a scenario which can be written using one of the following syntax:
## Login Page Test
OR
Login Page Test
----------------------
A scenario starts after the specification heading. A single spec file can have multiple scenarios. Each scenario represents the single workflow by providing the steps to test.
In the above spec file, we have 3 steps under the scenario, each step preceded with asterisk (*):
* Enter email as "thatperfectview@gmail.com"
* Enter password as "ramit1995"
* Click Login button
We also have a Tags keyword used in our spec file which is used for tagging the specifications and scenarios. The primary purpose of these Tags is to search or filter specifications and scenarios using the particular tags. We can also run our spec file by using tag name:
gauge run --tags "SmokeTest" specs
Any statement/text found in plain text without any syntax is a comment in the spec file. Comments can be used for better understanding and readability of spec files.
We have taken “thatperfectview@gmail.com” and “ramit1995” as parameter values for the variables defined in StepImplementation file. These values in the spec file are always declared in double quotes.
Step Implementation
Using any programming language, you can implement the steps for your specification. Let’s have a look at the implementation of above spec file:
@Step("Enter email as <email>")
public void enter_email(String email) throws InterruptedException {
driver.get("http://www.testyou.in/Login.aspx");
WebElement emailaddress = driver.findElement(By.id("ctl00_CPHContainer_txtUserLogin"));
emailaddress.sendKeys(email);
}
@Step("Enter password as <password>")
public void enter_password(String password) throws InterruptedException {
WebElement passwrd = driver.findElement(By.id("ctl00_CPHContainer_txtPassword"));
passwrd.sendKeys(password);
}
@Step("Click Login button")
public void loginTest() throws InterruptedException {
WebElement login = driver.findElement(By.name("ctl00$CPHContainer$btnLoginn"));
login.click();
String title = driver.getTitle();
assertEquals(title, "Student Dashboard | Test Maker - TestYou");
System.out.println("Successful Login");
Thread.sleep(2000);
driver.quit();
}
Gauge Hooks
Gauge Hooks are blocks of code that can run at various points in the Gauge test life cycle. It allows better management of code workflow and helps reduce redundancy in code.
Following are the hooks available in Gauge framework:
- @BeforeSuite: Runs before test suite
- @AfterSuite: Runs after test suite
- @BeforeSpec: Runs before specifications
- @AfterSpec: Runs after specifications
- @BeforeScenario: Runs before each scenario
- @AfterScenario: Runs after each scenario
- @BeforeStep: Runs before each step
- @AfterStep: Runs after each step
Maven project for Java Gauge Framework
Since we have taken an overview of the Gauge framework, now let’s have a look, how we can create a maven project for the Gauge framework. Below are the steps to be followed to create new Maven project:
- Navigate to File and select new Project
- Select Project type as Maven
- Create a Maven project from archetype-> Add archetype with:
- GroupId: com.thoughtworks.gauge.maven
- ArtifactId: gauge-archetype-selenium
- Version: 1.4.0
- After the Gauge Maven archetype is added, select the same from the list
- Add your desired names to GroupID and ArtifactID
Now let’s try performing parallel execution of our specifications on different browsers.
After you have created a Maven project, you get a folder named “env” in which you can set your test execution environment.
Create a new directory as “Chrome”, in this directory, you can create a new File as “localchrome.properties” and add the below environment in file:
BROWSER = chrome
Similarly, create a new directory as “Firefox”, in this directory, you can create a new File as “localfirefox.properties” and add the below environment in file:
BROWSER = firefox
Once you have set an environment, you can now create a Java class file for setting up your WebDriver for different environment.
File: DriverFactory.java
public class DriverFactory {
private static WebDriver driver= null;
public static WebDriver getDriver() {
return driver;
}
@BeforeSpec
public void setUp() {
try{
if(System.getenv("BROWSER").equals("chrome"))
{
System.setProperty("webdriver.chrome.driver", "C:\\Users\\Lenovo-I7\\Desktop\\chromedriver.exe");
driver = new ChromeDriver();
}
else if(System.getenv("BROWSER").equals("firefox"))
{
System.setProperty("webdriver.gecko.driver", "C:\\Users\\Lenovo-I7\\Desktop\\geckodriver.exe");
driver = new FirefoxDriver();
}
}
catch (Exception e) {
System.out.println(e.getMessage());
}
}
@AfterSpec
public void tearDown() {
if (driver != null) {
driver.quit();
}
}
}
The DriverFactory class file will get your test configuration (chrome or Firefox) from the environment variables that are set in “env” properties files. Here, we have also used hooks to setup and teardown our test environment before and after each specification.
File: testYou.spec
User Authentication
=====================
Testing of a new login page on production
Login Page Test
--------------------
Tags: SmokeTest
* Enter email as "thatperfectview@gmail.com"
* Enter password as "ramit1995"
* Click Login button
File: StepImplementation_testYou.java
public class StepImplementation_testYou {
private final WebDriver driver;
public StepImplementation_testYou()
{
this.driver = DriverFactory.getDriver();
}
@Step("Enter email as <email>")
public void mailfield(String email) throws InterruptedException {
driver.get("http://www.testyou.in/Login.aspx");
WebElement emailaddress = driver.findElement(By.id("ctl00_CPHContainer_txtUserLogin"));
emailaddress.sendKeys(email);
}
@Step("Enter password as <password>")
public void enter_password(String password) throws InterruptedException {
WebElement passwrd = driver.findElement(By.id("ctl00_CPHContainer_txtPassword"));
passwrd.sendKeys(password);
}
@Step("Click Login button")
public void loginTest() throws InterruptedException {
WebElement login = driver.findElement(By.name("ctl00$CPHContainer$btnLoginn"));
login.click();
String title = driver.getTitle();
assertEquals(title, "Student Dashboard | Test Maker - TestYou");
System.out.println("Successful Login");
Thread.sleep(2000);
driver.quit();
}
}
Now, in your existing Maven pom.xml file add the below code along with the existing dependencies for parallel execution of your specifications.
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.7.0</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<groupId>com.thoughtworks.gauge.maven</groupId>
<artifactId>gauge-maven-plugin</artifactId>
<version>1.3.3</version>
<executions>
<execution>
<id>test-chrome</id>
<phase>test</phase>
<configuration>
<env>chrome</env>
<inParallel>true</inParallel>
<nodes>4</nodes>
<specsDir>specs</specsDir>
</configuration>
<goals>
<goal>execute</goal>
</goals>
</execution>
<execution>
<id>test-firefox</id>
<phase>test</phase>
<configuration>
<env>firefox</env>
<inParallel>true</inParallel>
<nodes>4</nodes>
<specsDir>specs</specsDir>
</configuration>
<goals>
<goal>execute</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
Note: Please make sure that you have more than one Specification and StepImplementation file to run in parallel. The above code helps you running your different specifications on chrome and Firefox browser.
Command to run the test: mvn test OR mvn clean install
HTML Report:
Summarizing it all!!
Gauge framework came up for writing and running acceptance testing and is also majorly used for cross browser testing. Since, it is sponsored by Selenium, ThoughtWorks and has similar features of BDD framework; it is being highly preferred by the Software Testing Company. It helps other team members like product managers, business analysts to understand the testing workflow in simple language without any depth of coding knowledge. It helps you generate a beautiful report that can be shared with the development team to understand the success and failures of test cases. So guys give it a try and do not skip any of the steps mentioned in this article.
Good Luck - Happy Testing :)
You may like to go through Desired Capabilities in Test Automation with Selenium