This tutorial explains the steps to run the Selenium tests for Chrome browser in headless mode. We are going to run the tests in Selenium 4 as well as Selenium 3.
What is headless browser?
A headless browser is like any other browser, but without a Head/GUI (Graphical User Interface). A headless browser is used to automate the browser without launching the browser. While the tests are running, we could not see the browser, but we can see the test results coming on the console.
Headless browser testing is generally faster when compared to actual UI testing as it doesn’t wait for the whole page to render before performing any action.
When we need to execute automated test cases remotely on a server or in any of the build and release pipelines for continuous integration servers like Gitlab or Jenkins, it is not always possible to install real browsers on such remote machines. We can use headless browsers to run automation tests efficiently.
It is easy to perform multi-tasking with a Headless browser. The browser or our machine can do anything else while the tests run in the background.
There are 2 ways to add dependencies to the Selenium project.
Once the Selenium and WebDriverManager folders are downloaded, unzip the folder. Once the zip file is extracted, reference these jar files in the project. For this, navigate to project properties and click Build Path-> Configure Build Path in Eclipse. Click “Add External Jars“. After clicking on the “Add External JARs“, selected all the extracted JARs. The JARs files are present in the project.
2. Add the below dependencies to pom.xml or build.gradle.
package com.example.steps;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxOptions;
public class ChromeOptionsHeadless_Demo {
public static void main(String[] args) {
// Create an object of Chrome Options class
ChromeOptions options = new ChromeOptions();
// Set Firefox Headless mode as TRUE
options.addArguments("--headless=new");
// Create an object of WebDriver class and pass the Chrome Options object
// as an argument
WebDriver driver = new ChromeDriver(options);
// Navigate to site url
driver.get("https://duckduckgo.com/");
System.out.println("Executing Chrome Driver in Headless mode..");
System.out.println("Page Title : " + driver.getTitle());
System.out.println("Page URL : " + driver.getCurrentUrl());
// Close the driver
driver.quit();
}
}
We know that to execute Selenium automation scripts on browsers like chrome or firefox, we must download the binary files of these drivers like chromedriverand geckodriver, etc. After this, we need to set the path to these binaries in the automation script or add the classpath location. Here, we want to execute Selenium WebDriver automation scripts on the Chrome browser, then you need first to download chromedriver.exe and then use the System.setProperty method to set its path as follows:
// Set the path of ChromeDriver
System.setProperty("webdriver.chrome.driver",
"C:\\Users\\Vibha\\Software\\chromedriver_win32_98.0.4758.102\\chromedriver.exe");
The complete program looks like as shown below:
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
public class ChromeOptionsHeadless1 {
public static void main(String[] args) {
// Set the path of ChromeDriver
System.setProperty("webdriver.chrome.driver",
"C:\\Users\\Vibha\\Software\\chromedriver_win32_98.0.4758.102\\chromedriver.exe");
// Create an object of Chrome Options class
ChromeOptions options = new ChromeOptions();
// pass the argument –headless to Chrome Options class.
options.addArguments("--headless");
// Create an object of Chrome Driver class and pass the Chrome Options object as
// an argument
ChromeDriver driver = new ChromeDriver(options);
System.out.println("Executing Chrome Driver in Headless mode..");
driver.get("https://duckduckgo.com/");
System.out.println("Title of Page :" + driver.getTitle());
System.out.println("Page URL : " + driver.getCurrentUrl());
// Close the driver
driver.close();
}
}
The output of the above program is
How to run headless Chrome Tests in Selenium using WebDriverManager?
WebDriverManager has an automated way to download browser executables(exes) or binaries. It supports different browsers like Chrome, Firefox, Microsoft Edge, Internet Explorer, Opera, or PhantomJS.
WebDriverManager.chromedriver().setup: checks for the latest version of the specified WebDriver binary. If the binaries are not present on the machine, then it will download the WebDriver binaries. Next, it instantiates the Selenium WebDriver instance with the ChromeDriver.
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import io.github.bonigarcia.wdm.WebDriverManager;
public class ChromeOptionsHeadless2 {
public static void main(String[] args) {
// WebDriverManager downloads chrome browser executables or binaries.
WebDriverManager.chromedriver().setup();
// Create an object of Chrome Options class
ChromeOptions options = new ChromeOptions();
// pass the argument –headless to Chrome Options class.
options.addArguments("--headless");
// Create an object of Chrome Driver class and pass the Chrome Options object as
// an argument
ChromeDriver driver = new ChromeDriver(options);
System.out.println("Executing Chrome Driver in Headless mode..");
driver.get("https://duckduckgo.com/");
System.out.println("Title of Page :" + driver.getTitle());
System.out.println("Page URL : " + driver.getCurrentUrl());
// Close the driver
driver.close();
}
}
The output of the above program is
Congratulations!! We are able to run Chrome tests in Selenium in headless mode.
This tutorial explains the steps to run the Selenium tests on Firefox browser in headless mode. We are going to run the tests in Selenium. To run the Selenium tests on Chrome browser in headless mode, refer this tutorial.
To start with, we need to add dependencies to the project.
Download the latest version of WebDriverManager (Download this if you want to use WebDriverManager to download browser executables(exes) or binaries automatically, then skip downloading FireFox Binary).
Selenium 4
Add the below dependencies to pom.xml or build.gradle.
Below is an example to run the Firefox tests in the headless mode.
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxOptions;
public class FirefoxOptionsHeadless_Demo {
public static void main(String[] args) {
// Create an object of Firefox Options class
FirefoxOptions options = new FirefoxOptions();
// Set Firefox Headless mode as TRUE
options.addArguments("-headless");
// Create an object of WebDriver class and pass the Firefox Options object
// as an argument
WebDriver driver = new FirefoxDriver(options);
// Navigate to site url
driver.get("https://duckduckgo.com/");
System.out.println("Executing Firefox Driver in Headless mode..");
System.out.println("Page Title : " + driver.getTitle());
System.out.println("Page URL : " + driver.getCurrentUrl());
// Close the driver
driver.quit();
}
}
The output of the above program is
Selenium 3
Add the below dependencies to pom.xml or build.gradle.
A headless browser is like any other browser, but without a GUI (Graphical User Interface). A headless browser is used to automate the browser without launching the browser. While the tests are running, we could not see the browser, but we can see the test results coming on the console. This makes the test execution faster than normal execution. This is suitable for parallel testing as UI tests needs a lot of memory and resources.
The path of Gecko Driver (used for Firefox browser) needs to be set up in the Test using System.setProperty().Here, we use the methods setHeadless(true) of the FirfoxOptions class provided by Selenium WebDriver.
The complete program looks like as shown below:
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxOptions;
public class FirefoxOptionsHeadless1 {
public static void main(String[] args) {
// Set the path of GeckoDriver
System.setProperty("webdriver.gecko.driver",
"C:\\Users\\Vibha\\Software\\geckodriver\\geckodriver.exe");
// Create an object of Firefox Options class
FirefoxOptions options = new FirefoxOptions();
// Set Firefox Headless mode as TRUE
options.setHeadless(true);
// Create an object of WebDriver class and pass the Firefox Options object
// as an argument
WebDriver driver = new FirefoxDriver(options);
// Navigate to site url
driver.get("https://duckduckgo.com/");
System.out.println("Executing Firefox Driver in Headless mode..");
System.out.println("Page Title : " + driver.getTitle());
System.out.println("Page URL : " + driver.getCurrentUrl());
// Close the driver
driver.close();
}
}
The output of the above program is
How to run headless Firefox Tests in Selenium using WebDriverManager?
WebDriverManager.firefoxdriver().setup(): checks for the latest version of the specified WebDriver binary. If the binaries are not present on the machine, then it will download the WebDriver binaries. In this case, it is not needed to download Firefox binary and set up the path
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxOptions;
import io.github.bonigarcia.wdm.WebDriverManager;
public class FirefoxOptionsHeadless2 {
public static void main(String[] args) {
WebDriverManager.firefoxdriver().setup();
// Create an object of Firefox Options class
FirefoxOptions options = new FirefoxOptions();
// Set Firefox Headless mode as TRUE
options.setHeadless(true);
// Create an object of Firefox Driver class and pass the Firefox Options object
// as an argument
WebDriver driver = new FirefoxDriver(options);
// Navigate to the url
driver.get("https://duckduckgo.com/");
System.out.println("Executing Firefox Driver in Headless mode..");
System.out.println("Page Title : " + driver.getTitle());
System.out.println("Page URL : " + driver.getCurrentUrl());
// Close the driver
driver.close();
}
}
Congratulations!! We have run the tests in headless mode in FireFox.
Featurefile should be saved as an extension of .feature. Add the test scenarios in this feature file. I have added sample test scenarios. The test scenarios are written inGherkins language. A feature file is created under src/test/resources.
Feature: Login to HRM
@ValidCredentials
Scenario: Login with valid credentials
Given User is on Home page
When User enters username as "Admin"
And User enters password as "admin123"
Then User should be able to login successfully
@InValidCredentials
Scenario Outline: Login with invalid credentials
Given User is on Home page
When User enters username as '<username>'
And User enters password as '<password>'
Then User should be able to see error message '<errorMessage>'
Examples:
|username |password |errorMessage |
|admin |admin |Invalid credentials |
| |admin123 |Username cannot be empty |
|Admin | |Password cannot be empty |
| | |Username cannot be empty |
@ForgetPassword
Scenario: Verify Forget Password Functionality
Given User is on Home page
When User clicks on Forgot your password link
Then User should be able to see new page which contains Reset Password button
Step 5 – Create the Step Definition class or Glue Code
Create a StepDefinition class for LoginPage.feature.
package com.example.definitions;
import com.example.steps.StepDashboardPage;
import com.example.steps.StepForgetPasswordPage;
import com.example.steps.StepLoginPage;
import io.cucumber.java.en.Given;
import io.cucumber.java.en.Then;
import io.cucumber.java.en.When;
import net.serenitybdd.annotations.Steps;
import org.junit.Assert;
import static org.junit.Assert.assertTrue;
public class LoginPageDefinitions {
@Steps
StepLoginPage loginPage;
@Steps
StepDashboardPage dashPage;
@Steps
StepForgetPasswordPage forgetpasswordPage;
@Given("User is on Home page")
public void openApplication() {
loginPage.open();
System.out.println("Page is opened");
}
@When("User enters username as {string}")
public void enterUsername(String userName) {
System.out.println("Enter Username");
loginPage.inputUserName(userName);
}
@When("User enters password as {string}")
public void enterPassword(String passWord) {
loginPage.inputPassword(passWord);
loginPage.clickLogin();
}
@Then("User should be able to login successfully")
public void clickOnLoginButton() {
dashPage.loginVerify();
}
@Then("User should be able to see error message {string}")
public void unsuccessfulLogin(String expectedErrorMessage) {
String actualErrorMessage = loginPage.errorMessage();
Assert.assertEquals(expectedErrorMessage, actualErrorMessage);
}
@When("User clicks on Forgot your password link")
public void clickForgetPasswordLink() {
loginPage.clickForgetPasswordLink();
}
@Then("User should be able to see new page which contains Reset Password button")
public void verifyForgetPasswordPage() {
assertTrue(forgetpasswordPage.ForgetPasswordPage());
}
}
Serenity Step Libraries integrate smoothly into Cucumber Step Definition files; all you need to do is to annotate a step library variable with the @Steps annotation. Methods that represent a business task or action (inputUserName()), and that will appear in the reports as a separate step, is annotated with the @Step annotation. Other methods, such as loginVerify(), query the state of the application and are used in assert statements.
Here, I have created 3 StepClasses – StepLoginPage, StepDashboardPage, and StepForgetPasswordPage
We cannot run a Feature file on its own in a cucumber-based framework. We need to create a Java class, which will run the Feature File. It is the starting point for JUnit to start executing the tests. TestRunner class creates under src/ test/java. When you run the tests with serenity, you use the CucumberWithSerenity test runner. If the feature files are not in the same package as the test runner class, you also need to use the @CucumberOptions class to provide the root directory where the feature files are found.
import org.junit.runner.RunWith;
import io.cucumber.junit.CucumberOptions;
import net.serenitybdd.cucumber.CucumberWithSerenity;
@RunWith(CucumberWithSerenity.class)
@CucumberOptions(plugin = {}, features = "src/test/resources/features/LoginPage.feature", glue = "com.example.definitions")
public class SerenityRunnerTests {
}
Step 7 – Create serenity.conf file under src/test/resources
Serenity.conf file is used to specify various features like the type of webdriver used, various test environments, run tests in headless mode, and many more options.
webdriver.driver. – This tells Serenity which browser to use for the test execution. You can configure this in several locations – serenity.properties or serenity.conf. Here, I have provided this information in serenity.conf
We can also configure the webdriver.base.url property for different environments in the serenity.conf configuration file, in the src/test/resources directory. Below is an example of the same.
Once the environment section is present in your serenity.conf file, you can use the environment system property to use the properties for a given environment. For example, the following would cause the staging URLs to be used:
mvn clean verify -Denvironment=staging
The default environment will be used if no other value is provided. In our example, I will not provide any environment, so it will pick the default environment.
Step 8 – Create serenity.properties file at the root of the project
serenity.project.name = Serenity and Cucumber Report Demo
Step 9 – Run the tests through the command line which generates Serenity Report
Open the command line and go to the location where pom.xml of the project is present and type the below command.
mvn clean verify -Denvironment=firefox
I have provided the location of the Firefox driver through the command line. I believe this is the best way to run the test. We can hard-code the path in the test code or in serenity.conf file. If you don’t want to pass the location of webdriver through the command line, then mention the details of webdriver in serenity.conf and just use the below command for execution.
mvn clean verify
Below is the image of the execution status.
This also provides the location of the serenity report as highlighted in the above image.
Serenity Report
Requirement View
In Serenity, requirements are organized in a hierarchy. We can get an idea of the full directory structure (in src/test/features directory) for the project.
The Test Resultstab (shown below) tells you about the acceptance tests that were executed for this set of requirements.
Test Results
At the bottom of the Test Results tab, you will find the actual test results – the list of all the tests, automated and manual, that were executed for this requirement.
Feature
This provides the detail of all the Test Scenarios present in a Feature File.
Below is an example of a Scenario Outline in the Report. It shows all the examples mentioned in the feature file.
This screen shows the test steps and screenshots of each step.
We are done! Congratulations on making it through this tutorial and hope you found it useful! Happy Learning!!
Serenity BDD is an open-source library that aims to make the idea of living documentation a reality.
Serenity BDD helps you write cleaner and more maintainable automated acceptance and regression tests faster. Serenity also uses the test results to produce illustrated, narrative reports that document and describe what your application does and how it works. Serenity tells you not only what tests have been executed, but more importantly, what requirements have been tested.
One key advantage of using Serenity BDD is that you do not have to invest time in building and maintaining your own automation framework.
Serenity BDD provides strong support for different types of automated acceptance testing, including:
Rich built-in support for web testing with Selenium.
REST API testing with Rest Assured.
Highly readable, maintainable, and scalable automated testing with the Screenplay pattern.
Getting started Cucumber 6 with Serenity BDD
Cucumber is a popular tool for automating BDD-style acceptance criteria.
Serenity seamlessly supports Cucumber 2.x, Cucumber 5, and Cucumber 6. However, this flexibility requires a little tweaking in the build dependencies.
If you are using Maven, you need to do the following:
exclude the default cucumber-core dependency from your serenity-core dependency
Add dependencies on the Cucumber 6.x version of cucumber-java and cucumber-junit into your project.
Prerequisite:
Serenity – 4.0.30
JUnit4 – 4.13.2
Serenity Cucumber – 4.0.30
Maven Compiler – 3.12.1
Maven Surefire Plugin – 3.2.3
Java – 17
Project Structure
Relationship between Web Application, Serenity BDD, Cucumber, and Selenium
Implementation Steps
Step 1: Add Serenity, Cucumber, and JUnit4 dependencies to the Maven project
The pom.xml will look like something as shown below.
Step 2: Create a Feature File under src/test/resources
A Feature File is an entry point to the Cucumber tests. This is a file where you will describe your tests in Descriptive language (Like English). A feature file can contain a scenario or can contain many scenarios in a single feature file. Below is an example of a Feature file.
Feature: Login Page
@ValidCredentials
Scenario: Login with valid credentials
Given User is on Home page
When User enters username as "Admin"
And User enters password as "admin123"
Then User should be able to login successfully
@InValidCredentials
Scenario Outline: Login with invalid credentials
Given User is on Home page
When User enters username as '<username>'
And User enters password as '<password>'
Then User should be able to see error message '<errorMessage>'
Examples:
| username | password | errorMessage |
| $$$$$ | ££££££££ | Invalid credentials |
| admin | Admin123 | Invalid credentials |
| Admin123 | admin | Invalid credentials |
Step 3: Create the Step Definition class
The glue code shown below uses Serenity step libraries as action classes to make the tests easier to read and to improve maintainability.
These classes declare using the Serenity @Steps annotation. The @Steps annotation tells Serenity to create a new instance of the class, and inject any other steps or page objects that this instance might need.
Each action class models a particular facet of user behavior: navigating to a particular page, performing a search, or retrieving the results of a search. These classes design to be small and self-contained, which makes them more stable and easier to maintain.
LoginPageDefinitioncontains the steps to open the web browser, enter the username, enter the password and click on the Login Button
package com.example.definitions;
import com.example.steps.StepDashboardPage;
import com.example.steps.StepLoginPage;
import io.cucumber.java.en.Given;
import io.cucumber.java.en.Then;
import io.cucumber.java.en.When;
import net.serenitybdd.annotations.Steps;
import org.junit.Assert;
public class LoginPageDefinitions {
@Steps
StepLoginPage loginPage;
@Steps
StepDashboardPage dashPage;
@Given("User is on Home page")
public void openApplication() {
loginPage.open();
System.out.println("Page is opened");
}
@When("User enters username as {string}")
public void enterUsername(String userName) {
System.out.println("Enter Username");
loginPage.inputUserName(userName);
}
@When("User enters password as {string}")
public void enterPassword(String passWord) {
loginPage.inputPassword(passWord);
loginPage.clickLogin();
}
@Then("User should be able to login successfully")
public void clickOnLoginButton() {
dashPage.loginVerify();
}
@Then("User should be able to see error message {string}")
public void unsucessfulLogin(String expectedErrorMessage) throws InterruptedException {
String actualErrorMessage = loginPage.errorMessage();
Assert.assertEquals(expectedErrorMessage, actualErrorMessage);
}
}
This annotation lets you define a URL or a set of URLs that work with a particular page.
StepLoginPage is created by extending it from PageObject class. In this class, $() method used below, which locates a web element using a By locator or an XPath or CSS expression. This class is responsible for uniquely locating elements on the page, and it does this by defining locators or occasionally by resolving web elements dynamically.
Step 4: Create Serenity Test Runner under src/test/java
We cannot run a Feature file on its own in a cucumber-based framework. We need to create a Java class, which will run the Feature File. It is the starting point for JUnit to start executing the tests. TestRunner class creates under src/test/java. When you run the tests with serenity, you use the CucumberWithSerenity test runner. If the feature files are not in the same package as the test runner class, you also need to use the @CucumberOptions class to provide the root directory where the feature files found.
Below is the code for SerenityRunnerTests.
import io.cucumber.junit.CucumberOptions;
import net.serenitybdd.cucumber.CucumberWithSerenity;
import org.junit.runner.RunWith;
@RunWith(CucumberWithSerenity.class)
@CucumberOptions(plugin = { "pretty" }, features = "src/test/resources/features/LoginPage.feature",
glue="com.example.definitions")
public class SerenityRunnerTests {}
Step 5: Create serenity.conf (Configuration File)
Serenity uses serenity.conf file in thesrc/test/resources directory to configure test execution options. serenity.config can also contain the environment URL and other options like headless mode and soon.
GitHub Actions is a continuous integration and continuous delivery (CI/CD) platform that allows you to automate your build, test, and deployment pipeline. You can create workflows that build and test every pull request to your repository, or deploy merged pull requests to production.
What is a GitHub Page?
GitHub Pages is a static site hosting service that takes HTML, CSS, and JavaScript files straight from a repository on GitHub, optionally runs the files through a build process, and publishes a website.
With GitHub Pages, we get a subdomain as github.io. To host your site, first and foremost thing is we need to have a GitHub account and a repository that will have our Automation code to run the Test Cases. Secondly, your site will be named as <username>.github.io
Why use GitHub Page to host the Report?
As automation testers, we are familiar with the challenges of sharing daily or weekly status reports on the progress of our automation efforts with management. Typically, we rely on tools like Extent Report or Allure Report and send these reports via email for management to review the current status of our automation progression or regression. However, there is a more efficient solution available: leveraging GitHub Pages to host our automation report.
By using GitHub Pages, we can eliminate the pain of manually sending reports to management and instead provide them with a simple URL. This means that whenever management wants to check the latest update on our automation progress, they can simply browse through the provided URL.
Important points
1. The Web Application tests need to run in the headless mode in GitHub Workflow.
ChromeOptions options = new ChromeOptions();
options.addArguments("--no-sandbox");
options.addArguments("--disable-dev-shm-usage");
options.addArguments("--headless");
driver = new ChromeDriver(options);
2. Use the below code to use gh-pages branch to host the Extent Report:
I have a repository available in GitHub – ExtentReport_GitHubActions as shown in the below image. Go to the “Actions” tab. Click on the “Actions” tab.
Step 2 – Select the type of Actions
You will see that GitHub recommends Actions depending on the project. In our case, it is recommending actions suitable for a Java project. I have selected the “Java with Maven” option as my project is built in Maven.
Step 3 – Generation of Sample pipeline
If you choose an existing option, it will automatically generate a .yaml for the project as shown below.
We will replace the current workflow with the following yml file that will generate the ExtentReport in the GitHub as shown below:
This will give the option to add a description for the commit. It will also enable the user to commit either to the main branch or commit to any other branch that exists in the project. Click on the “Commit new file” button to set up the workflow file.
It will create a maven.yml file as shown below in the project.
Step 5 – Verify that the workflow is running
Next, head over to the “Actions” tab, and you will see your YAML workflow file present under the tab. The yellow sign represents that the job is in the queue. The pipeline will fail. It is because gh-pages is not configured yet now.
Navigate to the Repository Settings of GitHub, then select Action → General.
Check the “Read and Write Permissions” option, and save the changes.
Go to Settings->Pages and you will see “GitHub Pages”. Go to the Branch, select “main” and “/root” and click on the “Save” button.
Here we are using gh-pages to host our automation report. Once you run your workflow for the first time this branch will not be present, so run the workflow, and you will see gh-pages branch is made. Now again go to the pages section of your repository change the source branch of gh-pages as below and now again run it.
Click on the workflow and the below screen is displayed. It shows the status of the run of the workflow, the total time taken to run the workflow, and the name of the .yml file.
Below are all the steps of the workflow.
Published ExtentReport on GitHub
From the logs of the Workflow, you can see that the “Deploy Pages” stage was executed successfully.
Once the pipeline runs, a new workflow will be generated as shown in the below image:
When we click on the pages build and deployment workflow, we will see 3 stages. The deploy stage contains the link to the ExtentReport.
To have a successful and effective implementation of a test framework, it is always advisable that the test framework supports test execution in multiple ways. The most commonly used ways to execute tests in Cucumber Framework are by running the tests using JUnit and TestNG.
To execute tests using JUnit, we need to create a JUnit Test Runner. Whereas, we need a Maven project to execute Cucumber tests from Command-Line.
Feature: Login to HRM Application
Background:
Given User is on HRMLogin page "https://opensource-demo.orangehrmlive.com/"
@ValidCredentials
Scenario: Login with valid credentials
When User enters username as "Admin" and password as "admin123"
Then User should be able to login successfully
@InvalidCredentials
Scenario Outline: Login with invalid credentials
When User enters username as "<username>" and password as "<password>"
Then User should be able to see error message "<errorMessage>"
Examples:
| username | password | errorMessage |
| Admin | admin12$$ | Invalid credentials |
| admin$$ | admin123 | Invalid credentials |
| abc123 | xyz$$ | Invalid credentials |
HomePage.feature
Feature: Login to Home
Background:
Given User is on HRMLogin page "https://opensource-demo.orangehrmlive.com/"
@ValidCredentialsHome
Scenario: Login with valid credentials to got to home page
When User enters username as "Admin" and password as "admin123"
Then User should be able to login successfully and new Home page opens
Run Test from Command Line
1. Running all the tests
1. Open the command prompt and change the directory to the project location where pom.xml is present.
2. All feature files should be in src/test/resources and create the Cucumber Runner class as CucumberRunnerTest. Note:- The Runner class name should end with Test to execute the tests from Command Line Run the following command in the command prompt:
mvn clean test
mvn clean test runs Cucumber Features using Cucumber’s JUnit Runner. The @RunWith (Cucumber.class)annotation on the TestRunner class tells JUnit to start Cucumber. Cucumber runs time parses the command-line options to know what feature to run, where the Glue Code lives, what plugins to use, and so on.
3. The below screenshot shows that CucumberRunnerTest class is triggered.
4. The below screenshot shows the build success output.
Cucumber provides several options that can be passed to on the command line.
2. Running Scenarios using Tags
If you are using Maven and want to run a subset of scenarios tagged with @ValidCredentials.
mvn clean test -Dcucumber.filter.tags="@ValidCredentials"
The output of the above program is
3. Running a Feature file
Suppose you want to run a single Feature File from the command line, then use the below syntax
mvn clean test -Dcucumber.features="src/test/resources/features/HomePage.feature"
The output of the above program is
4. Creating Cucumber Report from Command Line
If we want to generate a different report, then we can use the following command and see the HTML report generate at the location mentioned:
mvn clean test -Dcucumber.plugin="html:target/cucumber-reports/cucumberReport.html"
The output of the above program is
5. Passing multiple Parameters
If we want to pass more than one parameter, then we can use the following command
mvn clean test -Dcucumber.features="src/test/resources/features/LoginPage.feature" -Dcucumber.filter.tags="@ValidCredentials"
The output of the above program is
6. Running a Scenario without a tag
If we want to run a single Scenario from the command line and no tag is assigned to that scenario, this is how we specify
mvn clean test -Dcucumber.features="src/test/resources/features/LoginPage.feature:7"
The output of the above program is
That’s it! Congratulations on making it through this tutorial and hope you found it useful! Happy Learning!!
GitHub serves as a collaborative platform that supports version control, code collaboration, automated testing, and issue tracking, all of which are crucial elements in the software testing process. It promotes transparency, collaboration, and efficiency in the development and testing workflows.
CI/CD pipelines have contributed to the success of the DevOps cycle in all software development projects. This is a holistic process that bridges development and operations. Continuous integration helps development teams deploy code efficiently, and continuous delivery automates code deployment.
Implementation Steps
Step 1 – Create GitHub Actions and Workflows
I have a repository available in GitHub – RobotFramework_POM as shown in the below image. Go to the “Actions” tab. Click on the “Actions” tab.
Step 2 – Select the type of Actions
You will see that GitHub recommends Actions depending on the project. In our case, it is recommending actions suitable for a Java project. I have selected the “Python application” option as my project is built in Maven.
Step 3 – Generation of Sample pipeline
If you choose an existing option, it will automatically generate a .yaml for the project as shown below.
We will replace the current workflow with the following yml file as shown below:
name: Robot Framework - Python
on:
push:
branches: [ "main" ]
pull_request:
branches: [ "main" ]
permissions:
contents: read
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Set up Python 3.12.1
uses: actions/setup-python@v3
with:
python-version: 3.12.1
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install robotframework
pip install robotframework-seleniumlibrary
- name: Test with RobotFramework
run: robot .
- name: Test Report Generation
uses: actions/upload-artifact@v4
if: success() || failure()
with:
name: Report # Name of the folder
path: report.html # Path to test results
This command will update the Python’s Pip version to the latest available.
python -m pip install --upgrade pip
As we need robotframework and seleniumlibrary to execute the tests, we have installed them also using the below command:
This will give the option to add a description for the commit. It will also enable the user to commit either to the main branch or commit to any other branch that exists in the project. Click on the “Commit new file” button to set up the workflow file.
It will create a python-app.yml file as shown below in the project.
Step 5 – Verify that the workflow is running
Next, head over to the “Actions” tab, and you will see your YAML workflow file present under the tab. The yellow sign represents that the job is in the queue.
In Progress – When the job starts building and running, you will see the status change from “Queued” to “In progress”.
Passed – If the build is successful, you will see a green tick mark.
Below is the execution log. Go to “Test with RobotFramework” phase.
Click on the workflow and the below screen is displayed. It shows the status of the run of the workflow, the total time taken to run the workflow, and the name of the .yml file.
Below shows all the steps of the workflow.
Step 6 – Published Test Report on GitHub
From the logs of the Workflow, you can see that the Test Report step was executed successfully.
Once the pipeline run, a Report folder will be generated as shown in the below image:
When we click on the folder Report, a zipped file will be downloaded, and we can extract it to see all the files contained within it.
Git is a Distributed Version Control System (VCS)which is originally developed in 2005 by Linus Torvalds (Creator of Linux) and is open source, i.e. freely available to use. It is the most popular and most used version control tool right now. A staggering number of software projects rely on Git for version control, including commercial projects as well as open source.
In this tutorial, we will change the remote repository URL of a project.
Imagine we have our project repository codebase in a specific hosting provider like GitLab. Our management has decided to migrate to another hosting provider like GitHub. In that case, we need to change the remote repository URL.
Steps to Follow:
1. Navigate to the local repository directory where you want to update the remote URL.
2. Open your terminal or command prompt.
3. Use the following command to view the current remote URLs associated with your repository:
git remote -v
4. Identify the name of the remote repository for which you want to change the URL (e.g., origin). Here, I want to push the code to GitHub. So, will change the URL to GitHub one.
5. To change the URL, use the “git remote set-url” command followed by: – The name of the remote repository. – The new URL that you want to set.
GitHub serves as a collaborative platform that supports version control, code collaboration, automated testing, and issue tracking, all of which are crucial elements in the software testing process. It promotes transparency, collaboration, and efficiency in the development and testing workflows.
CI/CD pipelines have contributed to the success of the DevOps cycle in all software development projects. This is a holistic process that bridges development and operations. Continuous integration helps development teams deploy code efficiently, and continuous delivery automates code deployment.
Implementation Steps
Step 1 – Create GitHub Actions and Workflows
I have a repository available in GitHub – RestAssured_TestNG_Demo as shown in the below image. Go to the “Actions” tab. Click on the “Actions” tab.
Step 2 – Select the type of Actions
You will see that GitHub recommends Actions depending on the project. In our case, it is recommending actions suitable for a Java project. I have selected the “Java with Maven” option as my project is built in Maven.
Step 3 – Generation of Sample pipeline
If you choose an existing option, it will automatically generate a .yaml for the project as shown below.
We will replace the current workflow with the following yml file as shown below:
name: Rest API Tests using Rest Assured with TestNG
on:
push:
branches: [ "main" ]
pull_request:
branches: [ "main" ]
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Set up JDK 17
uses: actions/setup-java@v4
with:
java-version: '17'
distribution: 'temurin'
cache: maven
- name: Test Execution
run: mvn clean test
- name: Test Report Generation
uses: actions/upload-artifact@v4
if: success() || failure()
with:
name: TestNG Report # Name of the folder
path: target/surefire-reports/ # Path to test results
Step 4 – Commit the changes
After the changes, hit the “Start Commit” button.
This will give the option to add a description for the commit. It will also enable the user to commit either to the main branch or commit to any other branch that exists in the project. Click on the “Commit new file” button to set up the workflow file.
It will create a maven.yml file as shown below in the project.
Step 5 – Verify that the workflow is running
Next, head over to the “Actions” tab, and you will see your YAML workflow file present under the tab. The yellow sign represents that the job is in the queue.
In Progress – When the job starts building and running, you will see the status change from “Queued” to “in progress”.
Passed – If the build is successful, you will see a green tick mark.
Below is the execution log.
Click on the workflow and the below screen is displayed. It shows the status of the run of the workflow, the total time taken to run the workflow, and the name of the .yml file.
Below shows all the steps of the workflow.
Step 6 – Published TestNG Report on GitHub
From the logs of the Workflow, you can see that the Test Report step was executed successfully.
Once the pipeline run, a TestNG Report folder will be generated as shown in the below image:
When we click on the folder TestNG Report, a zipped file will be downloaded, and we can extract it to see all the files contained within it.