WebDriver——PageObject

package com.ahchoo.automation.page;

 

importorg.openqa.selenium.By;

importorg.openqa.selenium.WebDriver;

importorg.openqa.selenium.WebElement;

public class BaiduIndexPage {

privateWebDriver driver;

privatefinal String url = "http://www.baidu.com";

publicBaiduIndexPage(WebDriverdriver) {

this.driver=driver;

driver.get(url);

}

 

publicSearchResultPage searchFor(String term) {

// Baidusearch input id is "kw"

WebElement searchField = driver.findElement(By.id("kw"));

searchField.clear();

searchField.sendKeys(term);

searchField.submit();

returnnew SearchResultPage(driver);

}

}

 

package com.ahchoo.automation.page;

importorg.openqa.selenium.WebDriver;

 

public class SearchResultPage {

privateWebDriver driver;

publicSearchResultPage(WebDriver driver) {

this.driver=driver;

}

public StringgetTitle() {

returndriver.getTitle();

}

publicString getContent() {

returndriver.getPageSource();

}

}

 

 

package com.ahchoo.automation;

importstatic org.junit.Assert.*;

importorg.junit.After;

importorg.junit.Before;

importorg.junit.Test;

importorg.openqa.selenium.WebDriver;

importorg.openqa.selenium.firefox.FirefoxDriver;

importcom.ahchoo.automation.page.BaiduIndexPage;

importcom.ahchoo.automation.page.SearchResultPage;

public class SearchTest {

privateWebDriver driver;

@Before

public voidsetUp() {

driver = newFirefoxDriver();

}

@After

public voidtearDown() {

driver.close();

}

 

@Test

public voidsearchTest() {

BaiduIndexPage home = new BaiduIndexPage(driver);

SearchResultPage searchResult = home.searchFor("pizza");

assertTrue(searchResult.getTitle().contains("pizza"));

assertTrue(searchResult.getContent().contains("pizza"));

}

}

f

package com.ahchoo.automation.page;

importorg.openqa.selenium.WebDriver;

importorg.openqa.selenium.WebElement;

importorg.openqa.selenium.support.FindBy;

importorg.openqa.selenium.support.How;

 

public class BaiduIndexPage {

privateWebDriver driver;

privatefinal String url = "http://www.baidu.com";

 

publicBaiduIndexPage(WebDriverdriver) {

this.driver=driver;

driver.get(url);

}

@FindBy(how = How.ID, using="kw")

privateWebElement searchField;

 

publicSearchResultPage searchFor(String text) {

// Wecontinue using the element just as before

searchField.sendKeys(text);

searchField.submit();

returnnew SearchResultPage(driver);

}

}

 

你可能感兴趣的:(webdriver)