我无法使我的代码正常工作...我将Appium与TestNG和Cucumber一起使用
这是我的Base.java
public class Base {
public AndroidDriver driver;
}
这是我的Hooks.java
public class Hooks extends Base{
private Base base;
public Hooks (Base base) {
this.base = base;
}
@Before("@homeScreenTest")
public void init() throws MalformedURLException {
DesiredCapabilities cap = new DesiredCapabilities();
cap.setCapability("no",true);
cap.setCapability(MobileCapabilityType.DEVICE_NAME, "Android Emulator");
cap.setCapability("platformVersion", "9");
cap.setCapability("platformName", "Android");
cap.setCapability(MobileCapabilityType.NEW_COMMAND_TIMEOUT, "60");
cap.setCapability("noRest", true);
cap.setCapability("autoGrantPermissions",true);
cap.setCapability("appPackage","myPackage");
cap.setCapability("appWaitActivity", "myActivity");
File f = new File("src");
File fs = new File(f, "app");
cap.setCapability(MobileCapabilityType.APP, fs.getAbsolutePath());
base.driver = new AndroidDriver<WebElement>(new URL("http://127.0.0.1:4723/wd/hub"), cap);
base.driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
}
}
这是我的Page对象类LanguageSelect.java
public class LanguageSelect extends Base{
public Base base;
public LanguageSelect (Base base) {
super();
this.base= base;
PageFactory.initElements(base.driver,this);
}
@AndroidFindBy(xpath = "//android.widget.Button[@text='SET LANGUAGE']")
public WebElement setLanguage;
}
最后这是我的步骤定义文件LandingScreenTest.java
public class LandingScreenTest extends Base{
private Base base;
public LandingScreenTest(Base base){
this.base = base;
}
@Then("^I click on button $")
public void i_click_on_something(String strArg1) throws Throwable {
LanguageSelect ls = new LanguageSelect(base);
ls.setLanguage.click();
}
}
现在按钮根本不单击,它在那里,它是可见的,如果我在LandingScreenTest.java中编写此按钮,它将起作用
WebElement setLng = base.driver.findElementByXPath("//android.widget.Button[@text='SET LANGUAGE']");
setLng.click();
但是,如果我尝试使用页面对象,则会得到
"org.openqa.selenium.InvalidSelectorException: Locator Strategy 'name' is not supported for this session"
有时甚至没有初始化驱动程序。我已经研究了整个stackoverflow和大量的教程,但我无法正确解决问题。
之所以将Base.java中的代码与Hooks.java中的代码分开是因为Cucumber不允许我扩展带有注释的类。
我想念什么?请帮助
注意:我没有发布运行器类或功能文件,除了尝试从页面对象类中获取对象外,其他所有东西都起作用。
答案 0 :(得分:0)
我设法使其正常工作。因此基本上是在LanguageSelect类而不是
public class LanguageSelect extends Base{
public Base base;
public LanguageSelect (Base base) {
super();
this.base= base;
PageFactory.initElements(base.driver,this);
}
我写了这个
public class LanguageSelect extends Base{
public LanguageSelect (AndroidDriver driver) {
this.driver = driver;
PageFactory.initElements(new AppiumFieldDecorator(driver),this);
}
它工作得很好,其他类中的其他所有东西都完全相同