赞
踩
目录
- implementation 'io.appium:java-client:8.0.0'
- implementation 'org.seleniumhq.selenium:selenium-java:4.1.3'
- import io.appium.java_client.AppiumDriver;
- import io.appium.java_client.android.AndroidDriver;
- import org.openqa.selenium.remote.DesiredCapabilities;
-
- import java.net.URL;
-
- public class AppAndroidDriver {
- DesiredCapabilities getCapabilities() {
- try{
- DesiredCapabilities capabilities = new DesiredCapabilities();
- capabilities.setCapability("noReset", true);
- capabilities.setCapability("fullReset", false);
- capabilities.setCapability("platformName", "Android");
- capabilities.setCapability("platformVersion", "11");
- capabilities.setCapability("deviceName", "YourdeviceName");
- capabilities.setCapability("automationName", "UiAutomator2");
- capabilities.setCapability("appActivity",".ApiDemos");
- capabilities.setCapability("appPackage", "io.appium.android.apis");
- capabilities.setCapability("appWaitActivity", "*");
- capabilities.setCapability("app", "D:\\JavaProject\\MobileAutoTest\\testapp\\android\\ApiDemos-debug.apk");
- return capabilities;
- }
- catch (Exception exception) {
- throw new RuntimeException("gen capabilities failed", exception);
- }
- }
-
- public AppiumDriver androidDriver() {
- try{
- return new AndroidDriver(new URL("http://localhost:4723/wd/hub"), getCapabilities());
- }
- catch (Exception exception) {
- throw new RuntimeException("failed to build android driver", exception);
- }
- }
- }
adb命令使用可以参考:【Appium系列】ADB的安装与简单使用_duoduozhengqian的博客-CSDN博客
- import io.appium.java_client.AppiumDriver;
- import io.appium.java_client.ios.IOSDriver;
- import org.openqa.selenium.remote.DesiredCapabilities;
-
- import java.net.URL;
-
- public class AppIOSDriver {
- private DesiredCapabilities getCapabilities() {
- try{
- DesiredCapabilities capabilities = new DesiredCapabilities();
- capabilities.setCapability("noReset", true);
- capabilities.setCapability("fullReset", false);
- capabilities.setCapability("platfromName", "iOS");
- capabilities.setCapability("platfromVersion", "14");
- capabilities.setCapability("deviceName", "iPhone13 Pro");
- capabilities.setCapability("automationName", "XCUITest");
- capabilities.setCapability("app","/path/to/my.app");
- capabilities.setCapability("udid", "devices udid");
- return capabilities;
- }
- catch (Exception exception) {
- throw new RuntimeException("gen capabilities failed", exception);
- }
- }
-
- public AppiumDriver iosDriver() {
- try{
- return new IOSDriver(new URL("http://localhost:4573/wd/hub"), getCapabilities());
- }
- catch (Exception exception) {
- throw new RuntimeException("failed to build ios driver", exception);
- }
- }
- }
- import com.appautomation.appiumdriver.AppiumDriverProvider;
- import io.appium.java_client.AppiumDriver;
-
- public class AppiumDriverImpl {
- private static AppiumDriver sessionDriver;
-
- public AppiumDriver getSessionDriver(String platForm) {
- if (sessionDriver == null) {
-
- if (platForm.equalsIgnoreCase("android")) {
- sessionDriver = new AppAndroidDriver().androidDriver();
- }
- else if(platForm.equalsIgnoreCase("ios")){
- sessionDriver = new AppIOSDriver().iosDriver();
- }
- else{
- System.out.println("input correct platform name");
- }
- return sessionDriver;
- }
- return sessionDriver;
- }
- }
1.打开Appium,端口设置为4723
2.运行下面的代码
- import io.appium.java_client.AppiumDriver;
- import org.openqa.selenium.By;
-
- public class AppiumDriverTest {
- public static void main(String[] args){
- AppiumDriver driver = new AppiumDriverImpl().getSessionDriver("android");
- System.out.println(driver);
- driver.findElement(By.id("id")).click();
- }
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。