TestNG Selenium:为什么@Before可以正常工作,但是@BeforeTest抛出NullPointerException?

时间:2018-10-21 15:38:24

标签: java selenium cucumber testng

这是我的步骤定义类,失败了,驱动程序获取URL的第一个@Test抛出NullPointerException:

public class stepDefinitionASOS extends base {

    AsosElements ae;

    @BeforeTest
    public void initializeTest() throws IOException {
        driver = initializeDriver();
        PageFactory.initElements(driver, this);
        driver.manage().window().maximize();
        ae = new AsosElements(driver);
    }

    @Test
    @Given("^User goes to the \"([^\"]*)\" website$")
    public void user_navigates_to_the_ASOS_website(String url) throws Throwable {
        driver.get(url);
    }

    @Test
    @Given("^User navigates to the account page$")
    public void user_navigates_to_the_account_page() throws Throwable {
        ae.clickAsosMyAccountDropdown(driver);
        ae.clickAsosMyAccountButton(driver);
    }

    @Test
    @When("^User logs in with (.+) and (.+)$")
    public void user_logs_in_with_username_and_password(String username, String password) throws Throwable {
        ae.typeEmailAddress(username);
        ae.typePassword(password);
        ae.clickSignInButton();
    }

    @Test
    @Then("^Login should be successful$")
    public void login_successful_is_something() throws Throwable {
        Assert.assertTrue(ae.getMyAccountTitle().isDisplayed());
    }

    @AfterTest
    public void teardown() {
        driver.close();
        driver = null;
    }
 }

当我使用@Before Cucumber注释而不是@BeforeTest TestNG注释时,这些测试似乎运行良好。

这是步骤定义类继承自的基类:

公共类库{

public WebDriver driver;
public Properties prop;
public static final String USERNAME = ""; // I have blanked this out for security reasons
public static final String AUTOMATE_KEY = ""; // I have blanked this out for security reasons
public static final String URL = "https://" + USERNAME + ":" + AUTOMATE_KEY + "@hub-cloud.browserstack.com/wd/hub";

public WebDriver initializeDriver() throws IOException {

    DesiredCapabilities caps = new DesiredCapabilities();
    DesiredCapabilities capability = new DesiredCapabilities();
    caps.setCapability("os", "OS X");
    caps.setCapability("os_version", "High Sierra");
    caps.setCapability("browser", "Firefox");
    caps.setCapability("browser_version", "54.0");
    caps.setCapability("browserstack.local", "false");
    caps.setCapability("browserstack.selenium_version", "3.5.2");
    driver = new RemoteWebDriver(new URL(URL), caps);
    driver.manage().timeouts().implicitlyWait(10,TimeUnit.SECONDS);
    return driver;
}

功能文件:

@AsosLogin
Feature: Logging into ASOS
Scenario Outline: Going to ASOS website and logging in correct details
Given User goes to the "https://www.asos.com" website
And User navigates to the account page
When User logs in with <username> and <password>
Then Login should be successful

Examples:
 |username |password |
 |random@hotmail.co.uk |password  |

StackTrace:

  

java.lang.NullPointerException在   stepDefinitions.stepDefinitionASOS.user_navigates_to_the_ASOS_website(stepDefinitionASOS.java:24)   在✽.Given,用户转到“ https://www.asos.com”   网站(C:/Users/aliba/Documents/googleTingYaKnaDisOne/src/test/java/features/asosLogin.feature:5)

有人知道我该怎么解决吗?

0 个答案:

没有答案