十一、双击某个元素
被测试网页的html源码:
1 <html> 2 <head> 3 <meta charset="UTF-8"> 4 </head> 5 <body> 6 <input type="text" id="inputBox" 7 ondblclick="javascript:this.style.background=‘red‘">请双击</input> 8 </body> 9 </html>
Java语言版本的API实例代码:
1 package test; 2 3 import org.testng.annotations.Test; 4 5 import org.testng.annotations.BeforeMethod; 6 7 import java.io.File; 8 9 import org.openqa.selenium.By; 10 import org.openqa.selenium.WebDriver; 11 import org.openqa.selenium.WebElement; 12 import org.openqa.selenium.chrome.ChromeDriver; 13 import org.openqa.selenium.interactions.Actions; 14 import org.testng.annotations.AfterMethod; 15 16 public class ChormeOpen { 17 WebDriver driver; 18 @Test 19 public void opentest() { 20 File file = new File(""); 21 System.out.println(file.getAbsolutePath()); 22 String url = file.getAbsolutePath() + "/html/" + "file3.html"; 23 driver.get(url); 24 System.out.println(driver.getCurrentUrl()); 25 // 26 WebElement inputBox = driver.findElement(By.id("inputBox")); 27 //声明Action对象 28 Actions builder = new Actions(driver); 29 //双击 30 builder.doubleClick(inputBox).build().perform(); 31 try { 32 Thread.sleep(3000); 33 } catch (InterruptedException e) { 34 // TODO Auto-generated catch block 35 e.printStackTrace(); 36 } 37 } 38 @BeforeMethod 39 public void beforeMethod() { 40 System.setProperty("webdriver.chrome.driver", "F:\\selenium\\chromedriver.exe"); 41 driver = new ChromeDriver(); 42 } 43 44 @AfterMethod 45 public void afterMethod() { 46 driver.quit(); 47 } 48 49 }
十二、操作单选下拉列表
被测试网页的html源码:
1 <html> 2 <head> 3 <meta charset="UTF-8"> 4 </head> 5 <body> 6 <select name="fruit" size="1"> 7 <option id="peach" value="taozi">桃子</option> 8 <option id="watermelon" value="xigua">西瓜</option> 9 <option id="orange" value="juzi">橘子</option> 10 <option id="kiwifruit" value="mihoutao">猕猴桃</option> 11 <option id="maybush" value="shanzha">山楂</option> 12 <option id="litchi" value="lizhi">荔枝</option> 13 </select> 14 </body> 15 </html>
Java语言版本的API实例代码:
1 package test; 2 3 import org.testng.annotations.Test; 4 5 import org.testng.annotations.BeforeMethod; 6 7 import java.io.File; 8 9 import org.openqa.selenium.By; 10 import org.openqa.selenium.WebDriver; 11 import org.openqa.selenium.WebElement; 12 import org.openqa.selenium.chrome.ChromeDriver; 13 import org.openqa.selenium.interactions.Actions; 14 import org.openqa.selenium.support.ui.Select; 15 import org.testng.Assert; 16 import org.testng.annotations.AfterMethod; 17 18 public class ChormeOpen { 19 WebDriver driver; 20 @Test 21 public void opentest() { 22 File file = new File(""); 23 System.out.println(file.getAbsolutePath()); 24 String url = file.getAbsolutePath() + "/html/" + "file4.html"; 25 driver.get(url); 26 System.out.println(driver.getCurrentUrl()); 27 // 28 WebElement fruit = driver.findElement(By.name("fruit")); 29 Select droplist = new Select(fruit); 30 //根据Index,下标从0开始 31 droplist.selectByIndex(3); 32 Assert.assertEquals("猕猴桃", droplist.getFirstSelectedOption().getText()); 33 //根据value属性值 34 droplist.selectByValue("shanzha"); 35 Assert.assertEquals("山楂", droplist.getFirstSelectedOption().getText()); 36 //通过显示的文字 37 droplist.selectByVisibleText("荔枝"); 38 Assert.assertEquals("荔枝", droplist.getFirstSelectedOption().getText()); 39 try { 40 Thread.sleep(3000); 41 } catch (InterruptedException e) { 42 // TODO Auto-generated catch block 43 e.printStackTrace(); 44 } 45 } 46 @BeforeMethod 47 public void beforeMethod() { 48 System.setProperty("webdriver.chrome.driver", "F:\\selenium\\chromedriver.exe"); 49 driver = new ChromeDriver(); 50 } 51 52 @AfterMethod 53 public void afterMethod() { 54 driver.quit(); 55 } 56 57 }
十三、检查单选列表的选项文字是否符合期望
被测试网页的html源码:
1 <html> 2 <head> 3 <meta charset="UTF-8"> 4 </head> 5 <body> 6 <select name="fruit" size="1"> 7 <option id="peach" value="taozi">桃子</option> 8 <option id="watermelon" value="xigua">西瓜</option> 9 <option id="orange" value="juzi">橘子</option> 10 <option id="kiwifruit" value="mihoutao">猕猴桃</option> 11 <option id="maybush" value="shanzha">山楂</option> 12 <option id="litchi" value="lizhi">荔枝</option> 13 </select> 14 </body> 15 </html>
Java语言版本的API实例代码:
1 package test; 2 3 import org.testng.annotations.Test; 4 5 import org.testng.annotations.BeforeMethod; 6 7 import java.io.File; 8 import java.util.ArrayList; 9 import java.util.Arrays; 10 import java.util.List; 11 12 import org.openqa.selenium.By; 13 import org.openqa.selenium.WebDriver; 14 import org.openqa.selenium.WebElement; 15 import org.openqa.selenium.chrome.ChromeDriver; 16 import org.openqa.selenium.interactions.Actions; 17 import org.openqa.selenium.support.ui.Select; 18 import org.testng.Assert; 19 import org.testng.annotations.AfterMethod; 20 21 public class ChormeOpen { 22 WebDriver driver; 23 @Test 24 public void opentest() { 25 File file = new File(""); 26 System.out.println(file.getAbsolutePath()); 27 String url = file.getAbsolutePath() + "/html/" + "file4.html"; 28 driver.get(url); 29 System.out.println(driver.getCurrentUrl()); 30 // 31 WebElement fruit = driver.findElement(By.name("fruit")); 32 Select droplist = new Select(fruit); 33 // 34 List<String> exp_options = Arrays.asList((new String[]{"桃子","西瓜","橘子","猕猴桃","山楂","荔枝"})); 35 List<String> act_option = new ArrayList<String>(); 36 for(WebElement option:droplist.getOptions()){ 37 act_option.add(option.getText()); 38 } 39 //断言 40 Assert.assertEquals(exp_options.toArray(), act_option.toArray()); 41 try { 42 Thread.sleep(3000); 43 } catch (InterruptedException e) { 44 // TODO Auto-generated catch block 45 e.printStackTrace(); 46 } 47 } 48 @BeforeMethod 49 public void beforeMethod() { 50 System.setProperty("webdriver.chrome.driver", "F:\\selenium\\chromedriver.exe"); 51 driver = new ChromeDriver(); 52 } 53 54 @AfterMethod 55 public void afterMethod() { 56 driver.quit(); 57 } 58 59 }
十四、操作多选的选择列表
被测试网页的html源码:
1 <html> 2 <head> 3 <meta charset="UTF-8"> 4 </head> 5 <body> 6 <select name="fruit" size="6" multiple=true> 7 <option id="peach" value="taozi">桃子</option> 8 <option id="watermelon" value="xigua">西瓜</option> 9 <option id="orange" value="juzi">橘子</option> 10 <option id="kiwifruit" value="mihoutao">猕猴桃</option> 11 <option id="maybush" value="shanzha">山楂</option> 12 <option id="litchi" value="lizhi">荔枝</option> 13 </select> 14 </body> 15 </html>
Java语言版本的API实例代码:
1 package test; 2 3 import org.testng.annotations.Test; 4 5 import org.testng.annotations.BeforeMethod; 6 7 import java.io.File; 8 9 import org.openqa.selenium.By; 10 import org.openqa.selenium.WebDriver; 11 import org.openqa.selenium.WebElement; 12 import org.openqa.selenium.chrome.ChromeDriver; 13 import org.openqa.selenium.support.ui.Select; 14 import org.testng.annotations.AfterMethod; 15 16 public class ChormeOpen { 17 WebDriver driver; 18 @Test 19 public void opentest() { 20 File file = new File(""); 21 System.out.println(file.getAbsolutePath()); 22 String url = file.getAbsolutePath() + "/html/" + "file5.html"; 23 driver.get(url); 24 System.out.println(driver.getCurrentUrl()); 25 // 26 WebElement fruit = driver.findElement(By.name("fruit")); 27 Select droplist = new Select(fruit); 28 //选择 29 droplist.selectByIndex(3); 30 droplist.selectByValue("shanzha"); 31 droplist.selectByVisibleText("桃子"); 32 droplist.deselectAll();//取消全部选择 33 34 //再次选择 35 droplist.selectByIndex(3); 36 droplist.selectByValue("shanzha"); 37 droplist.selectByVisibleText("桃子"); 38 39 //逐个取消 40 droplist.deselectByIndex(3); 41 droplist.deselectByValue("shanzha"); 42 droplist.deselectByVisibleText("桃子"); 43 44 try { 45 Thread.sleep(3000); 46 } catch (InterruptedException e) { 47 // TODO Auto-generated catch block 48 e.printStackTrace(); 49 } 50 } 51 @BeforeMethod 52 public void beforeMethod() { 53 System.setProperty("webdriver.chrome.driver", "F:\\selenium\\chromedriver.exe"); 54 driver = new ChromeDriver(); 55 } 56 57 @AfterMethod 58 public void afterMethod() { 59 driver.quit(); 60 } 61 62 }
原文:http://www.cnblogs.com/successcai/p/6661708.html