From 8d743d9fa97a00f260871d745ef5bc6bdcf4ce0b Mon Sep 17 00:00:00 2001 From: Greg Gauthier Date: Wed, 25 Jan 2023 16:39:47 +0000 Subject: [PATCH] first example using saucelabs swag shop example --- pom.xml | 21 ++++---- src/main/java/org/example/pages/Home.java | 18 +++++++ src/main/java/org/example/pages/Login.java | 46 +++++++++++++++++ src/test/java/org/example/TestContext.java | 33 +++++++++++++ .../java/org/example/features/login.feature | 15 ++++++ .../java/org/example/runner/DemoRunner.java | 14 ++++++ .../java/org/example/steps/LoginSteps.java | 49 +++++++++++++++++++ 7 files changed, 187 insertions(+), 9 deletions(-) create mode 100644 src/main/java/org/example/pages/Home.java create mode 100644 src/main/java/org/example/pages/Login.java create mode 100644 src/test/java/org/example/TestContext.java create mode 100644 src/test/java/org/example/features/login.feature create mode 100644 src/test/java/org/example/runner/DemoRunner.java create mode 100644 src/test/java/org/example/steps/LoginSteps.java diff --git a/pom.xml b/pom.xml index d4036d1..cd8db72 100644 --- a/pom.xml +++ b/pom.xml @@ -39,14 +39,7 @@ - - - org.junit.platform - junit-platform-suite - 1.9.2 - test - - + org.junit.jupiter @@ -55,6 +48,7 @@ test + com.microsoft.playwright @@ -83,13 +77,22 @@ 1.29.0 - + io.cucumber cucumber-java test + + + io.cucumber + cucumber-junit + 7.11.0 + test + + + io.cucumber cucumber-junit-platform-engine diff --git a/src/main/java/org/example/pages/Home.java b/src/main/java/org/example/pages/Home.java new file mode 100644 index 0000000..63cd127 --- /dev/null +++ b/src/main/java/org/example/pages/Home.java @@ -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); + } +} \ No newline at end of file diff --git a/src/main/java/org/example/pages/Login.java b/src/main/java/org/example/pages/Login.java new file mode 100644 index 0000000..8462a63 --- /dev/null +++ b/src/main/java/org/example/pages/Login.java @@ -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);} +} \ No newline at end of file diff --git a/src/test/java/org/example/TestContext.java b/src/test/java/org/example/TestContext.java new file mode 100644 index 0000000..7454119 --- /dev/null +++ b/src/test/java/org/example/TestContext.java @@ -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()); + } + } +} diff --git a/src/test/java/org/example/features/login.feature b/src/test/java/org/example/features/login.feature new file mode 100644 index 0000000..ec9eadf --- /dev/null +++ b/src/test/java/org/example/features/login.feature @@ -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 “” and password “” + Then User verify the product name “” + Then User logout from the application + + Examples: + | UserName | Password | ProductName | + | standard_user | secret_sauce | Sauce Labs Backpack | + diff --git a/src/test/java/org/example/runner/DemoRunner.java b/src/test/java/org/example/runner/DemoRunner.java new file mode 100644 index 0000000..98c7438 --- /dev/null +++ b/src/test/java/org/example/runner/DemoRunner.java @@ -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 { + +} diff --git a/src/test/java/org/example/steps/LoginSteps.java b/src/test/java/org/example/steps/LoginSteps.java new file mode 100644 index 0000000..5788635 --- /dev/null +++ b/src/test/java/org/example/steps/LoginSteps.java @@ -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();} +}