I am using Selenium web driver, I want to listen for events occurring in the browser. For instance, when some javascript on the page causes the url to change using window.location = "...";
I have a listener attached to the Navigated
event of my EventFiringWebDriver
. But this fails to fire when the browser's url is changed. I'm inferring from this that Selenium can't listen to the event in this way?
Is there a proper way to attach to browser or DOM events coming from the browser?
I was able to use the OpenQA.Selenium.Support.UI.WebDriverWait
object to achieve this. I just have to specify the predicate (a comparison on the browser url) and a timespan. Selenium checks the result once every 500ms until it returns true or the timespan elapses, in which case the test fails. Not quite event driven but it has the desired effect:
private readonly IWebDriver _driver = new FirefoxDriver();
[Test]
public void UrlIsCorrect()
{
var w = new WebDriverWait(_driver, TimeSpan.FromSeconds(30));
w.Until(driver => driver.Url == "http://test.com");
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With