Skip to content

Commit c110677

Browse files
kamal-kaur04francisf
authored andcommitted
added pixel and iphone tests for python and c#
1 parent a092893 commit c110677

File tree

9 files changed

+211
-8
lines changed

9 files changed

+211
-8
lines changed
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
using Microsoft.Playwright;
2+
using System.Threading.Tasks;
3+
using System;
4+
using System.Collections.Generic;
5+
using Newtonsoft.Json;
6+
7+
class PlaywrightIPhoneTest
8+
{
9+
public static async Task main(string[] args)
10+
{
11+
using var playwright = await Playwright.CreateAsync();
12+
13+
Dictionary<string, string> browserstackOptions = new Dictionary<string, string>();
14+
browserstackOptions.Add("name", "Test on Playwright emulated IPhone 11 Pro");
15+
browserstackOptions.Add("build", "playwright-dotnet-4");
16+
browserstackOptions.Add("browser", "playwright-webkit"); // allowed browsers are `chrome`, `edge`, `playwright-chromium`, `playwright-firefox` and `playwright-webkit`
17+
browserstackOptions.Add("browserstack.username", "BROWSERSTACK_USERNAME");
18+
browserstackOptions.Add("browserstack.accessKey", "BROWSERSTACK_ACCESS_KEY");
19+
string capsJson = JsonConvert.SerializeObject(browserstackOptions);
20+
string cdpUrl = "wss://cdp.browserstack.com/playwright?caps=" + Uri.EscapeDataString(capsJson);
21+
22+
await using var browser = await playwright.Chromium.ConnectAsync(cdpUrl);
23+
24+
var context = await browser.NewContextAsync(playwright.Devices["iPhone 11 Pro"]); // Complete list of devices - https://github.com/microsoft/playwright/blob/main/packages/playwright-core/src/server/deviceDescriptorsSource.json
25+
26+
var page = await context.NewPageAsync();
27+
try {
28+
await page.GotoAsync("https://www.google.co.in/");
29+
await page.Locator("[aria-label='Search']").ClickAsync();
30+
await page.FillAsync("[aria-label='Search']", "BrowserStack");
31+
await page.Keyboard.PressAsync("Enter");
32+
await page.WaitForTimeoutAsync(1000);
33+
var title = await page.TitleAsync();
34+
35+
if (title == "BrowserStack - Google Search")
36+
{
37+
// 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
38+
await MarkTestStatus("passed", "Title matched", page);
39+
} else {
40+
await MarkTestStatus("failed", "Title did not match", page);
41+
}
42+
}
43+
catch (Exception err) {
44+
await MarkTestStatus("failed", err.Message, page);
45+
}
46+
await browser.CloseAsync();
47+
}
48+
public static async Task MarkTestStatus(string status, string reason, IPage page) {
49+
await page.EvaluateAsync("_ => {}", "browserstack_executor: {\"action\": \"setSessionStatus\", \"arguments\": {\"status\":\"" + status + "\", \"reason\": \"" + reason + "\"}}");
50+
}
51+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
using Microsoft.Playwright;
2+
using System.Threading.Tasks;
3+
using System;
4+
using System.Collections.Generic;
5+
using Newtonsoft.Json;
6+
7+
class PlaywrightPixelTest
8+
{
9+
public static async Task main(string[] args)
10+
{
11+
using var playwright = await Playwright.CreateAsync();
12+
13+
Dictionary<string, string> browserstackOptions = new Dictionary<string, string>();
14+
browserstackOptions.Add("name", "Test on Playwright emulated Pixel 5");
15+
browserstackOptions.Add("build", "playwright-dotnet-4");
16+
browserstackOptions.Add("browser", "playwright-webkit"); // allowed browsers are `chrome`, `edge`, `playwright-chromium`, `playwright-firefox` and `playwright-webkit`
17+
browserstackOptions.Add("browserstack.username", "BROWSERSTACK_USERNAME");
18+
browserstackOptions.Add("browserstack.accessKey", "BROWSERSTACK_ACCESS_KEY");
19+
string capsJson = JsonConvert.SerializeObject(browserstackOptions);
20+
string cdpUrl = "wss://cdp.browserstack.com/playwright?caps=" + Uri.EscapeDataString(capsJson);
21+
22+
await using var browser = await playwright.Chromium.ConnectAsync(cdpUrl);
23+
24+
var context = await browser.NewContextAsync(playwright.Devices["Pixel 5"]); // Complete list of devices - https://github.com/microsoft/playwright/blob/main/packages/playwright-core/src/server/deviceDescriptorsSource.json
25+
26+
var page = await context.NewPageAsync();
27+
try {
28+
await page.GotoAsync("https://www.google.co.in/");
29+
await page.Locator("[aria-label='Search']").ClickAsync();
30+
await page.FillAsync("[aria-label='Search']", "BrowserStack");
31+
await page.Keyboard.PressAsync("Enter");
32+
await page.WaitForTimeoutAsync(1000);
33+
var title = await page.TitleAsync();
34+
35+
if (title == "BrowserStack - Google Search")
36+
{
37+
// 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
38+
await MarkTestStatus("passed", "Title matched", page);
39+
} else {
40+
await MarkTestStatus("failed", "Title did not match", page);
41+
}
42+
}
43+
catch (Exception err) {
44+
await MarkTestStatus("failed", err.Message, page);
45+
}
46+
await browser.CloseAsync();
47+
}
48+
49+
public static async Task MarkTestStatus(string status, string reason, IPage page) {
50+
await page.EvaluateAsync("_ => {}", "browserstack_executor: {\"action\": \"setSessionStatus\", \"arguments\": {\"status\":\"" + status + "\", \"reason\": \"" + reason + "\"}}");
51+
}
52+
}

playwright-dotnet/Program.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,14 @@ public static async Task Main(string[] args)
2121
Console.WriteLine("Running Local Test");
2222
await PlaywrightLocalTest.main(args);
2323
break;
24+
case "iphonetest":
25+
Console.WriteLine("Running iPhone Test");
26+
await PlaywrightIPhoneTest.main(args);
27+
break;
28+
case "pixeltest":
29+
Console.WriteLine("Running Pixel Test");
30+
await PlaywrightPixelTest.main(args);
31+
break;
2432
default:
2533
Console.WriteLine("Running Single Test by default");
2634
await PlaywrightTest.main(args);

playwright-python/local-playwright-test.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
'os': 'osx',
1010
'os_version': 'catalina',
1111
'name': 'Branded Google Chrome on Catalina',
12-
'build': 'playwright-build-3',
12+
'build': 'playwright-python-3',
1313
'browserstack.local': 'true',
1414
'browserstack.username': 'BROWSERSTACK_USERNAME',
1515
'browserstack.accessKey': 'BROWSERSTACK_ACCESS_KEY'

playwright-python/local-using-bindings-playwright-test.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
'os': 'osx',
1111
'os_version': 'catalina',
1212
'name': 'Branded Google Chrome on Catalina',
13-
'build': 'playwright-build-3',
13+
'build': 'playwright-python-3',
1414
'browserstack.local': 'true',
1515
'browserstack.username': 'BROWSERSTACK_USERNAME',
1616
'browserstack.accessKey': 'BROWSERSTACK_ACCESS_KEY'

playwright-python/parallel-playwright-test.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
'os': 'osx',
1212
'os_version': 'catalina',
1313
'name': 'Branded Google Chrome on Catalina',
14-
'build': 'playwright-build-2',
14+
'build': 'playwright-python-2',
1515
'browserstack.username': 'BROWSERSTACK_USERNAME',
1616
'browserstack.accessKey': 'BROWSERSTACK_ACCESS_KEY'
1717
},
@@ -21,7 +21,7 @@
2121
'os': 'osx',
2222
'os_version': 'catalina',
2323
'name': 'Branded Microsoft Edge on Catalina',
24-
'build': 'playwright-build-2',
24+
'build': 'playwright-python-2',
2525
'browserstack.username': 'BROWSERSTACK_USERNAME',
2626
'browserstack.accessKey': 'BROWSERSTACK_ACCESS_KEY'
2727
},
@@ -30,7 +30,7 @@
3030
'os': 'osx',
3131
'os_version': 'catalina',
3232
'name': 'Playwright firefox on Catalina',
33-
'build': 'playwright-build-2',
33+
'build': 'playwright-python-2',
3434
'browserstack.username': 'BROWSERSTACK_USERNAME',
3535
'browserstack.accessKey': 'BROWSERSTACK_ACCESS_KEY'
3636
},
@@ -39,7 +39,7 @@
3939
'os': 'osx',
4040
'os_version': 'catalina',
4141
'name': 'Playwright webkit on Catalina',
42-
'build': 'playwright-build-2',
42+
'build': 'playwright-python-2',
4343
'browserstack.username': 'BROWSERSTACK_USERNAME',
4444
'browserstack.accessKey': 'BROWSERSTACK_ACCESS_KEY'
4545
},
@@ -48,7 +48,7 @@
4848
'os': 'osx',
4949
'os_version': 'Catalina',
5050
'name': 'Chrome on Win10',
51-
'build': 'playwright-build-2',
51+
'build': 'playwright-python-2',
5252
'browserstack.username': 'BROWSERSTACK_USERNAME',
5353
'browserstack.accessKey': 'BROWSERSTACK_ACCESS_KEY'
5454
}]
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import json
2+
import urllib
3+
import subprocess
4+
from playwright.sync_api import sync_playwright
5+
6+
desired_cap = {
7+
'browser': 'playwright-chromium', # allowed browsers are `chrome`, `edge`, `playwright-chromium`, `playwright-firefox` and `playwright-webkit`
8+
'browser_version': 'latest', # this capability is valid only for branded `chrome` and `edge` browsers and you can specify any browser version like `latest`, `latest-beta`, `latest-1` and so on.
9+
'name': 'Test on Playwright emulated iPhone 11 Pro',
10+
'build': 'playwright-python-4',
11+
'browserstack.username': 'BROWSERSTACK_USERNAME',
12+
'browserstack.accessKey': 'BROWSERSTACK_ACCESS_KEY'
13+
}
14+
15+
def run_session(playwright):
16+
clientPlaywrightVersion = str(subprocess.getoutput('playwright --version')).strip().split(" ")[1]
17+
desired_cap['client.playwrightVersion'] = clientPlaywrightVersion
18+
19+
cdpUrl = 'wss://cdp.browserstack.com/playwright?caps=' + urllib.parse.quote(json.dumps(desired_cap))
20+
iphone = playwright.devices["iPhone 11 Pro"] # Complete list of devices - https://github.com/microsoft/playwright/blob/main/packages/playwright-core/src/server/deviceDescriptorsSource.json
21+
browser = playwright.chromium.connect(cdpUrl)
22+
context = browser.new_context(**iphone)
23+
page = context.new_page()
24+
try:
25+
page.goto("https://www.google.co.in/")
26+
page.fill("[aria-label='Search']", 'Browserstack')
27+
page.keyboard.press('Enter')
28+
page.wait_for_timeout(1000)
29+
title = page.title()
30+
31+
if title == "Browserstack - Google Search":
32+
# 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
33+
mark_test_status("passed", "Title matched", page)
34+
else:
35+
mark_test_status("failed", "Title did not match", page)
36+
except Exception as err:
37+
mark_test_status("failed", str(err), page)
38+
39+
browser.close()
40+
41+
def mark_test_status(status, reason, page):
42+
page.evaluate("_ => {}", "browserstack_executor: {\"action\": \"setSessionStatus\", \"arguments\": {\"status\":\""+ status + "\", \"reason\": \"" + reason + "\"}}");
43+
44+
with sync_playwright() as playwright:
45+
run_session(playwright)
46+
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import json
2+
import urllib
3+
import subprocess
4+
from playwright.sync_api import sync_playwright
5+
6+
desired_cap = {
7+
'browser': 'playwright-chromium', # allowed browsers are `chrome`, `edge`, `playwright-chromium`, `playwright-firefox` and `playwright-webkit`
8+
'browser_version': 'latest', # this capability is valid only for branded `chrome` and `edge` browsers and you can specify any browser version like `latest`, `latest-beta`, `latest-1` and so on.
9+
'name': 'Test on Playwright emulated Pixel 5',
10+
'build': 'playwright-python-4',
11+
'browserstack.username': 'BROWSERSTACK_USERNAME',
12+
'browserstack.accessKey': 'BROWSERSTACK_ACCESS_KEY'
13+
}
14+
15+
def run_session(playwright):
16+
clientPlaywrightVersion = str(subprocess.getoutput('playwright --version')).strip().split(" ")[1]
17+
desired_cap['client.playwrightVersion'] = clientPlaywrightVersion
18+
19+
cdpUrl = 'wss://cdp.browserstack.com/playwright?caps=' + urllib.parse.quote(json.dumps(desired_cap))
20+
pixel = playwright.devices["Pixel 5"] # // Complete list of devices - https://github.com/microsoft/playwright/blob/main/packages/playwright-core/src/server/deviceDescriptorsSource.json
21+
browser = playwright.chromium.connect(cdpUrl)
22+
context = browser.new_context(**pixel)
23+
page = context.new_page()
24+
try:
25+
page.goto("https://www.google.co.in/")
26+
page.fill("[aria-label='Search']", 'Browserstack')
27+
page.keyboard.press('Enter')
28+
page.wait_for_timeout(1000)
29+
title = page.title()
30+
31+
if title == "Browserstack - Google Search":
32+
# 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
33+
mark_test_status("passed", "Title matched", page)
34+
else:
35+
mark_test_status("failed", "Title did not match", page)
36+
except Exception as err:
37+
mark_test_status("failed", str(err), page)
38+
39+
browser.close()
40+
41+
def mark_test_status(status, reason, page):
42+
page.evaluate("_ => {}", "browserstack_executor: {\"action\": \"setSessionStatus\", \"arguments\": {\"status\":\""+ status + "\", \"reason\": \"" + reason + "\"}}");
43+
44+
with sync_playwright() as playwright:
45+
run_session(playwright)
46+

playwright-python/playwright-test.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
'os': 'osx',
1010
'os_version': 'catalina',
1111
'name': 'Branded Google Chrome on Catalina',
12-
'build': 'playwright-build-1',
12+
'build': 'playwright-python-1',
1313
'browserstack.username': 'BROWSERSTACK_USERNAME',
1414
'browserstack.accessKey': 'BROWSERSTACK_ACCESS_KEY'
1515
}

0 commit comments

Comments
 (0)