Skip to content

Commit 2d98acc

Browse files
add bidi test
1 parent c3e8f30 commit 2d98acc

File tree

2 files changed

+134
-2
lines changed

2 files changed

+134
-2
lines changed
Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
using System.Net.WebSockets;
2+
using System.Text.Json;
3+
using System.Text;
4+
using OpenQA.Selenium;
5+
using OpenQA.Selenium.BiDi;
6+
using OpenQA.Selenium.Remote;
7+
using OpenQA.Selenium.Chrome;
8+
using OpenQA.Selenium.Firefox;
9+
using OpenQA.Selenium.Safari;
10+
11+
namespace NUnit_TestingBot_Sample
12+
{
13+
[TestFixture("chrome", "latest", "Windows 10")]
14+
[Category("BiDi")]
15+
public class BidiTest
16+
{
17+
private IWebDriver _driver;
18+
private CancellationToken _token = CancellationToken.None;
19+
private readonly string _browser;
20+
private readonly string _version;
21+
private readonly string _os;
22+
23+
public BidiTest(string browser, string version, string os)
24+
{
25+
_browser = browser;
26+
_version = version;
27+
_os = os;
28+
}
29+
30+
[SetUp]
31+
public void SetUp()
32+
{
33+
var browserOptions = CreateBrowserOptions();
34+
var testingBotOptions = CreateTestingBotOptions();
35+
36+
browserOptions.AddAdditionalOption("tb:options", testingBotOptions);
37+
38+
_driver = new RemoteWebDriver(
39+
new Uri("https://hub.testingbot.com/wd/hub"),
40+
browserOptions.ToCapabilities(),
41+
TimeSpan.FromSeconds(60)
42+
);
43+
}
44+
45+
[Test]
46+
public async Task CaptureNetworkLog()
47+
{
48+
_driver.Navigate().GoToUrl("https://www.selenium.dev/selenium/web/bidi/logEntryAdded.html");
49+
var bidi = await _driver.AsBiDiAsync();
50+
51+
await bidi.Network.OnResponseStartedAsync(res =>
52+
{
53+
Console.WriteLine($"URL: {res.Request.Url}");
54+
Console.WriteLine($"Method: {res.Request.Method}");
55+
Console.WriteLine($"Status: {res.Response.Status}");
56+
Console.WriteLine($"Headers: {string.Join(", ", res.Response.Headers.Select(h => $"{h.Name}: {h.Value}"))}");
57+
});
58+
59+
_driver.FindElement(By.Id("jsException")).Click();
60+
System.Threading.Thread.Sleep(4000);
61+
}
62+
63+
[TearDown]
64+
public void TearDown()
65+
{
66+
if (_driver == null) return;
67+
68+
var testPassed = TestContext.CurrentContext.Result.Outcome.Status ==
69+
NUnit.Framework.Interfaces.TestStatus.Passed;
70+
71+
try
72+
{
73+
((IJavaScriptExecutor)_driver).ExecuteScript($"tb:test-result={testPassed.ToString().ToLower()}");
74+
}
75+
catch (Exception ex)
76+
{
77+
TestContext.Out.WriteLine($"Failed to report test result: {ex.Message}");
78+
}
79+
finally
80+
{
81+
_driver.Quit();
82+
_driver.Dispose();
83+
}
84+
}
85+
86+
private Dictionary<string, object> CreateTestingBotOptions()
87+
{
88+
var key = Environment.GetEnvironmentVariable("TESTINGBOT_KEY");
89+
var secret = Environment.GetEnvironmentVariable("TESTINGBOT_SECRET");
90+
91+
if (string.IsNullOrEmpty(key) || string.IsNullOrEmpty(secret))
92+
{
93+
throw new InvalidOperationException(
94+
"TESTINGBOT_KEY and TESTINGBOT_SECRET environment variables must be set");
95+
}
96+
97+
return new Dictionary<string, object>
98+
{
99+
["key"] = key,
100+
["secret"] = secret,
101+
["name"] = TestContext.CurrentContext.Test.Name,
102+
["selenium-version"] = "4.27.0"
103+
};
104+
}
105+
106+
private DriverOptions CreateBrowserOptions()
107+
{
108+
return _browser.ToLower() switch
109+
{
110+
"firefox" => new FirefoxOptions
111+
{
112+
BrowserVersion = _version,
113+
PlatformName = _os,
114+
UseWebSocketUrl = true
115+
},
116+
"safari" => new SafariOptions
117+
{
118+
BrowserVersion = _version,
119+
PlatformName = _os,
120+
UseWebSocketUrl = true
121+
},
122+
_ => new ChromeOptions
123+
{
124+
BrowserVersion = _version,
125+
PlatformName = _os,
126+
UseWebSocketUrl = true
127+
}
128+
};
129+
}
130+
}
131+
}

README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,15 @@ This example demonstrates how to use NUnit to run a test in parallel across seve
2020
3. Run tests
2121
* Single test: `dotnet test --filter "Single"`
2222
* Paralle tests: `dotnet test --filter "Parallel"` (run 2 tests simultaneously)
23-
* DevTools test: `dotnet test --filter "DevTools"`
23+
* DevTools (CDP) test: `dotnet test --filter "DevTools"` (runs a CDP test)
24+
* WebDriver BiDi test: `dotnet test --filter "BiDi"`
2425
2526
### Running your tests from Test Explorer via NUnit Test Adapter
2627
Click Run Unit Tests, you will see the test result in the [TestingBot Dashboard](https://testingbot.com/members/)
2728
2829
2930
### Resources
30-
##### [TestingBot Documentation](https://testingbot.com/support/)
31+
##### [TestingBot Documentation](hhttps://testingbot.com/support/web-automate/selenium/csharp/nunit)
3132
3233
##### [SeleniumHQ Documentation](http://www.seleniumhq.org/docs/)
3334

0 commit comments

Comments
 (0)