在 BDD Cucumber Selenium UI 中获取空指针

Got Null Pointer in BDD Cucumber Selenium UI

提问人:dody 提问时间:10/14/2023 最后编辑:hfontanezdody 更新时间:10/18/2023 访问量:41

问:

这是我的黄瓜特色

@Regression
Feature: Add to Cart Feature

  Background: user successfully login to website saucedemo
    Given [ui] user open web url "https://www.saucedemo.com/"
    And [ui] user input username "standard_user" and password "secret_sauce"
    When [ui] user click login button
    Then [ui] user success login and redirected to homepage

  @AddToCartTestCase-01
  Scenario: user success add one product to cart
    Given [ui] user click button add to cart on products page
    Then [ui] user should see count of product is "1" in cart on products page

在这里,我对用户的步骤定义在LoginSteps中访问网站.java

public class LoginSteps {

    WebDriver driver;

    LoginPages loginPages;

    @Given("[ui] user open web url {string}")
    public void uiUserOpenWebUrl(String url) {
        WebDriverManager.chromedriver().setup();
        driver = new ChromeDriver();
        driver.get(url);
        driver.manage().window().maximize();
        loginPages = PageFactory.initElements(driver, LoginPages.class);
    }

    @And("[ui] user input username {string} and password {string}")
    public void uiUserInputUsernameAndPassword(String username, String password) {
        loginPages.fillFormUsername(username);
        loginPages.fillFormPassword(password);
    }

    @When("[ui] user click login button")
    public void uiUserClickLoginButton() {
        loginPages.clickLoginButton();
    }

    @Then("[ui] user success login and redirected to homepage")
    public void uiUserSuccessLoginAndRedirectedToHomepage() {
        assertThat("wrong homepage", driver.getCurrentUrl(), equalTo("https://www.saucedemo.com/inventory.html"));
    }

    @Then("[ui] user should see error message {string}")
    public void uiUserShouldSeeErrorMessage(String errorMessage) {
        assertThat("wrong error message", loginPages.errorMessage(), equalTo(errorMessage));
    }

    @And("[ui] user close the browser")
    public void uiUserCloseTheBrowser() {
        driver.close();
        driver.quit();
    }

}

在这里,我在AddToCartSteps中为用户将产品添加到购物车的步骤定义.java

public class AddToCartSteps {
    AddToCartPages addToCartPages;

    @Given("[ui] user click button add to cart on products page")
    public void uiUserClickButtonAddToCartOnProductsPage() {
        addToCartPages.clickAddToCartButton();
    }

    @Then("[ui] user should see count of product is {string} in cart on products page")
    public void uiUserShouldSeeCountOfProductIsInCartOnProductsPage(String count) {
        assertThat("number of products in the cart does not change", addToCartPages.getCountOfProduct(), equalTo(count));
    }

}

如果我运行我的,我会得到一个步骤“给定 [ui] 用户单击按钮添加到产品页面上的购物车”。@AddToCartTestCase-01java.lang.NullPointerException

有人可以帮助我,这样我就可以运行我的方案了吗?谢谢。

这是我的文件夹项目

java selenium-webdriver cucumber bdd

评论

1赞 M.P. Korstanje 10/15/2023
您得到 NPE,因为您没有将值设置为 。这将要求您在步骤定义文件之间共享 Web 驱动程序。若要在步骤定义之间共享 Web驱动程序,请查看 github.com/cucumber/cucumber-jvm/tree/main/...addToCartPages

答: 暂无答案