简体   繁体   中英

Onblur event firing gets messed up when firefox window is not in focus while running a selenium test

I have a form where I check that 2 passwords are equal. The confirm password field uses the onblur event to check if what was typed is equal to the password field. Everything works fine when testing manually. With automated selenium tests it works too if I leave the focus on the browser. This is not always possible as I have to run parallel tests sometimes. Whenever focus is lost, validation fails. I tried using the line below to trigger an onblur event

public void triggerBlurEventById(String id)
    {
        ((JavascriptExecutor) WebDriverHelper.getDriver())
                .executeScript("document.getElementById('" + id + "').blur();");
    }
but the event never seems to get fired. Even if I trigger the blur event on the textfield in the browser console, it doesn't get called until I click on the textfield and run the command on console again. Not sure how I can get Selenium to fire the event properly.

This might not apply in your case, but one workaround I've done that seems to work, at least for focus, is to execute the script twice. This is literally what's in my code:

    ((JavascriptExecutor) driverHelper.getDriver()).executeScript("$(arguments[0]).focus();", element);
    ((JavascriptExecutor) driverHelper.getDriver()).executeScript("$(arguments[0]).focus();", element);

Theoretically, you should also be able to use something like this:

new Actions(driverHelper.getDriver()).moveToElement(element).click().build().perform();

I've also had some intermittent troubles with Actions as well, especially while doing dev work, wherein the action won't take place correctly unless the browser window is the actively selected window.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM