Skip to content

Commit

Permalink
update playwright examples
Browse files Browse the repository at this point in the history
  • Loading branch information
titusfortner committed Feb 4, 2025
1 parent f176bb0 commit a54cbd7
Show file tree
Hide file tree
Showing 11 changed files with 661 additions and 24 deletions.
26 changes: 22 additions & 4 deletions .github/workflows/playwright-examples.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: Playwright JUnit 5 Tests
name: Playwright Example

on:
schedule:
Expand Down Expand Up @@ -28,19 +28,37 @@ jobs:
if: success() || failure()
run: git --no-pager diff --exit-code

standalone:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Set up Java
uses: actions/setup-java@v4
with:
java-version: 11
distribution: "temurin"
- name: Example
working-directory: ./playwright-examples
env:
SAUCE_USERNAME: ${{ secrets.SAUCE_USERNAME }}
SAUCE_ACCESS_KEY: ${{ secrets.SAUCE_ACCESS_KEY }}
run: mvn test -Dtest=StandaloneTest

build:
suite:
runs-on: ubuntu-latest
strategy:
matrix:
browser: [Chrome, MicrosoftEdge]
steps:
- uses: actions/checkout@v4
- name: Set up Java
uses: actions/setup-java@v4
with:
java-version: 11
distribution: "temurin"
- name: Run JUnit 5 tests
- name: Example
working-directory: ./playwright-examples
env:
SAUCE_USERNAME: ${{ secrets.SAUCE_USERNAME }}
SAUCE_ACCESS_KEY: ${{ secrets.SAUCE_ACCESS_KEY }}
run: mvn test
run: mvn test -Dsauce.browser.name=${{ matrix.browser }}
62 changes: 56 additions & 6 deletions playwright-examples/README.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,61 @@
# Playwright Examples

Did you know you can run Playwright tests on Sauce Labs, using Java?
There is only one way to execute Playwright on Sauce Labs with Java.
It works with any production version of Chrome or Edge.

```bash
$ export SAUCE_USERNAME=your_username
$ export SAUCE_ACCESS_KEY=your_access_key
$ mvn clean test
```
[Setup Instructions](https://github.com/saucelabs-training/demo-java/blob/main/README.md#%EF%B8%8Fsetupprerequisites)
and
[Contribution Information](https://github.com/saucelabs-training/demo-java/blob/main/README.md#contributing)
can be found on the [Main README](https://github.com/saucelabs-training/demo-java/blob/main/README.md).

## Executing Examples
The Android app being tested is found in the [My Demo App Android Repository](https://github.com/saucelabs/my-demo-app-android)

1. After cloning this repo from instructions, change to this subdirectory:
```
$ cd playwright-examples
```

2. Run the following command to update any package dependencies:
```
$ mvn dependency:resolve
```
3. Then run the following command to compile your test code:
```
$ mvn test-compile
```
4. Finally, run the following test to see if you've properly configured the test environment:
```
$ mvn clean test
```
See passing tests on [GitHub Actions](https://github.com/saucelabs-training/demo-java/actions/workflows/playwright-examples.yml)
## Configurations
This code allows toggling which browser the tests will run on.
### Chrome (default)
Tests will execute on the latest version of Chrome:
```
$ mvn clean test -Dsauce.browser=Chrome
```
### Microsoft Edge
Tests will execute on the latest version of Edge:
```
$ mvn clean test -Dsauce.browser=MicrosoftEdge
```
## Disclaimer
> The code in these scripts is provided on an "AS-IS" basis without warranty of any kind, either
> express or implied, including without limitation any implied warranties of condition,
> uninterrupted
> use, merchantability, fitness for a particular purpose, or non-infringement. These scripts are
> provided for educational and demonstration purposes only and should not be used in production.
> Issues regarding these scripts should be submitted through GitHub. These scripts are maintained by
> the Technical Services team at Sauce Labs.
>
> Some examples in this repository, such as `appium-example`, `parallel-testing`, and `headless`,
> may require a different account tier beyond free trial. Please contact
> the [Sauce Labs Sales Team](https://saucelabs.com/contact) for support and information.
46 changes: 34 additions & 12 deletions playwright-examples/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,13 @@
<groupId>com.saucelabs</groupId>
<artifactId>playwright-examples</artifactId>
<version>1.0-SNAPSHOT</version>

<name>Sauce Labs Playwright Examples</name>

<properties>
<surefire.parallel>2</surefire.parallel>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.target>11</maven.compiler.target>
<maven.compiler.source>11</maven.compiler.source>
<junit.version>5.11.4</junit.version>
</properties>

<dependencies>
Expand All @@ -26,15 +25,25 @@
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>${junit.version}</version>
<artifactId>junit-jupiter</artifactId>
<version>5.10.3</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
<groupId>com.saucelabs</groupId>
<artifactId>sauce_bindings</artifactId>
<version>2.0.0-beta.1</version>
</dependency>
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20231013</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.34</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.saucelabs</groupId>
Expand All @@ -48,11 +57,24 @@
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.13.0</version>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.5.1</version>
<configuration>
<source>${maven.compiler.source}</source>
<target>${maven.compiler.source}</target>
<excludes>
<exclude>**/StandaloneTest.java</exclude>
</excludes>
<properties>
<configurationParameters>
junit.jupiter.execution.parallel.enabled = true
junit.jupiter.execution.parallel.mode.default = concurrent
junit.jupiter.execution.parallel.config.strategy = fixed
junit.jupiter.execution.parallel.config.fixed.parallelism = ${surefire.parallel}
junit.jupiter.execution.parallel.config.fixed.max-pool-size = ${surefire.parallel}
</configurationParameters>
</properties>
<includes>
<include>com/saucedemo/**/*.java</include>
</includes>
</configuration>
</plugin>
</plugins>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package com.saucedemo.playwright;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

public class AuthenticationTest extends TestBase {

@Test
public void signInUnsuccessful() {
page.navigate("https://www.saucedemo.com/");

page.fill("[data-test='username']", "locked_out_user");
page.fill("[data-test='password']", "secret_sauce");
page.click("[data-test='login-button']");

String errorText = page.textContent("[data-test='error']");
Assertions.assertTrue(errorText.contains("Sorry, this user has been locked out"), "Error Not Found");
}

@Test
public void signInSuccessful() {
page.navigate("https://www.saucedemo.com/");

page.fill("[data-test='username']", "standard_user");
page.fill("[data-test='password']", "secret_sauce");
page.click("[data-test='login-button']");

Assertions.assertEquals("https://www.saucedemo.com/inventory.html", page.url(), "Login Not Successful");
}

@Test
public void logout() throws InterruptedException {
page.navigate("https://www.saucedemo.com/");
page.fill("[data-test='username']", "standard_user");
page.fill("[data-test='password']", "secret_sauce");
page.click("[data-test='login-button']");

page.click("#react-burger-menu-btn");
page.click("#logout_sidebar_link");

Assertions.assertEquals("https://www.saucedemo.com/", page.url(), "Logout Not Successful");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package com.saucedemo.playwright;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

public class CartTest extends TestBase {

@Test
public void addFromProductPage() {
page.navigate("https://www.saucedemo.com/");
page.locator("[data-test='username']").fill("standard_user");
page.locator("[data-test='password']").fill("secret_sauce");
page.locator("[data-test='login-button']").click();

page.locator("[data-test='add-to-cart-sauce-labs-bolt-t-shirt']").click();

Assertions.assertEquals(
"1",
page.locator(".shopping_cart_badge").textContent(),
"Item not correctly added to cart");
}

@Test
public void removeFromProductPage() {
page.navigate("https://www.saucedemo.com/");
page.locator("[data-test='username']").fill("standard_user");
page.locator("[data-test='password']").fill("secret_sauce");
page.locator("[data-test='login-button']").click();
page.locator("[data-test='add-to-cart-sauce-labs-bolt-t-shirt']").click();

page.locator("[data-test='remove-sauce-labs-bolt-t-shirt']").click();

Assertions.assertEquals(0,
page.locator(".shopping_cart_badge").count(),
"Item not correctly removed from cart");
}

@Test
public void addFromInventoryPage() {
page.navigate("https://www.saucedemo.com/");
page.locator("[data-test='username']").fill("standard_user");
page.locator("[data-test='password']").fill("secret_sauce");
page.locator("[data-test='login-button']").click();

page.locator("[data-test='add-to-cart-sauce-labs-onesie']").click();

Assertions.assertEquals("1", page.locator(".shopping_cart_badge").textContent());
}

@Test
public void removeFromInventoryPage() {
page.navigate("https://www.saucedemo.com/");
page.locator("[data-test='username']").fill("standard_user");
page.locator("[data-test='password']").fill("secret_sauce");
page.locator("[data-test='login-button']").click();
page.locator("[data-test='add-to-cart-sauce-labs-bike-light']").click();

page.locator("[data-test='remove-sauce-labs-bike-light']").click();

Assertions.assertEquals(0,
page.locator(".shopping_cart_badge").count(),
"Shopping Cart is not empty");
}

@Test
public void removeFromCartPage() {
page.navigate("https://www.saucedemo.com/");
page.locator("[data-test='username']").fill("standard_user");
page.locator("[data-test='password']").fill("secret_sauce");
page.locator("[data-test='login-button']").click();
page.locator("[data-test='add-to-cart-sauce-labs-backpack']").click();
page.locator(".shopping_cart_link").click();

page.locator("[data-test='remove-sauce-labs-backpack']").click();

Assertions.assertEquals(0,
page.locator(".shopping_cart_badge").count(),
"Shopping Cart is not empty");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package com.saucedemo.playwright;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

public class CheckoutTest extends TestBase {

@Test
public void badInfo() {
page.navigate("https://www.saucedemo.com/");
page.locator("[data-test='username']").fill("standard_user");
page.locator("[data-test='password']").fill("secret_sauce");
page.locator("[data-test='login-button']").click();
page.locator("[data-test='add-to-cart-sauce-labs-onesie']").click();
page.locator(".shopping_cart_link").click();
page.locator("[data-test='checkout']").click();

page.locator("[data-test='continue']").click();

Assertions.assertTrue(
page.locator("[data-test='firstName']").getAttribute("class").contains("error"),
"Expected error not found on page");
}

@Test
public void goodInfo() {
page.navigate("https://www.saucedemo.com/");
page.locator("[data-test='username']").fill("standard_user");
page.locator("[data-test='password']").fill("secret_sauce");
page.locator("[data-test='login-button']").click();
page.locator("[data-test='add-to-cart-sauce-labs-onesie']").click();
page.locator(".shopping_cart_link").click();
page.locator("[data-test='checkout']").click();

page.locator("[data-test='firstName']").fill("Luke");
page.locator("[data-test='lastName']").fill("Perry");
page.locator("[data-test='postalCode']").fill("90210");

page.locator("[data-test='continue']").click();

Assertions.assertEquals(
"https://www.saucedemo.com/checkout-step-two.html",
page.url(),
"Information Submission Unsuccessful");
}

@Test
public void completeCheckout() {
page.navigate("https://www.saucedemo.com/");
page.locator("[data-test='username']").fill("standard_user");
page.locator("[data-test='password']").fill("secret_sauce");
page.locator("[data-test='login-button']").click();
page.locator("[data-test='add-to-cart-sauce-labs-onesie']").click();
page.locator(".shopping_cart_link").click();
page.locator("[data-test='checkout']").click();
page.locator("[data-test='firstName']").fill("Luke");
page.locator("[data-test='lastName']").fill("Perry");
page.locator("[data-test='postalCode']").fill("90210");
page.locator("[data-test='continue']").click();

page.locator("[data-test='finish']").click();

Assertions.assertEquals(
"https://www.saucedemo.com/checkout-complete.html",
page.url());

Assertions.assertTrue(page.locator(".complete-text").isVisible());
}
}
Loading

0 comments on commit a54cbd7

Please sign in to comment.