Selenium Webdriver Study Chapter ①

Source: Internet
Author: User
Tags xpath seleniumhq log4j

Selenium is a very good web testing tool, flexible, simple, with the increase of the content of the site, the original manual testing more complex, and often have forgotten places,

Automated testing is also important to record the learning process of selenium webdriver

Note: I do Java, I only learn Java and Selenium Webdriver method of cooperation.

First article Selenium IDE

For beginners, it may be possible to use the Selenium IDE to record programs at first, but after familiarity, I would prefer to write code to do standard functional testing, that is to say first Selenium IDE, and then start

Our first Helloword.

Selenium IDE is a plugin for Firefox, it is possible to make script recording and case conversion

1. First download Firefox and install additional build Selenium Ide,firebug

Then search the Selenium ide,firebug for these two build installations

2. Start the Selenium IDE

1 is the recording switch, 2 is the playback

This can be exported as a Java language

When the above IDE window pops up, we can start the selenium script recording, with a red dot in the upper right corner, and when it's on time (for example) indicates that the IDE is making a script recording. OK, start recording, recording, the direct operation of the Firefox browser window, the IDE will automatically record your actions

Article Two: Building a project

Build a Maven project with Eclipse, and when you are finished, modify the Pom.xml

  <Dependencies>    <Dependency>      <groupId>Junit</groupId>      <Artifactid>Junit</Artifactid>      <version>4.2</version>      <Scope>Test</Scope>    </Dependency>    <Dependency>        <groupId>Org.seleniumhq.selenium</groupId>         <Artifactid>Selenium-java</Artifactid>         <version>2.53.0</version>    </Dependency>    <Dependency>        <groupId>Com.opera</groupId>        <Artifactid>Operadriver</Artifactid>    </Dependency>    <Dependency>         <groupId>Org.apache.commons</groupId>         <Artifactid>Commons-exec</Artifactid>         <version>1.3</version>    </Dependency>    <Dependency>        <groupId>Log4j</groupId>        <Artifactid>Log4j</Artifactid>        <version>1.2.17</version>    </Dependency>  </Dependencies>  <dependencymanagement>        <Dependencies>            <Dependency>                <groupId>Com.opera</groupId>                <Artifactid>Operadriver</Artifactid>                <version>0.16</version>                <Exclusions>                    <exclusion>                        <groupId>Org.seleniumhq.selenium</groupId>                        <Artifactid>Selenium-remote-driver</Artifactid>                    </exclusion>                </Exclusions>            </Dependency>        </Dependencies>    </dependencymanagement>

The third chapter launches the browser

Ie

        New internetexplorerdriver ();             // let the browser access  Baidu        Driver.get ("http://www.baidu.com");  

Firefox

// If your FireFox is not installed in the default directory, you must set it in the program System.setproperty ("Webdriver.firefox.bin", "D:\\software\\firefox\\firefox.exe"); // Create a browser instance of FireFox New firefoxdriver ();d River.get (

Chrome

System.setproperty ("Webdriver.chrome.driver", "C:/Program Files (x86)/google/chrome/application/chromedriver.exe" ); // Create a Chromedriver interface to connect to Chrome, // must have Chromedriver.exe file, selenium cannot start chrome by default // Create a browser instance of Chrome New Chromedriver ();

Fourth article: Code

①selenium IDE Recording

② Program Crawl

Js

Javascriptexecutor js = (javascriptexecutor) driver;
Js.executescript ("document.getElementById (\" myprojectid\ "). Style=\" Display:block;\ "");

Driver.findelement (by.)

Id

Driver.findelement (By.id ("kw"));

ClassName

Driver.findelement (By.classname ("comment")

Cssseletor

Driver.findelement (By.cssselector ("img.plusimg"))

Linktext

Driver.findelements (By.linktext ("Customer Login"))

Xpath

Xpath

XPath is a language for finding information in an XML document, and XPath can be used to traverse elements and attributes in an XML document, providing the ability to browse the tree

XPath is supported by mainstream browsers

/html/body/div/form/input

ul[@id = ' ul-select ']/li[1]

Button[contains (Text (), ' submit ')]

ul[@id = ' Myprojectid ']//a[contains (text (), ' submit ')]

Select operation

New Select (Driver.findelement (by.id ("company-city"))). Selectbyvisibletext ("Shenzhen");

Fifth article: Special operation

Frame

Sometimes when we locate a page element, we find that we can't locate it, and check the locator that we wrote repeatedly without any problems, and the code doesn't have any problems. At this point you will have to look at whether this page element is in an IFRAME, which may be one of the reasons why you cannot find it. If you are looking for an element in the IFRAME in a default content, it must not be found. Conversely, if you look for another IFRAME element or an element in the default content in an IFRAME, it must not be located

// into the frame of id= "frame" or name= "frame",

Parent-child window

String Newhandle = "";//gets the current Window parent windowString Currenthandle =Driver.getwindowhandle ();//Click on the page element to open a new windowDriverutil.scrollintoview (Driver, driverutil.getwebelement (Driver, By.classname ("Indentinfo-table-div")));//get all browser windowsSet<string> handles =driver.getwindowhandles (); Iterator<String> Itwin =handles.iterator (); while(Itwin.hasnext ()) {String key=Itwin.next (); if(Currenthandle.equals (key)) {Continue; }    //get a new windowNewhandle =key;}//Toggle WindowWebdriver Newdriver =driver.switchto (). window (newhandle);//Business Operations//...//...//remove new window from setHandles.remove (newhandle);//cut back to main windowDriver.switchto (). window (Currenthandle);

Modal frame

Driver.switchto (). Activeelement ();

For Ajax

Webdriverwait

Sleep

Selenium Webdriver Study Chapter ①

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.