测试场景:打开myreaciton.apk,点击skip login按钮,点击“crazy fingers”菜单,选择left_hand及index_finger,开始模拟支付宝咻一咻类似的点击(10s倒计时),最后断言点击数。
1、首先建一个CapabilitiesFactory类用来定义所测的app(MyReaction.apk)
- package com.wiley.driver;
-
- import org.openqa.selenium.remote.DesiredCapabilities;
-
- public class CapabilitiesFactory {
-
- protected static DesiredCapabilities capabilities;
-
- public static DesiredCapabilities getCapabilities() throws Exception {
-
- capabilities = new DesiredCapabilities();
- capabilities.setCapability("platformName", "android");
- capabilities.setCapability("deviceName", "samsung");
- //以下两项可以通过appium客户端查看
- capabilities.setCapability("appPackage", "com.denyszaiats.myreactions");
- capabilities.setCapability("appActivity", ".StartActivity");
- //支持中文输入
- capabilities.setCapability("unicodeKeyboard", "True");
- //重置输入法为系统默认
- capabilities.setCapability("resetKeyboard", "True");
- //安装时不对apk进行重签名,设置很有必要,否则有的apk在重签名之后无法正常使用
- capabilities.setCapability("noSign", "True");
- return capabilities;
- }
-
- }
复制代码 2、其次,建一个SikuppiumDriver类,继承AppiumDriver
- package com.wiley.driver;
-
- import java.net.URL;
- import org.openqa.selenium.Capabilities;
- import io.appium.java_client.AppiumDriver;
- import io.appium.java_client.MobileElement;
-
- public class SikuppiumDriver extends AppiumDriver {
-
- private AppiumDriver driver;
-
- public SikuppiumDriver(URL remoteAddress, Capabilities desiredCapabilities) {
- super(remoteAddress, desiredCapabilities);
- }
-
- public void setDriver(AppiumDriver driver) {
- this.driver = driver;
- }
-
- @Override
- public MobileElement scrollTo(String s) {
- return null;
- }
-
- @Override
- public MobileElement scrollToExact(String s) {
- return null;
- }
- }
复制代码 3、测试
(1)为了方便,我们可以先建一个测试基类,如:BaseTest类 - package com.wiley.tests;
-
- import java.net.URL;
- import java.util.concurrent.TimeUnit;
- import org.testng.annotations.AfterMethod;
- import org.testng.annotations.BeforeMethod;
- import com.wiley.driver.CapabilitiesFactory;
- import com.wiley.driver.SikuppiumDriver;
-
- public class BaseTest {
-
- protected SikuppiumDriver driver;
-
- @BeforeMethod
- public void setUp() throws Exception {
- driver = new SikuppiumDriver(
- new URL("http://0.0.0.0:4723/wd/hub"),
- CapabilitiesFactory.getCapabilities()
- );
- driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);
- driver.setDriver(driver);
- }
-
- @AfterMethod
- public void tearDown() {
- if (driver != null) {
- driver.quit();
- }
- }
-
- }
复制代码(2)在正式编写代码之前,我们应该知道app元素的简单定位,可以通过sdk自带的uiautomatorviewer.bat,如下图: 比如:我们要定位Skip login这个按钮,可以通过以下方法:
(1)driver.findElementById("com.denyszaiats.myreactions:id/skip_login_button");
(2)driver.findElementByName("Skip login");
(3)driver.findElementByXpath("//android.widget.Button[1]");
更深层次的WebElement元素的操作方法,具体查看selenium webdriver2的api,此文不做具体介绍; (3)编写测试代码,代码中有关sikuli的方法可具体查看我博客中的相关文章 - package com.wiley.tests;
-
- import static org.testng.Assert.assertEquals;
- import org.sikuli.script.FindFailed;
- import org.sikuli.script.Screen;
- import org.testng.annotations.Test;
-
- public class SampleTest extends BaseTest{
-
- private static final String BaseId = "com.denyszaiats.myreactions:id/";
- private Screen screen = new Screen();
-
- @Test
- public void testSampleScenarioOfClicking() throws InterruptedException, FindFailed {
- driver.findElementById(BaseId + "skip_login_button").click();
- driver.findElementByName("Crazy Fingers").click();
- driver.findElementById(BaseId + "handButton").click();
- screen.click("left_hand.png");
- driver.findElementById(BaseId + "fingerButton").click();
- screen.click("index_finger.png");
- driver.findElementById(BaseId + "startButton").click();
- for (int i = 0; i < 3; i ++) {
- driver.findElementById(BaseId + "imageTapButton").click();
- }
-
- String result = driver.findElementById(BaseId + "resultsFasterClicker").getText();
- assertEquals(result, "3");
- }
复制代码
|