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?
- Actions action = New Actions (driver);
- 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?
- Javascriptexecutor js = (javascriptexecutor) driver;
- String jsstr = "$ (' Projectlistdiv>ul>li>a:eq (" + i+ ") '). MouseOver ()";
- 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?
- Public static void Searchindatasourceonsearchlogpage (Webdriver driver, string projectName, String DataSourceName) {
- Searchwebelement.getwebelementbyxpath (Driver, Projectlistxpath). Click ();
- //Get a list of project
- list<webelement> projects = Searchwebelement.getwebelementlistbyxpath (
- Driver, Projectsxpath);
- list<webelement> datasources = new arraylist<webelement> ();
- Javascriptexecutor js = (javascriptexecutor) driver;
- try {
- For (webelement projectelement:projects) {
- System.out.println (Projectelement.gettext ());
- if (Projectname.equals (Projectelement.gettext ())) {
- Actions action = New Actions (driver);
- //Why you need to movetoelement, because I need to determine whether the specified datasource, need to get the text of the DataSource label element
- //You can only get this text if you hover over project and a list of DataSource appears.
- Action.movetoelement (projectelement). Movebyoffset ( 3)
- . Build (). Perform ();
- //Get a list of DataSource
- datasources = Searchwebelement.getwebelementlistbyxpath (driver, Rawdataxpath);
- For (int i=1;i<=datasources.size (); i++) {
- Webelement datasourceelement = Datasources.get (i-1);
- System.out.println (Datasourceelement.gettext ());
- if (Datasourcename.equals (Datasourceelement.gettext ())) {
- //Use the JS click event to click on the hidden element
- String jsstr = "$ (' #projectListDiv >ul>li>ul>li>div:eq (" + i + ") '). Click ();";
- Js.executescript (JSSTR);
- }
- }
- }
- }
- } catch (Exception e) {
- E.printstacktrace ();
- }
- }
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