1. 程式人生 > 其它 >Selenium系列之--11 selenide 使用

Selenium系列之--11 selenide 使用

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

【Quality】 Quality is the value to someone who matters。做測試,首先要找到這個someone是誰,以及這個 someone重視的 value是什麼。