Selenide = UI Testing Framework powered by Selenium WebDriver
地址:https://github.com/codeborne
UI自動化測試框架
介紹:
Selenide is a framework for writing easy-to-read and easy-to-maintain automated tests in Java. It defines concise fluent API, natural language assertions and does some magic for ajax-bases applications to let you focus entirely on the business logic of your tests.
使用:
maven或者下載jar
常用api,都是簡單的英文,不翻譯
1. Create a browser
Selenium WebDriver:
DesiredCapabilities desiredCapabilities = DesiredCapabilities.htmlUnit();
desiredCapabilities.setCapability(HtmlUnitDriver.INVALIDSELECTIONERROR, true);
desiredCapabilities.setCapability(HtmlUnitDriver.INVALIDXPATHERROR, false);
desiredCapabilities.setJavascriptEnabled(true);
WebDriver driver = new HtmlUnitDriver(desiredCapabilities);
Selenide:
open("/my-application/login");
// And run tests with option -Dbrowser=htmlunit (or "chrome" or "ie", default value is "firefox")
啟動瀏覽器的方式變化:
如何啟動chrome或者ie?
啟動chrome
Configuration.browser = "chrome";
System.setProperty("webdriver.chrome.driver", Constants.GETCHROMEDRIVERPATH_STRING);
這里2選1,如果使用configuration的話,則需要在環境變量中提前配置好路徑,ie如下
System.setProperty("selenide.browser", "Chrome");
啟動IE
Configuration.browser ="ie";
System.setProperty("webdriver.ie.driver", Constants.GETIEIVERPATH_STRING);
System.setProperty("selenide.browser", "Ie");
因為我可能后續還會使用webdriver,所以這個對象還是需要的,使用driver = getWebDriver();混合使用
2. Shutdown a browser
Selenium WebDriver:
if (driver != null) {
driver.close();
}
Selenide:
// Do not care! Selenide closes the browser automatically.
With Selenide You don't need to operate with Selenium WebDriver directly. WebDriver will be automatically created/deleted during test start/finished.
不關心瀏覽器的關閉,自動關閉,還有一種情況就是當執行失敗的時候,我們不希望關閉瀏覽器,可以在configuration中配置
3. Find element by id
Selenium WebDriver:
WebElement customer = driver.findElement(By.id("customerContainer"));
Selenide:
WebElement customer = $("#customerContainer");
or a longer conservative option:
WebElement customer = $(By.id("customerContainer"));
這里的使用方法更簡單,就跟我們在chrome 中console控制台調試一樣,沒有坑,但是補充一些沒有提到到,
補充1:
如下:我們可能會使用xpath的定位方式處理的,這里更方便
WebElement customer = $(byText("Customer profile"));
WebElement temp = $(byValue("不使用")).click();
WebElement temp1 = $(byAttribute("data-name",“test name”)).click();
補充2:獲取多個元素:
webdriver:
driver.findElements(By.tagName("li")).get(5);
selenide:
$("li", 5);
$$("#multirowTable tr").filterBy(text("Norris"))
上面第一行代碼是所有標簽為li的元素中的第5個
第二行代碼是取特定的集合后再去搜尋text為期望值的元素。
注:
$開頭的是取一個元素
$$開頭的是取一個集合
selenide默認支持css selector,所以代碼看上去簡潔了很多。
4. Assert that element has a correct text
Selenium WebDriver:
assertEquals("Customer profile", driver.findElement(By.id("customerContainer")).getText());
Selenide:
$("#customerContainer").shouldHave(text("Customer profile"));
elenide提供一系列should標簽幫做斷言工作,而且有一批text()的這種選擇器來幫助我們斷言各種類型。上面的例子就是斷言控件是否有期望的text。selenide專門有一個condition包,里面有各種各樣的condition,這些condition就是should標簽的參數。上面的例子text就是一個condition。其他的還有id,value,attribute,readonly等等
5. Ajax support (waiting for some event to happen)
Selenium WebDriver (OMG!):
FluentWait<By> fluentWait = new FluentWait<By>(By.tagName("TEXTAREA"));
fluentWait.pollingEvery(100, TimeUnit.MILLISECONDS);
fluentWait.withTimeout(1000, TimeUnit.MILLISECONDS);
fluentWait.until(new Predicate<By>() {
public boolean apply(By by) {
try {
return browser.findElement(by).isDisplayed();
} catch (NoSuchElementException ex) {
return false;
}
}
});
assertEquals("John", browser.findElement(By.tagName("TEXTAREA")).getAttribute("value"));
Selenide:
$("TEXTAREA").shouldHave(value("John"));
This command automatically waits until element gets visible AND gets expected value.
Default timeout is 4 seconds and it's configurable.
可以看到selenide還是一個should 的api搞定了。 它默認4s超時。4s內會循環check控件的value是否變成了期望值。同樣的還有text,attribute等選擇器。
6. Assert that element has a correct CSS class
Selenium WebDriver:
assertTrue(driver.findElement(By.id("customerContainer")).getAttribute("class").indexOf("errorField") > -1);
Selenide:
$("#customerContainer").shouldHave(cssClass("errorField"));
如上說的,selenide默認支持css selector,
7. Find element by text
Selenium WebDriver:
No way (except XPath)
Selenide:
WebElement customer = $(byText("Customer profile"));
上面已經提到此處
8. Assert that element text matches a regular expression
Selenium WebDriver:
WebElement element = driver.findElement(By.id("customerContainer"));
assertTrue(Pattern.compile(".*profile.*", DOTALL).matcher(element.getText()).matches());
Selenide:
$("#customerContainer").should(matchText("profile"));
9. Assert that element does not exist
Selenium WebDriver:
try {
WebElement element = driver.findElement(By.id("customerContainer"));
fail("Element should not exist: " + element);
}
catch (WebDriverException itsOk) {}
Selenide:
$("#customerContainer").shouldNot(exist);
自帶的斷言,可以參考api
10. Looking for element inside parent element
Selenium WebDriver:
WebElement parent = driver.findElement(By.id("customerContainer"));
WebElement element = parent.findElement(By.className("user_name"));
Selenide:
$("#customerContainer").find(".user_name");
類似Selenimu的層級定位
11. Looking for Nth element
Selenium WebDriver:
WebElement element = driver.findElements(By.tagName("li")).get(5);
Selenide:
$("li", 5);
上面已經提到
12. Click "Ok" in alert dialog
Selenium WebDriver:
try {
Alert alert = checkAlertMessage(expectedConfirmationText);
alert.accept();
} catch (UnsupportedOperationException alertIsNotSupportedInHtmlUnit) {
return;
}
Thread.sleep(200); // sometimes it will fail
Selenide:
confirm("Are you sure to delete your profile?");
or
dismiss("Are you sure to delete your profile?");
一些彈出框的操作,但是現在的前段框架很少用了,可了解
13. Debugging info for elements
Selenium WebDriver:
WebElement element = driver.findElement(By.id("customerContainer"));
System.out.println("tag: " + element.getTag());
System.out.println("text: " + element.getText());
System.out.println("id: " + element.getAttribute("id"));
System.out.println("name: " + element.getAttribute("name"));
System.out.println("class: " + element.getAttribute("class"));
System.out.println("value: " + element.getAttribute("value"));
System.out.println("visible: " + element.isDisplayed());
// etc.
Selenide:
System.out.println($("#customerContainer"));
// output looks like this: "<option value=livemail.ru checked=true selected:true>@livemail.ru</option>"
打印一些元素信息,
14. Take a screenshot
Selenium WebDriver:
if (driver instanceof TakesScreenshot) {
File scrFile = ((TakesScreenshot) webdriver).getScreenshotAs(OutputType.FILE);
File targetFile = new File("c:\temp\" + fileName + ".png");
FileUtils.copyFile(scrFile, targetFile);
}
Selenide:
takeScreenShot("my-test-case");
For JUnit users it's even more simpler:
public class MyTest {
@Rule // automatically takes screenshot of every failed test
public ScreenShooter makeScreenshotOnFailure = ScreenShooter.failedTests();
}
截圖控件,默認的保存路徑可以通過configuration配置或者同時保存源代碼
15. Select a radio button
Selenium WebDriver:
for (WebElement radio : driver.findElements(By.name("sex"))) {
if ("woman".equals(radio.getAttribute("value"))) {
radio.click();
}
}
throw new NoSuchElementException("'sex' radio field has no value 'woman'");
Selenide:
selectRadio(By.name("sex"), "woman");
單選操作
16. Reload current page
Selenium WebDriver:
webdriver.navigate().to(webdriver.getCurrentUrl());
Selenide:
refresh();
17. Get the current page URL, title or source
Selenium WebDriver:
webdriver.getCurrentUrl();
webdriver.getTitle();
webdriver.getPageSource();
Selenide:
url();
title();
source();