简单介绍

讲解一个appium测试脚本需要导入哪些java模块。

导入的java模块

明确两点信息:

  • 一个脚本需要讲清楚测试环境:1、自动化平台方面,测试的平台是什么(appium或者selendroid,selendroid用于测试低版本的Android系统的app);2、被测试设备方面,被测试设备的平台(Android或者iOS),被测试设备的名字,被测试设备的系统版本;3、被测试应用方面,待测应用是移动web应用(使用移动端浏览器)还是原生/混合应用,若不是web应用,那么是否需要安装包进行安装,该应用的package和activity是什么。
  • appium是对selenium的扩展,又细分为Android方向的扩展和iOS方向的扩展。例如,WebElement可以操作selenium中的命令;MobileElement是appium中的元素,是WebElement的子类,添加了一些移动端独有的手势操作;AndroidElement和iOSElement都是MobileElement的子类,各自添加了一些系统特有的功能。又比如,RemoteWebDriver是selenium中的,AppiumDriver是其子类,添加了一些移动端的功能;AndroidDriver和iOSDriver是AppiumDriver的子类,分别适用于特定的系统。

看下面例子。

  1. 1 import org.openqa.selenium.remote.CapabilityType;//指明移动端浏览器的名字,移动web测试时需要此模块
  2. 2 import org.openqa.selenium.remote.DesiredCapabilities;//创建一个capability变量容纳测试环境参数,比如测试平台、测试设备、待测应用等。所有测试都需要这个模块。
  3. 3 import org.testng.Assert;//用于断言测试结果是否与期待值相符。testng和junit都是单元测试的模块,选用其一即可。
  4. 4
  5. 5 import org.testng.annotations.Test;//标注哪些方法是测试用例。
  6. 6 import org.testng.annotations.AfterTest;//标注哪些方法是清理工作,一般用于退出driver。
  7. 7
  8. 8 import io.appium.java_client.MobileElement;//用于操作待测应用中的元素,类似功能的还有AndroidElement和iOSElement,必须选用其中一个。
  9. 9 import io.appium.java_client.TouchAction;
  10. 10 import io.appium.java_client.android.AndroidDriver;//与待测设备建立session,用于操作待测应用,类似功能的还有AppiumDriver和iOSDriver,必须选用其中一个。
  11. 11 import io.appium.java_client.remote.AndroidMobileCapabilityType;//用于指明APP_PACKAGE、APP_ACTIVITY参数。
  12. 12 import io.appium.java_client.remote.AutomationName;//指明测试平台的名字,appium或者selendroid。
  13. 13 import io.appium.java_client.remote.MobileCapabilityType;//用于指明PLATFORM_NAME,DEVICE_NAME,PLATFROM_VERSION,APP等参数。
  14. 14
  15. 15 import java.io.File;//操作应用文件。
  16. 16
  17. 17 import java.net.URL;//配置appium server监听的地址。

完整脚本

  1. 1 package main.java.com.dbyl.appiumCore.tests;
  2. 2
  3. 3 import org.openqa.selenium.remote.CapabilityType;
  4. 4 import org.openqa.selenium.remote.DesiredCapabilities;
  5. 5 import org.testng.Assert;
  6. 6
  7. 7 import org.testng.annotations.Test;
  8. 8 import org.testng.annotations.AfterTest;
  9. 9
  10. 10 import io.appium.java_client.MobileElement;
  11. 11 import io.appium.java_client.TouchAction;
  12. 12 import io.appium.java_client.android.AndroidDriver;
  13. 13 import io.appium.java_client.remote.AndroidMobileCapabilityType;
  14. 14 import io.appium.java_client.remote.AutomationName;
  15. 15 import io.appium.java_client.remote.MobileCapabilityType;
  16. 16
  17. 17 import java.io.File;
  18. 18
  19. 19 import java.net.URL;
  20. 20 import java.util.List;
  21. 21 import java.util.concurrent.TimeUnit;
  22. 22
  23. 23 public class unlockerTest {
  24. 24 private AndroidDriver<MobileElement> driver;
  25. 25
  26. 26 @Test
  27. 27 public void Demo() throws Exception {
  28. 28 // set up appium
  29. 29 DesiredCapabilities capabilities = new DesiredCapabilities();
  30. 30 capabilities.setCapability(MobileCapabilityType.AUTOMATION_NAME, AutomationName.APPIUM);
  31. 31 // for native app set null, for web test please set chrome or firefox
  32. 32 capabilities.setCapability(CapabilityType.BROWSER_NAME, "");
  33. 33
  34. 34 capabilities.setCapability(MobileCapabilityType.PLATFORM_NAME, "Android");
  35. 35 capabilities.setCapability(MobileCapabilityType.DEVICE_NAME, "Android Emulator");
  36. 36 // simulator version 4.4
  37. 37 capabilities.setCapability(MobileCapabilityType.PLATFORM_VERSION, "4.4");
  38. 38 // if no need install don't add this
  39. 39 // File classpathRoot = new File(System.getProperty("user.dir"));
  40. 40 File classpathRoot = new File("/Users/wwh/IdeaProjects");
  41. 41 File appDir = new File(classpathRoot, "apps");
  42. 42 File app = new File(appDir, "Locker.apk");
  43. 43 capabilities.setCapability(MobileCapabilityType.APP, app.getAbsolutePath());
  44. 44 // package name
  45. 45 capabilities.setCapability(AndroidMobileCapabilityType.APP_PACKAGE, "com.AppiumGirls.locker");
  46. 46 // // support Chinese
  47. 47 capabilities.setCapability("unicodeKeyboard", "True");
  48. 48 capabilities.setCapability("resetKeyboard", "True");
  49. 49 // no need sign
  50. 50 capabilities.setCapability("noSign", "True");
  51. 51 // launcher activity
  52. 52 capabilities.setCapability(AndroidMobileCapabilityType.APP_ACTIVITY, ".MainActivity");
  53. 53 String url = "http://localhost:4723/wd/hub";
  54. 54 driver = new AndroidDriver<MobileElement>(new URL(url), capabilities);
  55. 55
  56. 56 driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
  57. 57
  58. 58 MobileElement button = driver.findElementByAndroidUIAutomator("new UiSelector().text(\"设置手势密码\")");
  59. 59 // tap
  60. 60 button.tap(1, 1000);
  61. 61 // get all the items of gesture locker
  62. 62 List<MobileElement> items = driver.findElementsByClassName("android.widget.ImageView");
  63. 63 for (MobileElement item : items) {
  64. 64 /**
  65. 65 * 0 1 2 3 4 5 6 7 8
  66. 66 */
  67. 67 item.click();
  68. 68 }
  69. 69
  70. 70 // create a Z from 0->1->2->4->6->7->8
  71. 71 TouchAction touches1 = new TouchAction(driver);
  72. 72 touches1.press(items.get(0)).waitAction(1000).moveTo(items.get(1)).waitAction(1000).moveTo(items.get(2))
  73. 73 .waitAction(1000).moveTo(items.get(4)).moveTo(items.get(6)).waitAction(1000).moveTo(items.get(7))
  74. 74 .waitAction(1000).moveTo(items.get(8)).release();
  75. 75 touches1.perform();
  76. 76 Thread.sleep(2000);
  77. 77 //create 0->1->2
  78. 78 TouchAction touches2 = new TouchAction(driver);
  79. 79 touches2.press(items.get(0)).waitAction(1000).moveTo(items.get(1)).waitAction(1000).moveTo(items.get(2))
  80. 80 .waitAction(1000).moveTo(items.get(4)).release();
  81. 81 touches2.perform();
  82. 82 MobileElement alert =driver.findElementById("com.AppiumGirls.locker:id/text_tip");
  83. 83 Assert.assertTrue(alert.getText().contains("与上一次绘制不一致,请重新绘制"));
  84. 84
  85. 85 }
  86. 86
  87. 87 @AfterTest
  88. 88 public void Teardown(){
  89. 89 driver.quit();
  90. 90 }
  91. 91 }

appium导入哪些java模块的更多相关文章

  1. python 模块化管理,导入自己的模块(包)

    很多东西似懂非懂,或者当时看懂了,但是感觉不是很重要,经常不用,时间久了,也就变得似懂非懂了.今天被某度电话面试问道一个问题,就是模块倒入,其实我之前也是很仔细的研究过的,不过由于平时做的东西太简单, ...

  2. Jigsaw 项目:Java 模块系统新手引导

    前言 随着 2017 年 10 月 Java 9 的发布,Java 能够使用模块系统了,但是中文互联网上的资料太少,许多关于 Java 模块系统的文章都只是介绍了模块系统的好处,或者给了一些毫无组织的 ...

  3. Exporter - 实现默认的导入方法用于模块

    Exporter - 实现默认的导入方法用于模块 简介: In module YourModule.pm: package YourModule; require Exporter; @ISA = q ...

  4. import 语句用于导入从外部模块,另一个脚本等导出的函数,对象或原语。

    import 语句用于导入从外部模块,另一个脚本等导出的函数,对象或原语. 注意:此功能目前无法在任何浏览器中实现.它在许多转换器中实现,例如 Traceur Compiler , Babel , R ...

  5. 在MEF中手动导入依赖的模块

    对于简单的场景来讲,在MEF中导入依赖模块非常简单,只要用ImportAttribute标记依赖的成员,MEF模块会自动找到并创建该模块.但有的时候我们依赖的模块是上下文相关的,此时MEF框架的自动组 ...

  6. 在Google Colab中导入一个本地模块或.py文件

    模块与单个.py文件的区别,模块中含有__init__.py文件,其中函数调用使用的是相对路径,如果使用导入.py文件的方法在Google Colab中导入模块 会报错:Attempted relat ...

  7. 导入maven多模块项目 出现的问题

    近日导入maven多模块项目 出现的问题以及解决过程2017年12月04日 20:43:04 守望dfdfdf 阅读数:815 标签: jdkmavenmaven pom.xml 更多个人分类: 工作 ...

  8. 如何把安全证书导入到java中的cacerts证书库(转)

    如何把安全证书导入到java中的cacerts证书库   提示: javax.net.ssl.SSLHandshakeException: sun.security.validator.Validat ...

  9. python模块之导入包及模块发布

    1.导入包(不常用的方法) 在使用python的包时,有时候想直接导入包名,然后通过包名来调用模块,例如: temp为我们创建的一个包,如果我们想通过下面的方式进行导入模块中的方法,将会出错 impo ...

随机推荐

  1. php解析json字符串变量总是空白null

    通过接口获取的json字符串使用json_decode始终无法正确解析,返回空白. 直接把结果字符串复制出来手动创建一个变量却正常,在前端js也能解析,搞了半天不得其解,最后发现是接口输出的结果包含有 ...

  2. Spring Cloud系列文,Feign整合Ribbon和Hysrix

    在本博客之前的Spring Cloud系列里,我们讲述了Feign的基本用法,这里我们将讲述下Feign整合Ribbon实现负载均衡以及整合Hystrix实现断路保护效果的方式. 1 准备Eureka ...

  3. Wireshark如何选择多行

    Wireshark如何选择多行   在Wireshark中,用户经常需要选择几行,然后进行批量操作,如导出或者分析.但Wireshark没有提供通过鼠标直接选择多行的功能.这个时候,用户需要采用标记分 ...

  4. HTTP基础认证Basic Authentication

    HTTP基础认证Basic Authentication Basic Authentication是一种HTTP访问控制方式,用于限制对网站资源的访问.这种方式不需要Cookie和Session,只需 ...

  5. Java NIO中的Buffer类

    Buffer     缓冲,用于批量读写数据 Buffer是一个抽象类,基本数据类型都有实现类:XxxBuffer,比如ByteBuffer.CharBuffer.IntBuffer.DoubleBu ...

  6. [UIDevice currentDevice]

    获取iphone的系统信息使用[UIDevice currentDevice],信息例如以下: [[UIDevice currentDevice] systemName]:系统名称,如iPhone O ...

  7. 【Nginx】开发一个HTTP过滤模块

    与HTTP处理模块不同.HTTP过滤模块的工作是对发送给用户的HTTP响应做一些加工. server返回的一个响应能够被随意多个HTTP过滤模块以流水线的方式依次处理.HTTP响应分为头部和包体,ng ...

  8. iOS UI08_TableView界面传值

    实现两个界面之间内容的传递 // // MainViewController.m // UI08_TableView界面传值 // // Created by dllo on 15/8/7. // C ...

  9. spoj 1811 LCS - Longest Common Substring (后缀自己主动机)

    spoj 1811 LCS - Longest Common Substring 题意: 给出两个串S, T, 求最长公共子串. 限制: |S|, |T| <= 1e5 思路: dp O(n^2 ...

  10. Name和:Name

    http://www.cnblogs.com/tianyutingxy/archive/2011/02/23/1962078.html http://www.tuicool.com/articles/ ...