首页 > 代码库 > selenium---封装的例子

selenium---封装的例子

1.在同一个包下创建2个继承类:

2.类中代码实现为:

package org.codeinfo.demo;

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

public class Utils {
WebDriver driver = new FirefoxDriver();

public void sendKeys(By by, String value){
driver.findElement(by).sendKeys(value);
}

public void click(By by){
driver.findElement(by).click();
driver.manage().timeouts().implicitlyWait(5000,TimeUnit.SECONDS);
}

}

------------------------------------------------------------------------

package org.codeinfo.demo;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

public class UtilsExtends extends Utils {
public static void main(String args[]){
Utils util = new Utils();

util.driver.navigate().to("http://www.baidu.com");//打开百度首页

util.sendKeys(By.xpath(".//*[@id=‘kw‘]"), "selenium");//在文本框中输入selenium

util.click(By.xpath(".//*[@id=‘su‘]")); //点击百度按钮

}
}

 

selenium---封装的例子