如何使用硒,cucmber和gradle修复“ java.lang.NullPointerException”?

时间:2019-07-16 23:40:34

标签: java selenium nullpointerexception

我尝试编写一些测试,但是驱动程序有问题。 我敢肯定,我对驱动程序有误。

BaseTest -对于初始化浏览器,设置一些选项

  public class BaseTest {
    public WebDriver driver;
    protected Properties prop;

    @BeforeClass
    public void init() throws IOException {
        loadSettings();
        if (prop.getProperty("browser.name").equals("chrome")) {
            System.setProperty("webdriver.chrome.driver", "D:\\SeleniumJava\\chromedriver.exe");
            ChromeOptions options = new ChromeOptions();
            options.addArguments("disable-infobars");
            //options.addArguments("--start-maximized");
            driver = new ChromeDriver(options);

        }
        driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
        driver.manage().window().maximize();
    }

    private void loadSettings() throws IOException {
        prop = new Properties();
        InputStream in = ClassLoader.getSystemResourceAsStream("settings.properties");
        prop.load(in);
        in.close();
    }
}

登录页面,我删除了凭据。

    public class LoginPage{

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

    public final String boardGameGeekHome_URL = "https://www.boardgamegeek.com/";
    public final String loginName = "xx";
    public final String password = "xx";

    @FindBy(how = How.XPATH, using = "//input[@id='login_username']")
    public WebElement loginUserNameInput;

    @FindBy(how = How.XPATH, using = "//input[@id='login_password']")
    public WebElement loginPasswordInput;

    @FindBy(how = How.XPATH, using = "//input[@type='submit']")
    public WebElement submitButton;

    public void loginUser(String loginName, String passoword)
    {
        loginUserNameInput.sendKeys(loginName);
        loginPasswordInput.sendKeys(passoword);
        submitButton.click();
    }
}

测试的一部分

public class CompareLangDepStep extends BaseTest {

@Given("User account with a game collection")
public void UserAccountWithAGameCollection() {
    LoginPage LoginPage = new LoginPage(driver);
    driver.get(LoginPage.boardGameGeekHome_URL);
    LoginPage.loginUser(LoginPage.loginName, LoginPage.password);
}

错误输出

java.lang.NullPointerException
    at Steps.CompareLangDepStep.UserAccountWithAGameCollection(CompareLangDepStep.java:30)
    at ✽.User account with a game collection

这看起来很奇怪,但我对黄瓜只有这个问题。

1 个答案:

答案 0 :(得分:0)

必须正确初始化驱动程序-查看更新的代码,

Source source = Source.CACHE;
        firebaseFirestore
                .collection("collectionname")
                .get(source)
                .addOnSuccessListener(new OnSuccessListener<QuerySnapshot>() {
                    @Override
                    public void onSuccess(QuerySnapshot documentSnapshots) {
                        if (documentSnapshots.isEmpty()) {

                            return;
                        } else {
                            // Convert the whole Query Snapshot to a list
                            // of objects directly! No need to fetch each
                            // document.
                            List<ModelClass> types = documentSnapshots.toObjects(ModelClass.class);
                            // Add all to your list
                            mArrayList.addAll(types);
                        }

                    }
                })
                .addOnFailureListener(new OnFailureListener() {
                    @Override
                    public void onFailure(@NonNull Exception e) {

                    }
                });