|
| 1 | +using Microsoft.Playwright; |
| 2 | +using Newtonsoft.Json; |
| 3 | +using NUnit.Framework; |
| 4 | +using BrowserStack; |
| 5 | +using Newtonsoft.Json.Linq; |
| 6 | + |
| 7 | +namespace CSharpPlaywrightBrowserStack |
| 8 | +{ |
| 9 | + [TestFixture] |
| 10 | + public class BrowserStackNUnitTest |
| 11 | + { |
| 12 | + protected IBrowser browser; |
| 13 | + protected IPage page; |
| 14 | + protected string profile; |
| 15 | + protected string environment; |
| 16 | + protected string configFile; |
| 17 | + |
| 18 | + private Local browserStackLocal; |
| 19 | + |
| 20 | + public BrowserStackNUnitTest(string profile, string environment, string configFile) |
| 21 | + { |
| 22 | + this.profile = profile; |
| 23 | + this.environment = environment; |
| 24 | + this.configFile = configFile; |
| 25 | + } |
| 26 | + |
| 27 | + [SetUp] |
| 28 | + public async Task Init() |
| 29 | + { |
| 30 | + // Get Configuration for correct profile |
| 31 | + string currentDirectory = Directory.GetCurrentDirectory(); |
| 32 | + string path = Path.Combine(currentDirectory, configFile); |
| 33 | + JObject config = JObject.Parse(File.ReadAllText(path)); |
| 34 | + if (config is null) |
| 35 | + throw new Exception("Configuration not found!"); |
| 36 | + |
| 37 | + // Get Environment specific capabilities |
| 38 | + JObject capabilitiesJsonArr = config.GetValue("environments") as JObject; |
| 39 | + JObject capabilities = capabilitiesJsonArr.GetValue(environment) as JObject; |
| 40 | + |
| 41 | + // Get Common Capabilities |
| 42 | + JObject commonCapabilities = config.GetValue("capabilities") as JObject; |
| 43 | + |
| 44 | + // Merge Capabilities |
| 45 | + capabilities.Merge(commonCapabilities); |
| 46 | + |
| 47 | + // Get username and accesskey |
| 48 | + string? username = Environment.GetEnvironmentVariable("BROWSERSTACK_USERNAME"); |
| 49 | + if (username is null) |
| 50 | + username = config.GetValue("user").ToString(); |
| 51 | + |
| 52 | + string? accessKey = Environment.GetEnvironmentVariable("BROWSERSTACK_ACCESS_KEY"); |
| 53 | + if (accessKey is null) |
| 54 | + accessKey = config.GetValue("key").ToString(); |
| 55 | + |
| 56 | + capabilities["browserstack.user"] = username; |
| 57 | + capabilities["browserstack.key"] = accessKey; |
| 58 | + |
| 59 | + // Start Local if browserstack.local is set to true |
| 60 | + if (profile.Equals("local") && accessKey is not null) |
| 61 | + { |
| 62 | + capabilities["browserstack.local"] = true; |
| 63 | + browserStackLocal = new Local(); |
| 64 | + List<KeyValuePair<string, string>> bsLocalArgs = new List<KeyValuePair<string, string>>() { |
| 65 | + new KeyValuePair<string, string>("key", accessKey) |
| 66 | + }; |
| 67 | + foreach (var localOption in config.GetValue("localOptions") as JObject) |
| 68 | + { |
| 69 | + if (localOption.Value is not null) |
| 70 | + { |
| 71 | + bsLocalArgs.Add(new KeyValuePair<string, string>(localOption.Key, localOption.Value.ToString())); |
| 72 | + } |
| 73 | + } |
| 74 | + browserStackLocal.start(bsLocalArgs); |
| 75 | + } |
| 76 | + |
| 77 | + string capsJson = JsonConvert.SerializeObject(capabilities); |
| 78 | + string cdpUrl = "wss://cdp.browserstack.com/playwright?caps=" + Uri.EscapeDataString(capsJson); |
| 79 | + |
| 80 | + var playwright = await Playwright.CreateAsync(); |
| 81 | + browser = await playwright.Chromium.ConnectAsync(cdpUrl); |
| 82 | + page = await browser.NewPageAsync(); |
| 83 | + } |
| 84 | + |
| 85 | + [TearDown] |
| 86 | + public async Task Cleanup() |
| 87 | + { |
| 88 | + if (browser != null) |
| 89 | + { |
| 90 | + browser.CloseAsync(); |
| 91 | + } |
| 92 | + if (browserStackLocal != null) |
| 93 | + { |
| 94 | + browserStackLocal.stop(); |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + public static async Task SetStatus(IPage browserPage, bool passed) |
| 99 | + { |
| 100 | + if (browserPage is not null) |
| 101 | + { |
| 102 | + if (passed) |
| 103 | + await browserPage.EvaluateAsync("_ => {}", "browserstack_executor: {\"action\": \"setSessionStatus\", \"arguments\": {\"status\":\"passed\", \"reason\": \"Test Passed!\"}}"); |
| 104 | + else |
| 105 | + await browserPage.EvaluateAsync("_ => {}", "browserstack_executor: {\"action\": \"setSessionStatus\", \"arguments\": {\"status\":\"failed\", \"reason\": \"Test Failed!\"}}"); |
| 106 | + } |
| 107 | + } |
| 108 | + } |
| 109 | +} |
0 commit comments