DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Last call! Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

Related

  • What BDD Is and Why You Need It: Java and Behavior Driven Development
  • TestCafe Integration With Cucumber
  • Automating Cucumber Data Table to Java Object Mapping in Your Cucumber Tests
  • A Maven Story

Trending

  • The 4 R’s of Pipeline Reliability: Designing Data Systems That Last
  • AI's Dilemma: When to Retrain and When to Unlearn?
  • Comprehensive Guide to Property-Based Testing in Go: Principles and Implementation
  • Unlocking AI Coding Assistants Part 2: Generating Code
  1. DZone
  2. Software Design and Architecture
  3. Integration
  4. Integrate Cucumber in Playwright With Java

Integrate Cucumber in Playwright With Java

Integrating Cucumber with Playwright combines natural language scenarios with browser automation, boosting project performance.

By 
Kailash Pathak user avatar
Kailash Pathak
DZone Core CORE ·
Jun. 05, 23 · Tutorial
Likes (4)
Comment
Save
Tweet
Share
13.2K Views

Join the DZone community and get the full member experience.

Join For Free

Cucumber is a popular behavior-driven development (BDD) framework that allows you to write executable specifications in a natural language format. It promotes collaboration between stakeholders, developers, and testers by providing a common language that everyone can understand. Cucumber supports various programming languages, including Java, and provides a rich set of features for defining and executing test scenarios.

Playwright, on the other hand, is a powerful open-source automation framework for web browsers that supports multiple programming languages, including Java. It provides a high-level API for automating web browsers such as Chrome, Firefox, and Safari. Playwright offers robust browser automation capabilities, including interactions with web pages, taking screenshots, handling cookies, and much more.

By integrating Cucumber with Playwright, you can leverage the expressive power of Cucumber’s natural language scenarios and combine it with Playwright’s browser automation capabilities. This combination allows you to write tests in a human-readable format and execute them against real browsers, enabling end-to-end testing of web applications.

Integrating Cucumber with Playwright in Java provides a seamless way to express and automate acceptance criteria, making it easier to collaborate and ensure the quality of your web applications.

Pre-Condition

  1. Eclipse IDE is installed.
  2. Java is installed.
  3. Maven is already installed in Eclipse.

Scenarios Covered Under This Blog With Cucumber

  • Launch application
  • Verify the title of the application.
  • Login into the site.
  • Verify the product
  • Click on Hamburger and Logout of the application.

High-Level Steps to Implement the Above Scenario Using Playwright With Java

  • Create Maven Project
  • Create packages for the pages class.
  • Create packages for testrunner, features, and stepdefinitions.
  • Create a feature file under ->features.
  • Create page class for HomePage and LoginPage.
  • Create methods under the LoginPage class.
  • Create methods under the HomePage class.
  • Create testclass under ->stepdefinitions.
  • Create testrunner class under ->testrunner.
  • Run the test cases.

Explanation of Steps

Add Playwright and Other Dependencies in POM.XML

Add the below dependency in POM.XML

JavaScript
 
<dependencies>
<dependency>
<groupId>com.microsoft.playwright</groupId>
<artifactId>playwright</artifactId>
<version>1.17.1</version>
</dependency>
</dependencies>

<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-java</artifactId>
<version>6.9.1</version>
</dependency>

<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-junit</artifactId>
<version>6.9.1</version>
<scope>compile</scope>
</dependency>


Step 1: Create Maven Project


We can see below project has been created with the Name “NewPlayWrightProject.”


Step 2: Create Packages for the Pages Class

Create package under src/main/java -> with name “com.saucedemo.pages.”


Step 3: Create Packages for Testrunner, Features, and Stepdefinitions

Create three packages, “testrunner,” “features,” and “stepdefinitions,” under -> src/test/java.


Step 4: Create a Feature File

Under the features package, create a .feature file with the name “LoginAndHomeTest.feature”.


Feature: Login

Scenario Outline: Login to the SwagLabs Application with the correct credentials.
Given User launched SwagLabs application.
When User verifies the Page title.
When User logged in to the app using the username “<UserName>” and password “<Password>.”
Then User verifies the product name “<ProductName>.”
Then User logout from the application.

Examples:
| UserName | Password | ProductName |
| standard_user | secret_sauce | Sauce Labs Backpack |

Step 5: Create Page Class for HomePage and LoginPage


Step 6: Create Methods Under the LoginPage Class

Under LoginPage.java, create methods to login /verify the product and logout from the site.

Create Methods under package com.saucedemo.pages.

1. verifyTitle();

2. loginIntoApplication(String email, String pass);

3. logoutFromApplication();

Other Methods:

enterUserName(String email),enterPassword(String pass),clickLoginButton(),clickOnHamburger() and clickOnLogout().

Java
 
import com.microsoft.playwright.Browser;
import com.microsoft.playwright.BrowserType;
import com.microsoft.playwright.Page;
import com.microsoft.playwright.Playwright;
import com.saucedemo.pages.HomePage;
import com.saucedemo.pages.LoginPage;

import io.cucumber.java.en.Given;
import io.cucumber.java.en.Then;
import io.cucumber.java.en.When;

public class LoginCucumberTest {

LoginPage login;
HomePage home;
Playwright playwright = Playwright.create();
BrowserType firefox = playwright.firefox();
Browser browser = firefox.launch(new BrowserType.LaunchOptions().setHeadless(false));
Page page = browser.newPage();

@Given(“User launched SwagLabs application”)
public void setUp() {
page.navigate(“https://www.saucedemo.com/“);
home = new HomePage(page);
login = new LoginPage(page);
}
@When(“User verify the Page title”)
public void verifyPageTitle() {
String title = login.verifyTitle();
Assert.assertEquals(title, “Swag Labs”);

}
//Login into the application
@When(“User logged in the app using username {string} and password {string}”)
public void loginIntoTheApplication(String username,String password ) {
login.loginIntoApplication(username, password);
}
//Verify product name after login
@Then(“User verify the product name {string}”)
public void verifyProductsName(String productname) {
String productName = home.getProductName();
Assert.assertEquals(productName, productname);

}
//Logout from application
@Then(“User logout from the application”)
public void logoutFromApplication() {
login.logoutApplication();}
}


Step 7: Create Methods for the HomePage Class

Under HomePage.java create the below methods.

productName(); //To verify the product name on the site.

Java
 
package com.saucedemo.pages;

import com.microsoft.playwright.Page;

public class HomePage {

Page page;

// Locator — — — –

String productName_1 =”id=item_4_title_link”;

//initialize Page using constructor

public HomePage(Page page) {

this.page =page;}

//Method

public String productName() {

String productName = page.textContent(productName_1);

return productName;}}


Step 8: Create a Test Class

Under the stepdefinitions -> Create “LoginCucumberTest.java” class to call all the methods that are created under LoginPage.java and HomePage.java.

Java
 
package stepdefinitions;
import org.testng.Assert;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

import com.microsoft.playwright.Browser;
import com.microsoft.playwright.BrowserType;
import com.microsoft.playwright.Page;
import com.microsoft.playwright.Playwright;
import com.saucedemo.pages.HomePage;
import com.saucedemo.pages.LoginPage;

import io.cucumber.java.en.Given;
import io.cucumber.java.en.Then;
import io.cucumber.java.en.When;

public class LoginCucumberTest {

LoginPage login;
HomePage home;
Playwright playwright = Playwright.create();
BrowserType firefox = playwright.firefox();
Browser browser = firefox.launch(new BrowserType.LaunchOptions().setHeadless(false));
Page page = browser.newPage();

@Given(“User launched SwagLabs application”)
public void setUp() {
page.navigate(“https://www.saucedemo.com/“);
home = new HomePage(page);
login = new LoginPage(page);
}
@When(“User verify the Page title”)
public void verifyPageTitle() {
String title = login.verifyTitle();
Assert.assertEquals(title, “Swag Labs”);

}
//Login into the application
@When(“User logged in the app using username {string} and password {string}”)
public void loginIntoTheApplication(String username,String password ) {
login.loginIntoApplication(username, password);
}
//Verify product name after login
@Then(“User verify the product name {string}”)
public void verifyProductsName(String productname) {
String productName = home.getProductName();
Assert.assertEquals(productName, productname);

}
//Logout from application
@Then(“User logout from the application”)
public void logoutFromApplication() {
login.logoutApplication();}
}


Step 9: Create testrunner Class to Run the Test Cases

Java
 
package com.testrunner;

import org.junit.runner.RunWith;

import io.cucumber.junit.Cucumber;
import io.cucumber.junit.CucumberOptions;

@RunWith(Cucumber.class)
@CucumberOptions(
features = “src/test/java/features/”,
glue = {“stepdefinitions”},
plugin = {“pretty”})
public class SauceDemoTestRunner {

}


Step 10: Run the Test Case

Two ways of running the test case.

Method 1:

Open testrunner class right-click and Run as -> Junit Test.


Method 2:

We can run it by right-clicking on the .feature file.


Report After Running the Test Cases


Conclusion

Integrating Cucumber with Playwright, you can leverage the expressive power of Cucumber’s feature files and step definitions to drive your Playwright automation tests. This combination allows you to write readable and maintainable tests that cover the behavior of your web application in a business-oriented language. Cucumber with Playwright in Java provides a seamless way to express and automate acceptance criteria, making it easier to collaborate and ensure the quality of your web applications.

Apache Maven application Cucumber (software) Java (programming language) Testing Integration

Opinions expressed by DZone contributors are their own.

Related

  • What BDD Is and Why You Need It: Java and Behavior Driven Development
  • TestCafe Integration With Cucumber
  • Automating Cucumber Data Table to Java Object Mapping in Your Cucumber Tests
  • A Maven Story

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!