|
| 1 | +using Microsoft.Playwright; |
| 2 | +using System; |
| 3 | +using System.Threading; |
| 4 | +using System.Threading.Tasks; |
| 5 | +using System.Collections.Generic; |
| 6 | +using Newtonsoft.Json; |
| 7 | + |
| 8 | +class PlaywrightParallelTest |
| 9 | +{ |
| 10 | + public static async Task main(string[] args) |
| 11 | + { |
| 12 | + // The following capability variables contains the set of os/browser environments where you want to run your tests. You can choose to alter this list according to your needs. Read more on https://browserstack.com/docs/automate/playwright/browsers-and-os |
| 13 | + try { |
| 14 | + // allowed browsers are `chrome`, `edge`, `playwright-chromium`, `playwright-firefox` and `playwright-webkit` |
| 15 | + // 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"); |
| 21 | + |
| 22 | + //Executing the methods |
| 23 | + await Task.WhenAll(device1, device2, device3, device4, device5); |
| 24 | + } catch (Exception e) { |
| 25 | + Console.WriteLine(e); |
| 26 | + } |
| 27 | + } |
| 28 | + static async Task SampleTestCase(String browser_name, String browser_version, String os, String os_version, String test_name, String build_name) |
| 29 | + { |
| 30 | + Dictionary<string, string> browserstackOptions = new Dictionary<string, string>(); |
| 31 | + string capsJson; |
| 32 | + |
| 33 | + 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 | + } |
| 90 | + } catch (Exception e) { |
| 91 | + Console.WriteLine(e); |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + //Executetestwithcaps function takes capabilities from 'SampleTestCase' function and executes the test |
| 96 | + public static async Task Executetestwithcaps(string capabilities) |
| 97 | + { |
| 98 | + using var playwright = await Playwright.CreateAsync(); |
| 99 | + string cdpUrl = "wss://cdp.browserstack.com/playwright?caps=" + Uri.EscapeDataString(capabilities); |
| 100 | + |
| 101 | + await using var browser = await playwright.Chromium.ConnectAsync(cdpUrl); |
| 102 | + var page = await browser.NewPageAsync(); |
| 103 | + try { |
| 104 | + await page.GotoAsync("https://www.google.co.in/"); |
| 105 | + await page.Locator("[aria-label='Search']").ClickAsync(); |
| 106 | + await page.FillAsync("[aria-label='Search']", "BrowserStack"); |
| 107 | + await page.Locator("[aria-label='Google Search'] >> nth=0").ClickAsync(); |
| 108 | + var title = await page.TitleAsync(); |
| 109 | + |
| 110 | + if (title == "BrowserStack - Google Search") |
| 111 | + { |
| 112 | + // 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\"}}"); |
| 114 | + } |
| 115 | + } |
| 116 | + catch (Exception e) { |
| 117 | + Console.WriteLine(e); |
| 118 | + await page.EvaluateAsync("_ => {}", "browserstack_executor: {\"action\": \"setSessionStatus\", \"arguments\": {\"status\":\"failed\", \"reason\": \" Title did not match\"}}"); |
| 119 | + } |
| 120 | + await browser.CloseAsync(); |
| 121 | + } |
| 122 | +} |
0 commit comments