Skip to content

Commit b384058

Browse files
kamal-kaur04francisf
authored andcommitted
added markTestStatus and other script changes
1 parent 3a82f38 commit b384058

File tree

9 files changed

+201
-176
lines changed

9 files changed

+201
-176
lines changed

playwright-dotnet/PlaywrightParallelTest.cs

Lines changed: 27 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,14 @@ public static async Task main(string[] args)
1313
try {
1414
// allowed browsers are `chrome`, `edge`, `playwright-chromium`, `playwright-firefox` and `playwright-webkit`
1515
// browser_version 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.
16-
var device1 = SampleTestCase("playwright-chromium", "latest", "osx", "Catalina", "macOS Catalina - Chrome latest", "Parallel-build-2");
17-
var device2 = SampleTestCase("chrome", "latest", "osx", "catalina", "Branded Google Chrome on Catalina", "Parallel-build-2");
18-
var device3 = SampleTestCase("edge", "latest", "osx", "catalina", "Branded Microsoft Edge on Catalina", "Parallel-build-2");
19-
var device4 = SampleTestCase("playwright-firefox", "latest", "osx", "catalina", "Playwright firefox on Catalina", "Parallel-build-2");
20-
var device5 = SampleTestCase("playwright-webkit", "latest", "osx", "catalina", "Playwright webkit on Catalina", "Parallel-build-2");
16+
var combination1 = SampleTestCase("playwright-chromium", "latest", "osx", "Catalina", "macOS Catalina - Chrome latest", "Parallel-build-2");
17+
var combination2 = SampleTestCase("chrome", "latest", "osx", "catalina", "Branded Google Chrome on Catalina", "Parallel-build-2");
18+
var combination3 = SampleTestCase("edge", "latest", "osx", "catalina", "Branded Microsoft Edge on Catalina", "Parallel-build-2");
19+
var combination4 = SampleTestCase("playwright-firefox", "latest", "osx", "catalina", "Playwright firefox on Catalina", "Parallel-build-2");
20+
var combination5 = SampleTestCase("playwright-webkit", "latest", "osx", "catalina", "Playwright webkit on Catalina", "Parallel-build-2");
2121

2222
//Executing the methods
23-
await Task.WhenAll(device1, device2, device3, device4, device5);
23+
await Task.WhenAll(combination1, combination2, combination3, combination4, combination5);
2424
} catch (Exception e) {
2525
Console.WriteLine(e);
2626
}
@@ -31,62 +31,17 @@ static async Task SampleTestCase(String browser_name, String browser_version, St
3131
string capsJson;
3232

3333
try {
34-
35-
switch (browser_name)
36-
{
37-
case "playwright-chromium":
38-
browserstackOptions.Add("build", build_name);
39-
browserstackOptions.Add("name", test_name);
40-
browserstackOptions.Add("os", os);
41-
browserstackOptions.Add("os_version", os_version);
42-
browserstackOptions.Add("browser", browser_name);
43-
browserstackOptions.Add("browser_version", browser_version);
44-
browserstackOptions.Add("browserstack.username", "BROWSERSTACK_USERNAME");
45-
browserstackOptions.Add("browserstack.accessKey", "BROWSERSTACK_ACCESS_KEY");
46-
capsJson = JsonConvert.SerializeObject(browserstackOptions);
47-
var task = Executetestwithcaps(capsJson);
48-
await task;
49-
break;
50-
case "chrome":
51-
browserstackOptions.Add("build", build_name);
52-
browserstackOptions.Add("name", test_name);
53-
browserstackOptions.Add("os", os);
54-
browserstackOptions.Add("os_version", os_version);
55-
browserstackOptions.Add("browser", browser_name);
56-
browserstackOptions.Add("browser_version", browser_version);
57-
browserstackOptions.Add("browserstack.username", "BROWSERSTACK_USERNAME");
58-
browserstackOptions.Add("browserstack.accessKey", "BROWSERSTACK_ACCESS_KEY");
59-
capsJson = JsonConvert.SerializeObject(browserstackOptions);
60-
task = Executetestwithcaps(capsJson);
61-
await task;
62-
break;
63-
case "playwright-firefox":
64-
browserstackOptions.Add("build", build_name);
65-
browserstackOptions.Add("name", test_name);
66-
browserstackOptions.Add("os", os);
67-
browserstackOptions.Add("os_version", os_version);
68-
browserstackOptions.Add("browser", browser_name);
69-
browserstackOptions.Add("browser_version", browser_version);
70-
browserstackOptions.Add("browserstack.username", "BROWSERSTACK_USERNAME");
71-
browserstackOptions.Add("browserstack.accessKey", "BROWSERSTACK_ACCESS_KEY");
72-
capsJson = JsonConvert.SerializeObject(browserstackOptions);
73-
task = Executetestwithcaps(capsJson);
74-
await task;
75-
break;
76-
default:
77-
browserstackOptions.Add("build", build_name);
78-
browserstackOptions.Add("name", test_name);
79-
browserstackOptions.Add("os", os);
80-
browserstackOptions.Add("os_version", os_version);
81-
browserstackOptions.Add("browser", browser_name);
82-
browserstackOptions.Add("browser_version", browser_version);
83-
browserstackOptions.Add("browserstack.username", "BROWSERSTACK_USERNAME");
84-
browserstackOptions.Add("browserstack.accessKey", "BROWSERSTACK_ACCESS_KEY");
85-
capsJson = JsonConvert.SerializeObject(browserstackOptions);
86-
task = Executetestwithcaps(capsJson);
87-
await task;
88-
break;
89-
}
34+
browserstackOptions.Add("build", build_name);
35+
browserstackOptions.Add("name", test_name);
36+
browserstackOptions.Add("os", os);
37+
browserstackOptions.Add("os_version", os_version);
38+
browserstackOptions.Add("browser", browser_name);
39+
browserstackOptions.Add("browser_version", browser_version);
40+
browserstackOptions.Add("browserstack.username", "BROWSERSTACK_USERNAME");
41+
browserstackOptions.Add("browserstack.accessKey", "BROWSERSTACK_ACCESS_KEY");
42+
capsJson = JsonConvert.SerializeObject(browserstackOptions);
43+
var task = Executetestwithcaps(capsJson);
44+
await task;
9045
} catch (Exception e) {
9146
Console.WriteLine(e);
9247
}
@@ -110,13 +65,19 @@ public static async Task Executetestwithcaps(string capabilities)
11065
if (title == "BrowserStack - Google Search")
11166
{
11267
// 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
113-
await page.EvaluateAsync("_ => {}", "browserstack_executor: {\"action\":\"setSessionStatus\",\"arguments\":{\"status\":\"passed\",\"reason\":\"Title matched\"}}");
68+
await MarkTestStatus("passed", "Title matched", page);
69+
} else
70+
{
71+
await MarkTestStatus("failed", "Title did not match", page);
11472
}
11573
}
116-
catch (Exception e) {
117-
Console.WriteLine(e);
118-
await page.EvaluateAsync("_ => {}", "browserstack_executor: {\"action\": \"setSessionStatus\", \"arguments\": {\"status\":\"failed\", \"reason\": \" Title did not match\"}}");
74+
catch (Exception err) {
75+
Console.WriteLine(err);
76+
await MarkTestStatus("failed", err.Message, page);
11977
}
12078
await browser.CloseAsync();
12179
}
80+
public static async Task MarkTestStatus(string status, string reason, IPage page) {
81+
await page.EvaluateAsync("_ => {}", "browserstack_executor: {\"action\": \"setSessionStatus\", \"arguments\": {\"status\":\"" + status + "\", \"reason\": \"" + reason + "\"}}");
82+
}
12283
}

playwright-dotnet/PlaywrightSingleTest.cs

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,19 @@ public static async Task main(string[] args)
3333
if (title == "BrowserStack - Google Search")
3434
{
3535
// 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
36-
await page.EvaluateAsync("_ => {}", "browserstack_executor: {\"action\":\"setSessionStatus\",\"arguments\":{\"status\":\"passed\",\"reason\":\"Title matched\"}}");
36+
await MarkTestStatus("passed", "Title matched", page);
37+
}
38+
else {
39+
await MarkTestStatus("failed", "Title did not match", page);
3740
}
3841
}
39-
catch {
40-
await page.EvaluateAsync("_ => {}", "browserstack_executor: {\"action\": \"setSessionStatus\", \"arguments\": {\"status\":\"failed\", \"reason\": \" Title did not match\"}}");
42+
catch (Exception err) {
43+
await MarkTestStatus("failed", err.Message, page);
4144
}
4245
await browser.CloseAsync();
4346
}
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+
}
4451
}

playwright-dotnet/README.md

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@
88

99
* Clone the repo and run `cd Playwright-dotnet`
1010
* Run `dotnet build`
11-
* Install required browsers - replace netX with actual output folder name, f.ex. net6.0.
12-
`pwsh bin/Debug/netX/playwright.ps1 install`
1311

1412
## Running your tests
1513

@@ -18,14 +16,15 @@
1816

1917
### Run sample test on privately hosted websites
2018

21-
1. You have to download the BrowserStack Local binary from the links below (depending on your environment):
22-
* [OS X (10.7 and above)](https://www.browserstack.com/browserstack-local/BrowserStackLocal-darwin-x64.zip)
23-
* [Linux 32-bit](https://www.browserstack.com/browserstack-local/BrowserStackLocal-linux-ia32.zip)
24-
* [Linux 64-bit](https://www.browserstack.com/browserstack-local/BrowserStackLocal-linux-x64.zip)
25-
* [Windows (XP and above)](https://www.browserstack.com/browserstack-local/BrowserStackLocal-win32.zip)
26-
2. Once you have downloaded and unzipped the file, you can initiate the binary by running the command: `./BrowserStackLocal --key YOUR_ACCESS_KEY`
27-
3. Once you see the terminal say "[SUCCESS]" You can now access your local server(s) in our remote browser”, your local testing connection is considered established.
28-
4. You can then run the sample Local test using `dotnet run local`
19+
**Using Command-line Interface**
20+
1. You have to download the BrowserStack Local binary from the links below (depending on your environment):
21+
* [OS X (10.7 and above)](https://www.browserstack.com/browserstack-local/BrowserStackLocal-darwin-x64.zip)
22+
* [Linux 32-bit](https://www.browserstack.com/browserstack-local/BrowserStackLocal-linux-ia32.zip)
23+
* [Linux 64-bit](https://www.browserstack.com/browserstack-local/BrowserStackLocal-linux-x64.zip)
24+
* [Windows (XP and above)](https://www.browserstack.com/browserstack-local/BrowserStackLocal-win32.zip)
25+
2. Once you have downloaded and unzipped the file, you can initiate the binary by running the command: `./BrowserStackLocal --key YOUR_ACCESS_KEY`
26+
3. Once you see the terminal say "[SUCCESS]" You can now access your local server(s) in our remote browser”, your local testing connection is considered established.
27+
4. You can then run the sample Local test using `dotnet run local`
2928

3029
Understand how many parallel sessions you need by using our [Parallel Test Calculator](https://www.browserstack.com/automate/parallel-calculator?ref=github)
3130

playwright-java/src/test/java/com/browserstack/PlaywrightLocalTest.java

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22

33
import com.google.gson.JsonObject;
44
import com.microsoft.playwright.*;
5-
import java.io.UnsupportedEncodingException;
65
import java.net.URLEncoder;
76

87
public class PlaywrightLocalTest {
98
public static void main(String[] args) {
9+
Page page = null;
1010
try (Playwright playwright = Playwright.create()) {
1111
JsonObject jsonObject = new JsonObject();
1212
jsonObject.addProperty("browser", "chrome"); // allowed browsers are `chrome`, `edge`, `playwright-chromium`, `playwright-firefox` and `playwright-webkit`
@@ -18,12 +18,12 @@ public static void main(String[] args) {
1818
jsonObject.addProperty("browserstack.username", "BROWSERSTACK_USERNAME");
1919
jsonObject.addProperty("browserstack.accessKey", "BROWSERSTACK_ACCESS_KEY");
2020
jsonObject.addProperty("browserstack.local", "true");
21-
21+
2222
BrowserType chromium = playwright.chromium();
2323
String caps = URLEncoder.encode(jsonObject.toString(), "utf-8");
2424
String ws_endpoint = "wss://cdp.browserstack.com/playwright?caps=" + caps;
2525
Browser browser = chromium.connect(ws_endpoint);
26-
Page page = browser.newPage();
26+
page = browser.newPage();
2727
page.navigate("https://www.google.co.in/");
2828
Locator locator = page.locator("[aria-label='Search']");
2929
locator.click();
@@ -33,15 +33,20 @@ public static void main(String[] args) {
3333

3434
if (title.equals("BrowserStack - Google Search")) {
3535
// 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
36-
Object result = page.evaluate("_ => {}", "browserstack_executor: { \"action\": \"setSessionStatus\", \"arguments\": { \"status\": \"passed\", \"reason\": \"Title matched\"}}");
37-
System.out.println(result);
36+
markTestStatus("passed", "Title matched", page);
3837
} else {
39-
Object result = page.evaluate("_ => {}", "browserstack_executor: { \"action\": \"setSessionStatus\", \"arguments\": { \"status\": \"failed\", \"reason\": \"Title did not matched\"}}");
40-
System.out.println(result);
38+
markTestStatus("failed", "Title did not match", page);
4139
}
4240
browser.close();
43-
} catch (UnsupportedEncodingException e) {
44-
System.out.println(e);
41+
} catch (Exception err) {
42+
assert page != null;
43+
markTestStatus("failed", err.getMessage(), page);
4544
}
4645
}
46+
47+
public static void markTestStatus(String status, String reason, Page page) {
48+
Object result;
49+
result = page.evaluate("_ => {}", "browserstack_executor: { \"action\": \"setSessionStatus\", \"arguments\": { \"status\": \"" + status + "\", \"reason\": \"" + reason + "\"}}");
50+
System.out.println(result);
51+
}
4752
}

0 commit comments

Comments
 (0)