1.查看网页的cookie信息
//查看cookie 登录后的cookie
Set<Cookie> setcoke= driver.manage().getCookies();
for(Cookie str: setcoke){
System.out.println("cookie="+str);
}
for(Iterator<Cookie> iterator = setcoke.iterator();iterator.hasNext();){
System.out.print(iterator.next()+" ******");
}
2.测试数据参数化,数据库,批量化,同时set集合的读取和使用
//测试数据参数化测试
@Test(enabled=true,dataProvider="datas",timeOut=120000,dataProviderClass=dlTest.class)
public void dlTest_6(String users,String pass) throws InterruptedException{
driver=bmm.Login(users, pass, 0);
System.out.println("users="+users);
//查看cookie 登录后的cookie
Set<Cookie> setcoke= driver.manage().getCookies();
for(Cookie str: setcoke){
System.out.println("cookie="+str);
}
/* //查看cookie
for(Iterator<Cookie> iterator = setcoke.iterator();iterator.hasNext();){
System.out.print(iterator.next()+" ******");
} */
String str_dlemail1=driver.findElement(By.xpath("html/body/div[1]/div/div[2]/div")).getText();
System.out.println("dlTest_6实际结果:"+str_dlemail1);
Assert.assertEquals("登录成功", str_dlemail1);
}
//登录数据等同记事本
@DataProvider
public static Object[][] datas(){
return new Object[][]{
new Object[]{"test@qq.com","123456"},
new Object[]{"test1@qq.com","123456"},
};
}
3.invocationCount代表执行次数,timeOut超时时间设置
@Test(enabled = false,invocationCount=1)
4.等待元素加载完成后,在继续执行。
WebDriverWait wait = new WebDriverWait(driver,100);
wait.until(new ExpectedCondition<WebElement>(){
@Override
public WebElement apply(WebDriver d) {
return d.findElement(By.xpath("html/body/div[1]/div/div/div/form/table/tbody/tr[12]/td[2]/div/div/img"));
}});
封装成函数后为:
/**
* 等待元素出现后再执行
* 也就是等待元素对象加载完成
* driver驱动,By对象
* return 元素对象
* */
public WebElement ElementFinish(WebDriver driver,By by ){
WebDriverWait wait = new WebDriverWait(driver,100);
return wait.until(new ExpectedCondition<WebElement>(){
@Override
public WebElement apply(WebDriver d) {
return d.findElement(by);
}});
}
5.操作js代码
//控制滚动条 下拉到最后 把滚动条下拉到最后,
String high="scroll(0,10000);";
((JavascriptExecutor)driver).executeScript(high);
6.判断元素是否存在
WebElement linkUsername = driver.findElement(By.xpath("//a[contains(text(),"+username+")]"));
return linkUsername.isDisplayed();
这一方法的前提是:该元素之前已经存在,仅仅需要判断是否被显示。