1. 什么是 Selenide
Selenide = UI Testing Framework powered by Selenium WebDriver, Selenide 基於Java語言,是對 selenium 的一個封裝,用於編寫簡潔 concise 而穩定 stable 的UI測試。 官網 Github
優點:
- 內置隱式等待
- 用例運行完后自動關閉瀏覽器
- 代碼簡單,易讀
- 自動對失敗用例截圖
2. 使用
2.1 准備(首先在pom.xml中加入如下依賴)
<!-- selenide 4.8版本對應selenium-java 3.7.1版本 -->
<dependency>
<groupId>com.codeborne</groupId>
<artifactId>selenide</artifactId>
<version>4.8</version>
</dependency>
2.2 使用
2.2.1 常用操作
# selenide默認使用Firefox瀏覽器,設置成chrome瀏覽器
System.setProperty("webdriver.chrome.driver", chromeDriverPath);
Configuration.browser = "chrome";
# 設置瀏覽器運行完不關閉
Configuration.holdBrowserOpen = true;
Configuration.timeout = 6000 ;
Configuration.timeout = 8000 ;
# 修改報告默認生成路徑
Configuration.reportsFolder = "target/reports/test"+Configuration.browser;
---------------------------------------------------------------------------------------
# 把webDriver傳給selenide (注意:如果使用自己設置的driver,selenide不會自動關閉browser,需要手動關閉!)
WebDriverRunner.setWebDriver(driver);
# 獲取當前driver
WebDriverRunner.getWebDriver();
# 獲取當前頁面url
WebDriverRunner.url()
# 返回當前頁面的源HTML代碼
WebDriverRunner.source()
---------------------------------------------------------------------------------------
# 打開一個url:
open("https://www.baidu.com/");
# 獲取當前頁面title
title()
# 刷新當前頁面
refresh();
# 清除瀏覽器cookie
clearBrowserCookies();
# 切換瀏覽器窗口
switchTo().window(nameOrHandle);
# 執行js
executeJavaScript(String jsCode,Object ... arguments)
2.2.2 元素定位
selenide使用“$”符號定位一個元素(導包:import static com.codeborne.selenide.Selenide.*;)
$(String cssSelector) - $符號默認參數是“CSS”,返回SelenideElement類的對象,SelenideElement繼承WebElement,該對象表示頁面上CSS選擇器找到的第一個元素。
$(By) - 由By類的定位器返回找到的第一個元素。
$(byText(“Sign in”)) - 通過text定位
$(byXpath("//*[text()=‘Sign in’]")) -通過Xpath定位
$(byAttribute(“placeholder”,“房源編號”)) - 通過屬性定位
$$("#search-results a").findBy(text(“selenide.org”)) 二級查找
2.2.3 元素操作
操作 | 意義 | 說明 |
---|---|---|
click() | 鼠標點擊 | |
doubleClick() | 鼠標雙擊 | |
contextClick() | 鼠標右擊 | |
hover() | 懸停 | |
dragAndDropTo(String) | 鼠標拖動操作 | |
setValue(String) / val(String) | 文本框賦值 | |
pressEnter() | 輸入 回車鍵 | |
pressEscape() | 輸入 Escape 鍵 | |
pressTab() | 輸入 Tab 鍵 | |
selectRadio(String value) | 點擊單選按鈕 | |
selectOption(String) | 下拉框選擇 | |
append(String) | ||
download() | 下載文件的操作(默認只能下載標簽的元素,若想下載其他標簽里面的元素則需另外設置) | |
uploadFile(file) | 上傳文件的操作(uploadFile的參數為File類型) |
獲取元素狀態和屬性值的方法
操作 | 意義 | 說明 |
---|---|---|
getValue() | val() | |
data() | ||
attr(String) | ||
text() | 返回“頁面上的可見文本” | |
innerText() | 返回“DOM中元素的文本” | |
getSelectedOption() | ||
getSelectedText() | ||
getSelectedValue() | ||
isDisplayed() | 如果元素被隱藏(不可見)或者DOM中不存在元素,則返回false; 否則 - 是的 | |
exists() | 如果DOM中存在元素,返回true;否則返回false |
實際例子:
$(byText(“Sign in”)).click() - 點擊
$(byName(“password”)).setValue(“qwerty”) - 文本輸入
$(byName(“password”)).setValue(“qwerty”).pressEnter() -文本輸入后敲回車
$(By.xpath(element)).exists() - 判斷元素是否存在
executeJavaScript(String jsCode,Object … arguments) 執行js代碼
2.2.4 元素集合定位和操作
通過 “$$”定位一組元素,如下:
$$("#search-results a") 返回ElementsCollection類型的元素集合
對元素集合的操作
size();
isEmpty();
get(int); //根據index獲取元素集合中的某一個元素
getTexts();//返回可見元素集合文本的數組,例如對於元素:<li>a</li><li hidden>b</li><li>c</li>將返回數組["a", "", "c"]
2.2.5 操作alert
confirm(); //確定alert
confirm("exceptionText"); //判斷alert的文字是否是期望的那個,如果是,接收alert
dismiss(); //取消alert
2.2.6 斷言
selenide通過should來進行斷言,相當於Assert。
例子如下:
$(“input”).should(exist) - 驗證元素應該存在
$(“input”).shouldBe(visible) - 驗證元素應該可見
$(“input”).shouldHave(exactText(“Some text”)) - 驗證元素的text等於Some text
$(“input”).shouldHave(value(“John”)) - 驗證元素的value屬性等於John“”
$(“#input”).shouldHave(id(“myForm”)) - 驗證元素的id屬性等於“myForm”
$("#btn-Search").shouldHave(text(“搜索”)) - 驗證元素是否包含“搜索”文本
$("#btn-Search").shouldNotHave(text(“搜索”)) - 驗證元素不包含“搜索”文本
$("#btn-Search").shouldNot(exist) - 不存在某個元素
斷言相當於顯示等待,等待的狀態(visible,enabled,text(“some text”))是否滿足,直到超時達到的值(默認設置為4000毫秒)。斷言返回的也是元素對象本身,所以可以繼續操作該元素。,例如:$("#submit").shouldBe(enabled).click();
還可以自行明確設置超時:
waitUntil(條件,毫秒)
waitWhile(條件,毫秒)
3. 源碼
3.1 學習Selenide
學習 Selenide 最快捷的方法是打開IDE 開發環境,輸入: $(selector). Selenide javadoc。
3.2 常用到的 Selenide 類:
-
com.codeborne.selenide.Selenide 核心類。主要方法有:open,$和$$
-
com.codeborne.selenide.SelenideElement 本類描述頁面上找到的元素。類的對象可以通過 $ 命令來獲取。有用的方法有:
-
com.codeborne.selenide.Condition Condition 主要用在 should/ shouldNot/ waitUntil/ waitWhile 等這些結構中。
-
com.codeborne.selenide.Selectors 本類相當於是原始選擇器的擴贊,包含通過文本或屬性來定位元素的 By選擇器
-
com.codeborne.selenide.ElementsCollection 本類描述頁面上找到的元素集合。通常可以通過 $$ 方法獲取 ElementsCollection類的對象。有用的方法有:
-
com.codeborne.selenide.CollectionCondition Collection conditions 主要用在 shouldBe/shouldHave 等這些結構中。
-
com.codeborne.selenide.WebDriverRunner 定義了一些瀏覽器管理方法:url() - 返回當前URL;source() - 返回當前頁面的源HTML代碼
-
com.codeborne.selenide.Configuration 包含測試執行時不同的配置選項。例如: timeout - collectionsTimeout - browser - baseUrl - reportsFolder
注意,這些配置項也可以作為系統屬性傳遞,例如,當配置測試在CI服務器上執行時e.g. -Dselenide.baseUrl=http://staging-server.com/start