Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/375.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 实现Pagefactory和显式等待时出错_Java_Selenium_Selenium Webdriver_Webdriver - Fatal编程技术网

Java 实现Pagefactory和显式等待时出错

Java 实现Pagefactory和显式等待时出错,java,selenium,selenium-webdriver,webdriver,Java,Selenium,Selenium Webdriver,Webdriver,我正在使用Pagefactory,我想在页面中的某些方法中实现显式等待。下面是我的代码的总体结构: public class HomePage{ private String username; private String password; ... @FindBy(id= "username") public static WebElement LoginUserName; ... public LoginPage(RemoteWebDriver driver, ExtentTest

我正在使用Pagefactory,我想在页面中的某些方法中实现显式等待。下面是我的代码的总体结构:

public class HomePage{ 

private String username;
private String password;
...

@FindBy(id= "username")
public static WebElement LoginUserName;
...

public LoginPage(RemoteWebDriver driver, ExtentTest test, Properties config) {
     this.driver = driver;
     this.test = test;
     this.config = config;
     PageFactory.initElements(driver,this);
}

public void enterUserName(String userName) throws Exception {
     WebDriverWait wait = new WebDriverWait(driver, 10);
     wait.until(ExpectedConditions.elementToBeClickable(LoginUserName));
     if (LoginUserName.isDisplayed()) {
            LoginUserName.sendKeys(userName);
     } else {
            test.log(LogStatus.ERROR, "Element Not found");
            throw new Exception("Element not found" + LoginUserName.toString());
     }
}
...
上面的代码工作正常,但是如果我定义
WebDriverWait wait=newwebdriverwait(driver,10)
enterUserName
之外,我得到一个错误。我想在外部定义
WebDriverWait wait
,因为我也想在其他方法中使用它。 那么,我如何定义
WebDriverWait wait=newwebdriverwait(driver,10)
enterUserName
之外而不出错

错误未显示有用的信息(
捕获的异常;>>>>:null


感谢您的帮助:)

我认为将wait对象初始化移动到类构造函数应该可以解决这个空指针问题

 public class HomePage{ 

private String username;
private String password;
private WebDriverWait wait;
...

@FindBy(id= "username")
public static WebElement LoginUserName;
...

public LoginPage(RemoteWebDriver driver, ExtentTest test, Properties config) {
     this.driver = driver;
     this.test = test;
     this.config = config;
     PageFactory.initElements(driver,this);
     this.wait = new WebDriverWait(this.driver, 10);
}

public void enterUserName(String userName) throws Exception {

     wait.until(ExpectedConditions.elementToBeClickable(LoginUserName));
     if (LoginUserName.isDisplayed()) {
            LoginUserName.sendKeys(userName);
     } else {
            test.log(LogStatus.ERROR, "Element Not found");
            throw new Exception("Element not found" + LoginUserName.toString());
     }
}
对吗? 根据您的代码: 课程名称:主页

public class HomePage{
构造函数:LoginPage

public LoginPage(RemoteWebDriver driver, ExtentTest test, Properties config) {
如果您正在创建一个框架,我建议将所有实用程序方法保存在一个单独的文件中

    /**
 * Explicit Wait for Element To Be Clickable.
 * @param driver
 * @param seconds
 * @param element
 */
public static void explicitWaitElementToBeClickable(WebDriver driver, int seconds, WebElement element) {
    try {
        wait = new WebDriverWait(driver, seconds);
        wait.until(ExpectedConditions.elementToBeClickable(element));
    } catch (NoSuchElementException e) {
        e.printStackTrace();
    }
}
然后使用类似于:

WaitUtility.explicitWaitElementToBeClickable(driver, 10, loginUserName);

您可以发布错误的完整堆栈跟踪吗?你是如何创建页面对象的?我有一个实用的方法,这是一个很好的建议。我以后再做。但是可用的代码有什么问题?