我正在尝试使用Page factory使用Appium和Page Object Model构建我的第一个框架测试。但是我无法成功启动测试,它总是返回Null指针异常。请在下面查看我的代码
BasePage:
package iPhone;
import io.appium.java_client.AppiumDriver;
import io.appium.java_client.pagefactory.AppiumFieldDecorator;
import org.openqa.selenium.support.PageFactory;
import io.appium.java_client.MobileElement;
import java.util.concurrent.TimeUnit;
public abstract class BasePage {
public static AppiumDriver<MobileElement> driver;
protected BasePage(AppiumDriver<MobileElement> driver) {
this.driver = driver;
PageFactory.initElements(new AppiumFieldDecorator(driver, 30,TimeUnit.SECONDS), this);
}
}
首页对象类:
import io.appium.java_client.AppiumDriver;
import io.appium.java_client.MobileElement;
import io.appium.java_client.ios.IOSDriver;
import io.appium.java_client.ios.IOSElement;
import io.appium.java_client.pagefactory.iOSFindBy;
import org.testng.Assert;
public class HomePage extends BasePage {
public IOSDriver<IOSElement> driver;
public HomePage(AppiumDriver driver) {
super(driver);
}
@iOSFindBy(accessibility = "NavbarLogo")
public MobileElement HomepageLogo;
@iOSFindBy(accessibility = "Make & Model")
public MobileElement HomepageMMTxt;
public void assertMakeModel(String expectedResult) {
Assert.assertEquals(expectedResult, HomepageMMTxt.getText());
}}
BaseTest类:
import AppiumSetup.AppiumBaseClass;
import org.junit.After;
import org.junit.Before;
import AppiumSetup.AppiumController;
public class BaseTest extends AppiumBaseClass{
@Before
public void setUp() throws Exception {
AppiumController.instance.start();
}
@After
public void tearDown() {
AppiumController.instance.stop();
}
}
主页测试类:
import org.testng.annotations.Test;
public class HomePageTest extends BaseTest {
@Test
public void testMakeModelText(){
HomePage oHome = new HomePage(driver());
oHome.assertMakeModel("Make & Model");
}
}
设置设备类别:
public class AppiumController {
//For Browserstack setup
private static String BS_USERNAME = "<your_browserstack_username>";
private static String BS_ACCESSKEY = "<your_browserstack_accesskey>";
private static String IOS_HASHED_APP_ID = "<your_ios_hashed_appid>";
private static String ANDROID_HASHED_APP_ID = "<your_android_hashed_appid>";
public static OS executionOS = OS.IOS;
public enum OS {
ANDROID,
IOS,
ANDROID_BROWSERSTACK,
IOS_BROWSERSTACK
}
public static AppiumController instance = new AppiumController();
public AppiumDriver driver;
public void start() throws MalformedURLException {
if (driver != null) {
return;
}
DesiredCapabilities capabilities = new DesiredCapabilities();
switch(executionOS){
case IOS:
File classpathRoot = new File(System.getProperty("user.dir"));
File appDir = new File(classpathRoot, "/src/AppDir/iOS/");
File app = new File(appDir, "Testapp.app");
capabilities.setCapability("platformName", "iOS");
capabilities.setCapability("platformVersion", "11.3");
capabilities.setCapability("forceDevice", "iPhone6");
capabilities.setCapability("deviceName", "iPhone6");
capabilities.setCapability("udid", "597cd7028828b3719dc9fe4fcbbasdadabfdfa3"); //Use this line for iOS real device
capabilities.setCapability("bundleId", "io.apptest.diva");
capabilities.setCapability("noReset", true);
capabilities.setCapability("fullReset", false);
capabilities.setCapability("language", "en");
capabilities.setCapability("locale", "en_US");
capabilities.setCapability("calendarFormat", "gregorian");
capabilities.setCapability("app", app.getAbsolutePath());
capabilities.setCapability("automationName", "XCUITest");
driver = new IOSDriver(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);
break;}
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
}
public void stop() {
if (driver != null) {
driver.quit();
driver = null;
}
}
}
Appium设置基础类:
public abstract class AppiumBaseClass {
protected AppiumDriver driver() {
return AppiumController.instance.driver;
}
}
以下错误
java.lang.NullPointerException
at io.appium.java_client.pagefactory.AppiumFieldDecorator.getTypeForProxy(AppiumFieldDecorator.java:211)
at io.appium.java_client.pagefactory.AppiumFieldDecorator.proxyForAnElement(AppiumFieldDecorator.java:226)
at io.appium.java_client.pagefactory.AppiumFieldDecorator.access$0(AppiumFieldDecorator.java:224)
at io.appium.java_client.pagefactory.AppiumFieldDecorator$3.proxyForLocator(AppiumFieldDecorator.java:111)
at org.openqa.selenium.support.pagefactory.DefaultFieldDecorator.decorate(DefaultFieldDecorator.java:62)
at io.appium.java_client.pagefactory.AppiumFieldDecorator.decorate(AppiumFieldDecorator.java:155)
at org.openqa.selenium.support.PageFactory.proxyFields(PageFactory.java:113)
at org.openqa.selenium.support.PageFactory.initElements(PageFactory.java:105)
at iPhone.BasePage.<init>(BasePage.java:17)
at iPhone.HomePage.<init>(HomePage.java:15)
at iPhone.HomePageTest.testMakeModelText(HomePageTest.java:8)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:124)
at org.testng.internal.Invoker.invokeMethod(Invoker.java:583)
at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:719)
at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:989)
at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:125)
at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:109)
at org.testng.TestRunner.privateRun(TestRunner.java:648)
at org.testng.TestRunner.run(TestRunner.java:505)
at org.testng.SuiteRunner.runTest(SuiteRunner.java:455)
at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:450)
at org.testng.SuiteRunner.privateRun(SuiteRunner.java:415)
at org.testng.SuiteRunner.run(SuiteRunner.java:364)
at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:84)
at org.testng.TestNG.runSuitesSequentially(TestNG.java:1208)
at org.testng.TestNG.runSuitesLocally(TestNG.java:1137)
at org.testng.TestNG.runSuites(TestNG.java:1049)
at org.testng.TestNG.run(TestNG.java:1017)
at org.testng.IDEARemoteTestNG.run(IDEARemoteTestNG.java:72)
at org.testng.RemoteTestNGStarter.main(RemoteTestNGStarter.java:123)