Селен, бросающий нулевой указатель для wait.until - PullRequest
0 голосов
/ 24 сентября 2019

Попытка автоматизировать страницу в Salesforce, странная проблема при попытке ожидания элемента.

@FindBy(xpath = "//span[@title='console']")
private WebElement consoleTitle;

public void switchApplicationLightening(String applicationName) throws InterruptedException {
    String st = util.driver.getPageSource(); //This step to debug I am seeing null here
    String str = util.driver.findElement(By.xpath("//span[@title='console']")).getText(); // This step is not required but added to debug and this is working fine
     if(!verifyElementVisible(consoleTitle, 5)){ //Its failing here and seeing issue
        switchToApplication(applicationName);
    }
}

public static Boolean verifyElementVisible(WebElement element, int explicitWait) {
    WebDriverWait wait = new WebDriverWait(util.driver, explicitWait);
    System.out.println(util.driver);
    try {
        wait.until(ExpectedConditions.visibilityOf(element));
        return true;
    } catch (NoSuchElementException | NoSuchFrameException | NoSuchWindowException | ErrorHandler.UnknownServerException | TimeoutException e) {
        VERIFICATION_ERRORS.append("Element: ").append(element).append(" is not present on page \n -Caugth exception: ").append(e.getMessage()).append("\n\n");
        return false;
    }
}

См. Ниже ошибку на шаге - wait.until(ExpectedConditions.visibilityOf(element));

java.lang.NullPointerException
    at org.openqa.selenium.remote.RemoteWebElement.isDisplayed(RemoteWebElement.java:320)
    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.openqa.selenium.support.pagefactory.internal.LocatingElementHandler.invoke(LocatingElementHandler.java:51)
    at com.sun.proxy.$Proxy31.isDisplayed(Unknown Source)
    at org.openqa.selenium.support.ui.ExpectedConditions.elementIfVisible(ExpectedConditions.java:315)
    at org.openqa.selenium.support.ui.ExpectedConditions.access$100(ExpectedConditions.java:44)
    at org.openqa.selenium.support.ui.ExpectedConditions$10.apply(ExpectedConditions.java:301)
    at org.openqa.selenium.support.ui.ExpectedConditions$10.apply(ExpectedConditions.java:298)
    at org.openqa.selenium.support.ui.FluentWait.until(FluentWait.java:248)

Этопохоже, что ему не нравится wait.until - каждый раз, когда это называется бросающим нулевым указателем

PS: пожалуйста, игнорируйте String str = util.driver.findElement (By.xpath ("// span [@ title ='консоль'] ")) GetText ().это я поставил для отладки нелогично, так как мы ждем того же элемента ниже.Также getPageSource () имеет значение null, но следующий шаг выполняется.

Ответы [ 2 ]

0 голосов
/ 24 сентября 2019

Если вы используете @FindBy, вы должны инициализировать webElements, используя PageFactory.initElements().

public void switchApplicationLightening(String applicationName) throws InterruptedException {
    PageFactory.initElements(util.driver, this);
    String st = util.driver.getPageSource(); //This step to debug I am seeing null here
    String str = util.driver.findElement(By.xpath("//span[@title='console']")).getText(); // This step is not required but added to debug and this is working fine
     if(!verifyElementVisible(consoleTitle, 5)){ //Its failing here and seeing issue
        switchToApplication(applicationName);
    }
}

После импорта:

import org.openqa.selenium.support.PageFactory;
0 голосов
/ 24 сентября 2019

Я думаю, что из элемента кода этот метод передается как нулевой, проверьте, как вы вызываете это.Я вижу, у вас есть consoleTitle, но вы никогда не присваивали значение этой переменной.Я думаю, вы должны назначить consoleTitle = str или использовать вместо него str.

...