Selenium原理
Selenium 是一個(gè)用于測(cè)試 Web 應(yīng)用程序的自動(dòng)化工具 ,用于模擬用戶操作,主要功能:功能測(cè)試,瀏覽器兼容性測(cè)試等
Selenium WebDriver 簡(jiǎn)稱 WebDriver,即 Selenium 2。
Selenium 安裝配置
- java環(huán)境
- selenium-java-3.9.1.zip 和 selenium-server-standalone-3.9.1.jar工具包
- 各大廠商瀏覽器驅(qū)動(dòng)
- SeleniumIDE 它是Firefox 瀏覽器的一個(gè)插件, 它會(huì)記錄你對(duì)Firefox的操作,并且可以回放它的操作
指定瀏覽器驅(qū)動(dòng),打開(kāi)瀏覽器
IE:
System.setProperty("webdriver.ie.driver", "<DRIVER_PATH>/IEDriverServer.exe");
WebDriver driver = new InternetExplorerDriver();
Firefox:
System.setProperty("webdriver.gecko.driver", "<DRIVER_PATH>/geckodriver.exe");
System.setProperty("webdriver.firefox.bin","E:/Mozilla Firefox/firefox.exe");
WebDriver driver = new FirefoxDriver();
Google:
System.setProperty("webdriver.chrome.driver", "<DRIVER_PATH>/chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.manage.window.maximize()窗口最大化
Selenium元素定位
driver.findElement(By.屬性)
屬性名包括以下內(nèi)容
- name("值")
- tagName("標(biāo)簽名")
- xpath("http://標(biāo)記名[@屬性名='屬性值']")
- id
- className : CSS class 屬性的值
- linkText : <a href>和</a>之間的內(nèi)容
- partialLinkText : <a href>和</a>之間的部分文本值
driver.findElement(By.屬性)用于查詢符合要求的第一個(gè)元素對(duì)象,主要包括于文本框、密碼框、命令按鈕、單選按鈕
driver.findElements(By.屬性).get(Index)
- 查詢符合屬性要求的所有對(duì)象;
- 適用于單選按鈕、復(fù)選框、鏈接等
- 使用 get(下標(biāo))可以選擇某個(gè),從 0 開(kāi)始。
Selenium元素操作
文本框操作
- sendKeys("數(shù)據(jù)") 發(fā)送數(shù)據(jù)到瀏覽器
- clear() 清空文本框數(shù)據(jù)
- getAttribute("屬性名") 獲取元素屬性值
- getText() 獲取文本值 (類似于JS的innerText)
文件上傳操作
input類型文件上傳
driver.findElement(By.id("file")).sendKeys("<File_local_path>")
單復(fù)選框操作
- click() 單擊/選中
- isSelected()
下拉列表
new Select(driver.findElement(By.name("Select控件名"))).
- selectByIndex(下標(biāo)) 根據(jù)下標(biāo)選擇某項(xiàng),從 0 開(kāi)始
- selectByVisibleText("數(shù)據(jù)") 根據(jù)列表框中顯示的文本選擇某項(xiàng)
- selectByValue("值") 根據(jù)值選擇某項(xiàng)
frame 框架
- driver.switchTo().defaultContent(); 切換到主文檔
- driver.switchTo().frame(frameName);切換到frameName的子文檔
多Tab窗口
- driver.getWindowHandle(): 獲得當(dāng)前窗口句柄。
- driver.getWindowHandles(): 返回的所有窗口的句柄到當(dāng)前會(huì)話。
- driver.switchTo().window(handle): 用于切換到相應(yīng)的窗口,與上一節(jié)的switchTo().frame()類似,前者用于不同窗口的切換, 后者用于不同表單之間
元素狀態(tài)
- isEnable()用于存儲(chǔ)input、select等元素的可編輯狀態(tài)
- isSelected()用于存儲(chǔ)input、select等元素的選中狀態(tài)
- isDisplayed()源于判斷元素是否在頁(yè)面中可獲取
元素操作之對(duì)話框處理
Alert 警告框
Alert alert = driver.switchTo().alert();
alert.accept();
Confirm 確認(rèn)框
Alert confirm = driver.switchTo().alert();
String text1 = confirm.getText(); //獲取confirm上的文本
System.out.println(text1);
confirm.dismiss(); //關(guān)閉confirm
prompt 輸入框
Alert prompt = driver.switchTo().alert();
prompt.sendKeys("hello world")//輸入值,如果支持輸入的話
prompt.accept(); //關(guān)閉prompt
Modal 模態(tài)框
WebElement el = driver.switchTo().activeElement();
getText() 得到它的文本值,accept() 相當(dāng)于點(diǎn)擊它的"確認(rèn)", dismiss() 相當(dāng)于點(diǎn)擊"取消"或者叉掉對(duì)話框,activeElement()用戶獲取當(dāng)前獲得焦點(diǎn)的元素
元素操作之鼠標(biāo)事件
Action action = new Action(driver);
- 左擊
action.click().perform() - 右擊
action.contextClick().perform() - 雙擊
action.doubleClick().perform() - 懸停
action.moveToElement(driver).perform()
執(zhí)行Javascript操作
import org.openqa.selenium.JavascriptExecutor;
String js = "document.getElementById(\"objId\").click()";//定義一個(gè)js
JavascriptExecutor JE = (JavascriptExecutor)driver;
JE.executeScript(js)
等待函數(shù)
隱式等待
driver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
隱式等待采用全局設(shè)置,所有的 findElement 方法都會(huì)隱式等待,上述方法針對(duì)執(zhí)行腳本的所有對(duì)象均會(huì)等待3s.
顯示等待
Thread.sleep(1000);
Maven+ Selenium
安裝Maven,配置Maven環(huán)境變量,設(shè)置本地倉(cāng)庫(kù)
(忽略。。。)
新建Maven工程
-
新建一個(gè)Maven工程,F(xiàn)ile>New>Other,搜索Maven
image.png
image.png
image.png
image.png
新建的Maven工程的JRE System Library可能是jre1.5的,需要在D:\Applications\apache-maven-3.6.1\conf\settings.xml文件的<profiles>標(biāo)簽對(duì)中加入
<profile>
<id>jdk-1.8</id>
<activation>
<activeByDefault>true</activeByDefault>
<jdk>1.8</jdk>
</activation>
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<maven.compiler.compilerVersion>1.8</maven.compiler.compilerVersion>
</properties>
</profile>
導(dǎo)入selenium+TestNG
<dependencies>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>3.9.1</version>
</dependency>
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>6.14.3</version>
</dependency>
</dependencies>



