我正在尝试使用 Browserstack 使用 Appium 和 TestNG 编写基本测试。我正在尝试添加页面对象,但是当我尝试构造页面对象时,我不断收到空指针异常。
配置:
public class BrowserStackConfig {
protected AppiumDriver<MobileElement> appiumDriver;
@BeforeMethod(alwaysRun = true)
@Parameters("platform")
public void setUp(String platform, ITestResult testMethod) throws Exception {
Map<String, Object> config = readConfigFromFile();
DesiredCapabilities capabilities = new DesiredCapabilities();
ConfigReader reader = new ConfigReader();
String device;
String os;
String app;
switch (platform) {
case "android":
app = (String) config.get("androidApp");
device = (String) config.get("androidDevice");
os = (String) config.get("androidVersion");
break;
case "ios":
device = (String) config.get("iOSDevice");
os = (String) config.get("iOSVersion");
app = (String) config.get("iosApp");
break;
default:
throw new IllegalArgumentException("Unsupported OS: " + platform);
}
capabilities.setCapability("device", device);
capabilities.setCapability("os_version", os);
capabilities.setCapability("platformName", platform);
capabilities.setCapability("app", app);
capabilities.setCapability(MobileCapabilityType.NEW_COMMAND_TIMEOUT, 60);
capabilities.setCapability("idleTimeout", "1000");
capabilities.setCapability("settings[enableMultiWindows]", true);
String url = reader.urlBrowserStack(testMethod);
if ("android".equals(platform)) {
appiumDriver = new AppiumDriver<>(new URL(url), capabilities);
} else {
appiumDriver = new IOSDriver<>(new URL(url), capabilities);
}
}
private Map<String, Object> readConfigFromFile() throws IOException {
Yaml yaml = new Yaml();
try (FileReader fileReader = new FileReader("browserstack.yml")) {
return yaml.load(fileReader);
}
}
@AfterMethod(alwaysRun = true)
public void tearDown(ITestResult testResult) throws Exception {
ResultsListener result = new ResultsListener(appiumDriver);
if (ITestResult.SUCCESS == testResult.getStatus()) {
result.passResult(appiumDriver);
Thread.sleep(3000);
} else {
result.failResult(appiumDriver);
Thread.sleep(3000);
}
appiumDriver.quit();
}
}
基本页面:
public class BasePage extends BrowserStackConfig{
protected AppiumDriver<MobileElement> appiumDriver;
public BasePage (AppiumDriver<MobileElement> appiumDriver) {
appiumDriver = this.appiumDriver;
}
}
登陆页面:
public class LandingPage extends BasePage {
public LandingPage(AppiumDriver<MobileElement> appiumDriver) {
super(appiumDriver);
}
// Element locators
private AndroidElement logInBtn = (AndroidElement ) new WebDriverWait(appiumDriver, 10).until(
ExpectedConditions.visibilityOfElementLocated(MobileBy.id(
"button_log_in"
))
);
// Methods
public boolean isInitialized() {
try {
return logInBtn.isDisplayed();
} catch (Exception e) {
return false;
}
}
}
和应用测试
public class AppTest extends BrowserStackConfig {
private final SoftAssert assertion = new SoftAssert();
@Test
public void test() throws Exception {
LandingPage landingPage = new LandingPage(appiumDriver);
// assert landing page is loaded
assertion.assertTrue(landingPage.isInitialized());
}
}
当我运行测试时,我得到
AppTest.test » NullPointer
对应线路 LandingPage landingPage = new LandingPage(appiumDriver);
我已经尝试了所有能找到的初始化和构建页面对象的组合,但每当我尝试构建页面时,我总是收到 NPE。
弄清楚了,为后代发布答案
一个重要的词:
protected
变量
appiumDriver
在BrowserStackConfig中受到保护,因此当我尝试将其传递到landingPage以创建页面对象时,它接收到null。将第一行更改为 public AppiumDriver<MobileElement> appiumDriver;
解决了问题。
(此外,BasePage 不应该扩展 BrowserStackConfig)