WebDriver API
1、打开浏览器访问指定链接
@Test
public void toUrl(){
System.setProperty("webdriver.firefox.bin", "C:\\Program Files\\Mozilla Firefox\\firefox.exe");
driver=new FirefoxDriver();
driver.get("http://192.168.100.2:8080");
driver.navigate().to("http://192.168.100.2:8080");
}
2、页面元素定位
public void findElement(){
driver.navigate().to("");
WebElement element = driver.findElement(By.id(""));
WebElement element2 = driver.findElement(By.name(""));
WebElement element3 = driver.findElement(By.className(""));
WebElement element4 = driver.findElements(By.className("")).get(0);
WebElement element5 = driver.findElement(By.xpath(""));
WebElement element6 = driver.findElement(By.linkText(""));
}
3、文本框文本输入
WebElement input = driver.findElement(By.name("username"));
input.clear();
input.sendKeys("12345");
4、单击、双击
WebElement link = driver.findElement(By.id("submit"));
link.click();
Actions builder = new Actions(driver);
builder.doubleClick(link).build().perform();
5、浏览器最大化
driver.manage().window().maximize();
6、刷新、后退、前进
driver.navigate().to("http://192.168.100.6:8099");
driver.navigate().to("http://192.168.100.2:8080");
driver.navigate().refresh();
Thread.sleep(2000);
driver.navigate().back();
Thread.sleep(2000);
driver.navigate().forward();
7、操作下拉列表
@Test
public void select() throws Exception{
driver.navigate().to("http://192.168.100.2:8080");
Select selectList = new Select(driver.findElement(By.id("roleId")));
Thread.sleep(2000);
selectList.selectByIndex(1);
Thread.sleep(2000);
selectList.selectByVisibleText("管理员登录");
Thread.sleep(2000);
selectList.selectByValue("3");
String test = selectList.getFirstSelectedOption().getText();
System.out.println(test);
}
8、操作单选框
WebElement radio = driver.findElement(By.id(""));
if(!radio.isSelected()){
radio.click();
}
9、操作复选框
WebElement box = driver.findElement(By.xpath(""));
Thread.sleep(2000);
if(!box.isSelected()){
box.click();
Thread.sleep(2000);
box.click();
}
10、截屏
driver.navigate().to("http://192.168.100.2:8080");
File screen = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(screen, new File("D://tmp.png"));
11、执行js脚本
driver.navigate().to("http://192.168.100.2:8080");
JavascriptExecutor js=(JavascriptExecutor) driver;
String title = (String) js.executeScript("var link=document.getElementById('lnkDownList');return link.href;");
System.out.println(title);
12、拖拽页面元素
WebElement link = driver.findElement(By.name(""));
Actions builder = new Actions(driver);
builder.dragAndDropBy(link, 10, 10).build().perform();
13、模拟键盘
driver.navigate().to("http://192.168.100.2:8080");
WebElement link = driver.findElement(By.id("username"));
Actions builder = new Actions(driver);
builder.keyDown(Keys.CONTROL);
builder.keyUp(Keys.CONTROL);
builder.keyDown(Keys.SHIFT);
builder.keyUp(Keys.SHIFT);
builder.keyDown(link, Keys.SHIFT).sendKeys("abcdef").perform();
14、模拟鼠标右击
driver.navigate().to("http://192.168.100.2:8080");
WebElement link = driver.findElement(By.id("username"));
Actions builder = new Actions(driver);
builder.contextClick(link).build().perform();
15、鼠标悬停
driver.navigate().to("");
WebElement link = driver.findElement(By.id(""));
Actions builder = new Actions(driver);
builder.moveToElement(link).build().perform();
16、鼠标单击左键和释放
driver.navigate().to("");
WebElement link = driver.findElement(By.id(""));
Actions builder = new Actions(driver);
builder.clickAndHold(link).build().perform();
builder.moveByOffset(10, 10);
builder.release(link);