首先定义登录页面,上代码吧
LoginPage.java
package com.test.selenium.page;import org.openqa.selenium.By;import static com.codeborne.selenide.Selenide.$;import static com.codeborne.selenide.Selenide.page;public class LoginPage {public SearchResultsPage searchFor(String text) {$(By.id("kw")).val(text).pressEnter();return page(SearchResultsPage.class);}}
LoginTest.java
package com.test.selenium.page;import static com.codeborne.selenide.Selenide.open;import org.junit.Before;import org.junit.Test;import com.codeborne.selenide.Configuration;import static com.codeborne.selenide.CollectionCondition.sizeGreaterThan;import static com.codeborne.selenide.Condition.text;public class LoginTest {@Beforepublic void test() {Configuration.browser ="chrome";System.setProperty("webdriver.chrome.driver", "D:\\work\\com.test.selenium\\drivers\\chromedriver.exe");}@Testpublic void testBaidu() {LoginPage page = open("/", LoginPage.class);SearchResultsPage results = page.searchFor("selenide");results.getResults().shouldHave(sizeGreaterThan(1));results.getResult(0).shouldHave(text("Selenide: concise UI tests in Java"));}}
SearchResultsPage.java
package com.test.selenium.page;import com.codeborne.selenide.ElementsCollection;import com.codeborne.selenide.SelenideElement;import static com.codeborne.selenide.Selenide.$;import static com.codeborne.selenide.Selenide.$$;import org.openqa.selenium.By;public class SearchResultsPage {public ElementsCollection getResults() {return $$(By.xpath(".//*[contains(@class, 'c-container')]"));}public SelenideElement getResult(int index) {return $(By.xpath(".//*[contains(@class, 'c-container')]"), index);}}
利用这样的方式进行,相互配合使用,可以最大化复用页面。
实例:可以参考git代码部分 /wallaceok/light_selenide 后续进阶介绍中会详情介绍这个封装的内容。请继续关注!