1、拖拽页面元素
//向下拖动10个像素,共拖动5次 for(int i=0;i<5;i++){ new Actions(driver).dragAndDropBy(driver.findElement(By.id('stb')), o, 10) } //向右拖动10个像素,共拖动5次 for(int i=0;i<5;i++){ new Actions(driver).dragAndDropBy(driver.findElement(By.id('stb')), 1o, 0) }
2、查看页面元素属性
String inputText=driver.findElement(By.id("query")).getAttribute("value");
3、获取页面CSS属性
WebElement input =driver.findElement(By.id("query")); String inputwidth=input.getCssValue("width"); Assert.assertEquals("529", inputwidth);
4、隐式等待
driver.manage().timeouts().implicitlyWait(10,TimeUnit.SECONDS);
5、常用显式等待
package cn.gloryroad;
import org.testng.annotations.Test;
import java.io.File;
import org.testng.annotations.BeforeMethod;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.Assert;
import org.testng.annotations.AfterMethod;
import org.openqa.selenium.support.ui.ExpectedCondition;
public class ApiTest2 {
public WebDriver driver;
String baseUrl;
@Test
public void operateCheckBox() {
File file = new File("D:\workspace\WebDriver-API\src\wait.html");
String filepath=file.getAbsolutePath();
System.out.printf("now accesss %s
", filepath);
driver.get(filepath);
WebElement myDynamicElement = (new WebDriverWait(driver, 10))
.until(new ExpectedCondition <WebElement>(){
@Override
public WebElement apply (WebDriver d) {
return d.findElement(By.xpath("//*[@type='text']"));
}
});
Assert.assertEquals("今年夏天西瓜相当甜!", myDynamicElement.getAttribute("value"));
}
@BeforeMethod
public void beforeMethod() {
System.setProperty("webdriver.chrome.driver", "C:\chromedriver\chromedriver.exe");
driver =new ChromeDriver();
}
@AfterMethod
public void afterMethod() {
try{
Thread.sleep(3000);
}catch(InterruptedException e){
e.printStackTrace();
}
driver.quit();
}
}