@FindBy WebElements未在PageFactory / Page Object框架中初始化

时间:2016-08-30 09:23:33

标签: java selenium pageobjects

更新代码:

post

页面对象:

public class FlightBookingTest extends PageBase{

@Test(priority = 1)
@Parameters({"from", "to"})
public void searchForAPackage(String from, String to) throws InterruptedException {

    customerHomePage().selectDepartureAirport(from);
    customerHomePage().selectDestinationAirport(to);
    customerHomePage().selectStartDate();
    customerHomePage().submitSearchRequest();

    assertThat(searchResultsPage().checkPageTitle(), equalTo("Flight Results"));
}

PageBase:

public class CustomerHomePage extends PageBase {

@FindBy(how = How.XPATH, using = ".//* [@id='container']/div/div[3]/div/div[1]/div/h3")
public WebElement searchResults;  
//loads more locators

public CustomerHomePage(WebDriver driver) {
    this.driver = driver;
    driver.manage().window().maximize();
}

public void visit(String url){
    driver.get(baseURL);
}

public void selectDepartureAirport(String departureAirport) {
    click(whereFromDropdown);
    selectOption(departureAirport);
}

public void selectDestinationAirport(String destination) {
    click(destinationLocator);
    type(destination, destinationLocator);
    selectOption("(" + destination + ")");
}

public void selectFromDate() {
    type("15/07/2016", dateFromField);
}

public void submitSearchRequest() {
    click(submitSearchButton);
    waitForIsDisplayed(searchResults, 120);
}

public void selectStartDate() {
    click(dateFromField);
    click(nextMonthSelector);
    click(dayOfMonth);
}

测试基地:

public class PageBase extends  TestBase {

public CustomerHomePage customerHomePage()
{
    return PageFactory.initElements(driver, CustomerHomePage.class);
}

看起来框架不尊重等待预期条件 - 元素的可见性。我怀疑与'visibilityOf(element)'的实现以及@FindBy初始化元素的方式有关

异常的堆栈跟踪:

public class TestBase implements Config {

public WebDriver driver;
//a bunch of methods to handle Driver instantiation and kill

//a bunch of Webdriver utility methods including:
public void click(WebElement element) {
    waitForIsDisplayed(element, 120);
    element.click();
}
 public Boolean waitForIsDisplayed(WebElement element, Integer... timeout) {
    try {
        waitFor(ExpectedConditions.visibilityOf(element),
                (timeout.length > 0 ? timeout[0] : null));
    } catch (org.openqa.selenium.TimeoutException exception) {
        return false;
    }
    return true;
}

private void waitFor(ExpectedCondition<WebElement> condition, Integer timeout) {
    timeout = timeout != null ? timeout : 5;
    WebDriverWait wait = new WebDriverWait(driver, timeout);
    wait.until(condition); //java.lang.reflect.UndeclaredThrowableException HERE...Caused by NoSuchElementException
}

引起:org.openqa.selenium.NoSuchElementException:没有这样的元素:无法找到元素:{“method”:“xpath”,“selector”:“.//* [@ id ='container'] / DIV / DIV [3] / DIV / DIV [1] / DIV / H3" }

2 个答案:

答案 0 :(得分:1)

将PageBase类切换到下面。您之前初始化代理但未返回该实例,只是一个新对象。否则,您可以使用'this'而不是'CustomerHomePage.class'

将initElements行粘贴到CustomerHomePage的构造函数中
public class PageBase extends SeleniumBase {

public CustomerHomePage customerHomePage()
{
    return PageFactory.initElements(driver, CustomerHomePage.class);
}

答案 1 :(得分:0)

@FindBy中的变量名称为drioDownLocator,而您传递的名称为dropDownContainer。您的班级中是否还有WebElement类型的其他变量?