diff --git a/selenium-examples/README.md b/selenium-examples/README.md index 0517abc9..f45ad1f2 100644 --- a/selenium-examples/README.md +++ b/selenium-examples/README.md @@ -2,17 +2,31 @@ This is the default project for all Selenium and Sauce Labs features +There are 3 packages: + * Demo package for seeing Sauce Demo site tests -* Sauce Features package for demos of executing special Sauce Labs features -* Selenium Features package for demos of executing Selenium on Sauce Labs +* Sauce Labs Features package for demos of functionality exclusive to Sauce Labs +* Selenium Features package for demos of Selenium features on Sauce Labs It is also to demonstrate how to run tests in parallel with Selenium 4 and JUnit 5. See [pom.xml](pom.xml) -## Execute tests in parallel: +## Execution options: + +First, ensure you are in `selenium-examples` directory + ```bash cd selenium-examples -mvn test -Dsurefire.parallel=20 ``` - - +1. Run everything with default parallelization: + ```bash + mvn clean test + ``` +2. Run only demo tests: + ```bash + mvn clean test -Dtest='com.saucedemo.selenium.demo.*Test' + ``` +3. Change parallelization + ```bash + mvn test -Dsurefire.parallel=20 + ``` diff --git a/selenium-examples/src/test/java/com/saucedemo/selenium/TestBase.java b/selenium-examples/src/test/java/com/saucedemo/selenium/TestBase.java index a8035881..9b57ca10 100644 --- a/selenium-examples/src/test/java/com/saucedemo/selenium/TestBase.java +++ b/selenium-examples/src/test/java/com/saucedemo/selenium/TestBase.java @@ -15,6 +15,7 @@ import org.openqa.selenium.WebDriver; import org.openqa.selenium.chrome.ChromeOptions; import org.openqa.selenium.firefox.FirefoxOptions; +import org.openqa.selenium.remote.AbstractDriverOptions; import org.openqa.selenium.remote.RemoteWebDriver; import org.openqa.selenium.remote.SessionId; @@ -49,7 +50,9 @@ protected void startSession( sauceOptions.put("username", System.getenv("SAUCE_USERNAME")); sauceOptions.put("accessKey", System.getenv("SAUCE_ACCESS_KEY")); sauceOptions.put("name", testInfo.getDisplayName()); + sauceOptions.put("seleniumVersion", "4.14.1"); ((MutableCapabilities) options).setCapability("sauce:options", sauceOptions); + ((AbstractDriverOptions) options).setPlatformName("Windows 11"); URL url; try { diff --git a/selenium-examples/src/test/java/com/saucedemo/selenium/demo/AuthenticationTest.java b/selenium-examples/src/test/java/com/saucedemo/selenium/demo/AuthenticationTest.java new file mode 100644 index 00000000..c4a9a996 --- /dev/null +++ b/selenium-examples/src/test/java/com/saucedemo/selenium/demo/AuthenticationTest.java @@ -0,0 +1,56 @@ +package com.saucedemo.selenium.demo; + +import com.saucedemo.selenium.TestBase; +import org.junit.jupiter.api.*; +import org.openqa.selenium.By; +import org.openqa.selenium.WebElement; + +public class AuthenticationTest extends TestBase { + + @BeforeEach + public void setup(TestInfo testInfo) { + startFirefoxSession(testInfo); + } + + @Test + public void signInUnsuccessful() { + driver.get("https://www.saucedemo.com/"); + + driver.findElement(By.cssSelector("input[data-test='username']")).sendKeys("locked_out_user"); + driver.findElement(By.cssSelector("input[data-test='password']")).sendKeys("secret_sauce"); + driver.findElement(By.cssSelector("input[data-test='login-button']")).click(); + + WebElement errorElement = driver.findElement(By.cssSelector("[data-test='error']")); + Assertions.assertTrue(errorElement.getText().contains("Sorry, this user has been locked out"), + "Error Not Found"); + } + + @Test + public void signInSuccessful() { + driver.get("https://www.saucedemo.com/"); + + driver.findElement(By.cssSelector("input[data-test='username']")).sendKeys("standard_user"); + driver.findElement(By.cssSelector("input[data-test='password']")).sendKeys("secret_sauce"); + driver.findElement(By.cssSelector("input[data-test='login-button']")).click(); + + Assertions.assertEquals("https://www.saucedemo.com/inventory.html", + driver.getCurrentUrl(), + "Login Not Successful"); + } + + @Test + public void logout() throws InterruptedException { + driver.get("https://www.saucedemo.com/"); + driver.findElement(By.cssSelector("input[data-test='username']")).sendKeys("standard_user"); + driver.findElement(By.cssSelector("input[data-test='password']")).sendKeys("secret_sauce"); + driver.findElement(By.cssSelector("input[data-test='login-button']")).click(); + driver.findElement(By.id("react-burger-menu-btn")).click(); + Thread.sleep(1000); + + driver.findElement(By.id("logout_sidebar_link")).click(); + + Assertions.assertEquals("https://www.saucedemo.com/", + driver.getCurrentUrl(), + "Logout Not Successful"); + } +} diff --git a/selenium-examples/src/test/java/com/saucedemo/selenium/demo/CartTest.java b/selenium-examples/src/test/java/com/saucedemo/selenium/demo/CartTest.java new file mode 100644 index 00000000..0654c54d --- /dev/null +++ b/selenium-examples/src/test/java/com/saucedemo/selenium/demo/CartTest.java @@ -0,0 +1,86 @@ +package com.saucedemo.selenium.demo; + +import com.saucedemo.selenium.TestBase; +import org.junit.jupiter.api.*; +import org.openqa.selenium.By; + +public class CartTest extends TestBase { + + @BeforeEach + public void setup(TestInfo testInfo) { + startFirefoxSession(testInfo); + } + + @Test + public void addFromProductPage() { + driver.get("https://www.saucedemo.com/"); + driver.findElement(By.cssSelector("input[data-test='username']")).sendKeys("standard_user"); + driver.findElement(By.cssSelector("input[data-test='password']")).sendKeys("secret_sauce"); + driver.findElement(By.cssSelector("input[data-test='login-button']")).click(); + + driver.findElement(By.id("item_1_title_link")).click(); + + driver.findElement(By.cssSelector("button[data-test='add-to-cart-sauce-labs-bolt-t-shirt']")).click(); + + Assertions.assertEquals("1", + driver.findElement(By.className("shopping_cart_badge")).getText(), + "Item not correctly added to cart"); + } + + @Test + public void removeFromProductPage() { + driver.get("https://www.saucedemo.com/"); + driver.findElement(By.cssSelector("input[data-test='username']")).sendKeys("standard_user"); + driver.findElement(By.cssSelector("input[data-test='password']")).sendKeys("secret_sauce"); + driver.findElement(By.cssSelector("input[data-test='login-button']")).click(); + driver.findElement(By.id("item_1_title_link")).click(); + driver.findElement(By.cssSelector("button[data-test='add-to-cart-sauce-labs-bolt-t-shirt']")).click(); + + driver.findElement(By.cssSelector("button[data-test='remove-sauce-labs-bolt-t-shirt']")).click(); + + Assertions.assertTrue(driver.findElements(By.className("shopping_cart_badge")).isEmpty(), + "Item not correctly removed from cart"); + } + + @Test + public void addFromInventoryPage() { + driver.get("https://www.saucedemo.com/"); + driver.findElement(By.cssSelector("input[data-test='username']")).sendKeys("standard_user"); + driver.findElement(By.cssSelector("input[data-test='password']")).sendKeys("secret_sauce"); + driver.findElement(By.cssSelector("input[data-test='login-button']")).click(); + + driver.findElement(By.cssSelector("button[data-test='add-to-cart-sauce-labs-onesie']")).click(); + + Assertions.assertEquals("1", + driver.findElement(By.className("shopping_cart_badge")).getText()); + } + + @Test + public void removeFromInventoryPage() { + driver.get("https://www.saucedemo.com/"); + driver.findElement(By.cssSelector("input[data-test='username']")).sendKeys("standard_user"); + driver.findElement(By.cssSelector("input[data-test='password']")).sendKeys("secret_sauce"); + driver.findElement(By.cssSelector("input[data-test='login-button']")).click(); + driver.findElement(By.cssSelector("button[data-test='add-to-cart-sauce-labs-bike-light']")).click(); + + driver.findElement(By.cssSelector("button[data-test='remove-sauce-labs-bike-light']")).click(); + + Assertions.assertTrue(driver.findElements(By.className("shopping_cart_badge")).isEmpty(), + "Shopping Cart is not empty"); + } + + @Test + public void removeFromCartPage() { + driver.get("https://www.saucedemo.com/"); + driver.findElement(By.cssSelector("input[data-test='username']")).sendKeys("standard_user"); + driver.findElement(By.cssSelector("input[data-test='password']")).sendKeys("secret_sauce"); + driver.findElement(By.cssSelector("input[data-test='login-button']")).click(); + driver.findElement(By.cssSelector("button[data-test='add-to-cart-sauce-labs-backpack']")).click(); + driver.findElement(By.className("shopping_cart_link")).click(); + + driver.findElement(By.cssSelector("button[data-test='remove-sauce-labs-backpack']")).click(); + + Assertions.assertTrue(driver.findElements(By.className("shopping_cart_badge")).isEmpty(), + "Shopping Cart is not empty"); + } +} diff --git a/selenium-examples/src/test/java/com/saucedemo/selenium/demo/CheckoutTest.java b/selenium-examples/src/test/java/com/saucedemo/selenium/demo/CheckoutTest.java new file mode 100644 index 00000000..4fe67ffb --- /dev/null +++ b/selenium-examples/src/test/java/com/saucedemo/selenium/demo/CheckoutTest.java @@ -0,0 +1,72 @@ +package com.saucedemo.selenium.demo; + +import com.saucedemo.selenium.TestBase; +import org.junit.jupiter.api.*; +import org.openqa.selenium.By; + +public class CheckoutTest extends TestBase { + + @BeforeEach + public void setup(TestInfo testInfo) { + startFirefoxSession(testInfo); + } + + + @Test + public void badInfo() { + driver.get("https://www.saucedemo.com/"); + driver.findElement(By.cssSelector("input[data-test='username']")).sendKeys("standard_user"); + driver.findElement(By.cssSelector("input[data-test='password']")).sendKeys("secret_sauce"); + driver.findElement(By.cssSelector("input[data-test='login-button']")).click(); + driver.findElement(By.cssSelector("button[data-test='add-to-cart-sauce-labs-onesie']")).click(); + driver.findElement(By.className("shopping_cart_link")).click(); + driver.findElement(By.cssSelector("button[data-test='checkout']")).click(); + + driver.findElement(By.cssSelector("input[data-test='continue']")).click(); + + Assertions.assertTrue(driver.findElement(By.cssSelector("input[data-test='firstName']")).getAttribute("class").contains("error"), + "Expected error not found on page"); + } + + @Test + public void goodInfo() { + driver.get("https://www.saucedemo.com/"); + driver.findElement(By.cssSelector("input[data-test='username']")).sendKeys("standard_user"); + driver.findElement(By.cssSelector("input[data-test='password']")).sendKeys("secret_sauce"); + driver.findElement(By.cssSelector("input[data-test='login-button']")).click(); + driver.findElement(By.cssSelector("button[data-test='add-to-cart-sauce-labs-onesie']")).click(); + driver.findElement(By.className("shopping_cart_link")).click(); + driver.findElement(By.cssSelector("button[data-test='checkout']")).click(); + + driver.findElement(By.cssSelector("input[data-test='firstName']")).sendKeys("Luke"); + driver.findElement(By.cssSelector("input[data-test='lastName']")).sendKeys("Perry"); + driver.findElement(By.cssSelector("input[data-test='postalCode']")).sendKeys("90210"); + + driver.findElement(By.cssSelector("input[data-test='continue']")).click(); + + Assertions.assertEquals("https://www.saucedemo.com/checkout-step-two.html", + driver.getCurrentUrl(), + "Information Submission Unsuccessful"); + } + + @Test + public void completeCheckout() { + driver.get("https://www.saucedemo.com/"); + driver.findElement(By.cssSelector("input[data-test='username']")).sendKeys("standard_user"); + driver.findElement(By.cssSelector("input[data-test='password']")).sendKeys("secret_sauce"); + driver.findElement(By.cssSelector("input[data-test='login-button']")).click(); + driver.findElement(By.cssSelector("button[data-test='add-to-cart-sauce-labs-onesie']")).click(); + driver.findElement(By.className("shopping_cart_link")).click(); + driver.findElement(By.cssSelector("button[data-test='checkout']")).click(); + driver.findElement(By.cssSelector("input[data-test='firstName']")).sendKeys("Luke"); + driver.findElement(By.cssSelector("input[data-test='lastName']")).sendKeys("Perry"); + driver.findElement(By.cssSelector("input[data-test='postalCode']")).sendKeys("90210"); + driver.findElement(By.cssSelector("input[data-test='continue']")).click(); + + driver.findElement(By.cssSelector("button[data-test='finish']")).click(); + + Assertions.assertEquals("https://www.saucedemo.com/checkout-complete.html", driver.getCurrentUrl()); + + Assertions.assertTrue(driver.findElement(By.className("complete-text")).isDisplayed()); + } +} diff --git a/selenium-examples/src/test/java/com/saucedemo/selenium/demo/LoginTest.java b/selenium-examples/src/test/java/com/saucedemo/selenium/demo/LoginTest.java deleted file mode 100644 index 01303847..00000000 --- a/selenium-examples/src/test/java/com/saucedemo/selenium/demo/LoginTest.java +++ /dev/null @@ -1,43 +0,0 @@ -package com.saucedemo.selenium.demo; - -import com.saucedemo.selenium.TestBase; -import java.time.Duration; -import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.DisplayName; -import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.TestInfo; -import org.openqa.selenium.By; -import org.openqa.selenium.WebElement; -import org.openqa.selenium.support.ui.WebDriverWait; - -public class LoginTest extends TestBase { - - @BeforeEach - public void setup(TestInfo testInfo) { - startChromeSession(testInfo); - } - - @DisplayName("Swag Labs Login with Selenium") - @Test - public void swagLabsLoginTest() { - driver.get("https://www.saucedemo.com"); - - By usernameFieldLocator = By.cssSelector("#user-name"); - By passwordFieldLocator = By.cssSelector("#password"); - By submitButtonLocator = By.cssSelector(".btn_action"); - - WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(20)); - wait.until((driver) -> driver.findElement(usernameFieldLocator).isDisplayed()); - - WebElement userNameField = driver.findElement(usernameFieldLocator); - WebElement passwordField = driver.findElement(passwordFieldLocator); - WebElement submitButton = driver.findElement(submitButtonLocator); - - userNameField.sendKeys("standard_user"); - passwordField.sendKeys("secret_sauce"); - submitButton.click(); - - Assertions.assertEquals("https://www.saucedemo.com/inventory.html", driver.getCurrentUrl()); - } -} diff --git a/selenium-examples/src/test/java/com/saucedemo/selenium/demo/NavigationTest.java b/selenium-examples/src/test/java/com/saucedemo/selenium/demo/NavigationTest.java new file mode 100644 index 00000000..a9388857 --- /dev/null +++ b/selenium-examples/src/test/java/com/saucedemo/selenium/demo/NavigationTest.java @@ -0,0 +1,74 @@ +package com.saucedemo.selenium.demo; + +import com.saucedemo.selenium.TestBase; +import org.junit.jupiter.api.*; +import org.openqa.selenium.By; + +public class NavigationTest extends TestBase { + + @BeforeEach + public void setup(TestInfo testInfo) { + startFirefoxSession(testInfo); + } + + @Test + public void cancelFromCart() { + driver.get("https://www.saucedemo.com/"); + driver.findElement(By.cssSelector("input[data-test='username']")).sendKeys("standard_user"); + driver.findElement(By.cssSelector("input[data-test='password']")).sendKeys("secret_sauce"); + driver.findElement(By.cssSelector("input[data-test='login-button']")).click(); + driver.findElement(By.className("shopping_cart_link")).click(); + + driver.findElement(By.cssSelector("button[data-test='continue-shopping']")).click(); + + Assertions.assertEquals("https://www.saucedemo.com/inventory.html", driver.getCurrentUrl()); + } + + @Test + public void cancelFromInfoPage() { + driver.get("https://www.saucedemo.com/"); + driver.findElement(By.cssSelector("input[data-test='username']")).sendKeys("standard_user"); + driver.findElement(By.cssSelector("input[data-test='password']")).sendKeys("secret_sauce"); + driver.findElement(By.cssSelector("input[data-test='login-button']")).click(); + driver.findElement(By.cssSelector("button[data-test='add-to-cart-sauce-labs-onesie']")).click(); + driver.findElement(By.className("shopping_cart_link")).click(); + driver.findElement(By.cssSelector("button[data-test='checkout']")).click(); + + driver.findElement(By.cssSelector("button[data-test='cancel']")).click(); + + Assertions.assertEquals("https://www.saucedemo.com/cart.html", driver.getCurrentUrl()); + } + + @Test + public void cancelFromCheckoutPage() { + driver.get("https://www.saucedemo.com/"); + driver.findElement(By.cssSelector("input[data-test='username']")).sendKeys("standard_user"); + driver.findElement(By.cssSelector("input[data-test='password']")).sendKeys("secret_sauce"); + driver.findElement(By.cssSelector("input[data-test='login-button']")).click(); + driver.findElement(By.cssSelector("button[data-test='add-to-cart-sauce-labs-onesie']")).click(); + driver.findElement(By.className("shopping_cart_link")).click(); + driver.findElement(By.cssSelector("button[data-test='checkout']")).click(); + driver.findElement(By.cssSelector("input[data-test='firstName']")).sendKeys("Luke"); + driver.findElement(By.cssSelector("input[data-test='lastName']")).sendKeys("Perry"); + driver.findElement(By.cssSelector("input[data-test='postalCode']")).sendKeys("90210"); + driver.findElement(By.cssSelector("input[data-test='continue']")).click(); + + driver.findElement(By.cssSelector("button[data-test='cancel']")).click(); + + Assertions.assertEquals("https://www.saucedemo.com/inventory.html", driver.getCurrentUrl()); + } + + @Test + public void startCheckout() { + driver.get("https://www.saucedemo.com/"); + driver.findElement(By.cssSelector("input[data-test='username']")).sendKeys("standard_user"); + driver.findElement(By.cssSelector("input[data-test='password']")).sendKeys("secret_sauce"); + driver.findElement(By.cssSelector("input[data-test='login-button']")).click(); + driver.findElement(By.cssSelector("button[data-test='add-to-cart-sauce-labs-onesie']")).click(); + driver.findElement(By.className("shopping_cart_link")).click(); + + driver.findElement(By.cssSelector("button[data-test='checkout']")).click(); + + Assertions.assertEquals("https://www.saucedemo.com/checkout-step-one.html", driver.getCurrentUrl()); + } +}