Mobile Testing 简明教程

Mobile Testing - Selendroid Framework

Selendroid 是一个用于测试安卓原生应用程序和混合应用程序的测试自动化框架。Selendroid 测试使用 Selenium Webdriver 客户端 API 编写。

Selendroid is a test automation framework for testing Android native and hybrid applications. Selendroid tests are written using the Selenium Webdriver client API.

Benefits of Selendroid

  1. Fully compatible with JSON Wire Protocol/Selenium 3 Ready.

  2. No modification of app under test required in order to automate it.

  3. Testing the mobile web using built in Android driver webview app.

  4. Same concept for automating native or hybrid apps.

  5. UI elements can be found by different locator types.

  6. Gestures are supported: Advanced User Interactions API.

  7. Existing Emulators are started automatically.

  8. Selendroid supports hot plugging of hardware devices.

  9. Full integration as a node into Selenium Grid for scaling and parallel testing.

  10. Multiple Android target API support (10 to 19).

  11. Built in Inspector to simplify test case development.

  12. Selendroid can be extended at runtime with your own extensions.

  13. Selendroid can interact with multiple Android devices (emulators or hardware devices) at the same time.

Drawbacks of Selendroid

本工具的缺点是它相当慢,在低于 4GB RAM 的某些机器上无法使用。

The drawback of this tool is that it is quite slow and on some machines with less than 4GB RAM, it is unusable.

How to Use Selendroid

Step 1 - 使用 Robotium 的前提条件是 Java SDK(最低 1.6)。如果您系统中尚未安装 Java,请按照以下步骤操作。

Step 1 − The prerequisites to use Robotium 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 下载 Android Studio(受文件大小影响,下载将花费一定时间)。

Step 2 − Download Android Studio from SDK Android (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 − 从 Selendroid 下载 Selenium jar 文件和测试应用程序

Step 3 − Download Selenium jar files and test application from Selendroid

  1. Download selenium jar file and test app.

  2. Place it into any folder i.e. D:\SelendroidJars.

Step 4 − 带有 USB 数据线的实体设备。

Step 4 − Physical device with USB cable.

  1. Make sure that the device is attached to the workstation with USB cable.

  2. Make sure the USB debugging mode (under setttings → Developer options) is enabled.

Test an App with Selendroid

要使用 Selendroid 测试应用程序,请按照以下步骤操作 −

To test an App using Selendroid, follow the steps given below −

Step 1 − 安装 Eclipse。

Step 1 − Install Eclipse.

Step 2 − 创建 Java 项目。

Step 2 − Create a Java project.

Step 3 − 将下载的 Selendroid jar 文件添加到新创建的项目中。

Step 3 − Add the downloaded Selendroid jar file to the newly created project.

Step 4 − 将下载的 Selenium jar 文件添加到新创建的项目中。

Step 4 − Add the downloaded Selenium jar files to the newly created project.

Step 5 − 在 Eclipse 中配置 testNG。

Step 5 − Configure testNG in the Eclipse.

Step 6 − 使用 USB 数据线将移动设备与系统连接起来。从设置中的开发者选项中设置 USB 调试模式。

Step 6 − Connect the mobile devices with the system using USB cable. Set the USB debugging mode from the developer’s options under settings.

Step 7 − 运行 Selendroid 服务器。打开命令提示符并编写以下代码,然后按 Enter:

Step 7 − Run the Selendroid server. Open command prompt and write the following code and hit Enter −

java -jar selendroid-standalone-0.17.0-with-dependencies.jar -app selendroid-test-app-0.17.0.apk

Selendroid-standalone 将在端口 4444 上启动一个 http 服务器,并将扫描用户已创建的全部 Android 虚拟设备 (avd)(~/.android/avd/)。

Selendroid-standalone will start an http server on port 4444 and will scan all Android virtual devices (avd) that the user has created (~/.android/avd/).

打开 Web 浏览器并导航到: http://localhost:4444/wd/hub/status

Open the web browser and navigate to: http://localhost:4444/wd/hub/status.

Step 8 − 创建 Java 项目;在构建路径中添加 Selendroid Standalone 库、Selenium jar 以及 JUnit 库。

Step 8 − Create a Java Project; add Selendroid Standalone libraries, Selenium jars, and JUnit libraries in the build path.

selenium master

Step 9 − 在 Java 项目中创建包。

Step 9 − Create package under the Java project.

Step 10 − 在软件包下创建一个类,并编写以下代码。

Step 10 − Create a class under the package and write the following code.

package selTest;

import io.selendroid.SelendroidDriver;
import io.selendroid.common.SelendroidCapabilities;
import io.selendroid.standalone.SelendroidConfiguration;
import io.selendroid.standalone.SelendroidLauncher;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;

import org.testng.Assert;
import org.testng.annotations.AfterSuite;
import org.testng.annotations.BeforeSuite;
import org.testng.annotations.Test;

public class SelendroidTest {

   private WebDriver driver ;

   @BeforeSuite
   public void setUp() throws Exception {
      SelendroidConfiguration config = new SelendroidConfiguration();
      config.addSupportedApp("selendroid-test-app-0.9.0.apk");
      SelendroidLauncher selendroidServer = new SelendroidLauncher(config);
      selendroidServer.launchSelendroid();

      SelendroidCapabilities caps = new
         SelendroidCapabilities("io.selendroid.testapp:0.9.0");
      driver = new SelendroidDriver(caps);
   }

   @Test
   public void selendroidTest() throws Exception {
      WebElement inputField = driver.findElement(By.id("my_text_field"));
      Assert.assertEquals("true", inputField.getAttribute("enabled"));
      inputField.sendKeys("Selendroid");

      Assert.assertEquals("Selendroid", inputField.getText());

      WebElement button = driver.findElement(By.id("buttonTest"));
      button.click();

      button = driver.findElement(By.id("button2"));
      button.click();

      Thread.sleep(5000);

      button = driver.findElement(By.id("startUserRegistration"));
      button.click();

      Thread.sleep(10000);

      WebElement element = driver.findElement(By.id("label_username"));

      String text = element.getText();
      System.out.println(text);
      element = driver.findElement(By.id("inputUsername"));
      element.sendKeys("bob");

      element = driver.findElement(By.id("inputEmail"));
      element.sendKeys("test@gmail.com");

      element = driver.findElement(By.id("inputPassword"));
      element.clear();
      element.sendKeys("test1233");

      element = driver.findElement(By.id("inputName"));
      element.clear();
      element.sendKeys("My Name ");

      element = driver.findElement(By.id("input_preferedProgrammingLanguage"));
      element.click();

      element = driver.findElement(By.id("text1"));
      element.click();

      element = driver.findElement(By.id("input_adds"));
      element.click();

      element = driver.findElement(By.id("btnRegisterUser"));
      element.click();

      element = driver.findElement(By.id("buttonRegisterUser"));
      element.click();
   }

   @AfterSuite
   public void tearDown(){
      driver.quit();
   }
}

Step 11 − 使用 testNG 运行配置运行类。

Step 11 − Run the class with testNG run configuration.