Mobile Testing 简明教程

Mobile Testing - Appium Framework

Appium 是一个用于测试原生应用程序、混合应用程序和移动网络应用程序的开源测试自动化框架。它使用 WebDriver 协议驱动 iOS 和 Android 应用程序。

Appium is an open-source test automation framework for testing native and hybrid apps and mobile web apps. It drives iOS and Android apps using the WebDriver protocol.

Advantages of Appium

  1. It’s free and (mostly) open source.

  2. It has a very well supported and active Google group.

  3. It’s in the Selenium 3 spec so should be future proof.

  4. It supports both Android and iOS.

  5. It does not require anything to be installed on the device – no server or code changes required.

Drawbacks of Appium

  1. No support for intelligent waits.

  2. On iOS, you can only execute one test at a time per Mac.

  3. Limited support for gestures.

  4. Limited support for Android < 4.1

How to Use Appium

Step 1 − 使用 Appium 的前提条件是 Java SDK(最低 1.6)。如果您在系统上未安装 Java,请按照下列步骤操作。

Step 1 − The prerequisites to use Appium is Java SDK (minimum 1.6). If you don’t have Java installed on your system, then follow the steps given below.

  1. Download JDK and JRE from Oracle JavaSE

  2. Accept license agreement.

  3. Install JDK and JRE.

  4. Set environment variable as shown in the screenshot below.

environment variable

Step 2 − 从 SDK 下载 Android Studio(由于文件大小,将花费一些时间)。

Step 2 − Download Android Studio from SDK (It will take time because of the size of the file).

  1. Double click the exe and run the installer.

  2. Continue with all default options.

  3. Set the ANDROID_HOME.

Step 3 − 安装 Android 镜像和工具。

Step 3 − Install Android images and tools.

  1. Click on SDK Manager −

sdk manager
  1. Select the necessary package. For example, if we are building an App for Android 4.4.2, then make sure the following packages are checked under the Tools section − Android SDK Tools rev 22.6.3Android Platform-tools rev 19.0.1Android SDK Build-tools rev 19.1

Step 4 − 创建 Android 虚拟设备 −

Step 4 − Create Android Virtual Devices −

  1. Open Android Studio and click AVD Manager in the toolbar. AVDs allow us to test and run our Android apps.

avd manager
  1. Use the following settings for a Nexus5 AVD − Device: Nexus 5 (4.95, 1080 x 1920; xxhdpi) Target: Google APIs x86 (Google Inc.) - API Level 19 Make sure you select the target with Google APIs in the name. CPU: Intel Atom (x86) Check the box for Use Host GPU Click OK.

  2. You should now see the AVD you created in the AVD Manager, where you can start it, delete it, or create another one!

Step 5 − 从 Appium 下载 Appium jar 文件

Step 5 − Download Appium jar files from Appium

Test an App with Appium

要使用 Appium 测试应用,请按照以下给出的步骤操作 −

To test an App with Appium, follow the steps given below −

Step 1 − 在 Android Studio 中创建一个名为 “RobotiumTest”的测试项目。

Step 1 − Create a test Project in the Android Studio named as “RobotiumTest”.

robotiumtest

选择所有默认选项,直至你到达主页面。

Choose all the default options until you reach to the main page.

Step 2 − 将 Appium jar 添加到你的项目中。单击 Project → App → 复制 lib 中的所有 jar。选择所复制的 jar,但不要选择 Selenium、Java 客户端和 Junit jar,然后右击该 jar 并单击“添加为库”。

Step 2 − Add the Appium jars into your project. Click Project → App → copy all the jars in lib. Select the copied jars except Selenium, Java client and Junit Jar, then right-click on it and click on "Add as Library".

Step 3 − 在 App 中单击 build.gradle。你将看到所有已添加的库,如以下屏幕截图所示。

Step 3 − Click on build.gradle in the App. You will see all the libraries added, as shown in the following screenshot.

build gradle

Step 4 − 现在创建一个 Java 类,如下所示 −

Step 4 − Now create a Java class as shown below −

AppiumDriver driver;
@Before

public void testCaseSetup()throws Exception {
   //service.start();
   //reader.readFile();

   DesiredCapabilities cap = new DesiredCapabilities();

   cap.setCapability(MobileCapabilityType.PLATFORM_NAME,"Android");
   cap.setCapability(MobileCapabilityType.DEVICE_NAME, "Android device");
   cap.setCapability(MobileCapabilityType.NEW_COMMAND_TIMEOUT, "4000");
   cap.setCapability(MobileCapabilityType.APP, "c://apk//sample.apk");

   driver = new AndroidDriver<MobileElement>("http://127.0.0.1:4444/wd/hub",cap);
}

@Test
public void testcase1()throws Exception {
   driver.findElementByID("Example").click();
   Asser.assertTrue(driver.findElementByID("Example").isDisplayed));
}

@After
public void testCaseTearDown() {
   driver.quit();
}

Step 5 − 运行测试用例。

Step 5 − Running the Test case.

  1. Click on build variant and select Unit Testing.

  2. Start the Appium server with the specific port "4444". Download the Appium for Windows from here.Double click on the .exe and install Appium.Click on the icon to up the UI.Change the port if required, as shown below.Click the Play button to start the server.

running test case
  1. Connect the device with USB debugging on or start an emulator.

  2. Right-click the test class and click on "Run".