Selenium Automated Test example

Source: Internet
Author: User
Tags xpath

11.1 Using Javascripexecutor to click an element

tested Pages: http://www.sogou.comJava code
public class newtest {     webdriver driver;      String baseUrl;     JavascriptExecutor js;   @Test   public void f ()  throws Exception {            webelement searchinputbox=driver.findelement (By.id ("Query"));      webelement searchbutton=driver.findelement (By.id ("STB"));      Searchinputbox.sendkeys ("Click on a page element using the Javascipt statement");      // Call the Javascipt method to click on the Sogou first page of the search button      javascriptclick (SearchButton);     }  public void javascriptclick (webelement element)  throws Exception{       try{          // The IF condition determines whether the element elements passed in by the function parameter are in a click and is displayed on the page            if (element.isenabled () &&element.isdisplayed ()) {               system.out.println (" Click on the page element using Javascipt ");               // Execute Javascipt Statement Argument[0].click ()                 ((javascriptexecutor)  driver). Executescript ("Arguments[0].click ();", Element);           }else{               system.out.println ("element on page cannot be clicked");           }          //When an exception occurs, the Catch statement is executed, printing the associated exception information and stack information       }catch (staleelementreferenceexception e) {           systEM.OUT.PRINTLN ("page element is not attached to Web page" +e.getstacktrace ());       }catch ( Nosuchelementexception e) {          system.out.println ( "Page does not find the page element to manipulate" +e.getstacktrace ());       }catch (exception e) {   &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;SYSTEM.OUT.PRINTLN ("Cannot Complete click action" +e.getstacktrace ());       }  }   code Interpretation: The method inside the code implementation is a package, the commonly used operations to unload a function inside, it can be very convenient to repeat the call, reduce the writing of redundant code, Improve the writing efficiency of test code. ———————————————————————————————————————————————————— 11.2 In the Ajax-generated float box, click to select the option that contains a keyword purpose: Some of the tested pages contain Ajax's local refresh mechanism, and will produce a floating box that displays multiple data, you need to click on the Select Float box to include a key option  tested Pages: http://www.sogou.comJava Code   @Test   public void f ()  throws Exception {            webelement searchinputbox=driver.findelement (By.id ("Query "));      //webelement searchbutton=driver.findelement (By.id (" STB "));      searchinputbox.click ();      //stores all the options in the Float box in the Suggetionoptions list container , the direct copy of the XPath is://*[@id = "VL"]/div[1]/ul/li[3], here the double quotes need to escape      //list need to enter the package, just started to introduce the import  java.awt.List; error, introducing import java.util.list; success      list <webelement >suggetionoptions=driver.findelements (By.xpath ("//*[@id =\" vl\ "]/div[1]/ul/li[3]"));      for (webelement element:suggetionoptions) {         if ( Element.gettext (). Contains ("")) {              System.out.println(Element.gettext ());              Element.click ();             break;          }     }  }   ————— ——————————————————————————————————————————————————— 11.3 Setting property values for a Page object objective: To master the method of setting all properties of a Page object, this section aims to set the editable state and display length of the text box. Tested Web pages:
 <! DOCTYPE html> 
<meta charset= "UTF-8";
<title> set text box genus </title>,
<body>
<input type= "text" id= "text" value= "This year's watermelon is pretty sweet" size= "100" > Text box
</body>
Java Code public class newtest {     webdriver driver;      String baseUrl;      JavascriptExecutor js;    @Test   public void f ()  throws Exception {            webelement textinputbox=driver.findelement (By.id ("text"));      //call the SetAttribute method to modify the value of the text box to change the text displayed in the textbox     setattribute (Driver,textinputbox, "value", "text box's text and length properties have been modified");     //call the SetAttribute method to modify the text box's Size property value, Change the length of the text box     setattribute (driver,textinputbox, "size", "ten");     // Call the RemoveAttribute method to delete the Size property value in the text box     //removeattribute (driver,textinputbox, "size");     //Add page Element property properties and modify the wrapping method of page element properties   } private void setattribute (Webdriver  driver2, webelement tEXTINPUTBOX,&NBSP;STRING&NBSP;STRING,&NBSP;STRING&NBSP;STRING2)  {    // todo  auto-generated method stub     javascriptexecutor js= ( Javascriptexecutor)  driver;      Object element = null;     Object attributeName =  "AA";     object value  = null;    js.executescript ("Arguments[0].setattribute (arguments[1],arguments[2 ]) ", element,attributename,value);} /*private void removeattribute (webdriver driver2, webelement textinputbox,  string string)  {    // TODO Auto-generated method stub       javascriptexecutor js= (Javascriptexecutor)  driver;       Object element = null;    Object attributeName  =&nbSp;null;    object value = null;    js.executescript (" Arguments[0].removeattribute (Arguments[1],arguments[2]) ", Element,attributename,value);     }*/  @BeforeMethod   public void beforemethod ()  {       baseurl= "C:\\users\\administrator\\webstormprojects\\untitled\\11.3.html";     System.setproperty ("Webdriver.chrome.driver", "c:\\program files  (x86) \\Google\\Chrome\\Application\\ Chromedriver.exe ");      driver=new chromedriver ();       driver.get (BASEURL);  }  

Selenium Automated Test example

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.