当前位置: 首页 > 知识库问答 >
问题:

@未在PageFactory/页面对象框架中初始化FindBy WebElements

封德华
2023-03-14

更新代码:

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"));
}

页面对象:

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);
}

测试基地:

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
}

看起来框架没有考虑等待预期条件-元素的可见性。我怀疑与“visibilityOf(element)”的实现和@FindBy初始化元素的方式有关

异常的堆栈跟踪:

java.lang.reflect.UndeclaredThrowableException
at com.sun.proxy.$Proxy7.findElement(Unknown Source)
at org.openqa.selenium.support.pagefactory.DefaultElementLocator.findElement(DefaultElementLocator.java:69)
at org.openqa.selenium.support.pagefactory.internal.LocatingElementHandler.invoke(LocatingElementHandler.java:38)
at com.sun.proxy.$Proxy9.isDisplayed(Unknown Source)
at org.openqa.selenium.support.ui.ExpectedConditions.elementIfVisible(ExpectedConditions.java:302)
at org.openqa.selenium.support.ui.ExpectedConditions.access$100(ExpectedConditions.java:41)
at org.openqa.selenium.support.ui.ExpectedConditions$10.apply(ExpectedConditions.java:288)
at org.openqa.selenium.support.ui.ExpectedConditions$10.apply(ExpectedConditions.java:285)
at org.openqa.selenium.support.ui.FluentWait.until(FluentWait.java:238)
at com.multicom.fabrix.framework.TestBase.waitFor(TestBase.java:152)
at com.multicom.fabrix.framework.TestBase.waitForIsDisplayed(TestBase.java:141)
at com.multicom.fabrix.pageobjects.CustomerHomePage.waitForResults(CustomerHomePage.java:75)
at com.multicom.fabrix.regressiontests.FlightBookingTest.searchForAPackage(FlightBookingTest.java:23)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:86)
at org.testng.internal.Invoker.invokeMethod(Invoker.java:643)
at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:820)
at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1128)
at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:129)
at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:112)
at org.testng.TestRunner.privateRun(TestRunner.java:782)
at org.testng.TestRunner.run(TestRunner.java:632)
at org.testng.SuiteRunner.runTest(SuiteRunner.java:366)
at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:361)
at org.testng.SuiteRunner.privateRun(SuiteRunner.java:319)
at org.testng.SuiteRunner.run(SuiteRunner.java:268)
at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
at org.testng.TestNG.runSuitesSequentially(TestNG.java:1244)
at org.testng.TestNG.runSuitesLocally(TestNG.java:1169)
at org.testng.TestNG.run(TestNG.java:1064)
at org.testng.IDEARemoteTestNG.run(IDEARemoteTestNG.java:74)
at org.testng.RemoteTestNGStarter.main(RemoteTestNGStarter.java:121)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144)

Caused by: java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at com.mycompany.mymodule.webdriver.WebDriverInvoker.invokeNormally(WebDriverInvoker.java:47)
at com.mycompany.mymodule.webdriver.WebDriverInvoker.invoke(WebDriverInvoker.java:38)
... 41 more

原因:org。openqa。硒。NoSuchElementException:没有这样的元素:无法定位元素:{“方法”:“xpath”,“选择器”:“/*[@id='container']]/div/div[3]/div/div[1]/div/h3”}

共有2个答案

庞安晏
2023-03-14

@FindBy中的变量名是drioDownLocator,您要传递的变量名是dropdowncainer。您的类中是否还有WebElement类型的其他变量?

万涵亮
2023-03-14

将PageBase类切换到下面。您之前初始化了代理,但没有返回该实例,只是返回了一个新对象。否则,您可以使用'this'而不是'Customer HomePage.class'将initElements行插入Customer HomePage的构造函数中

public class PageBase extends SeleniumBase {

public CustomerHomePage customerHomePage()
{
    return PageFactory.initElements(driver, CustomerHomePage.class);
}
 类似资料:
  • 问题内容: 我在用Java工作。 我通常会这样设置一些对象: 问题是:在此示例中是否等于,按原样我可以假定对未初始化的对象进行空检查将是准确的? 问题答案: 正确,未显式初始化的引用类型的静态成员和实例成员都由Java 设置为。相同的规则适用于数组成员。 根据Java语言规范的第4.12.5节: 变量的初始值 程序中的每个变量在使用值之前都必须具有一个值: 每个类变量,实例变量或数组组件在创建时均

  • 以下是MyService应用程序: 我已经指定了@AllargsConstructor。当我试图为上面的类编写单元测试时,它失败了。以下是我如何编写单元测试: 抛出的错误是: Java:类中的构造函数MyService不能应用于给定类型;必填:未找到参数:原因:实际参数列表和正式参数列表的长度不同 但当我在MyService中显式定义构造函数时,测试就会通过。这里有人能帮我吗?

  • 在app开发中,若要使用HTML5+扩展api,必须等plusready事件发生后才能正常使用,mui将该事件封装成了mui.plusReady()方法,涉及到HTML5+的api,建议都写在mui.plusReady方法中。如下为打印当前页面URL的示例: mui.plusReady(function(){ console.log("当前页面URL:"+plus.webview.cu

  • 问题内容: 我想问一下Java初始化的格式。 我目前所知道的是: 等等 现在,在main类中,我想初始化一个,我不知道该怎么做? 问题答案: 首先,文件是对象类型,与int和double不同,它们是原始类型。我不确定您对Java有多熟悉,但是要创建一个对象,请使用该对象的构造函数。 File具有一个构造函数,该构造函数接收该文件在计算机上的位置字符串。

  • 前面一节的 Fruit 类有两个实变量,分别表述水果的类型和状态.直到为这个类写了一个定制的inspect方法,我们方才了解它不会对一个缺乏属性的水果做出合理的解释.幸运的是,Ruby提供了一种允许实变量总是被初始化的方法. initalize方法 当Ruby创建一个新对象时,它总是会寻找一个名为 initialize 的方法并执行它.因此,我们可以简单通过一个initialize方法向实变量中加