Java+Uiautomator 自动化测试PageObjects模式实践

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

举个例子:

1. case:查看应用信息中的代码许可、隐私政策与服务条款

@Test
    public void testCallSetting_0005_appInformation() throws UiObjectNotFoundException {
        //查看应用信息中的代码许可、隐私政策与服务条款
        goToSetting();
        callSettingItem("About");
        mDevice.findObject(By.text("Open source licenses")).clickAndWait(Until.newWindow(), 3000);
        assertTrue("source licenses open fail", mDevice.hasObject(By.text("Android Annotations Support Library")));
        mDevice.pressBack();
        mDevice.findObject(By.text("Privacy policy")).clickAndWait(Until.newWindow(), 3000);
        SystemClock.sleep(5000);
        if (!mDevice.hasObject(By.text("Sign in to Chrome")
                .res("com.android.chrome:id/chooser_title"))) {
            assertTrue("Privacy policy open fail", mDevice.hasObject(By.text("Welcome to Chrome")
                    .res("com.android.chrome:id/title")));
            mDevice.pressBack();

        } else {
            assertTrue("Privacy policy open fail", mDevice.hasObject(By.text("Sign in to Chrome")
                    .res("com.android.chrome:id/chooser_title")));
            mDevice.pressBack();
            mDevice.pressBack();
        }
        SystemClock.sleep(2000);
        mDevice.findObject(By.text("Terms of service")).clickAndWait(Until.newWindow(), 3000);
        SystemClock.sleep(5000);
        if (!mDevice.hasObject(By.text("Sign in to Chrome")
                .res("com.android.chrome:id/chooser_title"))) {
            assertTrue("Privacy policy open fail", mDevice.hasObject(By.text("Welcome to Chrome")
                    .res("com.android.chrome:id/title")));
        } else {
            assertTrue("Privacy policy open fail", mDevice.hasObject(By.text("Sign in to Chrome")
                    .res("com.android.chrome:id/chooser_title")));
        }

    }

上面的代码就是我们去手机的about中查看应用信息中的代码许可、隐私政策与服务条款,如果按照上面的写法,有以下几个缺点:

1、假如我们UI变了,元素只要稍微一变动我们就需要去修改代码,假如你的case有10000条的时候,你是不是要修改疯了

2、没有办法进行代码复用

3、后续不好维护

4、逻辑不清晰,别人需要看好多遍才能看懂

那我们该如何去解决呢?

解决方式 PageObjects

  • PageObjects 中所有功能,都是由PageObject提供,功能的实现不在这个Page,就在另一个Page。
  • Page Objects是你的测试用例Code的唯一交互对象,是对实际UI(控件,元素),交互(界面跳转,弹出)的一种抽象,它可以指整个页面,也可以指Page上的某个部分
  • Page Object 只专注自身,其他东西不属于PageObject

             1. 功能,跳转到其他Page Object
             2. 自身元素,控件

实践

原则

  • public方法代表Page提供的功能,或者Page能够提供的元素,其他方法保持private
  • 尽量不要暴露Page的内部细节 , 每个PageObject都是 对界面 的抽象,测试用例只需要和 Page进行交互,
  • 不需要也不要去关注Page的内部实现细节
  • 不要assertion
  • 方法可以返回其他Page Objects ,表示Page 间的跳转交互
  • Page Objects不用代表整个页面,可以是任意一个部分 ,只关注你所需要的部分,不要多,不要少
  • 相同的操作,产生不同结果时,应该在方法上有区分
  • 可以记住Page 的当前状态,获取状态的方式,以便 页面回转时刷新

代码

例子:进入到手机的FileManager,验证能否进入到Apks文件中,存在APk文件。

分解下步骤,就是进入到File Manager -> 进入到Apks文件夹 -> 查看是否存在后缀APK的文件。

我们需要的写的Page有,BasePage,FileManagerHomePage, ApksPage 

BasePage:

import android.app.Activity;
import android.support.test.uiautomator.BySelector;
import android.support.test.uiautomator.UiDevice;
import android.support.test.uiautomator.UiObject;
import android.support.test.uiautomator.UiObject2;
import android.support.test.uiautomator.UiSelector;
import android.util.Log;

import com.xxxxxx.autotest.automator.AutomatorHelper;

import java.lang.ref.WeakReference;

public class BasePage {
    protected UiDevice mDevice;
    protected WeakReference<Activity> mActivity;
    protected AutomatorHelper mHelper;

    public BasePage(UiDevice device) {
        this(null, device);
    }

    public BasePage(WeakReference<Activity> activity, UiDevice device) {
        mActivity = activity;
        mDevice = device;
        Log.d("Page", "page: enter " + getClass().getCanonicalName());
    }

    public UiObject2 find(BySelector by) {
        return mDevice.findObject(by);
    }

    public UiObject find(UiSelector selector) {
        return mDevice.findObject(selector);
    }

    public void refresh() {

    }
}

FileManagerHomePage ,他在手机上是这个界面

代码如下:

import android.app.Activity;
import android.support.test.uiautomator.By;
import android.support.test.uiautomator.UiDevice;
import android.support.test.uiautomator.UiObject2;
import android.support.test.uiautomator.Until;

import com.XXXXX.autotest.automator.AutomatorHelper;
import com.XXXXX.autotest.page.BasePage;

import java.lang.ref.WeakReference;
import java.util.function.Function;

public class FileManagerHomePage extends BasePage {

    private static final int LAUNCH_TIMEOUT = 5000;

    private Function<String, UiObject2> getCategoryNameButton =
            s -> find(By.res("com.transsion.filemanager:id/category_name").text(s));

    public FileManagerHomePage(UiDevice device) {
        super(device);
    }

    public UiObject2 getZipsButton() {return getCategoryNameButton.apply("Zips");}


    public FileManagerZipsPage openZipsPage() {
        getZipsButton().click();
        mDevice.wait(Until.gone
                (By.res("com.transsion.filemanager:id/category_name")), LAUNCH_TIMEOUT);
        return new FileManagerZipsPage(AutomatorHelper.getCurrentActivity(), mDevice);
    }
}

接下来就是FileManagerApksPage

代码如下:

import android.app.Activity;
import android.support.test.uiautomator.By;
import android.support.test.uiautomator.UiDevice;
import android.support.test.uiautomator.UiObject2;

import com.xxxx.autotest.page.BasePage;

import java.lang.ref.WeakReference;

public class FileManagerApkPage extends BasePage {
    public FileManagerApkPage(WeakReference<Activity> activity, UiDevice device) {
        super(activity, device);
    }

    public UiObject2 getApkName() {
        return find(By.res("com.transsion.filemanager:id/file_name").textEndsWith(".apk"));
    }

    public boolean isExistsApkFiles() {
        UiObject2 apkName = getApkName();
        return getApkName() != null;
    }
}

页面写完了,我们就可以写Case了

新建一个,名字叫FileManagerTest.java


import android.support.test.uiautomator.UiObjectNotFoundException;

import com.xxxxx.autotest.automator.Automator;
import com.xxxx.autotest.automator.AutomatorHelper;
import com.xxxx.autotest.common.PackageConstants;
import com.xxx.autotest.page.filemanager.FileListPage;
import com.xxx.autotest.page.filemanager.FileManagerHomePage;
import com.xxx.autotest.page.filemanager.FileManagerZipsPage;


import org.junit.Assert;
import org.junit.Test;


import org.junit.Assert;
import org.junit.Test;

public class FileManagerTest extends Automator {

    private static final long LAUNCH_TIMEOUT = 5000;
    private FileManagerHomePage mHomePage;

    @Override
    public void setUp() throws Exception {
        super.setUp();
        mHelper.launchAppCleanTask(PackageConstants.FileManager.PACKAGE, LAUNCH_TIMEOUT);
        mHomePage = new FileManagerHomePage(mDevice);
    }

@Test
    public void testFileManager_0013_checkAPKFiles() {
        // 查看根目录的APK文件(前提更目录存在APK文件)
        FileManagerApkPage apkPage = mHomePage.openApkPage();
        boolean isExistsApkFile = apkPage.isExistsApkFiles();
        Assert.assertTrue("apk file not exists", isExistsApkFile);
    }
}

这样写的话,会比以前的代码逻辑更清晰一点,然后就算页面变了,我们修改一下元素就好了。然后也能进行代码的复用吧。

当然,代码写的不是很好,还有改进的空间,后续继续改进。大家看方法就好了,看思路,不要照着写,因为包名引入我没有写出来。

猜你喜欢

转载自blog.csdn.net/ricky_yangrui/article/details/89048635