first example using saucelabs swag shop example

This commit is contained in:
Greg Gauthier 2023-01-25 16:39:47 +00:00
parent d5490c9133
commit 8d743d9fa9
7 changed files with 187 additions and 9 deletions

21
pom.xml
View File

@ -39,14 +39,7 @@
<dependencies>
<!-- https://mvnrepository.com/artifact/org.junit.platform/junit-platform-suite -->
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-suite</artifactId>
<version>1.9.2</version>
<scope>test</scope>
</dependency>
<!-- *********************************** JUNIT5 **************************** -->
<!-- https://mvnrepository.com/artifact/org.junit.jupiter/junit-jupiter-api -->
<dependency>
<groupId>org.junit.jupiter</groupId>
@ -55,6 +48,7 @@
<scope>test</scope>
</dependency>
<!-- *************************** PLAYWRIGHT **************************** -->
<!-- https://mvnrepository.com/artifact/com.microsoft.playwright/playwright -->
<dependency>
<groupId>com.microsoft.playwright</groupId>
@ -83,13 +77,22 @@
<version>1.29.0</version>
</dependency>
<!-- *********************** CUCUMBER ******************************* -->
<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-java</artifactId>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/io.cucumber/cucumber-junit -->
<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-junit</artifactId>
<version>7.11.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-junit-platform-engine</artifactId>

View File

@ -0,0 +1,18 @@
package org.example.pages;
import com.microsoft.playwright.Page;
public class Home {
Page page;
String productName_1 ="id=item_4_title_link";
public Home(Page page) {
this.page =page;
}
public String getProductName() {
return page.textContent(productName_1);
}
}

View File

@ -0,0 +1,46 @@
package org.example.pages;
import com.microsoft.playwright.Page;
public class Login {
Page page;
// Locator strings for use in playwright
String username = "id=user-name";
String password = "id=password";
String clickLogin = "id=login-button";
String clickHamburger = "id=react-burger-menu-btn";
String clickLogout = "id=logout_sidebar_link";
public Login(Page page) {
this.page = page;
}public String verifyTitle() {
return page.title();}
public void loginIntoApplication(String email, String pass) {
enterUserName(email);
enterPassword(pass);
clickLoginButton();}
public void logoutApplication() {
clickOnHamburger();
clickOnLogout(); } // Logout from the application
public void enterUserName(String email) {
page.fill(username, email);}
public void enterPassword(String pass) {
page.fill(password, pass);}
public void clickLoginButton() {
page.click(clickLogin);}
public void clickOnHamburger() {
page.click(clickHamburger);
}
public void clickOnLogout() {
page.click(clickLogout);}
}

View File

@ -0,0 +1,33 @@
package org.example;
import com.microsoft.playwright.Browser;
import com.microsoft.playwright.BrowserType;
import com.microsoft.playwright.Page;
import com.microsoft.playwright.Playwright;
public class TestContext {
public Playwright playwright = Playwright.create();
public Browser browser;
public Page page;
/***
* TestContext returns the Playwright browser driver, and a page for executing steps
* @param driver: a string indiciating the kind of browser you want
* @param headless: a boolean indicating whether you'd like the browser headless or not
*
*/
public TestContext(String driver, boolean headless){
try {
switch (driver) {
case "webkit": browser =
playwright.webkit().launch(new BrowserType.LaunchOptions().setHeadless(headless));
case "chromium": browser =
playwright.chromium().launch(new BrowserType.LaunchOptions().setHeadless(headless));
case "firefox": browser = playwright.firefox().launch(new BrowserType.LaunchOptions().setHeadless(headless));
default: browser = playwright.webkit().launch();
}
page = browser.newPage();
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
}

View File

@ -0,0 +1,15 @@
# Sample Cucumber test using the SWAGLABS site
Feature: Login
Scenario Outline: Login to SwagLabs Application with Correct credentials
Given User launched SwagLabs application
When User verify the Page title
When User logged in the app using username <UserName> and password <Password>
Then User verify the product name <ProductName>
Then User logout from the application
Examples:
| UserName | Password | ProductName |
| standard_user | secret_sauce | Sauce Labs Backpack |

View File

@ -0,0 +1,14 @@
package org.example.runner;
import io.cucumber.junit.Cucumber;
import io.cucumber.junit.CucumberOptions;
import org.junit.runner.RunWith;
@RunWith(Cucumber.class)
@CucumberOptions(
features = "src/test/java/org/example/features/",
glue = {"steps"},
plugin = {"pretty"})
public class DemoRunner {
}

View File

@ -0,0 +1,49 @@
package org.example.steps;
import com.microsoft.playwright.Browser;
import com.microsoft.playwright.BrowserType;
import com.microsoft.playwright.Page;
import com.microsoft.playwright.Playwright;
import io.cucumber.java.en.Given;
import io.cucumber.java.en.Then;
import io.cucumber.java.en.When;
import org.example.TestContext;
import org.example.pages.Home;
import org.example.pages.Login;
import org.junit.Assert;
public class LoginSteps {
Login login;
Home home;
TestContext testContext = new TestContext("webkit", true);
Page page = testContext.page;
@Given("User launched SwagLabs application")
public void setUp() {
page.navigate("https://www.saucedemo.com/");
home = new Home(page);
login = new Login(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();}
}