selenium webdriver is clearing out fields after sendKeys had previously populated them

前端 未结 2 1362
北海茫月
北海茫月 2021-01-13 08:25

The webpage that I\'m testing is using knockout. On other pages on our site that are not currently using knockout I\'m not having the same problem. The scenario I have is

相关标签:
2条回答
  • 2021-01-13 09:09

    Firefox has a bug which prevents some events from being executed while the browser window is out of focus. This could be an issue when you're running your automation tests - which might be typing even if the window is out of focus.

    The point is that knockout model updates are triggered (by default) with the change event. If it's not being executed, it's underlying model won't be up-to-date.

    To fix this issue I triggered the change event "manually", injecting javascript into my tests.:

    //suppose "element" is an input field
    element.sendKeys("value");
    JavascriptExecutor jsExecutor = (JavascriptExecutor) driver;
    jsExecutor.executeScript("$(arguments[0]).change();", element);
    

    As you might have noticed, I'm using jQuery to trigger the change event. If you're not using jQuery on your app, you can check here how to trigger it using vanilla javascript.

    Hope that helps somebody.

    0 讨论(0)
  • 2021-01-13 09:11

    I had the exact same problem. I would guess also that your code works fine in Chrome but not firefox, and that it always works when you do it manually?

    Anyway, the problem is likely to be that Selenium doesn't really behave the same way as a real user, and doesnt trigger the same events. When you "submit" the form, it sometimes won't have executed the "change" event on a text area, meaning that it won't have changed.

    I had the same problem testing Backbone.modelbinding, which uses the "change" event to update the model from the form. Knockout also uses the "change" event, but fortunately it can also use the "keyup" event. See valueUpdate in the docs:

    <input data-bind="value: someValue, valueUpdate: 'keyup'" />
    

    Anyway, that reproducibly solved it for me, and didnt need any sleeps once I had that done. The downside is that you'd be running the event more than is necessary in production, in order to make tests work. Another downside is that you if you want to run some code when a value changes, you'll now get one event per keypress instead of one per field change, which sucks sometimes.

    There is another solution, which is to make Selenium fire the change event yourself, for example: Selenium IE change event not fired. It's also suboptimal, but what can you do.

    You could also try putting the focus on a button before you click it. Don't know if that will work, I haven't tried it.

    0 讨论(0)
提交回复
热议问题