调用 setUp 方法之前的 Appium 错误

Appium error before setUp method called

运行 测试时出现错误:

java.lang.NullPointerException
at loginTest.loginWithInvalidEmail(loginTest.java:47)

一切都设置得很好,除了问题出在这段代码中。

当我将 setUp() 方法代码放入 loginWithInvalidEmail() 方法时,测试为 运行 并成功通过。但最好不要将此方法包含在测试方法中。或者也许我最终应该创建摘要 class?

public class 登录测试 {

AppiumDriver driver;

@Before
public AppiumDriver setUp() {

    DesiredCapabilities capabilities = new DesiredCapabilities();
    capabilities.setCapability("deviceName", "AndroidTestDevice");

    try {
        driver = new AndroidDriver(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);
    } catch (MalformedURLException e) {
        e.printStackTrace();
    }
    return driver;

}

@After
public void tearDown() throws Exception {
    driver.quit();
}

@Test
public void loginWithInvalidEmail() throws Exception {

    WebElement emailLoginButton = driver.findElement(By.id("signupButton"));
    emailLoginButton.click();

    WebElement phoneNumberField = driver.findElement(By.id("phoneInput"));
    phoneNumberField.sendKeys("+33340954");

    WebElement firstNameField = driver.findElement(By.id("firstName"));
    firstNameField.sendKeys("Name");

    WebElement lastNameField = driver.findElement(By.id("lastName"));
    lastNameField.sendKeys("Name2");

    WebElement loginButton = driver.findElement(By.id("loginButton"));
    loginButton.click();

    WebElement invalidPhoneNumberErrorMessage = (new WebDriverWait(driver, 60))
            .until(ExpectedConditions.presenceOfElementLocated(By.id("message")));
    assertEquals(invalidPhoneNumberErrorMessage.getText(), "The phone number is incorrect, country code is required");



}

} 眼镜: 运行 在真实 Android Nexus 7 上测试,MAC OSX Yosemite,Appium v​​ 1.4.8,在 IntelliJ[=12= 上测试 运行 ]

所以我在每个测试中调用 setUp() 和 tearDown() 方法,这样就可以了。