十一、双击某个元素
被测试网页的html源码:
<html>
<head>
<meta charset="UTF-8">
</head>
<body>
<input type="text" id="inputBox"
ondblclick="javascript:this.style.background='red'">请双击</input>
</body>
</html>
Java语言版本的API实例代码:
package test; import org.testng.annotations.Test; import org.testng.annotations.BeforeMethod; import java.io.File; 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.interactions.Actions;
import org.testng.annotations.AfterMethod; public class ChormeOpen {
WebDriver driver;
@Test
public void opentest() {
File file = new File("");
System.out.println(file.getAbsolutePath());
String url = file.getAbsolutePath() + "/html/" + "file3.html";
driver.get(url);
System.out.println(driver.getCurrentUrl());
//
WebElement inputBox = driver.findElement(By.id("inputBox"));
//声明Action对象
Actions builder = new Actions(driver);
//双击
builder.doubleClick(inputBox).build().perform();
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@BeforeMethod
public void beforeMethod() {
System.setProperty("webdriver.chrome.driver", "F:\\selenium\\chromedriver.exe");
driver = new ChromeDriver();
} @AfterMethod
public void afterMethod() {
driver.quit();
} }
十二、操作单选下拉列表
被测试网页的html源码:
<html>
<head>
<meta charset="UTF-8">
</head>
<body>
<select name="fruit" size="1">
<option id="peach" value="taozi">桃子</option>
<option id="watermelon" value="xigua">西瓜</option>
<option id="orange" value="juzi">橘子</option>
<option id="kiwifruit" value="mihoutao">猕猴桃</option>
<option id="maybush" value="shanzha">山楂</option>
<option id="litchi" value="lizhi">荔枝</option>
</select>
</body>
</html>
Java语言版本的API实例代码:
package test; import org.testng.annotations.Test; import org.testng.annotations.BeforeMethod; import java.io.File; 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.interactions.Actions;
import org.openqa.selenium.support.ui.Select;
import org.testng.Assert;
import org.testng.annotations.AfterMethod; public class ChormeOpen {
WebDriver driver;
@Test
public void opentest() {
File file = new File("");
System.out.println(file.getAbsolutePath());
String url = file.getAbsolutePath() + "/html/" + "file4.html";
driver.get(url);
System.out.println(driver.getCurrentUrl());
//
WebElement fruit = driver.findElement(By.name("fruit"));
Select droplist = new Select(fruit);
//根据Index,下标从0开始
droplist.selectByIndex(3);
Assert.assertEquals("猕猴桃", droplist.getFirstSelectedOption().getText());
//根据value属性值
droplist.selectByValue("shanzha");
Assert.assertEquals("山楂", droplist.getFirstSelectedOption().getText());
//通过显示的文字
droplist.selectByVisibleText("荔枝");
Assert.assertEquals("荔枝", droplist.getFirstSelectedOption().getText());
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@BeforeMethod
public void beforeMethod() {
System.setProperty("webdriver.chrome.driver", "F:\\selenium\\chromedriver.exe");
driver = new ChromeDriver();
} @AfterMethod
public void afterMethod() {
driver.quit();
} }
十三、检查单选列表的选项文字是否符合期望
被测试网页的html源码:
<html>
<head>
<meta charset="UTF-8">
</head>
<body>
<select name="fruit" size="1">
<option id="peach" value="taozi">桃子</option>
<option id="watermelon" value="xigua">西瓜</option>
<option id="orange" value="juzi">橘子</option>
<option id="kiwifruit" value="mihoutao">猕猴桃</option>
<option id="maybush" value="shanzha">山楂</option>
<option id="litchi" value="lizhi">荔枝</option>
</select>
</body>
</html>
Java语言版本的API实例代码:
package test; import org.testng.annotations.Test; import org.testng.annotations.BeforeMethod; import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List; 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.interactions.Actions;
import org.openqa.selenium.support.ui.Select;
import org.testng.Assert;
import org.testng.annotations.AfterMethod; public class ChormeOpen {
WebDriver driver;
@Test
public void opentest() {
File file = new File("");
System.out.println(file.getAbsolutePath());
String url = file.getAbsolutePath() + "/html/" + "file4.html";
driver.get(url);
System.out.println(driver.getCurrentUrl());
//
WebElement fruit = driver.findElement(By.name("fruit"));
Select droplist = new Select(fruit);
//
List<String> exp_options = Arrays.asList((new String[]{"桃子","西瓜","橘子","猕猴桃","山楂","荔枝"}));
List<String> act_option = new ArrayList<String>();
for(WebElement option:droplist.getOptions()){
act_option.add(option.getText());
}
//断言
Assert.assertEquals(exp_options.toArray(), act_option.toArray());
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@BeforeMethod
public void beforeMethod() {
System.setProperty("webdriver.chrome.driver", "F:\\selenium\\chromedriver.exe");
driver = new ChromeDriver();
} @AfterMethod
public void afterMethod() {
driver.quit();
} }
十四、操作多选的选择列表
被测试网页的html源码:
<html>
<head>
<meta charset="UTF-8">
</head>
<body>
<select name="fruit" size="6" multiple=true>
<option id="peach" value="taozi">桃子</option>
<option id="watermelon" value="xigua">西瓜</option>
<option id="orange" value="juzi">橘子</option>
<option id="kiwifruit" value="mihoutao">猕猴桃</option>
<option id="maybush" value="shanzha">山楂</option>
<option id="litchi" value="lizhi">荔枝</option>
</select>
</body>
</html>
Java语言版本的API实例代码:
package test; import org.testng.annotations.Test; import org.testng.annotations.BeforeMethod; import java.io.File; 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.Select;
import org.testng.annotations.AfterMethod; public class ChormeOpen {
WebDriver driver;
@Test
public void opentest() {
File file = new File("");
System.out.println(file.getAbsolutePath());
String url = file.getAbsolutePath() + "/html/" + "file5.html";
driver.get(url);
System.out.println(driver.getCurrentUrl());
//
WebElement fruit = driver.findElement(By.name("fruit"));
Select droplist = new Select(fruit);
//选择
droplist.selectByIndex(3);
droplist.selectByValue("shanzha");
droplist.selectByVisibleText("桃子");
droplist.deselectAll();//取消全部选择 //再次选择
droplist.selectByIndex(3);
droplist.selectByValue("shanzha");
droplist.selectByVisibleText("桃子"); //逐个取消
droplist.deselectByIndex(3);
droplist.deselectByValue("shanzha");
droplist.deselectByVisibleText("桃子"); try {
Thread.sleep(3000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@BeforeMethod
public void beforeMethod() {
System.setProperty("webdriver.chrome.driver", "F:\\selenium\\chromedriver.exe");
driver = new ChromeDriver();
} @AfterMethod
public void afterMethod() {
driver.quit();
} }
十五、操作单选框
被测试网页的html源码:
<html>
<head>
<meta charset="UTF-8">
</head>
<body>
<form>
<input type="radio" name="fruit" value="berry">草莓</input>
<br/>
<input type="radio" name="fruit" value="watermelon">西瓜</input>
<br/>
<input type="radio" name="fruit" value="orange">橙子</input>
</form>
</body>
</html>
Java语言版本的API实例代码:
package test; import org.testng.annotations.Test; import org.testng.annotations.BeforeMethod; import java.io.File;
import java.util.List; import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.Assert;
import org.testng.annotations.AfterMethod; public class ChormeOpen {
WebDriver driver;
@Test
public void opentest() {
File file = new File("");
System.out.println(file.getAbsolutePath());
String url = file.getAbsolutePath() + "/html/" + "file6.html";
driver.get(url);
System.out.println(driver.getCurrentUrl());
//
WebElement radioOption = driver.findElement(By.xpath("//input[@value='berry']"));
if(!radioOption.isSelected()){
radioOption.click();
}
Assert.assertTrue(radioOption.isSelected());
//
List<WebElement> fruits = driver.findElements(By.name("fruit"));
for(WebElement fruit:fruits){
if(fruit.getAttribute("value").equals("watermelon")){
if(!fruit.isSelected()){
fruit.click();
}
Assert.assertTrue(fruit.isSelected());
break;
}
}
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@BeforeMethod
public void beforeMethod() {
System.setProperty("webdriver.chrome.driver", "F:\\selenium\\chromedriver.exe");
driver = new ChromeDriver();
} @AfterMethod
public void afterMethod() {
driver.quit();
} }
十六、操作复选框
被测试网页的html源码:
<html>
<head>
<meta charset="UTF-8">
</head>
<body>
<form>
<input type="checkbox" name="fruit" value="berry">草莓</input>
<br/>
<input type="checkbox" name="fruit" value="watermelon">西瓜</input>
<br/>
<input type="checkbox" name="fruit" value="orange">橙子</input>
</form>
</body>
</html>
Java语言版本的API实例代码:
package test; import org.testng.annotations.Test; import org.testng.annotations.BeforeMethod; import java.io.File;
import java.util.List; import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.Assert;
import org.testng.annotations.AfterMethod; public class ChormeOpen {
WebDriver driver;
@Test
public void opentest() {
File file = new File("");
System.out.println(file.getAbsolutePath());
String url = file.getAbsolutePath() + "/html/" + "file7.html";
driver.get(url);
System.out.println(driver.getCurrentUrl());
//
WebElement orangcheckbox = driver.findElement(By.xpath("//input[@value='orange']"));
if(!orangcheckbox.isSelected()){
orangcheckbox.click();
}
Assert.assertTrue(orangcheckbox.isSelected());
//
List<WebElement> checkboxs = driver.findElements(By.name("fruit"));
for(WebElement checkbox:checkboxs){
checkbox.click();
}
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@BeforeMethod
public void beforeMethod() {
System.setProperty("webdriver.chrome.driver", "F:\\selenium\\chromedriver.exe");
driver = new ChromeDriver();
} @AfterMethod
public void afterMethod() {
driver.quit();
} }
十七、检查页面元素的文本内容是否出现
被测试网页的HTML代码:
<html>
<head>
<meta charset="UTF-8">
</head>
<body>
<p>《三生三世十里桃花》这个电影真的很棒!</p>
<p>主要是杨洋不错!</p>
</body>
</html>
Java语言版本的API实例代码:
package test; import org.testng.annotations.Test; import org.testng.annotations.BeforeMethod; import java.io.File;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.Assert;
import org.testng.annotations.AfterMethod; public class ChormeOpen {
WebDriver driver;
@Test
public void opentest() {
File file = new File("");
System.out.println(file.getAbsolutePath());
String url = file.getAbsolutePath() + "/html/" + "file8.html";
driver.get(url);
System.out.println(driver.getCurrentUrl());
//
WebElement text = driver.findElement(By.xpath("//p[1]"));
String contentText = text.getText();
Assert.assertEquals("《三生三世十里桃花》这个电影真的很棒!", contentText);
Assert.assertTrue(contentText.contains("三生三世"));
Assert.assertTrue(contentText.startsWith("《三"));
Assert.assertTrue(contentText.endsWith("很棒!"));
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@BeforeMethod
public void beforeMethod() {
System.setProperty("webdriver.chrome.driver", "F:\\selenium\\chromedriver.exe");
driver = new ChromeDriver();
} @AfterMethod
public void afterMethod() {
driver.quit();
} }
十八、执行javaScript脚本
被测试网页的网址:
http://baidu.com
Java语言版本的API实例代码:
package test; import org.testng.annotations.Test; import org.testng.annotations.BeforeMethod;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.Assert;
import org.testng.annotations.AfterMethod; public class ChormeOpen {
WebDriver driver;
String url = "http://www.baidu.com";
@Test
public void opentest() {
driver.get(url);
JavascriptExecutor js = (JavascriptExecutor) driver;
String title = (String) js.executeScript("return document.title");
Assert.assertEquals("百度一下,你就知道", title); try {
Thread.sleep(3000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
String searchText = (String) js.executeScript("var button= document.getElementById('su');return button.value");
System.out.println(searchText);
}
@BeforeMethod
public void beforeMethod() {
System.setProperty("webdriver.chrome.driver", "F:\\selenium\\chromedriver.exe");
driver = new ChromeDriver();
} @AfterMethod
public void afterMethod() {
driver.quit();
} }
十九、拖曳页面元素
被测试网页的网址:
http://jqueryui.com/resources/demos/draggable/scroll.html
Java语言版本的API实例代码:
package test; import org.testng.annotations.Test; 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.interactions.Actions;
import org.testng.annotations.AfterMethod; public class ChormeOpen {
WebDriver driver;
String url = "http://jqueryui.com/resources/demos/draggable/scroll.html"; @Test
public void opentest() {
driver.get(url);
WebElement draggable = driver.findElement(By.id("draggable"));
//向下拖动10个像素、共拖动5次
for(int i=0;i<5;i++){
new Actions(driver).dragAndDropBy(draggable, 0, 10).build().perform();
}
//向右拖动10个像素、共拖动5次
for(int i=0;i<5;i++){
new Actions(driver).dragAndDropBy(draggable, 10, 0).build().perform();;
}
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} @BeforeMethod
public void beforeMethod() {
System.setProperty("webdriver.chrome.driver", "F:\\selenium\\chromedriver.exe");
driver = new ChromeDriver();
} @AfterMethod
public void afterMethod() {
driver.quit();
} }
二十、模拟键盘的操作
被测试网页的网址:
http://www.baidu.com
Java语言版本的API实例代码:
package test; import org.testng.annotations.Test; import org.testng.annotations.BeforeMethod;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.interactions.Actions;
import org.testng.annotations.AfterMethod; public class ChormeOpen {
WebDriver driver;
String url = "http://www.baidu.com"; @Test
public void opentest() {
driver.get(url);
Actions action = new Actions(driver);
action.keyDown(Keys.CONTROL);//按下Ctrl键
action.keyDown(Keys.SHIFT);//按下Shift键
action.keyDown(Keys.ALT);//按下Alt键
action.keyUp(Keys.CONTROL);//释放Ctrl键
action.keyUp(Keys.SHIFT);//释放Shift键
action.keyUp(Keys.ALT);//释放ALT键
//模拟键盘在搜索输入框输入大写的字符“ABCD”
action.keyDown(Keys.SHIFT).sendKeys("abcd").perform();
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} @BeforeMethod
public void beforeMethod() {
System.setProperty("webdriver.chrome.driver", "F:\\selenium\\chromedriver.exe");
driver = new ChromeDriver();
} @AfterMethod
public void afterMethod() {
driver.quit();
} }