前兩天分別講述了UI自動化測試基礎以及對頁面元素該如何進行定位,這一篇自然就是對定位到的頁面元素對象進行相應操作啦。
閱讀目錄
1.常用操作元素對象的方法
webdriver中常用的操作元素的方法有如下幾個:
clear : 清除對象的內容
send_keys : 在對象上模擬按鍵輸入, 注意如果是函數需要增加轉義符
click : 單擊對象, 強調對象的獨立性
submit : 提交表單, 要求對象必須是表單, 強調對象必須是表單
代碼如下,僅供參考:
1 package com.ui_auto; 2 3 import org.openqa.selenium.By; 4 import org.openqa.selenium.WebDriver; 5 import org.openqa.selenium.WebElement; 6 import org.openqa.selenium.chrome.ChromeDriver; 7 8 public class yihuqingjiu_test_12306 { 9 10 public static void main(String[] args) throws Exception { 11 //chrom瀏覽器驅動的位置 12 System.setProperty("webdriver.chrome.driver","C:\\Program Files (x86)\\Google\\Chrome\\Application\\chromedriver.exe"); 13 //web驅動指向chrom驅動並創建對象driver 14 WebDriver driver=new ChromeDriver(); 15 //獲取網址 16 driver.get("https://www.baidu.com"); 17 //瀏覽器最大化 18 driver.manage().window().maximize(); 19 //kw是輸入框的id,12306是在輸入框中葯輸入的內容 20 driver.findElement(By.id("kw")).sendKeys("12306"); 21 //su是搜索按鈕的id 22 WebElement btn=driver.findElement(By.id("su")); 23 //點擊事件 24 btn.submit(); 25 //休眠時間 26 Thread.sleep(3000); 27 //關閉頁面 28 driver.close(); 29 } 30 31 }
注意:
2.鼠標事件操作
1 package com.ui.day1; 2 3 import org.openqa.selenium.By; 4 import org.openqa.selenium.Keys; 5 import org.openqa.selenium.WebDriver; 6 import org.openqa.selenium.WebElement; 7 import org.openqa.selenium.chrome.ChromeDriver; 8 import org.openqa.selenium.interactions.Actions; 9 10 public class yihuqingjiu_test_demo3_mouse { 11 12 public static void main(String[] args) throws Exception { 13 System.setProperty("webdriver.chrome.driver", "C:\\Program Files (x86)\\Google\\Chrome\\Application\\chromedriver.exe"); 14 WebDriver driver=new ChromeDriver(); 15 //瀏覽器要加載的url 16 driver.get("https://www.baidu.com"); 17 //窗口最大化 18 driver.manage().window().maximize(); 19 20 //右擊操作 21 // WebElement e=driver.findElement(By.cssSelector("div#lg>img")); 22 // Actions action=new Actions(driver); 23 // action.contextClick(e).perform(); 24 //鼠標移動 25 WebElement move=driver.findElement(By.cssSelector("a[name='tj_briicon']")); 26 Actions action=new Actions(driver); 27 action.moveToElement(move).perform(); 28 Thread.sleep(2000); 29 driver.findElement(By.cssSelector("a[name='tj_mp3']")).click(); 30 driver.close(); 31 32 } 33 34 }
ActionChains用於生成用戶的行為,所有的行為都存儲在action對象,通過perform()執行存儲的行為perform()執行所有ActionChains存儲的行為,perform()同樣也是ActionChains類提供的方法,經常結合在一起使用
3.鍵盤事件操作
在實際的web測試工作中, 需要配合鍵盤按鍵來操作, 對於鍵盤的模擬操作,Actions 類中有提供 keyUp(theKey)、 keyDown(theKey)、 sendKeys(keysToSend)等方法來實現。
在 WebDriver API 中,KeyDown(Keys theKey)、KeyUp(Keys theKey) 方法的參數只能是修飾鍵:Keys.SHIFT、Keys.ALT、Keys.CONTROL, 否者將拋出IllegalArgumentException 異常。其次對於action.keyDown(theKey) 方法的調用,如果沒有顯示的調用 action.keyUp(theKey) 或者 action.sendKeys(Keys.NULL) 來釋放的話, 這個按鍵將一直保持按住狀態。
代碼實現如下,僅供參考:
1 package com.ui.day1; 2 3 import org.openqa.selenium.By; 4 import org.openqa.selenium.Keys; 5 import org.openqa.selenium.WebDriver; 6 import org.openqa.selenium.WebElement; 7 import org.openqa.selenium.chrome.ChromeDriver; 8 import org.openqa.selenium.interactions.Actions; 9 10 public class yihuqingjiu_test_demo3_mouse { 11 12 public static void main(String[] args) throws Exception { 13 System.setProperty("webdriver.chrome.driver", "C:\\Program Files (x86)\\Google\\Chrome\\Application\\chromedriver.exe"); 14 WebDriver driver=new ChromeDriver(); 15 //瀏覽器要加載的url 16 driver.get("https://www.baidu.com"); 17 //窗口最大化 18 driver.manage().window().maximize(); 19 20 //按下tab鍵 21 // WebElement tab=driver.findElement(By.cssSelector("#kw")); 22 // tab.sendKeys(Keys.TAB); 23 //按下退格鍵 24 // WebElement tab1=driver.findElement(By.cssSelector("#kw")); 25 // tab1.sendKeys("12306"); 26 // Thread.sleep(1000); 27 // tab1.sendKeys(Keys.BACK_SPACE); 28 // Thread.sleep(1000); 29 //按下組合鍵 30 // Actions action=new Actions(driver); 31 // WebElement ctr1=driver.findElement(By.cssSelector("#kw")); 32 // action.keyDown(Keys.CONTROL).sendKeys("v").sendKeys(Keys.NULL).keyUp(Keys.CONTROL).perform(); 33 34 //按下ctrl+f4 35 // WebElement ctr1=driver.findElement(By.cssSelector("#kw")); 36 // ctr1.sendKeys("12306"); 37 // Thread.sleep(1000); 38 // Actions action=new Actions(driver); 39 // action.keyDown(Keys.CONTROL).sendKeys(Keys.F4).keyUp(Keys.CONTROL).perform(); 40 //按下ctrl+shift+delete 41 Actions action=new Actions(driver); 42 action.keyDown(Keys.CONTROL).keyDown(Keys.SHIFT).sendKeys(Keys.DELETE).keyUp(Keys.CONTROL).keyUp(Keys.SHIFT).perform(); 43 driver.close(); 44 45 } 46 47 }
4.WebElement接口常用方法
WebElement接口常用方法
getSize(): 返回對象的尺寸
getText(): 獲取對象的文本
get_attribute("屬性名"): 獲取對象的屬性值
isDisplayed(): 用來判斷對象是否可見, 即css的display屬性是否為none
isEnabled(): 判斷對象是否被禁用
isSelected(): 判斷對象是否被選中
getTagName(): 獲取對象標簽名稱
getLocation(): 獲取元素坐標
getCookies()/getCookieNamed(cookie_name): 返回當前會話中的cookies
代碼實現舉例如下,僅供參考:
1 package com.ui.day1; 2 3 import org.openqa.selenium.By; 4 import org.openqa.selenium.WebDriver; 5 import org.openqa.selenium.chrome.ChromeDriver; 6 7 public class yihuqingjiu_test_demo3_WebElement { 8 9 public static void main(String[] args) throws Exception { 10 System.setProperty("webdriver.chrome.driver", "C:\\Program Files (x86)\\Google\\Chrome\\Application\\chromedriver.exe"); 11 WebDriver driver=new ChromeDriver(); 12 //瀏覽器要加載的url 13 driver.get("https://www.baidu.com"); 14 //窗口最大化 15 driver.manage().window().maximize(); 16 17 //返回對象尺寸 18 // int height=driver.findElement(By.cssSelector("#kw")).getSize().height; 19 // int width=driver.findElement(By.cssSelector("#kw")).getSize().width; 20 // System.out.println("height:"+height+"..."+"width:"+width); 21 //獲取對象文本 22 // String text=driver.findElement(By.cssSelector("a[name='tj_trmap']")).getText(); 23 // System.out.println(text); 24 //獲取對象屬性值 25 // String id=driver.findElement(By.cssSelector("#su")).getAttribute("id"); 26 // System.out.println(id); 27 // String value=driver.findElement(By.cssSelector("#su")).getAttribute("value"); 28 // System.out.println(value); 29 //判斷對象是否可見 30 // boolean su =driver.findElement(By.cssSelector("#su")).isDisplayed(); 31 // System.out.println(su); 32 // //判斷對象是否被禁用 33 // boolean su1=driver.findElement(By.cssSelector("#kw")).isEnabled(); 34 // System.out.println(su1); 35 // //判斷對象是否被選中 36 // boolean su2=driver.findElement(By.cssSelector("#su")).isSelected(); 37 // System.out.println(su2); 38 //獲取對象標簽名稱 39 // String input=driver.findElement(By.cssSelector("#kw")).getTagName(); 40 // System.out.println(input); 41 //獲取元素坐標,獲取到的只是左上角的坐標 42 int x=driver.findElement(By.cssSelector("#su")).getLocation().x; 43 System.out.println(x); 44 int y=driver.findElement(By.cssSelector("#su")).getLocation().y; 45 System.out.println(y); 46 String z=driver.findElement(By.cssSelector("#su")).getLocation().toString(); 47 System.out.println(z); 48 driver.close(); 49 50 51 } 52 53 }
5.設置等待時間
為了保證腳本的穩定性, 這就需要引入等待時間, 等待頁面加載元素后再進行操作,不然會出現后者頁面元素定位不到,而導致腳本運行出錯, selenium提供三種等待時間設置方式。
①Thread.sleep(): 固定休眠時間設置,Java的Thread類里提供了休眠方法sleep,導入包后就能使用;需要注意的是:sleep()方法以毫秒為單位。
Thread.sleep(2000);//固定延時2秒
② implicitlyWait():implicitlyWait()方法比sleep()方法智能,sleep()方法只能在一個固定的時間等待,而implicitlyWait()可以在一個時間范圍內等待,稱為隱式等待。
好比如:設置等待時間5s,頁面上的元素3s后出現,只等待3s。不會等待5s隱性的等待其實就相當於設置全局的等待,在定位元素時,對所有元素設置超時時間,但對於頁面跳轉的地方該方法會導致出錯,會拋出異常信息,如下所示:
Exception in thread "main" org.openqa.selenium.StaleElementReferenceException: stale element reference: element is not attached to the page document
這是提醒頁面元素過期,解決辦法,在出錯的上一行代碼加一個固定等待即可。
隱式等待代碼舉例如下,僅供參考:
1 package com.ui.day1; 2 3 import java.util.concurrent.TimeUnit; 4 5 import org.openqa.selenium.By; 6 import org.openqa.selenium.JavascriptExecutor; 7 import org.openqa.selenium.WebDriver; 8 import org.openqa.selenium.WebElement; 9 import org.openqa.selenium.chrome.ChromeDriver; 10 import org.openqa.selenium.support.ui.ExpectedCondition; 11 import org.openqa.selenium.support.ui.WebDriverWait; 12 13 public class yihuqingjiu_wait { 14 15 public static void main(String[] args) throws Exception { 16 System.setProperty("webdriver.chrome.driver", "C:\\Program Files (x86)\\Google\\Chrome\\Application\\chromedriver.exe"); 17 WebDriver driver=new ChromeDriver(); 18 //瀏覽器要加載的url 19 driver.get("https://www.baidu.com/"); 20 //窗口最大化 21 driver.manage().window().maximize(); 22 23 //隱式等待 24 driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS); 25 //定位百度首頁輸入框 26 driver.findElement(By.cssSelector("#kw")).sendKeys("蘇寧易購"); 27 //點位搜索按鈕 28 WebElement su=driver.findElement(By.cssSelector("#su")); 29 su.click(); 30 String cl = driver.findElement(By.cssSelector("#su")).getAttribute("class"); 31 System.out.println(cl); 32 //定位蘇寧易購的官方網址 33 Thread.sleep(2000); 34 WebElement left=driver.findElement(By.cssSelector("div#content_left a:nth-of-type(1)")); 35 left.click(); 36 //高亮代碼 37 ((JavascriptExecutor) driver).executeScript( 38 "arguments[0].style.border='5px solid yellow'", su); 39 ((JavascriptExecutor) driver).executeScript( 40 "arguments[0].style.border='5px solid yellow'", left); 41 42 } 43 44 }
此處的高亮代碼指的是,定位到的元素位置用js加個效果,從而實現高亮,效果圖如下所示:
③WebDriverWait():顯示等待就是明確的要等到某個元素的出現或者是某個元素的可點擊等條件,等不到,就一直等,除非在規定的時間之內都沒找到,那么就跳出Exception。
1 package com.ui.day1; 2 3 import org.openqa.selenium.By; 4 import org.openqa.selenium.JavascriptExecutor; 5 import org.openqa.selenium.WebDriver; 6 import org.openqa.selenium.WebElement; 7 import org.openqa.selenium.chrome.ChromeDriver; 8 import org.openqa.selenium.support.ui.ExpectedCondition; 9 import org.openqa.selenium.support.ui.WebDriverWait; 10 11 public class yihuqingjiu_wait { 12 13 public static void main(String[] args) throws Exception { 14 System.setProperty("webdriver.chrome.driver", "C:\\Program Files (x86)\\Google\\Chrome\\Application\\chromedriver.exe"); 15 WebDriver driver=new ChromeDriver(); 16 //瀏覽器要加載的url 17 driver.get("https://www.baidu.com/"); 18 //窗口最大化 19 driver.manage().window().maximize(); 20 21 //顯示等待 22 //定位百度首頁輸入框 23 driver.findElement(By.cssSelector("#kw")).sendKeys("蘇寧易購"); 24 //點位搜索按鈕 25 WebElement su=driver.findElement(By.cssSelector("#su")); 26 su.click(); 27 WebDriverWait wait = new WebDriverWait(driver, 10); 28 WebElement wt = wait.until(new ExpectedCondition<WebElement>() { 29 public WebElement apply(WebDriver d) { 30 return d.findElement(By.cssSelector("div#content_left a:nth-of-type(1)")); 31 } 32 }); 33 String cl = driver.findElement(By.cssSelector("#su")).getAttribute("class"); 34 System.out.println(cl); 35 ((JavascriptExecutor) driver).executeScript( 36 "arguments[0].style.border='5px solid yellow'", wt); 37 } 38 39 }
顯示等待是使用了一個匿名函數來實現,代碼中,driver會傳給d,d就相當於瀏覽器的驅動,然后再去查找需要的元素,其中只有十秒鍾的時間,時間可更改。
6.驗證信息
通過獲取頁面的title、URL地址,頁面上的標識信息來判斷用例是否執行成功。
代碼試下如下,僅供參考:
1 package com.ui_auto; 2 3 import org.openqa.selenium.By; 4 import org.openqa.selenium.WebDriver; 5 import org.openqa.selenium.WebElement; 6 import org.openqa.selenium.chrome.ChromeDriver; 7 8 public class yihuqingjiu_test_12306 { 9 10 public static void main(String[] args) throws Exception { 11 //chrom瀏覽器驅動的位置 12 System.setProperty("webdriver.chrome.driver","C:\\Program Files (x86)\\Google\\Chrome\\Application\\chromedriver.exe"); 13 //web驅動指向chrom驅動並創建對象driver 14 WebDriver driver=new ChromeDriver(); 15 //獲取網址 16 driver.get("https://www.baidu.com"); 17 //瀏覽器最大化 18 driver.manage().window().maximize(); 19 //kw是輸入框的id,12306是在輸入框中葯輸入的內容 20 driver.findElement(By.id("kw")).sendKeys("12306"); 21 //su是搜索按鈕的id 22 WebElement btn=driver.findElement(By.id("su")); 23 //點擊事件 24 btn.submit(); 25 //休眠時間 26 Thread.sleep(3000); 27 String title = driver.getTitle(); 28 if((title.compareTo("百度一下, 你就知道"))==0) 29 System.out.println("title is correct!!"); 30 else 31 System.out.println("title is error!!"); 32 33 //關閉頁面 34 driver.close(); 35 } 36 37 } 38
7.定位一組對象操作
webdriver使用findElement方法定位一個特定的對象,不過我們有時需定位一組對象, webdriver同樣提供了定位一組元素的方法叫findElements。
//單個元素定位 // driver.findElement(By.cssSelector("#web")).click(); // driver.findElement(By.cssSelector("#training")).click(); // driver.findElement(By.cssSelector("#friend")).click(); // driver.findElement(By.cssSelector("#other")).click(); //單個元素for循環定位 // for(int i=1;i<5;i++) //節點 // driver.findElement(By.cssSelector("div#checkbox > input:nth-of-type("+"i"+")")).click(); //集合 List<WebElement> lt=driver.findElements(By.cssSelector("input[type='checkbox']")); for(int i=0;i<lt.size();i++) lt.get(i).click(); Thread.sleep(2000); //for循環遍歷 for(WebElement e:lt) e.click(); Thread.sleep(2000); //迭代器 Iterator<WebElement> itr=lt.iterator(); while(itr.hasNext()) itr.next().click(); Thread.sleep(2000); driver.quit();
8.層級定位操作
經常會遇到無法直接定位到需要選取的元素, 但是其父元素比較容易定位, 通過定位父元素再遍歷其子元素選擇需要的目標元素,或者需要定位某個元素下所有的子元素。層級定位的思想是先定位父對象,然后再從父對象中精確定位出其我們需要選取的后代元素。
以登錄百度賬號為例,代碼實現如下,僅供參考:
1 package com.ui.day2; 2 3 import java.util.List; 4 import java.util.concurrent.TimeUnit; 5 6 import org.openqa.selenium.By; 7 import org.openqa.selenium.WebDriver; 8 import org.openqa.selenium.WebElement; 9 import org.openqa.selenium.chrome.ChromeDriver; 10 11 public class yihuqingjiu_tese_baidu_login { 12 13 public static void main(String[] args) throws Exception { 14 System.setProperty("webdriver.chrome.driver","C:\\Program Files (x86)\\Google\\Chrome\\Application\\chromedriver.exe"); 15 WebDriver driver=new ChromeDriver(); 16 17 driver.get("https://www.baidu.com"); 18 driver.manage().window().maximize(); 19 //定位登錄按鈕 20 //driver.findElement(By.cssSelector("div#u1 a[name='tj_login']")).click(); 21 //列表定位 22 List<WebElement> list=driver.findElements(By.cssSelector("a[name='tj_login']")); 23 list.get(1).click(); 24 //需要等待時間,不然定位不到 25 Thread.sleep(1000); 26 driver.findElement(By.id("TANGRAM__PSP_10__userNameWrapper")).findElement(By.id("TANGRAM__PSP_10__userName")).sendKeys("223456789@qq.com"); 27 //Thread.sleep(1000); 28 driver.findElement(By.id("TANGRAM__PSP_10__passwordWrapper")).findElement(By.id("TANGRAM__PSP_10__password")).sendKeys("1234567890"); 29 30 } 31 32 } 33
9.定位frame中對象操作
1 package com.ui.day1; 2 3 import org.openqa.selenium.By; 4 import org.openqa.selenium.JavascriptExecutor; 5 import org.openqa.selenium.Keys; 6 import org.openqa.selenium.WebDriver; 7 import org.openqa.selenium.WebElement; 8 import org.openqa.selenium.chrome.ChromeDriver; 9 10 public class yihuqingjiu_test_demo3_chandao { 11 12 public static void main(String[] args) throws Exception { 13 System.setProperty("webdriver.chrome.driver", "C:\\Program Files (x86)\\Google\\Chrome\\Application\\chromedriver.exe"); 14 WebDriver driver=new ChromeDriver(); 15 driver.get("http://127.0.0.1/zentao/my/"); 16 driver.manage().window().maximize(); 17 driver.findElement(By.id("account")).sendKeys("admin"); 18 Thread.sleep(1000); 19 driver.findElement(By.name("password")).sendKeys("123456"); 20 Thread.sleep(1000); 21 //點擊登錄 22 driver.findElement(By.xpath("//button[@id='submit']")).click(); 23 Thread.sleep(2000); 24 //點擊測試 25 driver.findElement(By.linkText("測試")).click(); 26 Thread.sleep(1000); 27 //點擊Bug 28 driver.findElement(By.linkText("Bug")).click(); 29 Thread.sleep(1000); 30 //點擊提交Bug 31 driver.findElement(By.xpath("//a[@href='/zentao/bug-create-5-0-moduleID=0.html']")).click(); 32 Thread.sleep(1000); 33 34 //重現步驟 35 //switchTo()從外html文件跳到內html文件中 36 driver.switchTo().frame(driver.findElement(By.xpath("//div[@class='ke-edit']/iframe"))); 37 WebElement step=driver.findElement(By.xpath("//body[@class='article-content']")); 38 step.sendKeys(Keys.ARROW_RIGHT); 39 step.sendKeys(Keys.ARROW_RIGHT); 40 step.sendKeys(Keys.ARROW_RIGHT); 41 step.sendKeys(Keys.ARROW_RIGHT); 42 step.sendKeys("打開禪道"); 43 Thread.sleep(1000); 44 //step.sendKeys(Keys.ARROW_DOWN); 45 step.sendKeys(Keys.ARROW_DOWN); 46 step.sendKeys("不能打開"); 47 Thread.sleep(1000); 48 step.sendKeys(Keys.ARROW_DOWN); 49 //step.sendKeys(Keys.ARROW_DOWN); 50 step.sendKeys("正常打開"); 51 Thread.sleep(1000); 52 driver.switchTo().defaultContent(); 53 //瀏覽器拉到最底部 54 ((JavascriptExecutor)driver).executeScript("window.scrollTo(0,document.body.scrollHeight)"); 55 //點擊保存按鈕 56 driver.findElement(By.xpath("//button[@id='submit']")).click(); 57 58 } 59 }
switch_to_frame的參數必須是id或者是name, 所以一個frame只要有id和name處理起來很容易。如果沒有的話,兩種解決思路:
①加上id或者name
②使用xpath等方式定位然后實現跳轉
10.alert/confirm/prompt處理
WebDriver中處理原生JS的 alert 、confirm 以及prompt非常方便。
具體思路是使用switchTo.alert()方法定位到當前的 alert/confirm/prompt(這里注意當前頁面只能同時含有一個控件,如果多了會報錯的,所以這就需要一一處理了),然后在調用Alert 的方法進行操作
alert只有確定按鈕
confirm有確定和取消按鈕
prompt可以輸入文字,有確定和取消按鈕
Alert提供了以下幾個方法:
①getText: 返回alert/confirm/prompt中的文字內容
②accept : 點擊確認按鈕
③dismiss : 點擊取消按鈕如果有取消按鈕的話
④sendKeys : 向prompt中輸入文字
1 package com.ui.day1; 2 3 import org.openqa.selenium.Alert; 4 import org.openqa.selenium.By; 5 import org.openqa.selenium.WebDriver; 6 import org.openqa.selenium.chrome.ChromeDriver; 7 8 public class yihuqingjiu_test_demo_js { 9 10 public static void main(String[] args) throws Exception { 11 //chrom瀏覽器驅動的位置 12 System.setProperty("webdriver.chrome.driver","C:\\Program Files (x86)\\Google\\Chrome\\Application\\chromedriver.exe"); 13 //web驅動指向chrom驅動並創建對象driver 14 WebDriver driver=new ChromeDriver(); 15 //獲取網址 16 driver.get("file:///C:/Users/hongboss/Desktop/js%E4%BB%A3%E7%A0%81/%E5%AF%B9%E8%B1%A1.html?"); 17 //瀏覽器最大化 18 driver.manage().window().maximize(); 19 driver.findElement(By.cssSelector("#btn")).click(); 20 Thread.sleep(1000); 21 Alert alter=driver.switchTo().alert(); 22 Thread.sleep(1000); 23 System.out.println(alter.getText()); 24 alter.accept(); 25 driver.findElement(By.cssSelector("#btn1")).click(); 26 Thread.sleep(1000); 27 Alert alter1=driver.switchTo().alert(); 28 Thread.sleep(1000); 29 System.out.println(alter1.getText()); 30 alter1.dismiss(); 31 driver.findElement(By.cssSelector("#btn2")).click(); 32 Thread.sleep(1000); 33 Alert alter2=driver.switchTo().alert(); 34 Thread.sleep(1000); 35 System.out.println(alter2.getText()); 36 alter2.sendKeys("hello"); 37 Thread.sleep(1000); 38 alter2.accept(); 39 40 } 41 42 }
11.下拉框處理
web頁面上經常會有下拉框,對下拉框的處理比較簡單,一般分為兩種情況:
①普通下拉框通過層級定位識別
②有些下拉框是鼠標移上去直接彈出的,可以使用(鼠標移動上去自動顯示的下拉框)
方法一實現代碼如層級定位中所舉例代碼思想一致,這里就不再列舉
方法二實現代碼如下,僅供參考:
1 package com.ui.day2; 2 3 import org.openqa.selenium.By; 4 import org.openqa.selenium.WebDriver; 5 import org.openqa.selenium.WebElement; 6 import org.openqa.selenium.chrome.ChromeDriver; 7 import org.openqa.selenium.interactions.Actions; 8 9 public class yihuqingjiu_test_baidu_float { 10 11 public static void main(String[] args) { 12 System.setProperty("webdriver.chrome.driver", "C:\\Program Files (x86)\\Google\\Chrome\\Application\\chromedriver.exe"); 13 WebDriver driver=new ChromeDriver(); 14 driver.get("https://www.baidu.com"); 15 driver.manage().window().maximize(); 16 //定位更多產品 17 WebElement move=driver.findElement(By.cssSelector(".bri")); 18 Actions action=new Actions(driver); 19 action.moveToElement(move).perform(); 20 //driver.findElement(By.cssSelector(".bdbriimgitem_3")).click(); 21 driver.findElement(By.cssSelector(".bdbriimgitem_6")).click(); 22 } 23 24 }
12.select菜單處理
實現方法有如下四種:
①鼠標點擊事件,move
②層級關系
③list列表
④selector方法
方法一實現代碼如下拉框處理中方法二所示
方法二實現代碼如層級定位中所示
方法三實現代碼如下,僅供參考:
1 package com.ui.day1; 2 3 import java.util.List; 4 5 import org.openqa.selenium.By; 6 import org.openqa.selenium.WebDriver; 7 import org.openqa.selenium.WebElement; 8 import org.openqa.selenium.chrome.ChromeDriver; 9 10 public class yihuqingjiu_test_demo_list { 11 12 public static void main(String[] args){ 13 System.setProperty("webdriver.chrome.driver", "C:\\Program Files (x86)\\Google\\Chrome\\Application\\chromedriver.exe"); 14 WebDriver driver=new ChromeDriver(); 15 driver.get("https://www.baidu.com/"); 16 driver.manage().window().maximize(); 17 //把列表內容放到list中 18 List<WebElement> list=driver.findElements(By.cssSelector("a.mnav")); 19 //Thread.sleep(3000); 20 //取出列表中的具體值,並點擊 21 list.get(2).click(); 22 23 } 24 25 }
方法四中還包含三種方法,分別是:
selectByIndex(int index) //通過index
selectByVisibleText(String text) //通過匹配到的可見字符
selectByValue(String value) //通過匹配到標簽里的value
代碼實現如下,僅供參考:
1 package com.ui.day1; 2 3 import org.openqa.selenium.By; 4 import org.openqa.selenium.WebDriver; 5 import org.openqa.selenium.WebElement; 6 import org.openqa.selenium.chrome.ChromeDriver; 7 import org.openqa.selenium.support.ui.Select; 8 9 public class yihuqingjiu_test_demo_select { 10 11 public static void main(String[] args) throws Exception { 12 System.setProperty("webdriver.chrome.driver", "C:\\Program Files (x86)\\Google\\Chrome\\Application\\chromedriver.exe"); 13 WebDriver driver=new ChromeDriver(); 14 driver.get("file:///F:/17new/12%20UI%E8%87%AA%E5%8A%A8%E5%8C%96/html/ui_day1_test.html"); 15 driver.manage().window().maximize(); 16 WebElement selector=driver.findElement(By.id("select")); 17 Select select = new Select(selector); 18 select.selectByVisibleText("有經驗"); 19 Thread.sleep(2000); 20 select.selectByValue("無經驗"); 21 Thread.sleep(2000); 22 select.selectByIndex(1); 23 24 } 25 26 }
特殊下拉框:
還有一種下拉框就是聯想形式的,比如下百度搜索框中輸入12306,會出現多個選項,用代碼實現去選擇下拉中的內容,代碼實現如下:
1 package com.ui.day3; 2 3 import java.util.List; 4 5 import org.openqa.selenium.By; 6 import org.openqa.selenium.WebDriver; 7 import org.openqa.selenium.WebElement; 8 import org.openqa.selenium.chrome.ChromeDriver; 9 10 public class yihuqingjiu_test_select_12306 { 11 12 public static void main(String[] args) throws Exception { 13 System.setProperty("webdriver.chrome.driver", "C:\\Program Files (x86)\\Google\\Chrome\\Application\\chromedriver.exe"); 14 WebDriver driver=new ChromeDriver(); 15 driver.get("https://www.baidu.com/"); 16 driver.manage().window().maximize(); 17 //定位輸入框,並輸入內容 18 driver.findElement(By.cssSelector("#kw")).sendKeys("12306"); 19 //點擊輸入框 20 driver.findElement(By.cssSelector("#kw")).click(); 21 Thread.sleep(1000); 22 //把列表內容放到list中 23 List<WebElement> list=driver.findElements(By.cssSelector("form#form div.bdsug ul li")); 24 //Thread.sleep(3000); 25 //取出列表中的具體值,並點擊 26 list.get(1).click(); 27 28 } 29 30 }
13.selenium中調用js
當webdriver遇到無法完成的操作時候,這個時候可以使用javascript來完成,webdriver提供了execute_script()接口來調用js代碼。
執行js有兩種場景:
①在頁面上直接執行js
②在某個已經定位的元素上執行js
場景①其實在alter/confirm/prompt處理已介紹過在頁面上如何執行js了,可參考。
場景②在設置等待時間已介紹如何高亮處理,都有介紹。
js還可以做下拉滾動條,在做自動化測試的時候,這個辦法缺一不可。之前我在做自動化時,就因為沒加滾動條,而導致腳本一直運行不成功,因為腳本只能執行在當前頁面所顯示的操作,因此需要滾動條。
JavascriptExecutor js = (JavascriptExecutor)driver; js.executeScript("scrollTo(0,10000)");----------------------------------------------向下拉到底 Thread.sleep(2000); js.executeScript("scrollBy(0, 0-document.body.scrollHeight *99/100)");--------------向上拉到頂 Thread.sleep(2000); js.executeScript("scrollTo(0,100000)");---------------------------------------------向下拉到底 Thread.sleep(2000); js.executeScript("scrollTo(0,1)");--------------------------------------------------向上拉到頂 Thread.sleep(2000); js.executeScript("scrollTo(0,1000)"); Thread.sleep(2000); js.executeScript("scrollBy(0, 0-document.body.scrollHeight *1/2)");-----------------拉到中間 Thread.sleep(2000); js.executeScript("scrollBy(0, 0-document.body.scrollWidht *1/2)");------------------左右拉到中間