webDriver(2)API

1、打开浏览器访问指定链接

1
2
3
4
5
6
7
8
@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/TrainingSystem/index");
driver.navigate().to("http://192.168.100.2:8080/TrainingSystem/index");
}

2、页面元素定位

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
public void findElement(){
driver.navigate().to("");
//通过id定位元素
WebElement element = driver.findElement(By.id(""));
//通过name定位元素
WebElement element2 = driver.findElement(By.name(""));
//通过class定位元素
WebElement element3 = driver.findElement(By.className(""));
//页面可能有多个class元素,可获取所有符合条件的元素,再进一步定位
WebElement element4 = driver.findElements(By.className("")).get(0);
//通过xpath定位元素
WebElement element5 = driver.findElement(By.xpath(""));
//通过超链接定位元素
WebElement element6 = driver.findElement(By.linkText(""));
}

3、文本框文本输入

1
2
3
4
5
WebElement input = driver.findElement(By.name("username"));
//清空文本框的值
input.clear();
//在文本输入框输入值
input.sendKeys("12345");

4、单击、双击

1
2
3
4
5
6
7
8

//通过name获取链接
WebElement link = driver.findElement(By.id("submit"));
//单击
link.click();
//双击
Actions builder = new Actions(driver);
builder.doubleClick(link).build().perform();

5、浏览器最大化

1
driver.manage().window().maximize();

6、刷新、后退、前进

1
2
3
4
5
6
7
8
9
10
driver.navigate().to("http://192.168.100.6:8099/tasklist_unfinished/");
driver.navigate().to("http://192.168.100.2:8080/TrainingSystem/index");
//刷新
driver.navigate().refresh();
Thread.sleep(2000);
//回退
driver.navigate().back();
Thread.sleep(2000);
//前进
driver.navigate().forward();

7、操作下拉列表

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
driver.navigate().to("");
//获取下拉列表
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、操作单选框

1
2
3
4
5
6
WebElement radio = driver.findElement(By.id(""));
//单选框是否选中
if(!radio.isSelected()){
//选中单选框
radio.click();
}

9、操作复选框

1
2
3
4
5
6
7
8
9
10
WebElement box = driver.findElement(By.xpath(""));
Thread.sleep(2000);
//复选选框是否选中
if(!box.isSelected()){
//选中复选框
box.click();
Thread.sleep(2000);
//再次点击则取消复选框选中状态
box.click();
}

10、截屏

1
2
3
driver.navigate().to("http://192.168.100.2:8080/TrainingSystem");
File screen = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(screen, new File("D://tmp.png"));

11、执行js脚本

1
2
3
4
5
driver.navigate().to("http://192.168.100.2:8080/TrainingSystem");
//执行javascript
JavascriptExecutor js=(JavascriptExecutor) driver;
String title = (String) js.executeScript("var link=document.getElementById('lnkDownList');return link.href;");
System.out.println(title);

12、拖拽页面元素

1
2
3
4
WebElement link = driver.findElement(By.name(""));
Actions builder = new Actions(driver);
//向右向下拖拽10个像素
builder.dragAndDropBy(link, 10, 10).build().perform();

13、模拟键盘

1
2
3
4
5
6
7
8
9
10
11
12
driver.navigate().to("http://192.168.100.2:8080/TrainingSystem/");
WebElement link = driver.findElement(By.id("username"));
Actions builder = new Actions(driver);
//按下ctrl键
builder.keyDown(Keys.CONTROL);
//释放ctrl键
builder.keyUp(Keys.CONTROL);
//按下SHIFT键
builder.keyDown(Keys.SHIFT);
builder.keyUp(Keys.SHIFT);
//输入大写ABCDEF
builder.keyDown(link, Keys.SHIFT).sendKeys("abcdef").perform();

14、模拟鼠标右击

1
2
3
4
5
driver.navigate().to("http://192.168.100.2:8080/TrainingSystem/");
WebElement link = driver.findElement(By.id("username"));
Actions builder = new Actions(driver);
//鼠标右击
builder.contextClick(link).build().perform();

15、鼠标悬停

1
2
3
4
driver.navigate().to("");
WebElement link = driver.findElement(By.id(""));
Actions builder = new Actions(driver);
builder.moveToElement(link).build().perform();

16、鼠标单击左键和释放

1
2
3
4
5
6
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);
>