A tentative study of Webdriver (login and first case)

Source: Internet
Author: User
Tags xpath

Today, we use Webdriver to make a preliminary access to the backend of the server, which realizes the small function of login, menu jump and preliminary data judgment.

The server uses ExtJS technology to automatically generate HTML, which is very unsuitable for automated testing and is very difficult to locate elements. But today we're just doing a little bit of trying, not involving in-depth knowledge.

All right, crap, don't say it, on the code.

1  Packagecom.xws.support.ly;2 3 Importorg.openqa.selenium.By;4 ImportOrg.openqa.selenium.WebDriver;5 Importorg.openqa.selenium.WebElement;6 ImportOrg.openqa.selenium.chrome.ChromeDriver;7 8  Public classLogin {9     PrivateString userName = "";Ten     PrivateString PassWord = ""; One     PrivateString identifying = ""; A     PrivateString url = ""; -Webdriver Driver =Newchromedriver (); -Webelement user =NULL; theWebelement pass=NULL; -Webelement Identifyingcode =NULL; -  -      + Login (String username,string password,string identifying,string url) { -          This. username=UserName; +          This. PassWord =PassWord; A          This. identifying =identifying; at          This. url =URL; -     } -      -      PublicWebdriver Systemlogin () { -         //open a test address via chrome -Driver.get ("http://172.23.69.144:8888/management/login.jsp"); in          -         //Get the login name, password, and captcha element by using the By.name method touser = Driver.findelement (by.name ("UserName"))); +pass = Driver.findelement (by.name ("password"))); -Identifyingcode = Driver.findelement (By.name ("Yzm")); the          *         //Empty the login, password, captcha text box $ user.clear ();Panax Notoginseng pass.clear (); - identifyingcode.clear (); the          +         //Enter login name, password, verification code A User.sendkeys (userName); the Pass.sendkeys (PassWord); + Identifyingcode.sendkeys (identifying); -          $         //to sign in $Driver.findelement (By.xpath ("//a[contains (@href, ' login ')]") . Click (); -          -         returndriver; the     } -}


The above code completes the operation of the page login (verification code Here I changed the servlet, enter any word nonalphanumeric validation passed)

1  Public classOpenmenu {2 3Webelement menuurl=NULL;4list<webelement> list =NULL;5         6      Public voidGetMenu (webdriver driver,string menutext) {7         //put all the tree picture elements in the list8List = Driver.findelements (By.cssselector ("Img.x-tree-elbow-img.x-tree-elbow-plus.x-tree-expander"));9         Ten         //Loop list, expand all menus One          for(intI=0;i<list.size (); i++){ A              -             Try { -                 //each time you expand the menu, do a wait to prevent the menu from loading and causing the exception theThread.Sleep (1000); -}Catch(interruptedexception e) { - e.printstacktrace (); -             } + List.get (i). click (); -         } +         Try{ A             //here to catch the exception of the menu not found, if the menu is not found, the prompt menu cannot be found atDriver.findelement (By.xpath ("//span[text () = '" + menutext + "']/parent::d IV") . Click (); -}Catch(nosuchelementexception e) { -System.out.println ("Sorry, this menu cannot be found:" +menutext); - e.printstacktrace (); -         } -     } in}

The above code completes the operation of expanding all menus and jumping to the corresponding menu

1  Public classContrastdata {2Webdriver Driver =NULL;3list<webelement> list =NULL;4Javascriptexecutor jse=(javascriptexecutor) driver;5String term = "";6     7 contrastdata (webdriver driver,string term) {8          This. driver=driver;9          This. term=term ;Ten     } One      A      Public voidDatacontrast () { -         //here, by invoking the JS method, the name is a term of the drop-down bar to click (This is not a select) -Jse.executescript ("Window.document.getElementsByName ('" +term+ ") [0].click ()]); the          -         Try { -             //set wait time, wait for hidden elements to show complete -Thread.Sleep (2000); +}Catch(interruptedexception e) { - e.printstacktrace (); +         } A         //matches the displayed lable element with an XPath, and when text is the corresponding value, click to select atDriver.findelement (By.xpath ("//*[text () = ' audit Success ']") . Click (); -         /* - * by Byid Way, find the search image and click - * Here I want to explain that this is not a button element, so it is not a submit - * This element is dynamically loaded, not directly in the Byid way, I am here just to do, the actual project can not be loaded with dynamic ID to locate the element -          */ inDriver.findelement (By.id ("Button-1108-btniconel") . Click (); -             Try { to                 //set the wait time and wait for the search results to show complete +Thread.Sleep (2000); -}Catch(interruptedexception e) { the e.printstacktrace (); *             } $          //The audit status elements that are displayed are all put in the listPanax NotoginsengList = Driver.findelements (By.cssselector ("Div.x-grid-cell-inner")); -         BooleanTest =true; the          for(intI=0;i<list.size (); i++){ +              A             //traversal judgment, if there is a status of non-audit success, then the test does not pass the             if(! (List.get (i). Equals ("Audit succeeded"))){ +test=false; -             } $         } $         if(!test) { -SYSTEM.OUT.PRINTLN ("Test does not pass"); -}Else{ theSYSTEM.OUT.PRINTLN ("Test Pass"); -         }Wuyi     } the}

The above code has completed a simple case where we can see whether the data retrieved through the ' Audit success ' status is audited successfully, or if it is printed through, otherwise printing does not pass.

All right, this is just a toy code, the real automation platform, the element positioning, the use case execution, the result output all need to encapsulate, rather than write like this, very difficult to maintain.

A tentative study of Webdriver (login and first case)

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.