如何在硒中解雇JS事件?

时间:2022-12-03 22:00:30

I'm using selenium WebDriver syntax. I know, that in selenium server-based syntax, you can fire an javascript event by doing:

我正在使用selenium WebDriver语法。我知道,在基于selenium server的语法中,您可以通过执行以下操作来触发javascript事件:

Selenium selenium = new DefaultSelenium("localhost", server.getPort(),
            "*iexplore", "http://www.eviltester.com");
selenium.fireEvent("lteq30", "blur");

how do I do the same in an application, created with WebDriver (for example, FirefoxDriver)?

如何在使用WebDriver创建的应用程序中执行相同操作(例如,FirefoxDriver)?

2 个解决方案

#1


17  

Unfortunately the Selenium WebDriver designers explicitly decided not to include this functionality in Selenium 2.

不幸的是,Selenium WebDriver设计者明确决定不在Selenium 2中包含此功能。

It's a deliberate decision to not include this feature in WebDriver, since it's usually a hack to work around synthesized events not behaving properly. We'd rather eliminate this need by providing great support for native events, so we'll continue improving that going forward. A user would never fire a focus event, they would click the form control. That's what your tests should be doing as well.

这是一个故意决定不在WebDriver中包含此功能,因为它通常是一个解决合成事件不正常的黑客。我们宁愿通过为本机事件提供强大支持来消除这种需求,因此我们将继续改进这一需求。用户永远不会触发焦点事件,他们会单击表单控件。这也是你的测试应该做的。

With that said, you can execute any javascript code you want. Thus you should look into how to fire events with javascript. Take at look at this * question for inspiration.

话虽如此,您可以执行您想要的任何JavaScript代码。因此,您应该研究如何使用javascript触发事件。看一下这个*问题的灵感。

Then you can do something like this:

然后你可以做这样的事情:

FirefoxDriver driver = new FirefoxDriver();
driver.ExecuteScript("[your fire event javascript code]");

I'm sure you could create a wrapper function to basically accomplish the same thing as fireEvent.

我相信你可以创建一个包装器函数来基本上完成与fireEvent相同的事情。

#2


5  

Another solution:

另一种方案:

((JavascriptExecutor) driver).executeScript("return document.getElementById('element').blur()");

#1


17  

Unfortunately the Selenium WebDriver designers explicitly decided not to include this functionality in Selenium 2.

不幸的是,Selenium WebDriver设计者明确决定不在Selenium 2中包含此功能。

It's a deliberate decision to not include this feature in WebDriver, since it's usually a hack to work around synthesized events not behaving properly. We'd rather eliminate this need by providing great support for native events, so we'll continue improving that going forward. A user would never fire a focus event, they would click the form control. That's what your tests should be doing as well.

这是一个故意决定不在WebDriver中包含此功能,因为它通常是一个解决合成事件不正常的黑客。我们宁愿通过为本机事件提供强大支持来消除这种需求,因此我们将继续改进这一需求。用户永远不会触发焦点事件,他们会单击表单控件。这也是你的测试应该做的。

With that said, you can execute any javascript code you want. Thus you should look into how to fire events with javascript. Take at look at this * question for inspiration.

话虽如此,您可以执行您想要的任何JavaScript代码。因此,您应该研究如何使用javascript触发事件。看一下这个*问题的灵感。

Then you can do something like this:

然后你可以做这样的事情:

FirefoxDriver driver = new FirefoxDriver();
driver.ExecuteScript("[your fire event javascript code]");

I'm sure you could create a wrapper function to basically accomplish the same thing as fireEvent.

我相信你可以创建一个包装器函数来基本上完成与fireEvent相同的事情。

#2


5  

Another solution:

另一种方案:

((JavascriptExecutor) driver).executeScript("return document.getElementById('element').blur()");