WebDriver 测试开发
作者:网络转载 发布时间:[ 2013/1/21 10:30:51 ] 推荐标签:
public class Login {
private WebDriver webDriver;
private String baseUrl;
private Logger logger = LoggerFactory.getLogger(this.getClass());
private WebElement element;
public void openBrowser() throws Exception{
webDriver = new FirefoxDriver();
webDriver.get(baseUrl);
}
public void clickLoginLink(){
try {
baseUrl = "http://www.lovo.cn/";
this.openBrowser();
element = webDriver.findElement(By.linkText("登录"));
if(element != null){
element.click();
if(webDriver.findElement(By.id("logusername")) != null){
logger.info("正常跳转到登陆页");
}else{
logger.error("打开登陆页面失败");
}
}else{
logger.error("没有找到登陆链接!!!");
}
} catch (Exception e) {
e.printStackTrace();
logger.error("发生未知错误!");
}
}
public void login(){
this.webDriver.findElement(By.id("logusername")).clear();
this.webDriver.findElement(By.id("logusername")).sendKeys("138****035");
this.webDriver.findElement(By.id("logpassword")).clear();
this.webDriver.findElement(By.id("logpassword")).sendKeys("123456");
this.webDriver.findElement(By.id("logimageCheck")).clear();
this.webDriver.findElement(By.id("logimageCheck")).sendKeys("5rkz");
this.webDriver.findElement(By.cssSelector("span.btntext")).click();
this.webDriver.findElement(By.cssSelector("div.text")).click();
if(this.webDriver.findElement(By.cssSelector("BODY")).getText().matches("^[\s\S]* 刘建东[\s\S]*$")){
this.logger.info("登陆成功!");
}else{
this.logger.error("登陆失败!");
}
}
public static void main(String[] args){
Login login = new Login();
login.clickLoginLink();
login.login();
}
}
有时候打开firefox的时候会报错,说没有安装firefox之类的错误,这是因为你改变了firefox的默认安装路径。这种情况下,你可以根据FirefoxBinary类实现。
方法如下:
public WebDriver openFirefox() throws Exception{
File file = new File("你的firefox的安装路径+firefox.exe"); //这里注意对进行转义
FirefoxBinary firefoxBin = new FirefoxBinary(file);
WebDriver webDriver = new FirefoxDriver(firefoxBin,null);
return webDriver;
}
或者使用setCapabilit来设置
方法如下:
public WebDriver openFirefox() throws Exception{
DesiredCapabilities des = DesiredCapabilities.firefox();
des.setCapability("firefox_binary", "你的firefox的安装路径+firefox.exe");
WebDirver webDriver = new FirefoxDriver(des);
return webDriver;
}
总结;
FirefoxDriver类有7个构造方法,意味着可以用7中方法打开firefox浏览器(其实比7种多),
FirefoxDriver()
FirefoxDriver(Capabilities desiredCapabilities)
FirefoxDriver(Capabilities desiredCapabilities, Capabilities requiredCapabilities)
FirefoxDriver(FirefoxBinary binary, FirefoxProfile profile)
FirefoxDriver(FirefoxBinary binary, FirefoxProfile profile, Capabilities capabilities)
FirefoxDriver(FirefoxBinary binary, FirefoxProfile profile, Capabilities desiredCapabilities, Capabilities requiredCapabilities)
FirefoxDriver(FirefoxProfile profile)
相关推荐
更新发布
功能测试和接口测试的区别
2023/3/23 14:23:39如何写好测试用例文档
2023/3/22 16:17:39常用的选择回归测试的方式有哪些?
2022/6/14 16:14:27测试流程中需要重点把关几个过程?
2021/10/18 15:37:44性能测试的七种方法
2021/9/17 15:19:29全链路压测优化思路
2021/9/14 15:42:25性能测试流程浅谈
2021/5/28 17:25:47常见的APP性能测试指标
2021/5/8 17:01:11