我基本上是在尝试在 Selenium 中运行 java 中的示例页面对象框架。我尝试运行一些站点和论坛提供的示例类。但由于某种原因,它似乎不起作用。我不知道我是否错过了什么。请帮忙。谢谢你
我已经尝试过这些示例 - https://weblogs.java.net/blog/johnsmart/archive/2010/08/09/selenium-2web-driver-land-where-page-objects-are-king
http://www.wakaleo.com/blog/selenium-2-webdriver-quick-tips-page-object-navigation-strategies
package google;
import org.junit.After;
import org.junit.Before;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.PageFactory;
import org.testng.annotations.Test;
public class WhenAUserSearchesOnGoogle {
private GoogleSearchPage page;
@Before
public void openTheBrowser() {
page = PageFactory.initElements(new ChromeDriver(), GoogleSearchPage.class);
page.open("http://google.co.nz/");
}
@After
public void closeTheBrowser() {
page.close();
}
@Test
public void whenTheUserSearchesForCatsTheResultPageTitleShouldContainCats() {
page.searchFor("cats");
//assertThat(page.getTitle(), containsString("cats") );
}
}
以上是我正在使用的页面工厂类。
以下是 Page 对象。
package google;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
//import org.openqa.selenium.firefox.FirefoxDriver;
public class GoogleSearchPage {
protected WebDriver driver;
private WebElement q;
private WebElement btnG;
public GoogleSearchPage(WebDriver driver) {
this.driver = driver;
}
public void open(String url) {
driver.get(url);
}
public void close() {
driver.quit();
}
public String getTitle() {
return driver.getTitle();
}
public void searchFor(String searchTerm) {
q.sendKeys(searchTerm);
btnG.click();
}
public void typeSearchTerm(String searchTerm) {
q.sendKeys(searchTerm);
}
public void clickOnSearch() {
btnG.click();
}
}
堆栈跟踪显示失败:whenTheUserSearchesForCatsTheResultPageTitleShouldContainCats