Skip to content

Commit 991e5ce

Browse files
kamal-kaur04francisf
authored andcommitted
improved wait strategy post pressing ENTER key
1 parent f57156a commit 991e5ce

File tree

7 files changed

+121
-4
lines changed

7 files changed

+121
-4
lines changed

playwright-dotnet/PlaywrightIPhoneTest.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public static async Task main(string[] args)
2929
await page.Locator("[aria-label='Search']").ClickAsync();
3030
await page.FillAsync("[aria-label='Search']", "BrowserStack");
3131
await page.Keyboard.PressAsync("Enter");
32-
await page.WaitForTimeoutAsync(1000);
32+
await page.Locator("[aria-current='page']").WaitForAsync();
3333
var title = await page.TitleAsync();
3434

3535
if (title == "BrowserStack - Google Search")

playwright-dotnet/PlaywrightPixelTest.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public static async Task main(string[] args)
2929
await page.Locator("[aria-label='Search']").ClickAsync();
3030
await page.FillAsync("[aria-label='Search']", "BrowserStack");
3131
await page.Keyboard.PressAsync("Enter");
32-
await page.WaitForTimeoutAsync(1000);
32+
await page.Locator("[aria-current='page']").WaitForAsync();
3333
var title = await page.TitleAsync();
3434

3535
if (title == "BrowserStack - Google Search")

playwright-java/README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,16 @@
1616
- To run parallel tests, run
1717
`mvn -Dexec.mainClass="com.browserstack.PlaywrightParallelTest" -Dexec.classpathScope=test test-compile exec:java
1818
`
19+
- To run sessions on emulated devices,
20+
```
21+
mvn -Dexec.mainClass="com.browserstack.PlaywrightIPhoneTest" -Dexec.classpathScope=test test-compile exec:java
22+
```
23+
or
24+
```
25+
mvn -Dexec.mainClass="com.browserstack.PlaywrightPixelTest" -Dexec.classpathScope=test test-compile exec:java
26+
```
27+
You can specify contextOptions() from thr below list:
28+
https://github.com/microsoft/playwright/blob/main/packages/playwright-core/src/server/deviceDescriptorsSource.json
1929
- Run `mvn -Dexec.mainClass="com.browserstack.PlaywrightSessionDetailsTest" -Dexec.classpathScope=test test-compile exec:java` to check how to get session details.
2030

2131
### Run sample test on privately hosted websites
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package com.browserstack;
2+
3+
import com.google.gson.JsonObject;
4+
import com.microsoft.playwright.*;
5+
import java.io.UnsupportedEncodingException;
6+
import java.net.URLEncoder;
7+
8+
public class PlaywrightIPhoneTest {
9+
public static void main(String[] args) {
10+
try (Playwright playwright = Playwright.create()) {
11+
JsonObject jsonObject = new JsonObject();
12+
jsonObject.addProperty("browser", "playwright-webkit"); // allowed browsers are `chrome`, `edge`, `playwright-chromium`, `playwright-firefox` and `playwright-webkit`
13+
jsonObject.addProperty("browser_version", "latest");
14+
jsonObject.addProperty("name", "Test on Playwright emulated Devices");
15+
jsonObject.addProperty("build", "playwright-java-4");
16+
jsonObject.addProperty("browserstack.username", "BROWSERSTACK_USERNAME");
17+
jsonObject.addProperty("browserstack.accessKey", "BROWSERSTACK_ACCESS_KEY");
18+
19+
BrowserType chromium = playwright.chromium();
20+
String caps = URLEncoder.encode(jsonObject.toString(), "utf-8");
21+
String ws_endpoint = "wss://cdp.browserstack.com/playwright?caps=" + caps;
22+
23+
Browser browser = chromium.connect(ws_endpoint);
24+
BrowserContext context = browser.newContext(new Browser.NewContextOptions()
25+
.setUserAgent("Mozilla/5.0 (iPhone; CPU iPhone OS 12_2 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/13.0 Mobile/15E148 Safari/604.1")
26+
.setViewportSize(375, 812)
27+
.setDeviceScaleFactor(3)
28+
.setIsMobile(true)
29+
.setHasTouch(true));
30+
31+
Page page = context.newPage();
32+
page.navigate("https://www.google.co.in/");
33+
Locator locator = page.locator("[aria-label='Search']");
34+
locator.click();
35+
page.fill("[aria-label='Search']", "BrowserStack");
36+
page.keyboard().press("Enter");
37+
page.locator("[aria-current='page']").waitFor();
38+
String title = page.title();
39+
40+
Object result;
41+
if (title.equals("BrowserStack - Google Search")) {
42+
// following line of code is responsible for marking the status of the test on BrowserStack as 'passed'. You can use this code in your after hook after each test
43+
result = page.evaluate("_ => {}", "browserstack_executor: { \"action\": \"setSessionStatus\", \"arguments\": { \"status\": \"passed\", \"reason\": \"Title matched\"}}");
44+
} else {
45+
result = page.evaluate("_ => {}", "browserstack_executor: { \"action\": \"setSessionStatus\", \"arguments\": { \"status\": \"failed\", \"reason\": \"Title did not matched\"}}");
46+
}
47+
48+
browser.close();
49+
} catch (UnsupportedEncodingException e) {
50+
System.out.println(e);
51+
}
52+
}
53+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package com.browserstack;
2+
3+
import com.google.gson.JsonObject;
4+
import com.microsoft.playwright.*;
5+
import java.io.UnsupportedEncodingException;
6+
import java.net.URLEncoder;
7+
8+
public class PlaywrightPixelTest {
9+
public static void main(String[] args) {
10+
try (Playwright playwright = Playwright.create()) {
11+
JsonObject jsonObject = new JsonObject();
12+
jsonObject.addProperty("browser", "playwright-webkit"); // allowed browsers are `chrome`, `edge`, `playwright-chromium`, `playwright-firefox` and `playwright-webkit`
13+
jsonObject.addProperty("browser_version", "latest");
14+
jsonObject.addProperty("name", "Test on Playwright emulated Devices");
15+
jsonObject.addProperty("build", "playwright-java-4");
16+
jsonObject.addProperty("browserstack.username", "BROWSERSTACK_USERNAME");
17+
jsonObject.addProperty("browserstack.accessKey", "BROWSERSTACK_ACCESS_KEY");
18+
19+
BrowserType chromium = playwright.chromium();
20+
String caps = URLEncoder.encode(jsonObject.toString(), "utf-8");
21+
String ws_endpoint = "wss://cdp.browserstack.com/playwright?caps=" + caps;
22+
23+
Browser browser = chromium.connect(ws_endpoint);
24+
BrowserContext context = browser.newContext(new Browser.NewContextOptions()
25+
.setUserAgent("Mozilla/5.0 (Linux; Android 11; Pixel 5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/101.0.4943.0 Mobile Safari/537.36")
26+
.setViewportSize(393, 727)
27+
.setScreenSize(393, 851)
28+
.setDeviceScaleFactor(3)
29+
.setIsMobile(true)
30+
.setHasTouch(true));
31+
32+
Page page = context.newPage();
33+
page.navigate("https://www.google.co.in/");
34+
Locator locator = page.locator("[aria-label='Search']");
35+
locator.click();
36+
page.fill("[aria-label='Search']", "BrowserStack");
37+
page.keyboard().press("Enter");
38+
page.locator("[aria-current='page']").waitFor();
39+
String title = page.title();
40+
41+
Object result;
42+
if (title.equals("BrowserStack - Google Search")) {
43+
// following line of code is responsible for marking the status of the test on BrowserStack as 'passed'. You can use this code in your after hook after each test
44+
result = page.evaluate("_ => {}", "browserstack_executor: { \"action\": \"setSessionStatus\", \"arguments\": { \"status\": \"passed\", \"reason\": \"Title matched\"}}");
45+
} else {
46+
result = page.evaluate("_ => {}", "browserstack_executor: { \"action\": \"setSessionStatus\", \"arguments\": { \"status\": \"failed\", \"reason\": \"Title did not matched\"}}");
47+
}
48+
49+
browser.close();
50+
} catch (UnsupportedEncodingException e) {
51+
System.out.println(e);
52+
}
53+
}
54+
}

playwright-python/playwright-test-on-iphone.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ def run_session(playwright):
2525
page.goto("https://www.google.co.in/")
2626
page.fill("[aria-label='Search']", 'Browserstack')
2727
page.keyboard.press('Enter')
28-
page.wait_for_timeout(1000)
28+
page.locator("[aria-current='page']").wait_for();
2929
title = page.title()
3030

3131
if title == "Browserstack - Google Search":

playwright-python/playwright-test-on-pixel.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ def run_session(playwright):
2525
page.goto("https://www.google.co.in/")
2626
page.fill("[aria-label='Search']", 'Browserstack')
2727
page.keyboard.press('Enter')
28-
page.wait_for_timeout(1000)
28+
page.locator("[aria-current='page']").wait_for();
2929
title = page.title()
3030

3131
if title == "Browserstack - Google Search":

0 commit comments

Comments
 (0)