二十一、模拟鼠标右键事件
被测试网页的网址:
http://www.sogou.com
Java语言版本的API实例代码:
1 package test; 2 3 import org.testng.annotations.Test; 4 5 import org.testng.annotations.BeforeMethod; 6 import org.openqa.selenium.By; 7 import org.openqa.selenium.WebDriver; 8 import org.openqa.selenium.chrome.ChromeDriver; 9 import org.openqa.selenium.interactions.Actions; 10 import org.testng.annotations.AfterMethod; 11 12 public class ChormeOpen { 13 WebDriver driver; 14 String url = "http://www.sogou.com"; 15 16 @Test 17 public void opentest() { 18 driver.get(url); 19 try { 20 Thread.sleep(3000); 21 } catch (InterruptedException e) { 22 // TODO Auto-generated catch block 23 e.printStackTrace(); 24 } 25 Actions action = new Actions(driver); 26 action.contextClick(driver.findElement(By.id("query"))).perform(); 27 try { 28 Thread.sleep(3000); 29 } catch (InterruptedException e) { 30 // TODO Auto-generated catch block 31 e.printStackTrace(); 32 } 33 } 34 35 @BeforeMethod 36 public void beforeMethod() { 37 System.setProperty("webdriver.chrome.driver", "F:\\selenium\\chromedriver.exe"); 38 driver = new ChromeDriver(); 39 } 40 41 @AfterMethod 42 public void afterMethod() { 43 driver.quit(); 44 } 45 46 }
二十二、在指定元素上方进行鼠标悬浮
被测试网页的HTML源码:
1 <html> 2 <head> 3 <meta charset="UTF-8" content="text/html"> 4 <script type="text/javascript"> 5 function showNone(){ 6 document.getElementById(‘div1‘).style.display = "none"; 7 } 8 function showBlock(){ 9 document.getElementById(‘div1‘).style.display = "block"; 10 } 11 </script> 12 <style type="text/css"> 13 #div1{ 14 position:absolute; 15 width:200px; 16 height:125px; 17 z-index:1; 18 left:28px; 19 top:34px; 20 background-color:#0033CC; 21 } 22 </style> 23 </head> 24 <body onload="showNone()"> 25 <div id="div1"></div> 26 <a onmouseover="showBlock()" id="link1">鼠标指过来</a> 27 <a onmouseover="showNone()" id="link2">鼠标指过来</a> 28 </body> 29 </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 19 @Test 20 public void opentest() { 21 File file = new File(""); 22 String url = file.getAbsolutePath() + "/html/" + "file9.html"; 23 driver.get(url); 24 WebElement link1 = driver.findElement(By.xpath("//a[@id=‘link1‘]")); 25 WebElement link2 = driver.findElement(By.xpath("//a[@id=‘link2‘]")); 26 Actions action = new Actions(driver); 27 action.moveToElement(link1).perform(); 28 try { 29 Thread.sleep(3000); 30 } catch (InterruptedException e) { 31 // TODO Auto-generated catch block 32 e.printStackTrace(); 33 } 34 action.moveToElement(link2).perform(); 35 try { 36 Thread.sleep(3000); 37 } catch (InterruptedException e) { 38 // TODO Auto-generated catch block 39 e.printStackTrace(); 40 } 41 } 42 43 @BeforeMethod 44 public void beforeMethod() { 45 System.setProperty("webdriver.chrome.driver", "F:\\selenium\\chromedriver.exe"); 46 driver = new ChromeDriver(); 47 } 48 49 @AfterMethod 50 public void afterMethod() { 51 driver.quit(); 52 } 53 54 }
二十三、在指定元素上进行鼠标单击左键和释放的操作
被测试网页的HTML源码:
1 <html> 2 <head> 3 <meta charset="UTF-8" content="text/html"> 4 <script type="text/javascript"> 5 function mouseDownFun(){ 6 document.getElementById(‘div1‘).innerHTML += ‘鼠标左键被按下<br/>‘; 7 } 8 function mouseUpFun(){ 9 document.getElementById(‘div1‘).innerHTML += ‘已经被按下的鼠标左键被释放抬起<br/>‘; 10 } 11 function clickFun(){ 12 document.getElementById(‘div1‘).innerHTML += ‘单击动作发生<br/>‘; 13 } 14 </script> 15 16 </head> 17 <body> 18 <div id="div1" onmousedown="mouseDownFun();" onmouseup="mouseUpFun();" onclick="clickFun();" 19 style="background: #CCC;border: 3px solid #999;width: 200px;height: 200px;padding: 10px"></div> 20 <input style="margin-top:10px;" type="button" onclick="document.getElementById(‘div1‘).innerHTML=‘‘;" value="清除信息"> 21 </body> 22 </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 19 @Test 20 public void opentest() { 21 File file = new File(""); 22 String url = file.getAbsolutePath() + "/html/" + "file10.html"; 23 driver.get(url); 24 WebElement div = driver.findElement(By.xpath("//div[@id=‘div1‘]")); 25 Actions action = new Actions(driver); 26 action.clickAndHold(div).perform();//单击不释放 27 try { 28 Thread.sleep(3000); 29 } catch (InterruptedException e) { 30 // TODO Auto-generated catch block 31 e.printStackTrace(); 32 } 33 action.release(div).perform();//释放 34 try { 35 Thread.sleep(3000); 36 } catch (InterruptedException e) { 37 // TODO Auto-generated catch block 38 e.printStackTrace(); 39 } 40 } 41 42 @BeforeMethod 43 public void beforeMethod() { 44 System.setProperty("webdriver.chrome.driver", "F:\\selenium\\chromedriver.exe"); 45 driver = new ChromeDriver(); 46 } 47 48 @AfterMethod 49 public void afterMethod() { 50 driver.quit(); 51 } 52 53 }
二十四、查看页面元素的属性
被测试网页的网址:
http://www.baidu.com
Java语言版本的API实例代码:
1 package test; 2 3 import org.testng.annotations.Test; 4 5 import org.testng.annotations.BeforeMethod; 6 7 import org.openqa.selenium.By; 8 import org.openqa.selenium.WebDriver; 9 import org.openqa.selenium.WebElement; 10 import org.openqa.selenium.chrome.ChromeDriver; 11 import org.testng.Assert; 12 import org.testng.annotations.AfterMethod; 13 14 public class ChormeOpen { 15 WebDriver driver; 16 String url = "http://www.baidu.com"; 17 @Test 18 public void opentest() { 19 driver.get(url); 20 WebElement input = driver.findElement(By.id("kw")); 21 input.sendKeys("百度一下"); 22 // 23 try { 24 Thread.sleep(3000); 25 } catch (InterruptedException e) { 26 // TODO Auto-generated catch block 27 e.printStackTrace(); 28 } 29 String text = input.getAttribute("value"); 30 Assert.assertEquals(text, "百度一下"); 31 } 32 33 @BeforeMethod 34 public void beforeMethod() { 35 System.setProperty("webdriver.chrome.driver", "F:\\selenium\\chromedriver.exe"); 36 driver = new ChromeDriver(); 37 } 38 39 @AfterMethod 40 public void afterMethod() { 41 driver.quit(); 42 } 43 44 }
二十五、获取页面元素的CSS属性值
被测试网页的网址:
http://www.baidu.com
Java语言版本的API实例代码:
1 package test; 2 3 import org.testng.annotations.Test; 4 5 import org.testng.annotations.BeforeMethod; 6 7 import org.openqa.selenium.By; 8 import org.openqa.selenium.WebDriver; 9 import org.openqa.selenium.WebElement; 10 import org.openqa.selenium.chrome.ChromeDriver; 11 import org.testng.Assert; 12 import org.testng.annotations.AfterMethod; 13 14 public class ChormeOpen { 15 WebDriver driver; 16 String url = "http://www.baidu.com"; 17 @Test 18 public void opentest() { 19 driver.get(url); 20 WebElement input = driver.findElement(By.id("kw")); 21 String inputWidth = input.getCssValue("width"); 22 System.out.println(inputWidth); 23 try { 24 Thread.sleep(3000); 25 } catch (InterruptedException e) { 26 // TODO Auto-generated catch block 27 e.printStackTrace(); 28 } 29 } 30 31 @BeforeMethod 32 public void beforeMethod() { 33 System.setProperty("webdriver.chrome.driver", "F:\\selenium\\chromedriver.exe"); 34 driver = new ChromeDriver(); 35 } 36 37 @AfterMethod 38 public void afterMethod() { 39 driver.quit(); 40 } 41 42 }
二十六、隐匿等待
被测试网页的网址:
http://www.baidu.com
Java语言版本的API实例代码:
1 package test; 2 3 import org.testng.annotations.Test; 4 5 import org.testng.annotations.BeforeMethod; 6 7 import java.util.concurrent.TimeUnit; 8 9 import org.openqa.selenium.By; 10 import org.openqa.selenium.NoSuchElementException; 11 import org.openqa.selenium.WebDriver; 12 import org.openqa.selenium.WebElement; 13 import org.openqa.selenium.chrome.ChromeDriver; 14 import org.testng.Assert; 15 import org.testng.annotations.AfterMethod; 16 17 public class ChormeOpen { 18 WebDriver driver; 19 String url = "http://www.baidu.com"; 20 @Test 21 public void opentest() { 22 driver.get(url); 23 //隐式等待 24 driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS); 25 try { 26 WebElement input = driver.findElement(By.id("kw")); 27 WebElement button = driver.findElement(By.id("su")); 28 input.sendKeys("三生三世"); 29 button.click(); 30 } catch (NoSuchElementException e) { 31 // TODO: handle exception 32 Assert.fail("没有找到元素"); 33 e.printStackTrace(); 34 } 35 try { 36 Thread.sleep(3000); 37 } catch (InterruptedException e) { 38 // TODO Auto-generated catch block 39 e.printStackTrace(); 40 } 41 } 42 43 @BeforeMethod 44 public void beforeMethod() { 45 System.setProperty("webdriver.chrome.driver", "F:\\selenium\\chromedriver.exe"); 46 driver = new ChromeDriver(); 47 } 48 49 @AfterMethod 50 public void afterMethod() { 51 driver.quit(); 52 } 53 54 }
二十七、常用的显式等待
显式等待比隐式等待更节约测试脚本执行的时间,推荐尽量使用显式等待方式来判断页面元素是否存在。使用ExpectedConditions类中自带的方法,可以进行显式等待的判断。显式等待可以自定义等待的条件,用于更加复杂的页面元素状态判断。常用的显式等待条件如下表所示:
等待的条件 | WebDriver方法 |
页面元素是否在页面上可用(enabled) | elementToBeClickable(By locator) |
页面元素处于被选中状态 | elementToBeSelected(WebElement element) |
页面元素在页面中存在 | presenceOfElementLocated(By locator) |
在页面元素中是否包含特定的文本 | textToBePresentInElement(By locator) |
页面元素值 | textToBePresentInElementValue(By locator,java.lang.String text) |
标题(title) | titleContains(java.lang.String title) |
只有满足显式等待的条件要求,测试代码才会继续向后执行后续的测试逻辑。当显式等待条件未被满足的时候,在设定的最大显式等待时间阈值内,会停在当前代码位置进行等待,直到设定的等待条件被满足,才能继续执行后续的测试逻辑。如果超过设定的最大显式等待时间阈值,则测试程序会抛出异常,测试用例被认为执行失败。
被测试网页的HTML源码:
1 <html> 2 <head> 3 <meta charset="UTF-8" content="text/html"> 4 <title>你喜欢的水果</title> 5 </head> 6 <body> 7 <p>请选择你爱吃的水果</p> 8 <br> 9 <select name="fruit"> 10 <option id="peach" value="taozi">桃子</option> 11 <option id="watermelon" value="xigua">西瓜</option> 12 </select> 13 <br> 14 <input type="checkbox">是否喜欢吃水果?</input> 15 <br><br> 16 <input type="text" id="text" value="今年夏天西瓜相当甜!">文本框</input> 17 </body> 18 </html>
Java语言版本的API实例代码:
原文:http://www.cnblogs.com/successcai/p/6662258.html