java+selenium自動化-IE瀏覽器搭建自動化環境
阿新 • • 發佈:2019-02-10
在做web的UI層面自動化時,首先需要進行環境的搭建,即瀏覽器的一些操作,保證在瀏覽器啟動的情況下才可以進行自動化測試。
下面介紹如何在IE瀏覽器上搭建自動化環境
1.建立一個IE驅動
webDriver driver = new InternetExplorerDriver();
driver.get("http://www.baidu.com");
注意:WebDriver類是一個最基本的類,是用來建立各種驅動
2.往專案中新增IE驅動包,並載入驅動的配置
IE驅動下載網址:selenium官網 http://selenium-release.storage.googleapis.com/index.html
3.元素定位:百度輸入框
1)在火狐瀏覽器中使用快捷鍵F12,或者設定中選擇開發者選項。開啟網頁原始碼(在IE瀏覽器中也可以在需要定位的元素上點選右鍵,選擇檢視元素,可以達到一樣的效果)
2)選擇定位工具,選中輸入框檢視原始碼
3)找到元素--百度輸入框(通過驅動去找元素)
4.往輸入框內輸入關鍵字:
5.點選百度一下,使用click函式
6.斷言,驗證結果是否符合預期
完整程式碼如下:
package com.lemon.future.auto; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.ie.InternetExplorerDriver; import org.testng.Assert; import org.testng.annotations.Test; public class BaiduTest { @Test public void f() { //1:載入驅動設定 System.setProperty("webdriver.ie.driver", "src/test/resources/IEDriverServer.exe"); //:2:建立一個ie驅動 WebDriver driver = new InternetExplorerDriver(); //3:輸入百度的網址 driver.get("http://www.baidu.com"); //4:找到元素--百度輸入框(元素定位) WebElement input = driver.findElement(By.id("kw")); //5:往輸入框內輸入關鍵字 input.sendKeys("檸檬班,你好"); //6:點選百度一下 driver.findElement(By.id("su")).click(); /**以下為一個簡單的斷言**/ //1:獲得輸入框的value值 String actualKeyword = driver.findElement(By.id("kw")).getAttribute("value"); //2:驗證結果是否符合預期 Assert.assertEquals("檸檬班,你好", actualKeyword); } }