Skip to content

Instantly share code, notes, and snippets.

@elgatov
Created June 15, 2022 14:59
Show Gist options
  • Save elgatov/ef8f08201f0299d5874364d147e9ba80 to your computer and use it in GitHub Desktop.
Save elgatov/ef8f08201f0299d5874364d147e9ba80 to your computer and use it in GitHub Desktop.
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();
}
}
@elgatov
Copy link
Author

elgatov commented Jun 15, 2022

fails with:

Message:Test method UnitTestProject1.UnitTest1.TestMethod1 threw exception: 
System.NullReferenceException: Object reference not set to an instance of an object.

  Stack Trace:<.ctor>b__0()
Lazy`1.CreateValue()
Lazy`1.LazyInitValue()
Lazy`1.get_Value()
<StartEventMonitoring>d__24.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
TaskAwaiter.ThrowForNonSuccess(Task task)
TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
TaskAwaiter.GetResult()
<TestMethod1>d__2.MoveNext() line 45
--- End of stack trace from previous location where exception was thrown ---
TaskAwaiter.ThrowForNonSuccess(Task task)
TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
ThreadOperations.ExecuteWithAbortSafety(Action action)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment