当前位置:   article > 正文

【Appium系列】兼容IOSDriver和AndroidDriver_appium2 new iosdriver

appium2 new iosdriver

目录

Gradle依赖

 AndroidDriver

 IOSDriver

 AppiumDriverImpl

测试使用


Gradle依赖

  1. implementation 'io.appium:java-client:8.0.0'
  2. implementation 'org.seleniumhq.selenium:selenium-java:4.1.3'

 AndroidDriver

  1. import io.appium.java_client.AppiumDriver;
  2. import io.appium.java_client.android.AndroidDriver;
  3. import org.openqa.selenium.remote.DesiredCapabilities;
  4. import java.net.URL;
  5. public class AppAndroidDriver {
  6. DesiredCapabilities getCapabilities() {
  7. try{
  8. DesiredCapabilities capabilities = new DesiredCapabilities();
  9. capabilities.setCapability("noReset", true);
  10. capabilities.setCapability("fullReset", false);
  11. capabilities.setCapability("platformName", "Android");
  12. capabilities.setCapability("platformVersion", "11");
  13. capabilities.setCapability("deviceName", "YourdeviceName");
  14. capabilities.setCapability("automationName", "UiAutomator2");
  15. capabilities.setCapability("appActivity",".ApiDemos");
  16. capabilities.setCapability("appPackage", "io.appium.android.apis");
  17. capabilities.setCapability("appWaitActivity", "*");
  18. capabilities.setCapability("app", "D:\\JavaProject\\MobileAutoTest\\testapp\\android\\ApiDemos-debug.apk");
  19. return capabilities;
  20. }
  21. catch (Exception exception) {
  22. throw new RuntimeException("gen capabilities failed", exception);
  23. }
  24. }
  25. public AppiumDriver androidDriver() {
  26. try{
  27. return new AndroidDriver(new URL("http://localhost:4723/wd/hub"), getCapabilities());
  28. }
  29. catch (Exception exception) {
  30. throw new RuntimeException("failed to build android driver", exception);
  31. }
  32. }
  33. }

adb命令使用可以参考:【Appium系列】ADB的安装与简单使用_duoduozhengqian的博客-CSDN博客

 IOSDriver

  1. import io.appium.java_client.AppiumDriver;
  2. import io.appium.java_client.ios.IOSDriver;
  3. import org.openqa.selenium.remote.DesiredCapabilities;
  4. import java.net.URL;
  5. public class AppIOSDriver {
  6. private DesiredCapabilities getCapabilities() {
  7. try{
  8. DesiredCapabilities capabilities = new DesiredCapabilities();
  9. capabilities.setCapability("noReset", true);
  10. capabilities.setCapability("fullReset", false);
  11. capabilities.setCapability("platfromName", "iOS");
  12. capabilities.setCapability("platfromVersion", "14");
  13. capabilities.setCapability("deviceName", "iPhone13 Pro");
  14. capabilities.setCapability("automationName", "XCUITest");
  15. capabilities.setCapability("app","/path/to/my.app");
  16. capabilities.setCapability("udid", "devices udid");
  17. return capabilities;
  18. }
  19. catch (Exception exception) {
  20. throw new RuntimeException("gen capabilities failed", exception);
  21. }
  22. }
  23. public AppiumDriver iosDriver() {
  24. try{
  25. return new IOSDriver(new URL("http://localhost:4573/wd/hub"), getCapabilities());
  26. }
  27. catch (Exception exception) {
  28. throw new RuntimeException("failed to build ios driver", exception);
  29. }
  30. }
  31. }

 AppiumDriverImpl

  1. import com.appautomation.appiumdriver.AppiumDriverProvider;
  2. import io.appium.java_client.AppiumDriver;
  3. public class AppiumDriverImpl {
  4. private static AppiumDriver sessionDriver;
  5. public AppiumDriver getSessionDriver(String platForm) {
  6. if (sessionDriver == null) {
  7. if (platForm.equalsIgnoreCase("android")) {
  8. sessionDriver = new AppAndroidDriver().androidDriver();
  9. }
  10. else if(platForm.equalsIgnoreCase("ios")){
  11. sessionDriver = new AppIOSDriver().iosDriver();
  12. }
  13. else{
  14. System.out.println("input correct platform name");
  15. }
  16. return sessionDriver;
  17. }
  18. return sessionDriver;
  19. }
  20. }

测试使用

1.打开Appium,端口设置为4723

2.运行下面的代码

  1. import io.appium.java_client.AppiumDriver;
  2. import org.openqa.selenium.By;
  3. public class AppiumDriverTest {
  4. public static void main(String[] args){
  5. AppiumDriver driver = new AppiumDriverImpl().getSessionDriver("android");
  6. System.out.println(driver);
  7. driver.findElement(By.id("id")).click();
  8. }
  9. }
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小小林熬夜学编程/article/detail/206070
推荐阅读
相关标签
  

闽ICP备14008679号