Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
272 views
in Technique[技术] by (71.8m points)

java - How to fire JS event in selenium?

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

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)?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Unfortunately the Selenium WebDriver designers explicitly decided not to include this functionality in 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.

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 StackOverflow question for inspiration.

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...