Webdriver provides a common way to manipulate Web controls, such as: Buttons, input boxes, hyperlinks, and so on, not much to say, directly on the code:
Importorg.openqa.selenium.By;ImportOrg.openqa.selenium.WebDriver;Importorg.openqa.selenium.WebElement;ImportOrg.openqa.selenium.firefox.FirefoxDriver;ImportOrg.openqa.selenium.support.ui.Select; Public classwebdriversimple {webdriver driver; PublicWebdriversimple () {
System.setproperty ("Webdriver.firefox.bin", "D:/program Files/mozilla Firefox/firefox.exe"); This. Driver =NewFirefoxdriver (); } //Launch Browser Public voidstartbrowser (String url) {driver.get (URL); //Maximize Your browserdriver.manage (). window (). Maximize (); } //get an Element object using XPath Publicwebelement Locateelementbyxpath (String locator) {webelement element=driver.findelement (By.xpath (locator)); returnelement; } //getting element objects using CSS Publicwebelement locateelementbycss (String locator) {webelement element=driver.findelement (By.cssselector (locator)); returnelement; } /** operation input Box input * 1.sendKeys for input, parameter is the value to be entered * 2.clear means clear data from the input box*/ Public voidtestinput (String locator,string content) {//webelement input = This.locateelementbyxpath (locator);Webelement input = This. LOCATEELEMENTBYCSS (Locator); Input. Clear (); Input. SendKeys (content); Input.clear (); Input.sendkeys (content); } // /** Action Hyperlink link * 1.click represents click on this a link*/ Public voidTestlink (String locator) {webelement link= This. Locateelementbyxpath (Locator); Link. Click (); } /**Action drop-down Box select * 1. Requires a Select class * 2.selectByValue parameter for option in the Value property * 3.selectByIndex parameter for option Order * 4.selectByVisibleText parameter is the text value of option*/ Public voidTestselect (String Locator,intindex) {webelement element= This. Locateelementbyxpath (Locator); Select Select=NewSelect (Element); Select. Selectbyindex (index); } Public voidtestselect (String locator,string value) {webelement element= This. Locateelementbyxpath (Locator); Select Select=NewSelect (Element); Select. Selectbyvalue (value); //Select. Selectbyvisibletext (text); } /** Action radio button Radiobox * 1.click for click to select this radio box * 2.isSelected representative Check whether this radio box is selected*/ Public voidTestraidobox (String locator) {webelement Radio= This. Locateelementbyxpath (Locator); Radio. Click (); Radio. isSelected (); } /** action check box checkbox * 1.click for click to select this multi box * 2.isSelected representative Check whether this multi box is selected*/ Public voidTestcheckbox (String locator) {webelement checkbox= This. Locateelementbyxpath (Locator); CheckBox. Click (); CheckBox. isSelected (); } /** action Button * 1.click means click this button * 2.sEnabled representative check whether this button is available*/ Public voidTestbutton (String locator) {Webelement Submit= This. Locateelementbyxpath (Locator); Submit. Click (); Submit. isenabled (); }
/** operation upload Control upload
* 1. Generally, SendKeys the path directly into the input box.
* 2. If the input box is added readonly attribute, cannot enter, then need to use JS to remove the ReadOnly attribute!
*/
Public void testupload (String locator,string path)
{ this. Locateelementbyxpath (locator); Load. SendKeys (path); } // Close and exit driver Public void CloseBrowser () { driver. Close (); Driver. quit (); } }
SELENIUM2 (Webdriver) Summary (iv)---basic element operations