Selendroid UI自动化测试入门

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/TalorSwfit20111208/article/details/82467912

Selendroid UI 自动化测试入门

Selenroid 官方自称为Selenium for Android,这个名称真不是吹的,执行速度相较于目前大红大紫的Appium真的是很快了,不知道为啥这么好一个项目就这么黄了(摊手),按照目前Android版本的迭代速度,估计也是为了顺应潮流吧,以至于出了后面的Appium。

写这个主要是为了记录一下研究成果不打算深究,毕竟用的人少,网上资料也相对较少,后期坑肯定比较多,还是用比较主流的Appium吧。

  之前用官方的示例跑都是可以跑成功的,但是用自己公司的App跑老是不成功,看到官方的一句话: The reason for this is that a customized selendroid-server for the app under test (AUT) will be created. Both apps (selendroid-server and AUT) must be signed with the same certificate in order to install the apks on the device.

 意思是说selendroid-server AUT App都会签上相同的签名,言外之意就是启动selendroid server会自动给App签名,但是运行命令

java -jar selendroid-standalone-0.17.0-with-dependencies.jar -app  xxx.apk虽然能签名,但是每次跑脚本安装App,启动App程序就会崩溃。心想难道只能玩官方的App?不可能吧,开发者没那么菜吧…但是网上又没有找到有详细记录跑非官方App成功的记录,后面看到官网有server启动参数

Selendroid-Standalone command line parameters

-keystore

Specify the file of the keystore to use.

-keystoreAlias

The alias of the keystore to be used

-keystorePassword

The password for the keystore to be used

 

发现有签名相关的参数,然后想大胆尝试一下,问开发童鞋要了打包的签名文件及参数,加在服务启动命令行一跑,程序安装后启动不崩溃了,另外多启动两次还是不会崩溃,心里很是惊喜(离成功不远了) 

 

后面运行命令

 

java -jar selendroid-standalone-0.17.0-with-dependencies.jar -keystore xxx.keystore -keystoreAlias mykey -keystorePassword 123456  -selendroidServerPort 9999 –app  xxxx.apk 

显示端口正常启动

 

启动成功后就可以在浏览器http://localhost:4444/wd/hub/status查看服务信息了

 

 

然后按照官方示例所说,在脚本中加个断点,然后以调试的方式运行即可以在浏览器中对App中的元素进行定位了,运行脚本,只要App能起来不退基本就是能通过在浏览器中http://localhost:4444/inspector通过selendroid的inspector 来定位元素了,selendroid的inspector可以根据用户的操作进行简单的脚本录制,如下图:

 

 

好了,可以尽情的写脚本了。

 

需要注意的坑:

Selendroid用的是老版本的Android SDK,其中有个命令list avds这个命令在新版本的Android SDK中是没有的,故小编是用了老版本的SDK,名字是这个android-sdk_r24.4.1-windows,不然会一直提示没有那个命令(其实替换里面的tools文件夹里的文件即可)

 

附上脚本(基本是录制的):

/*

 * Copyright 2012-2014 eBay Software Foundation and selendroid committers.

 *

 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except

 * in compliance with the License. You may obtain a copy of the License at

 *

 * http://www.apache.org/licenses/LICENSE-2.0

 *

 * Unless required by applicable law or agreed to in writing, software distributed under the License

 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express

 * or implied. See the License for the specific language governing permissions and limitations under

 * the License.

 */

package sf.ui.test;

 

import io.selendroid.client.SelendroidDriver;

import io.selendroid.common.SelendroidCapabilities;

 

import org.junit.After;

import org.junit.Assert;

import org.junit.Before;

import org.junit.Test;

import org.openqa.selenium.By;

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.WebElement;

 

 

public class UserPostsTest {

  private WebDriver driver = null;

 

  @Before

  public void setup() throws Exception {

           //appId

    driver = new SelendroidDriver(new SelendroidCapabilities("com.sf.bulktransit.flight:2.8.0.5"));

  }

 

  @Test

  public void assertUserAccountCanRegistered() throws Exception {

           System.out.println("=================");

          

           WebElement element1 = driver.findElement(By.xpath("//EditText[@id='etWorkName']"));

           element1.sendKeys("140399");

          

          

           WebElement element2 = driver.findElement(By.xpath("//Button[@id='btnLogin']"));

           element2.click();

           Thread.sleep(500);

 

           WebElement element3 = driver.findElement(By.xpath("//TextView[@id='tvScanWork']"));

           element3.click();

           Thread.sleep(500);

 

           WebElement element4 = driver.findElement(By.xpath("//AllCapEditText[@id='etPostNo']"));

           element4.sendKeys("DB");

           Thread.sleep(500);

           WebElement element5 = driver.findElement(By.xpath("//EditText[@id='etStationNo']"));

           element5.sendKeys("1");;

 

           WebElement element6 = driver.findElement(By.xpath("//Button[@id='btnConfirm']"));

           element6.click();

           Thread.sleep(1000);

 

           WebElement element7 = driver.findElement(By.xpath("//Button[@id='btnConfirmWork']"));

           element7.click();

          

 

  }

 

 

 

  @After

  public void teardown() {

    driver.quit();

  }

}

总结:

其实也没啥,就是要多换个角度去思考,去尝试,官方资料绝对是权威参考!

主要2点需要注意:

  1. Android SDK的版本
  2. 签名问题,如果默认命令启动App签名有问题,建议问下开发童鞋签名文件和用户名、密码应该是可以签过的
  3. 多琢磨官方给出的示例教程,绝对没有一句废话,需要细细琢磨。

参考:

http://selendroid.io/setup.html

http://selendroid.io/inspector.html

 

猜你喜欢

转载自blog.csdn.net/TalorSwfit20111208/article/details/82467912