Appium typescript webdriver.io appium 自动化 (2) 实现 driver 的单例模式

imzack · 2020年02月27日 · 1229 次阅读

在上一篇文章中使用 ts 初步基础入门 使用 typescript+webdriver 做 appium 自动化(1)- 基础入门:https://testerhome.com/topics/22278,driver这一篇实现 的单例模式。

driver.ts

import * as wdio from 'webdriverio';
import {Options} from 'webdriver';

export class SingleDriver{
    private static client:wdio.BrowserObject;

    public static config:Options = {

        hostname:'127.0.0.1',
        port:4723,
        logLevel:'info',
        capabilities:{
            automationName:'uiautomator2',
            platformName:'android',
            platformVersion:'6',
            deviceName:'emulator-5554',
            unicodeKeyboard:true,
            skipDeviceInitialization:true,
            skipServerInstallation:true,
            noReset:false,
            appPackage:'org.cnodejs.android.md',
            appActivity:'org.cnodejs.android.md.ui.activity.LaunchActivity',
            newCommandTimeout:24*3600,
        }
    }

    public static async createClient():Promise<wdio.BrowserObject>{
        if (!this.client){
            this.client = await wdio.remote(this.config);
        }
        return this.client;
    }
}

实现思路就是:
1.声明一个空的变量 client
2.提供静态方法,如果 clietn 为 null,就创建一个新的 client 对象并返回
3.每次创建的是返回的都是同一个实例。

暂无回复。
需要 登录 后方可回复, 如果你还没有账号请点击这里 注册