Skip to content

Commit d4c9fe2

Browse files
authored
update playwright examples (#273)
1 parent c23b1f7 commit d4c9fe2

File tree

11 files changed

+673
-25
lines changed

11 files changed

+673
-25
lines changed

.github/workflows/playwright-examples.yml

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
name: Playwright JUnit 5 Tests
1+
name: Playwright Example
22

33
on:
44
schedule:
@@ -28,19 +28,37 @@ jobs:
2828
if: success() || failure()
2929
run: git --no-pager diff --exit-code
3030

31+
standalone:
32+
runs-on: ubuntu-latest
33+
steps:
34+
- uses: actions/checkout@v4
35+
- name: Set up Java
36+
uses: actions/setup-java@v4
37+
with:
38+
java-version: 11
39+
distribution: "temurin"
40+
- name: Example
41+
working-directory: ./playwright-examples
42+
env:
43+
SAUCE_USERNAME: ${{ secrets.SAUCE_USERNAME }}
44+
SAUCE_ACCESS_KEY: ${{ secrets.SAUCE_ACCESS_KEY }}
45+
run: mvn test -Dtest=StandaloneTest -Dsurefire.parallel=1
3146

32-
build:
47+
suite:
3348
runs-on: ubuntu-latest
49+
strategy:
50+
matrix:
51+
browser: [Chrome, MicrosoftEdge]
3452
steps:
3553
- uses: actions/checkout@v4
3654
- name: Set up Java
3755
uses: actions/setup-java@v4
3856
with:
3957
java-version: 11
4058
distribution: "temurin"
41-
- name: Run JUnit 5 tests
59+
- name: Example
4260
working-directory: ./playwright-examples
4361
env:
4462
SAUCE_USERNAME: ${{ secrets.SAUCE_USERNAME }}
4563
SAUCE_ACCESS_KEY: ${{ secrets.SAUCE_ACCESS_KEY }}
46-
run: mvn test
64+
run: mvn test -Dsauce.browser.name=${{ matrix.browser }}

playwright-examples/README.md

Lines changed: 56 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,61 @@
11
# Playwright Examples
22

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

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

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

14+
1. After cloning this repo from instructions, change to this subdirectory:
15+
```
16+
$ cd playwright-examples
17+
```
18+
19+
2. Run the following command to update any package dependencies:
20+
```
21+
$ mvn dependency:resolve
22+
```
23+
3. Then run the following command to compile your test code:
24+
```
25+
$ mvn test-compile
26+
```
27+
4. Finally, run the following test to see if you've properly configured the test environment:
28+
```
29+
$ mvn clean test
30+
```
31+
32+
See passing tests on [GitHub Actions](https://github.com/saucelabs-training/demo-java/actions/workflows/playwright-examples.yml)
33+
34+
## Configurations
35+
This code allows toggling which browser the tests will run on.
36+
37+
### Chrome (default)
38+
Tests will execute on the latest version of Chrome:
39+
```
40+
$ mvn clean test -Dsauce.browser=Chrome
41+
```
42+
43+
### Microsoft Edge
44+
Tests will execute on the latest version of Edge:
45+
```
46+
$ mvn clean test -Dsauce.browser=MicrosoftEdge
47+
```
48+
49+
## Disclaimer
50+
51+
> The code in these scripts is provided on an "AS-IS" basis without warranty of any kind, either
52+
> express or implied, including without limitation any implied warranties of condition,
53+
> uninterrupted
54+
> use, merchantability, fitness for a particular purpose, or non-infringement. These scripts are
55+
> provided for educational and demonstration purposes only and should not be used in production.
56+
> Issues regarding these scripts should be submitted through GitHub. These scripts are maintained by
57+
> the Technical Services team at Sauce Labs.
58+
>
59+
> Some examples in this repository, such as `appium-example`, `parallel-testing`, and `headless`,
60+
> may require a different account tier beyond free trial. Please contact
61+
> the [Sauce Labs Sales Team](https://saucelabs.com/contact) for support and information.

playwright-examples/pom.xml

Lines changed: 34 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,13 @@
77
<groupId>com.saucelabs</groupId>
88
<artifactId>playwright-examples</artifactId>
99
<version>1.0-SNAPSHOT</version>
10-
1110
<name>Sauce Labs Playwright Examples</name>
1211

1312
<properties>
13+
<surefire.parallel>10</surefire.parallel>
1414
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
1515
<maven.compiler.target>11</maven.compiler.target>
1616
<maven.compiler.source>11</maven.compiler.source>
17-
<junit.version>5.11.4</junit.version>
1817
</properties>
1918

2019
<dependencies>
@@ -26,15 +25,25 @@
2625
</dependency>
2726
<dependency>
2827
<groupId>org.junit.jupiter</groupId>
29-
<artifactId>junit-jupiter-api</artifactId>
30-
<version>${junit.version}</version>
28+
<artifactId>junit-jupiter</artifactId>
29+
<version>5.10.3</version>
3130
<scope>test</scope>
3231
</dependency>
3332
<dependency>
34-
<groupId>org.junit.jupiter</groupId>
35-
<artifactId>junit-jupiter-engine</artifactId>
36-
<version>${junit.version}</version>
37-
<scope>test</scope>
33+
<groupId>com.saucelabs</groupId>
34+
<artifactId>sauce_bindings</artifactId>
35+
<version>2.0.0-beta.1</version>
36+
</dependency>
37+
<dependency>
38+
<groupId>org.json</groupId>
39+
<artifactId>json</artifactId>
40+
<version>20231013</version>
41+
</dependency>
42+
<dependency>
43+
<groupId>org.projectlombok</groupId>
44+
<artifactId>lombok</artifactId>
45+
<version>1.18.34</version>
46+
<scope>provided</scope>
3847
</dependency>
3948
<dependency>
4049
<groupId>com.saucelabs</groupId>
@@ -48,11 +57,24 @@
4857
<plugins>
4958
<plugin>
5059
<groupId>org.apache.maven.plugins</groupId>
51-
<artifactId>maven-compiler-plugin</artifactId>
52-
<version>3.13.0</version>
60+
<artifactId>maven-surefire-plugin</artifactId>
61+
<version>3.5.1</version>
5362
<configuration>
54-
<source>${maven.compiler.source}</source>
55-
<target>${maven.compiler.source}</target>
63+
<excludes>
64+
<exclude>**/StandaloneTest.java</exclude>
65+
</excludes>
66+
<properties>
67+
<configurationParameters>
68+
junit.jupiter.execution.parallel.enabled = true
69+
junit.jupiter.execution.parallel.mode.default = concurrent
70+
junit.jupiter.execution.parallel.config.strategy = fixed
71+
junit.jupiter.execution.parallel.config.fixed.parallelism = ${surefire.parallel}
72+
junit.jupiter.execution.parallel.config.fixed.max-pool-size = ${surefire.parallel}
73+
</configurationParameters>
74+
</properties>
75+
<includes>
76+
<include>com/saucedemo/**/*.java</include>
77+
</includes>
5678
</configuration>
5779
</plugin>
5880
</plugins>
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package com.saucedemo.playwright;
2+
3+
import org.junit.jupiter.api.Assertions;
4+
import org.junit.jupiter.api.Test;
5+
6+
public class AuthenticationTest extends TestBase {
7+
8+
@Test
9+
public void signInUnsuccessful() {
10+
page.navigate("https://www.saucedemo.com/");
11+
12+
page.fill("[data-test='username']", "locked_out_user");
13+
page.fill("[data-test='password']", "secret_sauce");
14+
page.click("[data-test='login-button']");
15+
16+
String errorText = page.textContent("[data-test='error']");
17+
Assertions.assertTrue(
18+
errorText.contains("Sorry, this user has been locked out"), "Error Not Found");
19+
}
20+
21+
@Test
22+
public void signInSuccessful() {
23+
page.navigate("https://www.saucedemo.com/");
24+
25+
page.fill("[data-test='username']", "standard_user");
26+
page.fill("[data-test='password']", "secret_sauce");
27+
page.click("[data-test='login-button']");
28+
29+
Assertions.assertEquals(
30+
"https://www.saucedemo.com/inventory.html", page.url(), "Login Not Successful");
31+
}
32+
33+
@Test
34+
public void logout() throws InterruptedException {
35+
page.navigate("https://www.saucedemo.com/");
36+
page.fill("[data-test='username']", "standard_user");
37+
page.fill("[data-test='password']", "secret_sauce");
38+
page.click("[data-test='login-button']");
39+
40+
page.click("#react-burger-menu-btn");
41+
page.click("#logout_sidebar_link");
42+
43+
Assertions.assertEquals("https://www.saucedemo.com/", page.url(), "Logout Not Successful");
44+
}
45+
}
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
package com.saucedemo.playwright;
2+
3+
import org.junit.jupiter.api.Assertions;
4+
import org.junit.jupiter.api.Test;
5+
6+
public class CartTest extends TestBase {
7+
8+
@Test
9+
public void addFromProductPage() {
10+
page.navigate("https://www.saucedemo.com/");
11+
page.locator("[data-test='username']").fill("standard_user");
12+
page.locator("[data-test='password']").fill("secret_sauce");
13+
page.locator("[data-test='login-button']").click();
14+
15+
page.locator("[data-test='add-to-cart-sauce-labs-bolt-t-shirt']").click();
16+
17+
Assertions.assertEquals(
18+
"1",
19+
page.locator(".shopping_cart_badge").textContent(),
20+
"Item not correctly added to cart");
21+
}
22+
23+
@Test
24+
public void removeFromProductPage() {
25+
page.navigate("https://www.saucedemo.com/");
26+
page.locator("[data-test='username']").fill("standard_user");
27+
page.locator("[data-test='password']").fill("secret_sauce");
28+
page.locator("[data-test='login-button']").click();
29+
page.locator("[data-test='add-to-cart-sauce-labs-bolt-t-shirt']").click();
30+
31+
page.locator("[data-test='remove-sauce-labs-bolt-t-shirt']").click();
32+
33+
Assertions.assertEquals(
34+
0, page.locator(".shopping_cart_badge").count(), "Item not correctly removed from cart");
35+
}
36+
37+
@Test
38+
public void addFromInventoryPage() {
39+
page.navigate("https://www.saucedemo.com/");
40+
page.locator("[data-test='username']").fill("standard_user");
41+
page.locator("[data-test='password']").fill("secret_sauce");
42+
page.locator("[data-test='login-button']").click();
43+
44+
page.locator("[data-test='add-to-cart-sauce-labs-onesie']").click();
45+
46+
Assertions.assertEquals("1", page.locator(".shopping_cart_badge").textContent());
47+
}
48+
49+
@Test
50+
public void removeFromInventoryPage() {
51+
page.navigate("https://www.saucedemo.com/");
52+
page.locator("[data-test='username']").fill("standard_user");
53+
page.locator("[data-test='password']").fill("secret_sauce");
54+
page.locator("[data-test='login-button']").click();
55+
page.locator("[data-test='add-to-cart-sauce-labs-bike-light']").click();
56+
57+
page.locator("[data-test='remove-sauce-labs-bike-light']").click();
58+
59+
Assertions.assertEquals(
60+
0, page.locator(".shopping_cart_badge").count(), "Shopping Cart is not empty");
61+
}
62+
63+
@Test
64+
public void removeFromCartPage() {
65+
page.navigate("https://www.saucedemo.com/");
66+
page.locator("[data-test='username']").fill("standard_user");
67+
page.locator("[data-test='password']").fill("secret_sauce");
68+
page.locator("[data-test='login-button']").click();
69+
page.locator("[data-test='add-to-cart-sauce-labs-backpack']").click();
70+
page.locator(".shopping_cart_link").click();
71+
72+
page.locator("[data-test='remove-sauce-labs-backpack']").click();
73+
74+
Assertions.assertEquals(
75+
0, page.locator(".shopping_cart_badge").count(), "Shopping Cart is not empty");
76+
}
77+
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
package com.saucedemo.playwright;
2+
3+
import org.junit.jupiter.api.Assertions;
4+
import org.junit.jupiter.api.Test;
5+
6+
public class CheckoutTest extends TestBase {
7+
8+
@Test
9+
public void badInfo() {
10+
page.navigate("https://www.saucedemo.com/");
11+
page.locator("[data-test='username']").fill("standard_user");
12+
page.locator("[data-test='password']").fill("secret_sauce");
13+
page.locator("[data-test='login-button']").click();
14+
page.locator("[data-test='add-to-cart-sauce-labs-onesie']").click();
15+
page.locator(".shopping_cart_link").click();
16+
page.locator("[data-test='checkout']").click();
17+
18+
page.locator("[data-test='continue']").click();
19+
20+
Assertions.assertTrue(
21+
page.locator("[data-test='firstName']").getAttribute("class").contains("error"),
22+
"Expected error not found on page");
23+
}
24+
25+
@Test
26+
public void goodInfo() {
27+
page.navigate("https://www.saucedemo.com/");
28+
page.locator("[data-test='username']").fill("standard_user");
29+
page.locator("[data-test='password']").fill("secret_sauce");
30+
page.locator("[data-test='login-button']").click();
31+
page.locator("[data-test='add-to-cart-sauce-labs-onesie']").click();
32+
page.locator(".shopping_cart_link").click();
33+
page.locator("[data-test='checkout']").click();
34+
35+
page.locator("[data-test='firstName']").fill("Luke");
36+
page.locator("[data-test='lastName']").fill("Perry");
37+
page.locator("[data-test='postalCode']").fill("90210");
38+
39+
page.locator("[data-test='continue']").click();
40+
41+
Assertions.assertEquals(
42+
"https://www.saucedemo.com/checkout-step-two.html",
43+
page.url(),
44+
"Information Submission Unsuccessful");
45+
}
46+
47+
@Test
48+
public void completeCheckout() {
49+
page.navigate("https://www.saucedemo.com/");
50+
page.locator("[data-test='username']").fill("standard_user");
51+
page.locator("[data-test='password']").fill("secret_sauce");
52+
page.locator("[data-test='login-button']").click();
53+
page.locator("[data-test='add-to-cart-sauce-labs-onesie']").click();
54+
page.locator(".shopping_cart_link").click();
55+
page.locator("[data-test='checkout']").click();
56+
page.locator("[data-test='firstName']").fill("Luke");
57+
page.locator("[data-test='lastName']").fill("Perry");
58+
page.locator("[data-test='postalCode']").fill("90210");
59+
page.locator("[data-test='continue']").click();
60+
61+
page.locator("[data-test='finish']").click();
62+
63+
Assertions.assertEquals("https://www.saucedemo.com/checkout-complete.html", page.url());
64+
65+
Assertions.assertTrue(page.locator(".complete-text").isVisible());
66+
}
67+
}

0 commit comments

Comments
 (0)