我尝试在使用Appium的移动自动化中使用时遇到异常。虽然它适用于Web测试自动化。
*Given The app is opened # CommonSteps.OpenApp()
Then I should see "Skip button" on welcome screen # WelcomeScreenSteps.CheckElementPresent(String)
cucumber.runtime.CucumberException: Failed to instantiate class steps.WelcomeScreenSteps
at cucumber.runtime.java.DefaultJavaObjectFactory.cacheNewInstance(DefaultJavaObjectFactory.java:40)
at cucumber.runtime.java.DefaultJavaObjectFactory.getInstance(DefaultJavaObjectFactory.java:26)
at cucumber.runtime.java.JavaStepDefinition.execute(JavaStepDefinition.java:37)
at cucumber.runtime.StepDefinitionMatch.runStep(StepDefinitionMatch.java:37)
at cucumber.runtime.Runtime.runStep(Runtime.java:298)
at cucumber.runtime.model.StepContainer.runStep(StepContainer.java:44)
at cucumber.runtime.model.StepContainer.runSteps(StepContainer.java:39)
at cucumber.runtime.model.CucumberScenario.run(CucumberScenario.java:48)
at cucumber.runtime.model.CucumberFeature.run(CucumberFeature.java:163)
at cucumber.runtime.Runtime.run(Runtime.java:120)
at cucumber.api.cli.Main.run(Main.java:36)
at cucumber.api.cli.Main.main(Main.java:18)
Caused by: java.lang.reflect.InvocationTargetException
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at cucumber.runtime.java.DefaultJavaObjectFactory.cacheNewInstance(DefaultJavaObjectFactory.java:34)
... 11 more
Caused by: java.lang.RuntimeException: java.lang.InstantiationException: pages.Pages
at org.openqa.selenium.support.PageFactory.instantiatePage(PageFactory.java:136)
at org.openqa.selenium.support.PageFactory.initElements(PageFactory.java:66)
at steps.WelcomeScreenSteps.<init>(WelcomeScreenSteps.java:16)
... 16 more
Caused by: java.lang.InstantiationException: pages.Pages
at java.lang.Class.newInstance(Unknown Source)
at org.openqa.selenium.support.PageFactory.instantiatePage(PageFactory.java:133)
... 18 more*
我们可以在Appium Mobile自动化中使用PageFactory.initElements吗?
功能文件:
Feature: As a user, I want to open the app when not logged in and check the welcome screens
@TestTag
Scenario: Open app when not logged in and check 3 welcome screens using swipes
Given The app is opened
Then I should see "Skip button" on welcome screen
步骤文件:
public class WelcomeScreenSteps extends DriverStore {
// WebDriver driver = new FirefoxDriver();
Pages pages = PageFactory.initElements(driver, Pages.class);
//WelcomeSreenPage welcomeScreen = new WelcomeSreenPage(driver);
public WelcomeScreenSteps() {
// System.out.println("I am in Welcome Constructor");
}
@Given("^the app is open$")
public void the_app_is_open() throws Throwable {
// Code to open the app
System.out.println("in the step");
}
@Then("^I should see \"(.*?)\" on welcome screen$")
public void CheckElementPresent(String identifire) {
Assert.assertTrue(pages.welcomeScreen.checkElementPresent(identifire));
}
Pages.java
public class Pages {
/*AppiumDriver driver;*/
static AppiumDriver driver;
public WelcomeSreenPage welcomeScreen = PageFactory.initElements(driver, WelcomeSreenPage.class);
/*public Pages(AppiumDriver driver) {
this.driver = driver;
}*/
public Pages(AppiumDriver driver) {
Pages.driver = driver;
}
public WelcomeSreenPage welcomeScreen() {
welcomeScreen = new WelcomeSreenPage(driver);
return welcomeScreen;
}
}
WelcomeScreenPage.java
public class WelcomeSreenPage {
// all mobile element identifier here
// WebDriver driver;
AppiumDriver driver;
public boolean checkElementPresent(String identifire) {
MobileElement element = actions.getElement(mapElement(identifire),
driver);
if (element != null)
return true;
else
return false;
}
答案 0 :(得分:0)
与我,我的项目Selenium,TestNG和Appium一起使用时,使用PageFactory.initElements
public class Login extends Setup {
@Test
public void loginAlert() throws InterruptedException {
Button button = new Button(driver);
PageFactory.initElements(driver, button);
Input input = new Input(driver);
PageFactory.initElements(driver, input);
Input input1 = new Input(driver);
System.out.println("Test Alert Login");
button.ById("navigation_more");
button.ById("btnLogin");
input.ById("et_email_or_phone_number", "dikakoko.com");
input1.ById("tet_password", "dikakoko");
}
}
下面是我上面调用的函数。
public class Input {
AppiumDriver<MobileElement> driver;
Root root = new Root();
public Input(AppiumDriver<MobileElement> driver) {
this.driver = driver;
}
public void ById(String selector, String textValue) {
MobileElement element = driver.findElement(By.id(root.element() + ":id/" + selector));
waitForVisible(driver, element);
Actions actions = new Actions(driver);
actions.moveToElement(element);
actions.click();
actions.sendKeys(textValue);
actions.build().perform();
System.out.println("Input: " + textValue);
}
private void waitForVisible(AppiumDriver<MobileElement> driver, MobileElement element) {
try {
Thread.sleep(5000);
System.out.println("Waiting for element visibility");
WebDriverWait wait = new WebDriverWait(driver, 20);
wait.until(ExpectedConditions.visibilityOf(element));
} catch (Exception e) {
e.printStackTrace();
}
}
}
和这个
public class Button {
AppiumDriver<MobileElement> driver;
Root root = new Root();
public Button(AppiumDriver<MobileElement> driver) {
this.driver = driver;
}
public void ById(String selector) {
MobileElement element = driver.findElement(By.id(root.element() + ":id/" + selector));
Actions actions = new Actions(driver);
actions.moveToElement(element);
actions.click();
actions.build().perform();
System.out.println("Button is Clicked!");
}
}
我用这个
Button button = new Button(driver);
PageFactory.initElements(driver, button);
Input input = new Input(driver);
PageFactory.initElements(driver, input);