首先您必须创建加载驱动程序的基类。
注意:如果您使用的是本地线程,我们可以毫无问题地实现并行执行
package com.vg.ui.utils.mobile;
import io.appium.java_client.AppiumDriver;
import io.appium.java_client.android.AndroidDriver;
import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;
import org.openqa.selenium.remote.CapabilityType;
import org.openqa.selenium.remote.DesiredCapabilities;
public class Mobile {
public static ThreadLocal<AppiumDriver> driverThread = new ThreadLocal<AppiumDriver>();
public void setDriver(String deviceName, String platformVersion)
throws MalformedURLException, InterruptedException {
// TODO Auto-generated method stub
File classpathRoot = new File(System.getProperty("user.dir"));
File appDir = new File(classpathRoot, "Apps");
File app = new File(appDir, "android-debug.apk");
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability(CapabilityType.BROWSER_NAME, "");
capabilities.setCapability("deviceName", deviceName);
capabilities.setCapability("platformVersion", platformVersion);
capabilities.setCapability("platformName", "Android");
capabilities.setCapability("app", app.getAbsolutePath());
if (deviceName.equals("Nexus6")) {
driverThread.set(new AndroidDriver(new URL("http://127.0.0.1:4723/wd/hub"), capabilities));
} else if (deviceName.equals("Nexus7")) {
driverThread.set(new AndroidDriver(new URL(
"http://127.0.0.1:4724/wd/hub"), capabilities));
} else if (deviceName.equals("Lenovo")) {
driverThread.set(new AndroidDriver(new URL(
"http://127.0.0.1:4723/wd/hub"), capabilities));
} else {
System.out.println("Check the device name and platformversion");
}
}
public static AppiumDriver getDriver() {
return driverThread.get();
}
public static void closeDriver() {
if (!getDriver().equals(null)) {
getDriver().quit();
}
}
}
第二:使用 PageFactory 方法为特定页面创建一个对象类。
package com.vg.ui.pageobjects;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
public class LandingPageObject {
@FindBy(xpath = "//*[@text='Account Create']")
WebElement btn_accountCreate;
public void click_AccountCreate(){
/*driverUtil.clickandWait(btn_accountCreate);*/
btn_accountCreate.click();
}
}
第三:然后在您的步骤定义类中,使用相应屏幕的 PageFactory 扩展 Mobile 驱动程序类,如下所示。
package com.vg.ui.stepdefinitions;
import org.openqa.selenium.support.PageFactory;
import com.vg.ui.pageobjects.LandingPageObject;
import com.vg.ui.utils.mobile.Mobile;
import cucumber.api.java.en.When;
public class LandingPage extends Mobile{
LandingPageObject lp = PageFactory.initElements(getDriver(), LandingPageObject.class);
@When("^click on the button account create\\.$")
public void click_on_the_button_account_create() throws Throwable {
// Write code here that turns the phrase above into concrete actions
lp.click_AccountCreate();
}
}