Получение ошибки при использовании FindBy в селене.Исключение нулевой точки сообщения об ошибке - PullRequest
0 голосов
/ 28 февраля 2019
import com.sun.javafx.PlatformUtil;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.How;
import org.openqa.selenium.support.ui.Select;
import org.testng.annotations.Test;

public class HotelBookingTest {
    WebDriver driver;
    @FindBy(xpath= "//*[@class='hotelApp ']")
    public static WebElement hotelLink;


    @Test
    public void shouldBeAbleToSearchForHotels() {
        setDriverPath();
        driver = new ChromeDriver();
        driver.get("https://www.cleartrip.com/");
        boolean hotelLinkDisplayed = hotelLink.isDisplayed();

       hotelLink.click();
        driver.quit();

    }
}

Получение ошибки в строке "HotelLink.click", и этот элемент hotelLink определен с использованием аннотации findBy, но с ошибкой "java.lang.NullPointerException"

Ответы [ 3 ]

0 голосов
/ 28 февраля 2019

Поскольку вы используете аннотацию @FindBy, вам необходимо инициализировать элемент перед использованием.

Вы можете сделать это, создав параметризованный конструктор, принимающий тип WebDriver в качестве аргумента.

        PageFactory.initElements(driver, this);```

and call this constructor after opening the browser.
i.e after this line 
```driver = new ChromeDriver();```
0 голосов
/ 28 февраля 2019

Для @FindBy аннотаций вам необходимо реализовать его перед поиском в WebElement.

Вы можете добавить метод, который сделает это за вас простым способом:

import com.sun.javafx.PlatformUtil;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.How;
import org.openqa.selenium.support.ui.Select;
import org.testng.annotations.Test;

import org.openqa.selenium.support.PageFactory;

public class HotelBookingTest {
    WebDriver driver;
    @FindBy(xpath= "//*[@class='hotelApp ']")
    public static WebElement hotelLink;


    @Test
    public void shouldBeAbleToSearchForHotels() {
        setDriverPath();
        driver = new ChromeDriver();
        HotelBookingTest.setPageObject(driver);
        driver.get("https://www.cleartrip.com/");
        boolean hotelLinkDisplayed = hotelLink.isDisplayed();

       hotelLink.click();
        driver.quit();

    }

    public static void setPageObject (WebDriver wd) {
        PageFactory.initElements(wd, new HotelBookingTest ());
    }
}
0 голосов
/ 28 февраля 2019

Поскольку вы используете аннотации @FindBy, вам необходимо инициализировать все веб-элементы перед его использованием.

Создать конструкцию для класса HotelBookingTest и инициализировать, используя PageFactory, как показано ниже:

import com.sun.javafx.PlatformUtil;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.How;
import org.openqa.selenium.support.ui.Select;
import org.testng.annotations.Test;

public class HotelBookingTest {
    WebDriver driver;

    @FindBy(xpath= "//*[@class='hotelApp ']")
    public WebElement hotelLink;

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

    @Test
    public void shouldBeAbleToSearchForHotels() {
        setDriverPath();
        driver = new ChromeDriver();
        new HotelBookingTest(driver);
        driver.get("https://www.cleartrip.com/");
        boolean hotelLinkDisplayed = hotelLink.isDisplayed();

        hotelLink.click();
        driver.quit();
    }
}

Импорт PageFactory из соответствующего пакета и удаление static перед `hotelLink.

Надеюсь, это поможет ...

...