TestNG Selenium: почему @Before работает нормально, но @BeforeTest выдает исключение NullPointerException? - PullRequest
0 голосов
/ 21 октября 2018

Это мой класс определения шага, который не работает, первый @Test, где драйвер получает URL, выбрасывает 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 {

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) в to.Givehttps://www.asos.com" веб-сайт (C: /Users/aliba/Documents/googleTingYaKnaDisOne/src/test/java/features/asosLogin.feature: 5)

Кто-нибудь знает, как я могу исправить это?

...