【发布时间】:2021-07-08 01:43:06
【问题描述】:
在我的测试课程中,我为 Appium 测试设置了 DesiredCapabilities。在那个类中,我初始化了包含 pagefactory 元素的 BasePage 类。当我运行测试时,它按预期工作。
现在,我尝试将 DediredCapabilities 移到一个单独的类 CapacityManager 中,从而更具创造性。在我的测试类中,我从CapacityManager 中调用了保存DesiredCapabilities 的方法。方法调用成功,我的应用程序已启动,但 pagefactory 元素不再工作。我不知道为什么。
当我运行测试时,我在第一个移动元素上得到一个 nullPointerExceptio,这让我怀疑是 pagefactory 或驱动程序初始化问题。
工作测试类如下所示:
public class LoginTest {
private final BaseUtil baseUtil = new BaseUtil();
private static BasePage basePage;
public static AndroidDriver<AndroidElement> driver;
public static File classpathRoot;
public void startApp() throws MalformedURLException {
classpathRoot = new File(System.getProperty("user.dir"));
File appDir = new File(classpathRoot, "");
File app = new File(appDir, baseUtil.getMyApp());
DesiredCapabilities cap = new DesiredCapabilities();
cap.setCapability(MobileCapabilityType.PLATFORM_NAME, "Android");
cap.setCapability(MobileCapabilityType.PLATFORM_VERSION, "11.0");
cap.setCapability(MobileCapabilityType.DEVICE_NAME, "Android Emulator");
cap.setCapability(MobileCapabilityType.AUTOMATION_NAME, "uiautomator2");
cap.setCapability(MobileCapabilityType.APP, app.getAbsolutePath());
URL url = new URL("http://localhost:4723/wd/hub");
driver = new AndroidDriver<>(url, cap);
basePage = PageFactory.initElements(driver, BasePage.class); //pagefactory initialised
}
public void login(String sheetname, int rowNumber) throws InterruptedException, IOException, InvalidFormatException {
FeatureUtils featureUtils = new FeatureUtils();
File excelDir = new File(classpathRoot, "");
File exceldoc = new File(excelDir, baseUtil.getUsersExcelDoc());
List<Map<String, String>> testData = featureUtils.getData(exceldoc.getAbsolutePath(), sheetname);
String username = testData.get(rowNumber).get("username");
String password = testData.get(rowNumber).get("password");
basePage.yesIAgreeButton.click(); //first element clicks successfully
这是包含pagefactory 元素的 BasePage 类:
public class BasePage {
private final WebDriver driver;
public BasePage(WebDriver driver) { //constructor
this.driver = driver;
}
@FindBy(id = "com.test")
public WebElement yesIAgreeButton;
使用上面的两个类,LoginTest 类按预期工作。
现在,我从测试类中删除了 DesiredCapabilities 并将它们放入一个新类中,CapacityManager:
public class CapacityManager {
public static AndroidDriver<AndroidElement> driver;
public static File classpathRoot;
private final BaseUtil baseUtil = new BaseUtil();
private static BasePage basePage;
public DesiredCapabilities appDesiredCapabilities() throws MalformedURLException {
DesiredCapabilities desiredCapabilities = null;
classpathRoot = new File(System.getProperty("user.dir"));
File appDir = new File(classpathRoot, "");
File app = new File(appDir, baseUtil.getMyApp());
DesiredCapabilities cap = new DesiredCapabilities();
cap.setCapability(MobileCapabilityType.PLATFORM_NAME, "Android");
cap.setCapability(MobileCapabilityType.PLATFORM_VERSION, "11.0");
cap.setCapability(MobileCapabilityType.DEVICE_NAME, "Android Emulator");
cap.setCapability(MobileCapabilityType.AUTOMATION_NAME, "uiautomator2");
cap.setCapability(MobileCapabilityType.APP, app.getAbsolutePath());
URL url = new URL("http://localhost:4723/wd/hub");
driver = new AndroidDriver<>(url, cap);
basePage = PageFactory.initElements(driver, BasePage.class); //pagefactory initialisation
return desiredCapabilities;
}
}
然后我从测试类中调用了DesiredCapabilities,如下所示:
public class LoginTest {
private final BaseUtil baseUtil = new BaseUtil();
private static BasePage basePage;
public static AndroidDriver<AndroidElement> driver;
public static File classpathRoot;
CapacityManager capacityManager = new CapacityManager();
public void startApp() throws MalformedURLException {
capacityManager.appDesiredCapabilities(); //method call. App launches successfully.
}
public void login(String sheetname, int rowNumber) throws InterruptedException, IOException, InvalidFormatException {
FeatureUtils featureUtils = new FeatureUtils();
File excelDir = new File(classpathRoot, "");
File exceldoc = new File(excelDir, baseUtil.getUsersExcelDoc());
List<Map<String, String>> testData = featureUtils.getData(exceldoc.getAbsolutePath(), sheetname);
String username = testData.get(rowNumber).get("username");
String password = testData.get(rowNumber).get("password");
basePage.yesIAgreeButton.click(); //getting nullpointer error on this line. Looks like an issue with the pagefactory elements initialisation. basePage is null.
driver.pressKey(new KeyEvent(AndroidKey.TAB));
【问题讨论】:
标签: java selenium-webdriver appium page-factory