Selenium Webdriver + junit mouse hover, another element appears, click on this element's workaround

Source: Internet
Author: User

Reprinted from http://blog.csdn.net/hcx1234567/article/details/17605533

After the arduous work, finally solved the UI TA (test automation) in this puzzle, must be recorded.

The premise is: This page that needs to be tested is written by Google's Angularjs. Many of the page effects are implemented with some of the events that Angularjs comes with, in conjunction with CSS hover. The UI TA framework for the test is selenium Webdriver + junit.

The problem is that there is an effect on the page: Click a button A, a list of Project B appears, hover over any project, and a list of all DataSource for this project appears. Click on any of the DataSource to search for the data inside this datasource. I'm just going to simulate this operation and do the following:

Try one: Use the Selenium's Actions class's Movetoelement (Webelement) method to simulate mouse hover. About 10 times, 1 times can be successfully clicked to the specified datasource. The effect of the unsuccessful is, as if the mouse is dangling on project, the DataSource list flashes and flashes many times, but it is impossible to click. We suspect that the mouse hovers over the selected webelement boundary, so the effect is not stable, so try to move the mouse to the specified offset to ensure the middle of the element, but it is useless. On the internet to find someone said action Movetoelement is a mouse flash, this code is finished, the mouse is not on the element above. It is recommended to use jquery's MouseOver (), the mouse will always be above the element. Code:

[Java]View Plaincopyprint?
    1. Actions action = New Actions (driver);
    2. Action.movetoelement (projectelement). Movebyoffset ( 3). Build (). Perform ();
Actions action = new actions (driver); Action.movetoelement (projectelement). Movebyoffset (3). Build (). Perform ();

Try two: Use jquery's mouseover () to simulate mouse hover. Tried for a long time, the effect of hover is not appear, later only to know because this element of the test page does not use JS bound mouseover () event, so the test code in the MouseOver () will not work.

[Java]View Plaincopyprint?
    1. Javascriptexecutor js = (javascriptexecutor) driver;
    2. String jsstr = "$ (' Projectlistdiv>ul>li>a:eq (" + i+ ") '). MouseOver ()";
    3. Js.executescript (JSSTR);
Javascriptexecutor js = (javascriptexecutor) driver; String jsstr = "$ (' Projectlistdiv>ul>li>a:eq (" + i+ ") '). MouseOver ()"; Js.executescript (JSSTR);

Try three: Some selenium API documents say there is mouseover (), I find AH find ah find, found Selenium1 is mouseover (), Selenium2 No, and SELENIUM1 and selenium2 driver enabled party Style is completely different, not too good to embed selenium1 in.

Try four: Webdriver's click () is valid for hidden elements. Tried, the conclusion is: No.

Try five: Javascript's click () is valid for hidden elements. Tried, the conclusion is: Yes. This way, the problem is solved, you can skip the mouse hover project This step, directly click on the DataSource on the line. Attached code:

[Java]View Plaincopyprint?
  1. Public static void Searchindatasourceonsearchlogpage (Webdriver driver, string projectName, String DataSourceName) {
  2. Searchwebelement.getwebelementbyxpath (Driver, Projectlistxpath). Click ();
  3. //Get a list of project
  4. list<webelement> projects = Searchwebelement.getwebelementlistbyxpath (
  5. Driver, Projectsxpath);
  6. list<webelement> datasources = new arraylist<webelement> ();
  7. Javascriptexecutor js = (javascriptexecutor) driver;
  8. try {
  9. For (webelement projectelement:projects) {
  10. System.out.println (Projectelement.gettext ());
  11. if (Projectname.equals (Projectelement.gettext ())) {
  12. Actions action = New Actions (driver);
  13. //Why you need to movetoelement, because I need to determine whether the specified datasource, need to get the text of the DataSource label element
  14. //You can only get this text if you hover over project and a list of DataSource appears.
  15. Action.movetoelement (projectelement). Movebyoffset ( 3)
  16. . Build (). Perform ();
  17. //Get a list of DataSource
  18. datasources = Searchwebelement.getwebelementlistbyxpath (driver, Rawdataxpath);
  19. For (int i=1;i<=datasources.size (); i++) {
  20. Webelement datasourceelement = Datasources.get (i-1);
  21. System.out.println (Datasourceelement.gettext ());
  22. if (Datasourcename.equals (Datasourceelement.gettext ())) {
  23. //Use the JS click event to click on the hidden element
  24. String jsstr = "$ (' #projectListDiv >ul>li>ul>li>div:eq (" + i + ") '). Click ();";
  25. Js.executescript (JSSTR);
  26. }
  27. }
  28. }
  29. }
  30. } catch (Exception e) {
  31. E.printstacktrace ();
  32. }
  33. }

Summing up, this problem has taken a lot of time to solve, a big problem is that I am not familiar with JavaScript, JQUERY,ANGULARJS, so the vision is narrow, do not know what method to solve, so only in the development of the point of continuous trial.

Selenium Webdriver + junit mouse hover, another element appears, click on this element's workaround

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.