Skip to content

Commit

Permalink
add demo tests to selenium examples
Browse files Browse the repository at this point in the history
  • Loading branch information
titusfortner committed Oct 18, 2023
1 parent 0edd38b commit 32a59fc
Show file tree
Hide file tree
Showing 7 changed files with 311 additions and 49 deletions.
26 changes: 20 additions & 6 deletions selenium-examples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
```
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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<AbstractDriverOptions>) options).setPlatformName("Windows 11");

URL url;
try {
Expand Down
Original file line number Diff line number Diff line change
@@ -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");
}
}
Original file line number Diff line number Diff line change
@@ -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");
}
}
Original file line number Diff line number Diff line change
@@ -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());
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -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());
}
}

0 comments on commit 32a59fc

Please sign in to comment.