Created
June 15, 2022 14:59
-
-
Save elgatov/ef8f08201f0299d5874364d147e9ba80 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Microsoft.VisualStudio.TestTools.UnitTesting; | |
using System; | |
using System.Collections.Generic; | |
using System.Threading.Tasks; | |
using OpenQA.Selenium; | |
using OpenQA.Selenium.Chrome; | |
using OpenQA.Selenium.Support.Events; | |
using OpenQA.Selenium.Support.UI; | |
namespace UnitTestProject1 | |
{ | |
[TestClass] | |
public class UnitTest1 | |
{ | |
private IWebDriver driver; | |
[TestInitialize] | |
public void TestInit() | |
{ | |
var options = new ChromeOptions(); | |
options.AddArgument("enable-automation"); | |
options.AddArgument("disable-infobars"); | |
options.AddArgument("disable-notifications"); | |
options.AddArgument("start-maximized"); | |
options.AddUserProfilePreference("profile.default_content_settings.popups", 0); | |
options.BinaryLocation = @"C:\Program Files\Chromium\Application\chrome.exe"; | |
var chromeService = ChromeDriverService.CreateDefaultService(); | |
var chromeDriver = new ChromeDriver(chromeService, options, TimeSpan.FromSeconds(60)); | |
driver = new EventFiringWebDriver(chromeDriver); | |
} | |
[TestMethod] | |
public async Task TestMethod1() | |
{ | |
List<DomMutationData> attributeValueChanges = new List<DomMutationData>(); | |
DefaultWait<List<DomMutationData>> wait = new DefaultWait<List<DomMutationData>>(attributeValueChanges); | |
wait.Timeout = TimeSpan.FromSeconds(3); | |
IJavaScriptEngine monitor = new JavaScriptEngine(driver); | |
monitor.DomMutated += (sender, e) => | |
{ | |
attributeValueChanges.Add(e.AttributeData); | |
}; | |
await monitor.StartEventMonitoring(); | |
driver.Navigate().GoToUrl("http://www.google.com"); | |
IWebElement span = driver.FindElement(By.CssSelector("span")); | |
await monitor.EnableDomMutationMonitoring(); | |
((IJavaScriptExecutor)driver).ExecuteScript("arguments[0].setAttribute('cheese', 'gouda');", span); | |
wait.Until((list) => list.Count > 0); | |
Console.WriteLine("Found {0} DOM mutation events", attributeValueChanges.Count); | |
foreach (var record in attributeValueChanges) | |
{ | |
Console.WriteLine("Attribute name: {0}", record.AttributeName); | |
Console.WriteLine("Attribute value: {0}", record.AttributeValue); | |
} | |
await monitor.DisableDomMutationMonitoring(); | |
} | |
[TestCleanup] | |
public void Cleanup() => driver.Dispose(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
fails with: