二十一、模拟鼠标右键事件
被测试网页的网址:
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; 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实例代码:
1 package test; 2 3 import org.testng.annotations.Test; 4 5 import org.testng.annotations.BeforeMethod; 6 import java.io.File; 7 8 import org.openqa.selenium.By; 9 import org.openqa.selenium.WebDriver; 10 import org.openqa.selenium.WebElement; 11 import org.openqa.selenium.chrome.ChromeDriver; 12 import org.openqa.selenium.support.ui.ExpectedConditions; 13 import org.openqa.selenium.support.ui.WebDriverWait; 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 String url = file.getAbsolutePath() + "/html/" + "file11.html"; 22 driver.get(url); 23 WebDriverWait wait = new WebDriverWait(driver, 10); 24 wait.until(ExpectedConditions.titleContains("水果"));// 25 26 // 27 WebElement select = driver.findElement(By.xpath("//option[@id='peach']")); 28 wait.until(ExpectedConditions.elementToBeSelected(select)); 29 30 // 31 wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//input[@type='checkbox']"))); 32 33 // 34 WebElement p = driver.findElement(By.xpath("//p")); 35 wait.until(ExpectedConditions.textToBePresentInElement(p, "爱吃的水果")); 36 37 try { 38 Thread.sleep(3000); 39 } catch (InterruptedException e) { 40 // TODO Auto-generated catch block 41 e.printStackTrace(); 42 } 43 } 44 45 @BeforeMethod 46 public void beforeMethod() { 47 System.setProperty("webdriver.chrome.driver", "F:\selenium\chromedriver.exe"); 48 driver = new ChromeDriver(); 49 } 50 51 @AfterMethod 52 public void afterMethod() { 53 driver.quit(); 54 } 55 56 }
二十八、自定义显式等待
被测试网页的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实例代码:
1 package test; 2 3 import org.testng.annotations.Test; 4 5 import org.testng.annotations.BeforeMethod; 6 import java.io.File; 7 8 import org.openqa.selenium.By; 9 import org.openqa.selenium.NoSuchElementException; 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.ExpectedCondition; 14 import org.openqa.selenium.support.ui.WebDriverWait; 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 String url = file.getAbsolutePath() + "/html/" + "file11.html"; 24 driver.get(url); 25 try { 26 WebDriverWait wait = new WebDriverWait(driver, 10); 27 WebElement textInputbox = wait.until(new ExpectedCondition<WebElement>() { 28 29 @Override 30 public WebElement apply(WebDriver d) { 31 // TODO Auto-generated method stub 32 return d.findElement(By.xpath("//*[@type='text']")); 33 } 34 }); 35 Assert.assertEquals("今年夏天西瓜相当甜!", textInputbox.getAttribute("value")); 36 37 // 38 Boolean containTextFlag = wait.until(new ExpectedCondition<Boolean>() { 39 40 @Override 41 public Boolean apply(WebDriver d) { 42 // TODO Auto-generated method stub 43 return d.findElement(By.xpath("//p")).getText().contains("爱吃"); 44 } 45 }); 46 Assert.assertTrue(containTextFlag); 47 48 // 49 Boolean inputTextVisibleFlag = wait.until(new ExpectedCondition<Boolean>() { 50 51 @Override 52 public Boolean apply(WebDriver d) { 53 // TODO Auto-generated method stub 54 return d.findElement(By.xpath("//*[@type='text']")).isDisplayed(); 55 } 56 }); 57 Assert.assertTrue(inputTextVisibleFlag); 58 } catch (NoSuchElementException e) { 59 // TODO: handle exception 60 } 61 62 try { 63 Thread.sleep(3000); 64 } catch (InterruptedException e) { 65 // TODO Auto-generated catch block 66 e.printStackTrace(); 67 } 68 } 69 70 @BeforeMethod 71 public void beforeMethod() { 72 System.setProperty("webdriver.chrome.driver", "F:\selenium\chromedriver.exe"); 73 driver = new ChromeDriver(); 74 } 75 76 @AfterMethod 77 public void afterMethod() { 78 driver.quit(); 79 } 80 81 }
二十九、判断页面元素是否存在
被测试网页的网址:
http://www.sogou.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.NoSuchElementException; 9 import org.openqa.selenium.WebDriver; 10 import org.openqa.selenium.WebElement; 11 import org.openqa.selenium.chrome.ChromeDriver; 12 import org.testng.Assert; 13 import org.testng.annotations.AfterMethod; 14 15 public class ChormeOpen { 16 WebDriver driver; 17 String url = "http://www.sogou.com"; 18 19 private Boolean IsElementPresent(By by){ 20 try { 21 driver.findElement(by); 22 return true; 23 } catch (NoSuchElementException e) { 24 // TODO: handle exception 25 return false; 26 } 27 } 28 @Test 29 public void opentest() { 30 driver.get(url); 31 if(IsElementPresent(By.id("query"))){ 32 WebElement searchInputbox = driver.findElement(By.id("query")); 33 if(searchInputbox.isEnabled()==true){ 34 searchInputbox.sendKeys("sogou找到了"); 35 } 36 }else{ 37 Assert.fail("元素未被找到"); 38 } 39 try { 40 Thread.sleep(3000); 41 } catch (InterruptedException e) { 42 // TODO Auto-generated catch block 43 e.printStackTrace(); 44 } 45 } 46 47 @BeforeMethod 48 public void beforeMethod() { 49 System.setProperty("webdriver.chrome.driver", "F:\selenium\chromedriver.exe"); 50 driver = new ChromeDriver(); 51 } 52 53 @AfterMethod 54 public void afterMethod() { 55 driver.quit(); 56 } 57 58 }
三十、使用Title属性识别和操作新弹出的浏览器窗口
被测试网页的HTML源码:
1 <html> 2 <head> 3 <meta charset="UTF-8" content="text/html"> 4 <title>你喜欢的水果</title> 5 </head> 6 <body> 7 <p id="p1">你爱吃的水果么?</p> 8 <br><br> 9 <a href="http://www.sogou.com" target="_blank">sogou搜索</a> 10 </body> 11 </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.NoSuchWindowException; 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 20 @Test 21 public void opentest() { 22 File file = new File(""); 23 String url = file.getAbsolutePath() + "/html/" + "file12.html"; 24 driver.get(url); 25 String parentWindowHandle = driver.getWindowHandle(); 26 WebElement sogouLink = driver.findElement(By.xpath("//a")); 27 sogouLink.click(); 28 java.util.Set<String> allWindowsHandles = driver.getWindowHandles(); 29 if(!allWindowsHandles.isEmpty()){ 30 for(String windowHandle:allWindowsHandles){ 31 try { 32 if(driver.switchTo().window(windowHandle).getTitle().equals("搜狗搜索引擎 - 上网从搜狗开始")){ 33 driver.findElement(By.id("query")).sendKeys("sogou"); 34 } 35 } catch (NoSuchWindowException e) { 36 // TODO: handle exception 37 e.printStackTrace(); 38 } 39 } 40 } 41 driver.switchTo().window(parentWindowHandle); 42 Assert.assertEquals(driver.getTitle(), "你喜欢的水果"); 43 try { 44 Thread.sleep(3000); 45 } catch (InterruptedException e) { 46 // TODO Auto-generated catch block 47 e.printStackTrace(); 48 } 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 }