wait.until(ExpectedConditions.visibilityOf(userName))无效

时间:2017-12-05 15:17:19

标签: java selenium

在第33行以下代码中,在wait.until

附近收到以下错误消息

此行有多个标记      - 无法解析com.google.common.base.Function类型。它是从所需的.class文件间接引用的      - FluentWait类型中的(Function)方法不适用于      参数(ExpectedCondition)

   package MPA;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.PageFactory;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

public class LoginhelperClass {

    WebDriver driver;

    @FindBy(xpath = "//input[@ng-model='login.username']")
    private WebElement userName;

    @FindBy(xpath = "//input[@type='password']")
    private WebElement Password;

    @FindBy(xpath = "//input[@type='submit' and @value='Sign In']")
    private WebElement SignIn;

    @FindBy(xpath = "//span[@class='icon-avatar-round']")
    private WebElement Avatar;

    public LoginhelperClass(WebDriver driver) {
        this.driver = driver;
        PageFactory.initElements(driver, this);
    }

    public void logIn(String uName, String Pswd) throws InterruptedException {
        WebDriverWait wait=new WebDriverWait(driver,7);
        wait.until(ExpectedConditions.visibilityOf(userName));
        userName.sendKeys(uName);
        Password.sendKeys(Pswd);
        SignIn.click();
        try {
            Thread.sleep(4000);
            if (Avatar.isDisplayed()) {
                System.out.println("Login Successfull: " + uName);
            } else {
                System.out.println("Login failure check the credentials: " + uName);
            }
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
    }

}




 package MPA;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.PageFactory;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

public class LoginhelperClass {

    WebDriver driver;

    @FindBy(xpath = "//input[@ng-model='login.username']")
    private WebElement userName;

    @FindBy(xpath = "//input[@type='password']")
    private WebElement Password;

    @FindBy(xpath = "//input[@type='submit' and @value='Sign In']")
    private WebElement SignIn;

    @FindBy(xpath = "//span[@class='icon-avatar-round']")
    private WebElement Avatar;

    public LoginhelperClass(WebDriver driver) {
        this.driver = driver;
        PageFactory.initElements(driver, this);
    }

    public void logIn(String uName, String Pswd) throws InterruptedException {
        WebDriverWait wait=new WebDriverWait(driver,7);
        wait.until(ExpectedConditions.visibilityOf(userName));
        userName.sendKeys(uName);
        Password.sendKeys(Pswd);
        SignIn.click();
        try {
            Thread.sleep(4000);
            if (Avatar.isDisplayed()) {
                System.out.println("Login Successfull: " + uName);
            } else {
                System.out.println("Login failure check the credentials: " + uName);
            }
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
    }

}

2 个答案:

答案 0 :(得分:1)

您看到的错误如下:

Multiple markers at this line - The type com.google.common.base.Function cannot be resolved. It is indirectly referenced from required .class files - The method until(Function) in the type FluentWait is not applicable for the arguments (ExpectedCondition)

让我们分析代码中发生的事情。

Page Object中,您已将WebElement定义为:

@FindBy(xpath = "//input[@ng-model='login.username']")
private WebElement userName;

继续前进,您尝试在方法中使用WebElement

public void logIn(String uName, String Pswd) throws InterruptedException {
    WebDriverWait wait=new WebDriverWait(driver,7);
    wait.until(ExpectedConditions.visibilityOf(userName));
    //
}

所以基本上我们在这里等待间接引用 Angular WebElement userName 的可见性,这取决于属性 ng-model 。在这种情况下,我们永远无法确定在方法 userName 中引用WebElement时,必须强制引用非NULL 值。它也可能是 NULL

现在让我们看一下 visibilityOf 方法定义,它被定义为:

visibilityOf(WebElement element)
An expectation for checking that an element, known to be present on the DOM of a page, is visible.

很明显, visibilityOf 方法需要WebElement直接引用。因此,如果没有 userName 的明确值(非NULL ), WebDriverWait ,这是<的变体< strong> FluentWait 显示错误。

答案 1 :(得分:0)

使用By / WebElement播放条件:

ExpectedConditions.presenceOfAllElementsLocatedBy(By)
ExpectedConditions.elementExists(By)
ExpectedConditions.elementIsVisible(By)
ExpectedConditions.elementToBeClickable(By/WebElement)
ExpectedConditions.visibilityOfAllElementsLocatedBy(By)