In the previous tutorial, I discussed the Jenkins pipeline. This tutorial will discuss the steps to create the Jenkins pipeline for Cucumber tests.
Pre-Requisite:
Jenkins was installed and started on the computer.
To generate Cucumber Report in Jenkins, we need to download HTML Publisher. Please refer to this tutorial to install the plugin – How to install Plugins in Jenkins
Implementation Steps
Step 1: Create a new pipeline project.
Give the Name of the project – Cucumber_PipelineDemo
Click on the pipeline project.
Click on the OK button.
In the General section, enter the project description in the Description box.
Step 2: Scroll down to Pipeline
From the Definition field, choose the “Pipeline script from SCM” option. This option instructs Jenkins to obtain your Pipeline from Source Control Management (SCM), which will be your locally cloned Git repository.
From the SCM field, choose Git.
The Repositories section contains the Repository URL and Credentials.
In the Repository URL field, specify the directory path of the GitLab/GitHub project.
In the Credentials field, specify the username and password needed to log in to GitLab/GitHub.
In this case, I have the project is present in GitLab and using it.
Step 3: Create Jenkinsfile
Create and save a new text file with the name Jenkinsfile at the root of the project in the GitLab repository. Here, we are using the Cucumber project with Selenium and TestNG. To know more about the Integration of Cucumber with Serenity and TestNG, please refer to this tutorial – Implemention of ‘Masterthought’ Reports in Cucumber with TestNG.
For this tutorial, we are using Declarative syntax. The sample example is given below:
pipeline {
agent any
stages {
stage('Test') {
steps {
// To run Maven on a Windows agent, use
bat "mvn -D clean test"
}
post {
// If Maven was able to run the tests, even if some of the test
// failed, record the test results and archive the jar file.
success {
cucumber buildStatus: 'null',
customCssFiles: '',
customJsFiles: '',
failedFeaturesNumber: -1,
failedScenariosNumber: -1,
failedStepsNumber: -1,
fileIncludePattern: '**/*.json',
pendingStepsNumber: -1,
skippedStepsNumber: -1,
sortingMethod: 'ALPHABETICAL',
undefinedStepsNumber: -1
}
}
}
}
}
Step 4: Specify branches to build a section under Repositories.
Branch Specifier – */master (This is my main branch)
ScriptPath – Jenkinsfile
Click on the Apply and Save buttons.
We have created a new Maven project “Cucumber_PipelineDemo” with the configuration to run the Cucumber Tests with TestNG.
Step 5: Execute the tests
Let’s execute it now by clicking on the “Build Now” button.
Right-click on Build Number (here in my case it is #4) and click on Console Output to see the result.
Below is the test execution summary.
Step 6: Pipeline Steps
Once the execution is completed, and we want to see the Pipeline Steps, click on the Pipeline Steps mentioned on the left side of the page.
Step 7: View the Report
Once the execution is completed, go back to “Cucumber_PipelineDemo”. We can see below that the Cucumber report is generated.
We could see a link to view the “Cucumber Report“. Click on the Cucumber Report. It displays the Report.
Tip: If you don’t see the Report UI intact, then you need to configure a simple groovy script. For that, go to Dashboard–>Manage Jenkins–>Script Console and add the script as:
ExtentReports is a logger-style reporting library for automated tests. ExtentReports is a library that can be used to build a customized detailed report. It can be integrated with TestNG, JUnit, etc. This report can be built in JAVA, .NET and it provides a detailed summary of each test case and each test step too in a graphical manner. Extent reports produce HTML-based documents that offer several advantages like pie charts, graphs, screenshots addition, and test summary. ExtentReports 4 is built on an open-Core.
Generation of Extent Reports in Selenium with TestNG
In this tutorial, I will describe How to generate an ExtentReport in a Selenium with the TestNG maven project.
Extent Reports Project Structure
Implementation Steps
1. Add the dependencies to the POM.xml in the case of the Maven project.
In this class, we created a createInstance() method. Also, you need to set your ExtentReports report HTML file location.
import java.io.File;
import java.io.IOException;
import java.util.Date;
import org.apache.commons.io.FileUtils;
import org.openqa.selenium.TakesScreenshot;
import com.aventstack.extentreports.ExtentReports;
import com.aventstack.extentreports.reporter.ExtentHtmlReporter;
import com.aventstack.extentreports.reporter.configuration.Theme;
import com.example.testcases.BaseTests;
import org.openqa.selenium.OutputType;
public class ExtentManager extends BaseTests{
private static ExtentReports extent;
public static String screenshotName;
public static ExtentReports createInstance(String fileName) {
ExtentHtmlReporter htmlReporter = new ExtentHtmlReporter(fileName);
htmlReporter.config().setTheme(Theme.DARK);
htmlReporter.config().setDocumentTitle(fileName);
htmlReporter.config().setEncoding("utf-8");
htmlReporter.config().setReportName(fileName);
extent = new ExtentReports();
extent.attachReporter(htmlReporter);
extent.setSystemInfo("Release No", "22");
extent.setSystemInfo("Environment", "QA");
extent.setSystemInfo("Build no", "B-12673");
return extent;
}
public static void captureScreenshot() {
TakesScreenshot screenshot = (TakesScreenshot)driver;
// Call method to capture screenshot
File src = screenshot.getScreenshotAs(OutputType.FILE);
try
{
Date d = new Date();
screenshotName = d.toString().replace(":", "_").replace(" ", "_") + ".jpg";
FileUtils.copyFile(src,new File(System.getProperty("user.dir") + "\\reports\\" + screenshotName));
System.out.println("Successfully captured a screenshot");
} catch (IOException e) {
System.out.println("Exception while taking screenshot " + e.getMessage());
}
}
}
The ExtentHtmlReporter is used for creating an HTML file, and it accepts a file path as a parameter.
ExtentHtmlReporter htmlReporter = new ExtentHtmlReporter(fileName);
The file path represents the path in which our extent report would be generated. This is defined in ExtentListeners class.
static Date d = new Date();
static String fileName = "ExtentReport_" + d.toString().replace(":", "_").replace(" ", "_") + ".html";
private static ExtentReports extent = ExtentManager.createInstance(System.getProperty("user.dir")+"\\reports\\"+fileName);
ExtentHtmlReporter is also used to customize the extent reports. It allows many configurations to be made through the config() method. Some of the configurations that can be made are described below.
We have two themes – STANDARD and DARK for customizing the look and feel of our extent reports.
htmlReporter.config().setTheme(Theme.DARK);
STANDARD Look
htmlReporter.config().setTheme(Theme.STANDARD);
captureScreenshot() is a method in ExtentTest class that attaches the captured screenshot in the Extent Report. It takes the image path where the screenshot has been captured as the parameter and attaches the screenshot to the Extent Report in Selenium.
public static void captureScreenshot() {
TakesScreenshot screenshot = (TakesScreenshot)driver;
// Call method to capture screenshot
File src = screenshot.getScreenshotAs(OutputType.FILE);
try
{
Date d = new Date();
screenshotName = d.toString().replace(":", "_").replace(" ", "_") + ".jpg";
FileUtils.copyFile(src,new File(System.getProperty("user.dir") + "\\reports\\" + screenshotName));
System.out.println("Successfully captured a screenshot");
} catch (IOException e) {
System.out.println("Exception while taking screenshot " + e.getMessage());
}
}
Step 3 – Create ExtentListeners class
This class contains the action done by extent report on each step. In our tests, we implement ITestListener and use its methods. TestNG provides the @Listeners annotation, which listens to every event that occurs in a Selenium code. TestNG Listeners are activated either before the test or after the test case. It is an interface that modifies the TestNG behavior. If any event matches an event for which we want the listener to listen then it executes the code, which ultimately results in modifying the default behavior of TestNG. To know more about ITestListener, please refer to this tutorial.
I have defined actions for onTestStart(), onTestSuccess(), onTestFailure(), onTestSkipped() and onFinish() methods.
The ITestListener is an interface that has unimplemented methods by default and we can add lines of code within each method. So whenever a specific event occurs, the code written within that method will be executed.
onTestFailure() is a method in which this listener will be invoked whenever the test fails. Within this method, we shall add our code to capture screenshots whenever the test case fails on execution. The screenshot of the failed test case is also embedded in the report.
onTestSuccess() is a method that is invoked once the test execution is complete and the test has been passed. We shall add the log included in the Extent Report to mark the test case as passed within this method
This class contains the methods to initialize the browser and exit the browser after every test.
import java.time.Duration;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import io.github.bonigarcia.wdm.WebDriverManager;
public class BaseTests {
public static WebDriver driver;
public WebDriverWait wait;
@BeforeTest
public void setup() throws Exception {
driver = WebDriverManager.firefoxdriver().create();
driver.get("https://opensource-demo.orangehrmlive.com/");
wait = new WebDriverWait(driver, Duration.ofSeconds(10));
driver.manage().window().maximize();
}
@AfterTest
public void closeBrowser() {
driver.close();
}
5. Create the LoginPage class
This class contains the locator of all the web elements and methods needed for the testing of the page.
package com.example.testcases;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.PageFactory;
public class LoginPage extends BaseTests{
WebDriver driver;
@FindBy(name = "txtUsername")
WebElement userName;
@FindBy(name = "txtPassword")
WebElement password;
@FindBy(id = "logInPanelHeading")
WebElement titleText;
@FindBy(id = "btnLogin")
WebElement login;
@FindBy(id="spanMessage")
WebElement errorMessage;
@FindBy(id="forgotPasswordLink")
WebElement forgetPasswordLink;
@FindBy(xpath="//*[@id='social-icons']/a[1]/img")
WebElement linkedInIcon;
public LoginPage(WebDriver driver) {
this.driver = driver;
// This initElements method will create all WebElements
PageFactory.initElements(driver, this);
}
// Set user name in textbox
public void setUserName(String strUserName) {
userName.sendKeys(strUserName);
}
// Set password in password textbox
public void setPassword(String strPassword) {
password.sendKeys(strPassword);
}
// Click on login button
public void clickLogin() {
login.click();
}
// Get the title of Login Page
public String getLoginTitle() {
return titleText.getText();
}
// Get the text of forgotPasswordLink
public String getforgotPasswordLinkText() {
return forgetPasswordLink.getText();
}
// Get the errorMessage
public String getErrorMessage() {
return errorMessage.getText();
}
// Verify linkedInIcon is enabled
public Boolean isEnabledLinkedIn() {
return linkedInIcon.isEnabled();
}
public void login(String strUserName, String strPasword) {
// Fill user name
this.setUserName(strUserName);
// Fill password
this.setPassword(strPasword);
// Click Login button
this.clickLogin();
}
}
6. Create the LoginTests class
This class contains all the tests. As we are using, I have assigned priority to all the tests to run the tests in a specified order.
import static org.testng.Assert.assertTrue;
import org.testng.Assert;
import org.testng.SkipException;
import org.testng.annotations.Test;
public class LoginTests extends BaseTests{
LoginPage objLogin;
@Test(priority = 0)
public void verifyLoginPageTitle() {
// Create Login Page object
objLogin = new LoginPage(driver);
// Verify login page text
String loginPageTitle = objLogin.getLoginTitle();
Assert.assertTrue(loginPageTitle.contains("LOGIN Panel"));
}
@Test(priority = 1)
public void verifyforgetPasswordLink() {
String expectedText= objLogin.getforgotPasswordLinkText();
Assert.assertTrue(expectedText.contains("Forgot your password?"));
}
@Test(priority = 2)
public void HomeTest() {
// login to application
objLogin.login("Admin1", "admin1234");
String expectedError = objLogin.getErrorMessage();
// Verify home page
Assert.assertTrue(expectedError.contains("Username cannot be empty"));
}
@Test(priority = 3)
public void verifyLinkedIn() {
System.out.println("Actual linkedIn Text :" + objLogin.isEnabledLinkedIn());
assertTrue(objLogin.isEnabledLinkedIn());
System.out.println("Im in skip exception");
throw new SkipException("Skipping this exception");
}
}
In this example, there are 4 tests and out of 4, 2 should pass, 1 should fail and 1 should skip.
7. Create TestNG.xml
In the TestNG.xml file, we shall add our classes and also the listener class.
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd">
<suite name="Extent Report Demo">
<listeners>
<listener class-name ="com.example.extentlisteners.ExtentListeners"/>
</listeners>
<test name="Login Tests">
<classes>
<class name="com.example.testcases.LoginTests"/>
</classes>
</test> <!-- Test -->
</suite> <!-- Suite -->
8. Execute the tests from testng.xml
Right-click on testng.xml and select Run As -> TestNG Suite.
9 Test Execution Result
The test execution result can be seen in the console.
10. Extent Report Generation
Refresh the project and will see a folder with the name of the reports present in the project.
Right-click the report and open it in your choice of browser.
Upon opening the Extent Report, you can see the summary of the tests executed.
This is the view of the dashboard in the Extent Report. This page provides a complete view of the total number of tests executed, passed tests, failed tests, the total time taken for executing the tests, and also the classification of the tests based on the category.
Extent reports produce simple and visually appealing reports. Furthermore, the HTML-based report is simple to share with other stakeholders. Extent Reports provide greater detail, allowing testers to be more effective when it comes to quickly debug software.
Congratulations!! We are able to generate an ExtentReport. Happy Learning!!
JUnit5 also provides the support to exclude/disable the tests. The @Disabled annotation in JUnit5 is used to exclude the test methods from the test suite. This annotation can be applied over a test class as well as over individual test methods. This annotation accepts only one optional parameter where we can mention the reason to skip the tests. @Disabled annotation can be used without providing any reason but its always good to provide a reason why this particular test case has been disabled, or issue tracker id for better understanding.
1.Disable Test Methods
In the below example, I have annotated 2 test methods out of 5 test methods as @Disabled with a parameter which specify the reason for disabling the test that means these 2 test methods should not be executed.
import io.github.bonigarcia.wdm.WebDriverManager;
import org.junit.jupiter.api.*;
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.chrome.ChromeOptions;
import static org.junit.jupiter.api.Assertions.*;
class DisabledTestsDemo {
WebDriver driver;
@BeforeEach
public void setUp() {
WebDriverManager.chromedriver().setup();
ChromeOptions chromeOptions = new ChromeOptions();
driver = new ChromeDriver(chromeOptions);
driver.manage().window().fullscreen();
driver.get("http://automationpractice.com/index.php");
}
@Disabled("This test is not applicable for Sprint 14")
@Test
void verifyPopularLink() {
boolean displayed = driver.findElement(By.xpath("//*[@id='home-page-tabs']/li[1]/a")).isDisplayed();
assertTrue(displayed);
}
@Test
void verifyContactNumber() {
String contactDetail = driver.findElement(By.xpath("//span[@class='shop-phone']/strong")).getText();
assertEquals("0123-456-789", contactDetail);
}
@Disabled("This test is blocked till bug 1290 is fixed")
@Test
void verifyWomenLink() {
boolean enabled = driver.findElement(By.xpath("//*[@id='block_top_menu']/ul/li[1]/a")).isEnabled();
assertTrue(enabled);
}
@Test
void contactUSPageTest() {
driver.findElement(By.xpath("//*[@id='contact-link']/a")).click();
String contactPage = driver.getTitle();
assertEquals("Contact us - My Store", contactPage);
}
@Test
void signInPageTest() {
driver.findElement(By.className("login")).click();
String signInPage = driver.getTitle();
assertEquals("Login - My Store", signInPage);
}
@AfterEach
public void tearDown() {
driver.close();
}
}
The output of the above test shows, the 2 tests that are annotated with @Disabled are not executed.
2. Disable Test Class
When we annotate a class as @Disabled, then all the test methods present within that test class will not be executed.
In the below example, there are 2 test classes – Demo and DisabledTestsDemo. Demo class contains 1 test method whereas DisabledTestsDemo contains 5 test methods. I have annotated DisabledTestsDemo class as @Disabled which means all 5 tests present within it will not be executed.
In the below example, there is a base class that contains the initialization of webDriver as well as closing of the same.
import io.github.bonigarcia.wdm.WebDriverManager;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
public class Base {
static WebDriver driver=null;
@BeforeEach
public void setUp() {
WebDriverManager.chromedriver().setup();
ChromeOptions chromeOptions = new ChromeOptions();
driver = new ChromeDriver(chromeOptions);
driver.manage().window().fullscreen();
driver.get("http://automationpractice.com/index.php");
}
@AfterEach
public void tearDown() {
driver.close();
}
}
Class 1 – Demo
import org.junit.jupiter.api.*;
public class Demo extends Base{
@Test
public void homePage() {
Assertions.assertEquals("My Store",driver.getTitle());
}
}
Allure Framework is a flexible lightweight multi-language test report tool that not only shows a very concise representation of what has been tested in a neat web report form but allows everyone participating in the development process to extract the maximum useful information from the everyday execution of tests.
How Allure Report is generated?
Allure is based on standard xUnit results output but adds some supplementary data. Any report is generated in two steps. During test execution (first step), a small library called adapter attached to the testing framework saves information about executed tests to XML files. We already provide adapters for popular Java, PHP, Ruby, Python, Scala, and C# test frameworks. During report generation (second step), the XML files are transformed into an HTML report. This can be done with a command line tool, a plugin for CI, or a build tool.
Similarly, when we run our tests, every popular test framework generates junit-style XML report or testng style which will be used by Allure to generate HTML report.
In the below example, we use maven surefire plugin which automatically generates xml test reports and stores them in target/surefire-reports. And these XML files are transformed into an HTML report by Allure.
Allure reports have provided adapters for Java, PHP, Ruby, Python, Scala, and C# test frameworks.
Allure report has below mention annotation.
@Epic @Features @Stories/@Story We can add Epic, Feature, and Stories annotations to the test to describe the behaviour of the test.
@Severity(SeverityLevel.BLOCKER) – @Severity annotation is used in order to prioritize test methods by severity.
@Description(“Regression Testing”) – We can add a detailed description for each test method. To add such a description, use the @Description annotation.
@Step – In order to define steps in Java code, you need to annotate the respective methods with @Step annotation. When not specified, the step name is equal to the annotated method name.
@Attachment – An attachment in Java code is simply a method annotated with @Attachment that returns either a String or byte[], which should be added to the report.
@Link – We can link the tests to Defect Tracker or JIRA Ticket.
Below is an example that shows how to use various Allure Annotations in the Test.
@Epic("Web Application Regression Testing")
@Feature("Login Page Tests")
@Listeners(TestExecutionListener.class)
public class LoginTests extends BaseTest {
LoginPage objLogin;
DashboardPage objDashboardPage;
@Severity(SeverityLevel.NORMAL)
@Test(priority = 0, description = "Verify Login Page")
@Description("Test Description : Verify the title of Login Page")
@Story("Title of Login Page")
public void verifyLoginPage() {
// Create Login Page object
objLogin = new LoginPage(driver);
// Verify login page text
objLogin.verifyPageTitle();
}
}
Install Allure
For Windows, Allure is available from the Scoop commandline installer.
Logging plays an important role in understanding the behaviour of the test. When we are testing an API, it is good to know how the APIs are behaving, how the request is made and how we received the response from API, what the headers looks like, what the body looks like, what parameters are we providing to the request. All this helps us in debugging the test code and to identify the reason for the failure of the test.
REST Assured, provide support to a different type of logging as shown below:-
Request Logging
Response Logging
Logging to console
Conditional logging
Logging to a txt file on your machine
Request Logging
To log all request specification details including parameters, headers, and body of the request, log().all() needs to be added post given() section.
@Test
public void requestLoggingDemo() {
String json = "{\"name\":\"apitest\",\"salary\":\"5000\",\"age\":\"30\"}";
// GIVEN
given()
.log().all()
.baseUri("http://dummy.restapiexample.com/api")
.contentType(ContentType.JSON)
.body(json)
// WHEN
.when()
.post("/v1/create")
// THEN
.then()
.assertThat()
.statusCode(200)
.body("data.name", equalTo("apitest"))
.body("message", equalTo("Successfully! Record has been added."));
}
The output of the above program is
Other different request logging options are:-
given().log().params(). .. // Log only the parameters of the request
given().log().body(). .. // Log only the request body
given().log().headers(). .. // Log only the request headers
given().log().cookies(). .. // Log only the request cookies
given().log().method(). .. // Log only the request method
given().log().path(). .. // Log only the request path
Response Logging
If you want to print the response body regardless of the status code, you can do
get("/x").then().log().body()..
This will print the response body regardless if an error occurred.
@Test
public void responseLoggingDemo() {
String json = "{\"name\":\"apitest\",\"salary\":\"5000\",\"age\":\"30\"}";
// GIVEN
given()
.baseUri("http://dummy.restapiexample.com/api")
.contentType(ContentType.JSON)
.body(json)
// WHEN
.when()
.post("/v1/create")
// THEN
.then()
.log().all()
.statusCode(200)
.body("data.name", equalTo("apitest"))
.body("message", equalTo("Successfully! Record has been added."));
}
The output of the above program is
Conditional Logging
What if you want to perform logging conditionally? For example, you want to log in only if validation fails or if the status code is equal to 200, or if the server returned status code >=400.
.then().log().ifStatusCodeIsEqualTo(302). .. // Only log if the status code is equal to 302
.then().log().ifStatusCodeMatches(matcher). .. // Only log if the status code matches the supplied Hamcrest matcher
@Test
public void responseLoggingDemo() {
String json = "{\"name\":\"apitest\",\"salary\":\"5000\",\"age\":\"30\"}";
// GIVEN
given()
.baseUri("http://dummy.restapiexample.com/api")
.contentType(ContentType.JSON)
.body(json)
// WHEN
.when()
.post("/v1/create")
// THEN
.then()
.log().ifStatusCodeIsEqualTo(200)
.assertThat().statusCode(200)
.body("data.name", equalTo("apitest"))
.body("message", equalTo("Successfully! Record has been added."));
}
}
The output of the above program is
Logging to a text file with Rest Assured
We will see how we can log all the request and response data to a txt file using Rest Assured.
Create a PrintStream object. You have to provide an object of FileInputStream() to the PrintStream() constructor. Provide the path to the logging.txt file in FileInputStream().
REST Assured gives us filter() method, this filter method accepts RequestLoggingFilter and ResponseLoggingFilter. They have two methods logRequestTo() and logResponseTo() methods respectively. These methods expects a Stream.
Pass the log stream we created to these methods.
@Test
public void responseLoggingDemo() throws FileNotFoundException {
PrintStream log = new PrintStream(new FileOutputStream("logging.txt"));
String json = "{\"name\":\"apitest\",\"salary\":\"5000\",\"age\":\"30\"}";
// GIVEN
given()
.baseUri("http://dummy.restapiexample.com/api")
.contentType(ContentType.JSON)
.body(json)
.filter(RequestLoggingFilter.logRequestTo(log))
.filter(ResponseLoggingFilter.logResponseTo(log))
// WHEN
.when()
.post("/v1/create")
// THEN
.then()
.log().ifStatusCodeIsEqualTo(200)
.assertThat().statusCode(200)
.body("data.name", equalTo("apitest"))
.body("message", equalTo("Successfully! Record has been added."));
}
}
Congratulations on making it through this tutorial and hope you found it useful! Happy Learning!!
In this tutorial, will find out how the Radio Buttons can automated in Selenium WebDriver. Radio Button is also a Web Element like Checkbox. Below image shows the Radio Buttons before they selected
Here, In below image Male option is selected and clicked on Get Checked value, then a message will be displayed.
Here, Check if Option 1 is already selected or not If Option 1 is already selected, then select Option 2, else select Option 1.
Let’s go through the scenario below:-
1) Launch Chrome Browser 2) Maximize the current window 3) Implicitly wait for 30 sec 4) Open browser – https://www.seleniumeasy.com/test/basic-radiobutton-demo.html. 5) Find locator of all Radio Buttons 6) Find no of Radio Buttons available and print their values 7) Verify that first Radio button is selected or not and print 8) Find the value of Get Checked Value button and print the value 9) Identify if Radio Button 1 selected or not. If Button 1 already selected, then select Button 2 10) Click on “Get Checked Value” button 11) Find the value of “Get Checked Value” button after selecting the option and print the value 12) Close the browser
import java.util.List;
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;
public class RadioButtonDemo {
public static void main(String[] args) throws InterruptedException {
System.setProperty("webdriver.chrome.driver", "src\\test\\resources\\webdrivers\\window\\chromedriver.exe");
// Initiate Chrome browser
WebDriver driver = new ChromeDriver();
// Maximize the browser
driver.manage().window().maximize();
// Put an Implicit wait and launch URL
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
driver.get("https://www.seleniumeasy.com/test/basic-radiobutton-demo.html");
// Find locator of all Radio Buttons
List Radio_Options = driver.findElements(By.xpath("//*[@name='optradio']"));
// Find no of Radio Buttons available and print their values
int radioSize = Radio_Options.size();
System.out.println("No Of Radio Button Options :" + radioSize);
for (int i= 0; i< radioSize; i++) {
System.out.println("Name of Radio Button :"+ Radio_Options.get(i).getAttribute("value"));
}
// Create a boolean variable which will hold the value (True/False)
boolean radio_value = false;
// This statement will return True, in case of first Radio button is selected
radio_value = Radio_Options.get(0).isSelected();
System.out.println("First Radio Option is Checked :" + radio_value);
// Find the value of "Get Checked Value" button and print the value
String preButtonSelected = driver.findElement(By.xpath("//*[@id='easycont']/div/div[2]/div[1]/div[2]/p[3]"))
.getText();
if (preButtonSelected.isEmpty() == true) {
System.out.println("Get Checked Value before selection is Empty");
} else {
System.out.println("Get Checked Value before selection is :" + preButtonSelected);
}
Thread.sleep(1000);
// Identify if Radio Button 1 is selected or not. If Button 1 is already
// selected, then select Button 2
if (radio_value == true) {
Radio_Options.get(1).click();
System.out.println("Button Selected is :"+ Radio_Options.get(1).getAttribute("value"));
} else {
Radio_Options.get(0).click();
System.out.println("Button Selected is :"+ Radio_Options.get(0).getAttribute("value"));
}
// Click on "Get Checked Value" button
driver.findElement(By.id("buttoncheck")).click();
// Find the value of "Get Checked Value" button after selecting
// the option and print the value
String postButtonSelected = driver.findElement(By.xpath("//*[@id='easycont']/div/div[2]/div[1]/div[2]/p[3]"))
.getText();
System.out.println("Get Checked Value is :"+ postButtonSelected);
Thread.sleep(1000);
// Close the browser
driver.close();
}
}
Output
No Of Radio Button Options :2
Name of Radio Button :Male
Name of Radio Button :Female
First Radio Option is Checked :false
Get Checked Value before selection is Empty
Button Selected is :Male
Get Checked Value is :Radio button 'Male' is checked
Bootstrap is a free front-end framework for faster and easier web development
Bootstrap includes HTML and CSS based design templates for typography, forms, buttons, tables, navigation, modals, image carousels and many other, as well as optional JavaScript plugins
Bootstrap also gives you the ability to easily create responsive designs
Bootstrap is compatible with all modern browsers (Chrome, Firefox, Internet Explorer, Edge, Safari, and Opera)
What is Responsive Web Design?
Responsive web design is about creating web sites, which automatically adjust themselves to look good on all devices, from small phones to large desktops. Bootstrap dropdowns and interactive dropdowns that are dynamically position and formed using list of ul and li html tags. To know more about Bootstrap, please click here
How to get all the options of a Bootstrap dropdown
Below is an example which will explain how to get all the options of a Bootstrap dropdown.
import java.util.List;
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;
public class BootStrapDemo {
public static void main(String[] args) {
System.setProperty("webdriver.chrome.driver","C:\\Users\\SingVi04\\Desktop\\Drivers\\chromedriver_win32\\chromedriver.exe");
WebDriver driver= new ChromeDriver();
driver.manage().window().maximize();
driver.get("https://www.seleniumeasy.com/test/");
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
// Clicking on Bootstrap Dropdown
driver.findElement(By.xpath("//*[@id='navbar-brand-centered']/ul[1]/li[1]/a")).click();
// Get the all WebElements inside the dropdown in List
List dropdown_list = driver.findElements(By.xpath("//ul[contains(@class,'dropdown-menu')]//li//a"));
// Printing the amount of WebElements inside the list
System.out.println("The Options in the Dropdown are: " + dropdown_list.size());
// Condition to get the WebElement for list
for(int i=0; i<dropdown_list.size(); i++)
{
// Printing All the options from the dropdown
System.out.println(dropdown_list.get(i).getText());
}
}
}
Output
The Options in the Dropdown are: 29
Simple Form Demo
Checkbox Demo
Radio Buttons Demo
Select Dropdown List
Input Form Submit
Ajax Form Submit
JQuery Select dropdown
Here,
1) Open a web page – https://www.seleniumeasy.com/test/ 2) Click on BootStrap DropDown – Input Forms by using (“//*[@id=’navbar-brand-centered’]/ul[1]/li[1]/a” 3) Get the all WebElements inside the dropdown in List by using (“//ul[contains(@class,’dropdown-menu’)]//li//a”) 4) Print all the options of DropDown using dropdown_list.get(i).getText()
How to select a particular option from Bootstrap dropdown
In the below example, there is a Bootstrap dropdown. I want to Check if an element – Checkbox Demo is present in the dropdown or not. If Yes, click on that option.
import java.util.List;
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;
public class BootStrapDemo {
public static void main(String[] args) {
System.setProperty("webdriver.chrome.driver","C:\\Users\\SingVi04\\Desktop\\Drivers\\chromedriver_win32\\chromedriver.exe");
WebDriver driver= new ChromeDriver();
driver.manage().window().maximize();
driver.get("https://www.seleniumeasy.com/test/");
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
// Clicking on Bootstrap Dropdown
driver.findElement(By.xpath("//*[@id='navbar-brand-centered']/ul[1]/li[1]/a")).click();
// Get the all WebElements inside the dropdown in List
List dropdown_list = driver.findElements(By.xpath("//ul[contains(@class,'dropdown-menu')]//li//a"));
// Printing the amount of WebElements inside the list
System.out.println("The Options in the Dropdown are: " + dropdown_list.size());
// Condition to get the WebElement for list
for(int i=0; i<dropdown_list.size(); i++)
{
// Printing All the options from the dropdown
System.out.println(dropdown_list.get(i).getText());
// Checking the condition whether option in text "Checkbox Demo" is coming
if(dropdown_list.get(i).getText().contains("Checkbox Demo"))
{
// Clicking if text "Checkbox Demo" is there
dropdown_list.get(i).click();
// Breaking the condition if the condition get satisfied
break;
}
}
driver.quit();
}
}
OutPut
The Options in the Dropdown are: 29
Simple Form Demo
Checkbox Demo
This program can be re-written by using Enhanced for loop instead of For loop.
getText() can be replaced by getAttribute(“innerHTML”)
import java.util.List;
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;
public class BootStrapDemo {
public static void main(String[] args) {
System.setProperty("webdriver.chrome.driver","C:\\Users\\SingVi04\\Desktop\\Drivers\\chromedriver_win32\\chromedriver.exe");
WebDriver driver= new ChromeDriver();
driver.manage().window().maximize();
driver.get("https://www.seleniumeasy.com/test/");
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
// Clicking on Bootstrap Dropdown
driver.findElement(By.xpath("//*[@id='navbar-brand-centered']/ul[1]/li[1]/a")).click();
// Get the all WebElements inside the dropdown in List
List dropdown_list = driver.findElements(By.xpath("//ul[contains(@class,'dropdown-menu')]//li//a"));
// Printing the amount of WebElements inside the list
System.out.println("The Options in the Dropdown are: " + dropdown_list.size());
// Condition to get the WebElement using Enhanced For loop
for(WebElement element:dropdown_list)
{
// Printing All the options from the dropdown
System.out.println(element.getAttribute("innerHTML"));
// Checking the condition whether option in text "Checkbox Demo" is coming
if(element.getAttribute("innerHTML").contains("Checkbox Demo"))
{
// Clicking if text "Checkbox Demo" is there
element.click();
// Breaking the condition if the condition get satisfied
break;
}
}
driver.quit();
}
}
In the previous tutorial, we have discussed about what is TestNG and why it is important. In this tutorial, will discuss how can we download and install TestNG in Eclipse and how to use it.
Pre-Requisite 1) Eclipse should be installed and configure. Please refer Install and Configure to setup Eclipse to your system.
Install/Setup TestNG 1) Launch Eclipse and go to Help option present at the top and select -“Install New Software”.
2) A dialog box will appear, click the Add button.
4) This time we will see TestNG is added to Install dialog box.
5) Accept the terms and condition and then click the “Finish” button.
6) Once the installation is completed, you will get a message to Restart the Eclipse .Select to Restart the Eclipse
7) To verify if TestNG is installed successfully or not, go to Window, select Show View and then Other.
8) Select Java and see within Java folder, you will see TestNG. This shows that TestNG is successfully installed on the machine.
Steps to follow to create a TestNG class
1) Create a new TestNG class. Right click on Folder where you want to create the TestNG class. Select TestNG and then Create TestNG class as shown in the below image.
2) In the below image, we can see that Source folder is the name of the folder we want to create the class and we can mention the name of the class in Class name. Under annotations, I have checked @BeforeTest and @AfterTest and click the Finish button.
3) We can see that the structure of new TestNG class looks like as shown below.
4) In the below example, we want to navigate to Amazon page and search for Hard Drive.
@BeforeTest : Launch Firefox and direct it to the Base URL
@Test : Search for HardDrive
@AfterTest : Close Firefox browser
import org.testng.annotations.Test;
import org.testng.annotations.BeforeTest;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.AfterTest;
public class TestNG_Demo {
public WebDriver driver;
@BeforeTest
public void beforeTest() {
System.setProperty("webdriver.gecko.driver","C:\\Users\\vibha\\Downloads\\geckodriver-v0.26.0-win64\\geckodriver.exe");
driver = new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);
driver.manage().window().maximize();
driver.get("https://www.amazon.com//");
}
@Test
public void Validation() {
driver.findElement(By.xpath("//*[@id='twotabsearchtextbox']")).sendKeys("hard drive");
//XPath for search button
driver.findElement(By.xpath("//*[@class='nav-input']")).click();
}
@AfterTest
public void afterTest() {
driver.quit();
}
}
5) To execute this program, we need to Right click and select Run as – TestNGTest.
6) The result will look like something shown below. Here, we can see that Test Case Passed is 1, Failed 0 and Skipped 0.
7) As we know that TestNG also produce HTML Reports. To access the report, go to the Eclipse folder and you can see a folder with name test-output inside the Project where we have created TestNG class. Here, it is C:\Users\vibha\Downloads\eclipse-workspace\Demo
8) Open ‘emailable-report.html‘, as this is a html report open it with browser. It will look like something below. TestNG also produce ‘index.html‘ report and it resides in the same test-output folder. This reports gives the link to all the different component of the TestNG reports like Groups& Reporter Output. On clicking these will display detailed descriptions of execution.
We are done! Congratulations on making it through this tutorial and hope you found it useful! Happy Learning!!
TestNG is a testing framework inspired from JUnit and NUnit but introducing some new functionalities that make it more powerful and easier to use.
A famous programmer named as “Cedric Beust” developed TestNG. It is distribute under Apache Software License and is easily available to download. TestNG requires JDK 7 or higher.
Features of TestNG
1) Report Generation – Selenium does not support report generation. TestNG provides the ability to generate HTML Reports
2) Support for Annotations – It has inbuilt annotations, like @Test, @BeforeTest, @AfterTest, and soon which makes code more clean. These annotations are strongly typed, so the compiler will flag mistakes right away if any detected in the code.
3) Grouping of Test Cases – It enable user to group the test cases easily. “Groups” is one annotation of TestNG that can be use in the execution of multiple tests.
4) Set Test Execution Priority – It enable user to set execution priorities of the test cases. There is a parameter called “Priority” which is used to execute the methods in a particular order. It also allows user to run the test cases of a particular group.
Let us consider a scenario where we have created two groups such as ‘Smoke’ and ‘Regression’. If we want to execute the test cases in a ‘Regression’ group, then this can only be possible in the TestNG framework. If we want to skip the execution of a particular test case, there is a parameter called (enabled = true or false)
5) Support Data Driven Testing – It provide support to Data Driven testing using @DataProviders. 6) Powerful Execution Model – TestNG does not extend any class. TestNG framework allows user to define the test cases where each test case is independent of other test cases. 7) Supports Parallel or Multi Threading Testing – It allow user to run same test cases on 2 different browsers at the same time. 8) Supports Logging – It also provides the logging facility for the test. For example during the execution of test case, user wants some information to be log in the console. Information could be any detail depends upon the purpose. Keeping this in mind that we are using Selenium for testing, we need the information that helps the user to understand the test steps or any failure during the test case execution. With the help of TestNG Logs, it is possible to enable logging during the Selenium test case execution. 9) Integration with other third party tools – It can be easily integrated with other tools or plugins like build tool Maven, Integrated Development Environment (Eclipse), Selenium, Cucumber and many other.
There are lot more features apart from mentioned above, but I feel these are the most commonly used features in TestNG.