|
| 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 | +} |
0 commit comments