Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Release 4.22 Updates #1765

Merged
merged 13 commits into from
Jul 10, 2024
29 changes: 29 additions & 0 deletions examples/dotnet/SeleniumDocs/BiDi/CDP/CDPTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using System.Collections.Generic;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;

namespace SeleniumDocs.BiDi.CDP
{
[TestClass]
public class CDPTest : BaseChromeTest
{
[TestMethod]
public void SetCookie()
{
var cookie = new Dictionary<string, object>
{
{ "name", "cheese" },
{ "value", "gouda" },
{ "domain", "www.selenium.dev" },
{ "secure", true }
};
((ChromeDriver)driver).ExecuteCdpCommand("Network.setCookie", cookie);

driver.Url = "https://www.selenium.dev";
Cookie cheese = driver.Manage().Cookies.GetCookieNamed("cheese");
Assert.AreEqual("gouda", cheese.Value);

}
}
}
56 changes: 56 additions & 0 deletions examples/dotnet/SeleniumDocs/BiDi/CDP/LoggingTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Microsoft.IdentityModel.Tokens;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using OpenQA.Selenium;
using OpenQA.Selenium.Support.UI;

namespace SeleniumDocs.BiDi.CDP
{
[TestClass]
public class LoggingTest : BaseChromeTest
{
[TestMethod]
public async Task ConsoleLogs()
{
driver.Url = "https://www.selenium.dev/selenium/web/bidi/logEntryAdded.html";

using IJavaScriptEngine monitor = new JavaScriptEngine(driver);
var messages = new List<string>();
monitor.JavaScriptConsoleApiCalled += (_, e) =>
{
messages.Add(e.MessageContent);
};
await monitor.StartEventMonitoring();

driver.FindElement(By.Id("consoleLog")).Click();
driver.FindElement(By.Id("consoleError")).Click();
new WebDriverWait(driver, TimeSpan.FromSeconds(5)).Until(_ => messages.Count > 1);
monitor.StopEventMonitoring();

Assert.IsTrue(messages.Contains("Hello, world!"));
Assert.IsTrue(messages.Contains("I am console error"));
}

[TestMethod]
public async Task JsErrors()
{
driver.Url = "https://www.selenium.dev/selenium/web/bidi/logEntryAdded.html";

using IJavaScriptEngine monitor = new JavaScriptEngine(driver);
var messages = new List<string>();
monitor.JavaScriptExceptionThrown += (_, e) =>
{
messages.Add(e.Message);
};
await monitor.StartEventMonitoring();

driver.FindElement(By.Id("jsException")).Click();
new WebDriverWait(driver, TimeSpan.FromSeconds(5)).Until(_ => !messages.IsNullOrEmpty());
monitor.StopEventMonitoring();

Assert.IsTrue(messages.Contains("Uncaught"));
}
}
}
Original file line number Diff line number Diff line change
@@ -1,17 +1,24 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Threading.Tasks;
using Microsoft.IdentityModel.Tokens;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using OpenQA.Selenium;
using OpenQA.Selenium.Support.UI;
using OpenQA.Selenium.DevTools;
using System.Linq;
using OpenQA.Selenium.DevTools.V126.Network;
using OpenQA.Selenium.DevTools.V126.Performance;

namespace SeleniumDocs.Bidirectional.ChromeDevTools

namespace SeleniumDocs.BiDi.CDP
{
[TestClass]
public class BidiApiTest : BaseChromeTest
public class NetworkTest : BaseTest
{
[TestInitialize]
public void Startup()
{
StartDriver("126");
}

[TestMethod]
public async Task BasicAuthentication()
{
Expand All @@ -20,104 +27,16 @@ public async Task BasicAuthentication()
UriMatcher = uri => uri.AbsoluteUri.Contains("herokuapp"),
Credentials = new PasswordCredentials("admin", "admin")
};

var networkInterceptor = driver.Manage().Network;
networkInterceptor.AddAuthenticationHandler(handler);

await networkInterceptor.StartMonitoring();

driver.Navigate().GoToUrl("https://the-internet.herokuapp.com/basic_auth");
await networkInterceptor.StopMonitoring();

Assert.AreEqual("Congratulations! You must have the proper credentials.",
driver.FindElement(By.TagName("p")).Text);
}

[TestMethod]
public async Task PinScript()
{
driver.Url = "https://www.selenium.dev/selenium/web/xhtmlTest.html";
var element = driver.FindElement(By.Id("id1"));

var key = await new JavaScriptEngine(driver).PinScript("return arguments;");

var arguments = ((WebDriver)driver).ExecuteScript(key, 1, true, element);

var expected = new List<object>
{
1L,
true,
element
};
CollectionAssert.AreEqual(expected, (ICollection)arguments);
}

[TestMethod]
public async Task MutatedElements()
{
driver.Url = "https://www.selenium.dev/selenium/web/dynamic.html";

var mutations = new List<IWebElement>();
using IJavaScriptEngine monitor = new JavaScriptEngine(driver);
monitor.DomMutated += (_, e) =>
{
var locator = By.CssSelector($"*[data-__webdriver_id='{e.AttributeData.TargetId}']");
mutations.Add(driver.FindElement(locator));
};

await monitor.StartEventMonitoring();
await monitor.EnableDomMutationMonitoring();

driver.FindElement(By.Id("reveal")).Click();

new WebDriverWait(driver, TimeSpan.FromSeconds(5)).Until(_ => !mutations.IsNullOrEmpty());
await monitor.DisableDomMutationMonitoring();
monitor.StopEventMonitoring();

var revealed = driver.FindElement(By.Id("revealed"));
Assert.AreEqual(revealed, mutations[0]);
}

[TestMethod]
public async Task ConsoleLogs()
{
driver.Url = "https://www.selenium.dev/selenium/web/bidi/logEntryAdded.html";

using IJavaScriptEngine monitor = new JavaScriptEngine(driver);
var messages = new List<string>();
monitor.JavaScriptConsoleApiCalled += (_, e) =>
{
messages.Add(e.MessageContent);
};

await monitor.StartEventMonitoring();
driver.FindElement(By.Id("consoleLog")).Click();
driver.FindElement(By.Id("consoleError")).Click();
new WebDriverWait(driver, TimeSpan.FromSeconds(5)).Until(_ => messages.Count > 1);
monitor.StopEventMonitoring();

Assert.IsTrue(messages.Contains("Hello, world!"));
Assert.IsTrue(messages.Contains("I am console error"));
}

[TestMethod]
public async Task JsErrors()
{
driver.Url = "https://www.selenium.dev/selenium/web/bidi/logEntryAdded.html";

using IJavaScriptEngine monitor = new JavaScriptEngine(driver);
var messages = new List<string>();
monitor.JavaScriptExceptionThrown += (_, e) =>
{
messages.Add(e.Message);
};

await monitor.StartEventMonitoring();
driver.FindElement(By.Id("jsException")).Click();
new WebDriverWait(driver, TimeSpan.FromSeconds(5)).Until(_ => !messages.IsNullOrEmpty());
monitor.StopEventMonitoring();

Assert.IsTrue(messages.Contains("Uncaught"));
}

[TestMethod]
public async Task RecordNetworkResponse()
Expand All @@ -129,8 +48,8 @@ public async Task RecordNetworkResponse()
{
contentType.Add(e.ResponseHeaders["content-type"]);
};

await networkInterceptor.StartMonitoring();

driver.Navigate().GoToUrl("https://www.selenium.dev/selenium/web/blank.html");
await networkInterceptor.StopMonitoring();

Expand All @@ -149,11 +68,10 @@ public async Task TransformNetworkResponse()
Body = "Creamy, delicious cheese!"
}
};

INetwork networkInterceptor = driver.Manage().Network;
networkInterceptor.AddResponseHandler(handler);

await networkInterceptor.StartMonitoring();

driver.Navigate().GoToUrl("https://www.selenium.dev");
await networkInterceptor.StopMonitoring();

Expand All @@ -174,16 +92,60 @@ public async Task TransformNetworkRequest()
return request;
}
};

INetwork networkInterceptor = driver.Manage().Network;
networkInterceptor.AddRequestHandler(handler);

await networkInterceptor.StartMonitoring();

driver.Url = "https://www.selenium.dev/selenium/web/devToolsRequestInterceptionTest.html";
driver.FindElement(By.TagName("button")).Click();
await networkInterceptor.StopMonitoring();

Assert.AreEqual("two", driver.FindElement(By.Id("result")).Text);
}

[TestMethod]
public async Task PerformanceMetrics()
{
driver.Url = "https://www.selenium.dev/selenium/web/frameset.html";

var session = ((IDevTools)driver).GetDevToolsSession();
var domains = session.GetVersionSpecificDomains<OpenQA.Selenium.DevTools.V126.DevToolsSessionDomains>();

await domains.Performance.Enable(new OpenQA.Selenium.DevTools.V126.Performance.EnableCommandSettings());
var metricsResponse =
await session.SendCommand<GetMetricsCommandSettings, GetMetricsCommandResponse>(
new GetMetricsCommandSettings()
);

var metrics = metricsResponse.Metrics.ToDictionary(
dict => dict.Name,
dict => dict.Value
);

Assert.IsTrue(metrics["DevToolsCommandDuration"] > 0);
Assert.AreEqual(12, metrics["Frames"]);
}

[TestMethod]
public async Task SetCookie()
{
var session = ((IDevTools)driver).GetDevToolsSession();
var domains = session.GetVersionSpecificDomains<OpenQA.Selenium.DevTools.V126.DevToolsSessionDomains>();
await domains.Network.Enable(new OpenQA.Selenium.DevTools.V126.Network.EnableCommandSettings());

var cookieCommandSettings = new SetCookieCommandSettings
{
Name = "cheese",
Value = "gouda",
Domain = "www.selenium.dev",
Secure = true
};
await domains.Network.SetCookie(cookieCommandSettings);

driver.Url = "https://www.selenium.dev";
OpenQA.Selenium.Cookie cheese = driver.Manage().Cookies.GetCookieNamed("cheese");
Assert.AreEqual("gouda", cheese.Value);
}

}
}
58 changes: 58 additions & 0 deletions examples/dotnet/SeleniumDocs/BiDi/CDP/ScriptTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Threading.Tasks;
using Microsoft.IdentityModel.Tokens;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using OpenQA.Selenium;
using OpenQA.Selenium.Support.UI;

namespace SeleniumDocs.BiDi.CDP
{
[TestClass]
public class ScriptTest : BaseChromeTest
{
[TestMethod]
public async Task PinScript()
{
driver.Url = "https://www.selenium.dev/selenium/web/xhtmlTest.html";
var element = driver.FindElement(By.Id("id1"));

var key = await new JavaScriptEngine(driver).PinScript("return arguments;");
var arguments = ((WebDriver)driver).ExecuteScript(key, 1, true, element);

var expected = new List<object>
{
1L,
true,
element
};
CollectionAssert.AreEqual(expected, (ICollection)arguments);
}

[TestMethod]
public async Task MutatedElements()
{
driver.Url = "https://www.selenium.dev/selenium/web/dynamic.html";
var mutations = new List<IWebElement>();

using IJavaScriptEngine monitor = new JavaScriptEngine(driver);
monitor.DomMutated += (_, e) =>
{
var locator = By.CssSelector($"*[data-__webdriver_id='{e.AttributeData.TargetId}']");
mutations.Add(driver.FindElement(locator));
};
await monitor.StartEventMonitoring();
await monitor.EnableDomMutationMonitoring();

driver.FindElement(By.Id("reveal")).Click();

new WebDriverWait(driver, TimeSpan.FromSeconds(5)).Until(_ => !mutations.IsNullOrEmpty());
await monitor.DisableDomMutationMonitoring();
monitor.StopEventMonitoring();

var revealed = driver.FindElement(By.Id("revealed"));
Assert.AreEqual(revealed, mutations[0]);
}
}
}
Loading
Loading