我沒有拼寫錯誤,確實不是 Selenium ,但是,只要是 Web UI 自動化測試框架,基本上都是基於Selenium 的。Selenide 也不例外。那為啥不直接用Selenium呢? 因為原生的 Selenium 不好用啊!
舉個例子,用原生成Selenium去寫 顯式等待。
……
//顯式等待, 針對某個元素等待
WebDriverWait wait = new WebDriverWait(driver,10,1); wait.until(new ExpectedCondition<WebElement>(){ @Override public WebElement apply(WebDriver text) { return text.findElement(By.id("kw")); } }).sendKeys("selenium");
如果每一個元素都設置顯示等待(為了保證腳本的穩定性),你不會瘋掉? 所以, 用 Selenium 做UI 自動化測試,不去封裝 Selenium, 不去考慮設計模式。你的項目很難做得好!
什么是 Selenide ?
Concise UI Tests with Java! 所以,其它語言的同學靠邊。
Selenide = UI Testing Framework powered by Selenium WebDriver
如何學習?
Github 項目地址:https://github.com/codeborne/selenide
官方網站:http://selenide.org/index.html
如何安裝?
這里只介紹 Maven 安裝:
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>6.11</version>
<scope>test</scope>
</dependency>
<dependency> <groupId>com.codeborne</groupId> <artifactId>selenide</artifactId> <version>4.8</version> <scope>test</scope> </dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.5</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.14</version>
<scope>provided</scope>
</dependency>
我的測試用例是基於 testNG 單元測試框架運行的,所以,把 TestNG 的配置加進來了。
Selenide 在安裝運行的過程中報錯: Failed to load class "org.slf4j.impl.StaticLoggerBinder" 所以,指定了一下slf4j 和 log4j 的版本。
如何使用?
Selenium 老司機直接看代碼,不管怎么的封裝, 一眼就能看出來它的“特色”。 官方 demo。
@Test public void userCanLoginByUsername() { open("/login"); $(By.name("user.name")).setValue("johny"); $("#submit").click(); $(".loading_progress").should(disappear); // Waits until element disappears
$("#username").shouldHave(text("Hello, Johny!")); // Waits until element gets text
}
URL 地址在哪兒? 瀏覽器怎改? 估計你跑不起來! 反正,我沒跑起來。 打開 Friefox 瀏覽器就不動了。
好在!作者很負責,在官方上放了一段自己錄制的10分鍾教程(其實,我聽不懂英文,但看他的操作就明白了。哈哈!)
import com.codeborne.selenide.Configuration; import org.testng.annotations.Test; import static com.codeborne.selenide.CollectionCondition.size; import static com.codeborne.selenide.Selenide.*; import static com.codeborne.selenide.Condition.*; public class SelenideTest { @Test public void TestBaidu() throws InterruptedException { Configuration.browser = "Chrome"; Configuration.baseUrl="https://www.baidu.com"; open("/"); $("#kw").setValue("selenide"); $("#su").click(); Thread.sleep(3000); // 斷言
$$("h3 > a").shouldHave(size(9)); $("h3 > a").setValue(String.valueOf(text("selenide_百度翻譯"))); } }
這才是可以運行的第一個demo。
通過這第一個demo,感官性的談談它的優點:
1、 API更簡潔
2、默認使用 CSS 定位
3、自己封裝了斷言
4、錯誤后自動截圖 (不信你運行上面的代碼試試)
5、自動關閉瀏覽器。
缺點:
不知道為什么 我的 Chrome 瀏覽器啟動的特別慢。
希望我進一步介紹 Selenide UI自動化框架的使用,歡迎留言!
--------------
我自己也有封裝 selenium 的框架:
java selenium :https://github.com/defnngj/knife
python selenium : https://github.com/defnngj/pyse
