-
Notifications
You must be signed in to change notification settings - Fork 6
/
PlaywrightTestonIPad.cs
72 lines (59 loc) · 3.07 KB
/
PlaywrightTestonIPad.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
using Microsoft.Playwright;
using System.Threading.Tasks;
using System;
using System.Collections.Generic;
using Newtonsoft.Json;
using System.Threading;
class PlaywrightTestonIPad
{
public static async Task main(string[] args)
{
using var playwright = await Playwright.CreateAsync();
string user, accessKey;
user = Environment.GetEnvironmentVariable("LT_USERNAME");
accessKey = Environment.GetEnvironmentVariable("LT_ACCESS_KEY");
Dictionary<string, object> capabilities = new Dictionary<string, object>();
Dictionary<string, string> ltOptions = new Dictionary<string, string>();
ltOptions.Add("name", "Playwright Test");
ltOptions.Add("build", "Playwright C-Sharp tests on Hyperexecute");
ltOptions.Add("platform", Environment.GetEnvironmentVariable("HYPEREXECUTE_PLATFORM"));
ltOptions.Add("user", user);
ltOptions.Add("accessKey", accessKey);
capabilities.Add("browserName", "Chrome");
capabilities.Add("browserVersion", "latest");
capabilities.Add("LT:Options", ltOptions);
string capabilitiesJson = JsonConvert.SerializeObject(capabilities);
string cdpUrl = "wss://cdp.lambdatest.com/playwright?capabilities=" + Uri.EscapeDataString(capabilitiesJson);
await using var browser = await playwright.Chromium.ConnectAsync(cdpUrl);
// Documentation: https://playwright.dev/docs/emulation#devices
// Supported devices: https://github.com/microsoft/playwright/blob/main/packages/playwright-core/src/server/deviceDescriptorsSource.json
var context = await browser.NewContextAsync(playwright.Devices["iPad Pro 11 landscape"]);
var page = await context.NewPageAsync();
try {
await page.GotoAsync("https://www.google.com/");
await page.FillAsync("[aria - label =\"Search\"]", "Playwright");
await page.Keyboard.PressAsync("Enter");
await page.ClickAsync("xpath =//h3[contains(text(),’Playwright: Fast and reliable end-to-end testing’)]");
await page.GotoAsync("https://www.bing.com");
await page.Locator("[aria-label='Enter your search term']").ClickAsync();
await page.FillAsync("[aria-label='Enter your search term']", "LambdaTest");
await page.Keyboard.PressAsync("Enter");
var title = await page.TitleAsync();
if (title.Contains("LambdaTest"))
{
// Use the following code to mark the test status.
await SetTestStatus("passed", "Title matched", page);
}
else {
await SetTestStatus("failed", "Title not matched", page);
}
}
catch (Exception err) {
await SetTestStatus("failed", err.Message, page);
}
await browser.CloseAsync();
}
public static async Task SetTestStatus(string status, string remark, IPage page) {
await page.EvaluateAsync("_ => {}", "lambdatest_action: {\"action\": \"setTestStatus\", \"arguments\": {\"status\":\"" + status + "\", \"remark\": \"" + remark + "\"}}");
}
}