WebDriver:更改事件未触发 [英] WebDriver: Change event not firing

查看:95
本文介绍了WebDriver:更改事件未触发的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用KnockoutJS的应用程序,我正在尝试编写一些测试表单的测试。如果您不了解KnockoutJS,那么它的简短故事就是它提供了从我的视图到我的数据模型的绑定。这意味着当我在输入字段中键入值时,我的基础对象会自动使用该输入字段值进行更新。这是通过默认情况下的更改事件完成的。

I have an application that is using KnockoutJS and I'm attempting to write some tests that test a form. If you don't know KnockoutJS, the short story for it is that it provides bindings from my view to my data model. This means that when I type a value in the an input field, my underlying object automatically gets updated with that input fields value. This is done through a change event by default.

我遇到的问题是,当我的WebDriver测试在字段中输入时,更改事件未触发,因此我的基础数据模型没有适当的值。这会导致我的表单验证失败,但不应该。

The problem I am having is that when my WebDriver test is typing into the field, the change event is not firing so my underlying data model does not have the appropriate values. This causes my form validation to fail when it should not.

我已经完成了在互联网上找到的所有工作。我已经:

I've done everything I could find on the internet to make this work. I've:


  1. 发送标签键

  2. 点击表单字段

  3. 发送JavaScript代码以激活焦点并模糊事件(模糊时进行验证)

  4. 在输入之前点击表单字段

  5. 设置等待只是因为这是一个时间问题

  6. 更改KnockoutJS以更新afterkeydown上的输入字段

  1. sent the tab key
  2. clicked away from the form field
  3. send JavaScript code to fire focus and blur events (validation occurs on blur)
  4. clicked the form field before typing
  5. set waits just incase it was a timing issue
  6. changed KnockoutJS to update input field on afterkeydown



<这些都没有对我有用。我需要一些帮助。

None of these have worked for me. I am in need of some help.

此外,我已经确认这不是一个事件冒泡问题,因为我明确删除了所有其他事件,只留下KnockoutJS更改我正在寻找的解决方案适用于所有浏览器驱动程序(...至少是IE,FF,Chrome,Safari等主要解决方案)。事件。

In addition, I have verified that this is not a event bubbling issue as I removed all other events explicitly, leaving just the KnockoutJS change event.

)并且不需要使用jQuery。

for the solution i'm looking for is one that works for all browser drivers (... at least the main ones e.g. IE, FF, Chrome, Safari) and does not require the use of jQuery.

任何帮助将不胜感激。

这是相对代码我用来在字段中键入值:

Here is the relative code i'm using to type values into the field:

// find element
WebElement input = this.element.findElement(By.className("controls"))
                               .findElement(By.tagName("input"));

// to set focus?
input.click();

// erase any existing value (because clear does not send any events
for (int i = 0; i < input.getAttribute("value").length(); i++) {
    input.sendKeys(Keys.BACK_SPACE);
}

// type in value
input.sendKeys(text);

// to fire change & blur? (doesnt fire change)
//input.sendKeys(Keys.TAB);

// to fire change & blur? (doesnt fire change)
driver.findElement(By.tagName("body")).click();


推荐答案

所以我相信我已经发现了我的问题。我会完全承认这是PEBKAC。如果浏览器窗口没有主动关注,我忘记了WebDriver有问题机器(这对我来说仍然很奇怪)。当我调试问题时,我正在使用我的编辑器逐步完成代码。通过正常运行代码而不从浏览器中移除焦点,事件按照预期使用所有三个解决方案激活(标签,点击,和javascript)。

So I believe I have found out my problem. I will fully admit this was PEBKAC. I had forgotten that WebDriver has issues if the browser window is not in active focus on the machine (which is still weird to me). When I was debugging the problem, I was using my editor to step through the code. By running the code normally and without removing focus from the browser, the events fire as expected using all three solutions (tab, click-away, and javascript).

我有一个显示所有三种方法的示例项目,但是我是git和github的主要菜鸟,并且在交付项目时遇到了问题。一旦我明白了,我将与大家分享这个项目。

I have an example project showing all three methods, however I am a major noob with git and github and am having problems delivering the project. Once i figure that out, I will share the project with you all.

编辑:在GitHub上获取示例代码( https://github.com/loesak/knockout.webdriver.change.event.test )。您可以将项目作为服务器中的webapp启动并手动运行测试,也可以通过maven运行测试( mvn clean install )。我并没有付出太多努力让这个工作变得健壮,所以假设你安装了Firefox并且端口 0808 8080已经打开。

EDIT: Got the example code up on GitHub (https://github.com/loesak/knockout.webdriver.change.event.test). You can either start the project as a webapp in a server and run the test manually or you can run the tests via maven (mvn clean install). I didn't put a whole lot of effort into getting this to work robustly so it is assuming you have Firefox installed and port 0808 8080 is open.

编辑:修正了已声明的端口号

EDIT: Fixed stated port number

这篇关于WebDriver:更改事件未触发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆