1.新建项目
打开Webstorm新建项目
打开终端输入命令
npm init -y
npm install @wdio/cli allure-commandline --save-dev
npx wdio config
然后在终端依次选择如下:
然后在终端输入命令:
npm install @wdio/local-runner@latest @wdio/mocha-framework@latest appium-uiautomator2-driver --save-dev
如果图中"run 'npm install'"这一步选择了yes,则不用执行上述命令
2.编写脚本
test/pageobjects/page.js
module.exports = class Page {get loginWidget() {let xpath = '//*[@content-desc="Login"]'return $(xpath);}
}
test/pageobjects/login.page.js
const Page = require('./page');class LoginPage extends Page {get btnSignUpContainer() {let xpath = '//*[@content-desc="button-sign-up-container"]'return $(xpath);}get inputEmail() {return $('~input-email');}get inputPassword() {return $('~input-password');}get inputRepeatPassword() {return $('~input-repeat-password');}get btnSignUp() {let xpath = '//*[@content-desc="button-SIGN UP"]'return $(xpath);}get popupSignupSuccessMessage() {let xpath = '//*[@resource-id="android:id/message"]'return $(xpath);}async signUp(email, password) {await super.loginWidget.click();await this.btnSignUpContainer.click();await this.inputEmail.setValue(email);await this.inputPassword.setValue(password);await this.inputRepeatPassword.setValue(password);await this.btnSignUp.click();}
}module.exports = new LoginPage();
test/specs/test.e2e.js
const LoginPage = require('../pageobjects/login.page')describe('Login/Sign Up Form', () => {it('should signup with valid credentials', async () => {await LoginPage.signUp('testing123@mailnator.com', 'SuperSecretPassword!')await expect(LoginPage.popupSignupSuccessMessage).toBeExisting()const text = await LoginPage.popupSignupSuccessMessage.getText();expect(text).toContain('You successfully signed up!');})
})
package.json
{"name": "appdemo","version": "1.0.0","main": "index.js","scripts": {"test": "npx wdio wdio.conf.js","report": "npx allure generate allure-results --clean && npx allure open"},"keywords": [],"author": "","license": "ISC","description": "","devDependencies": {"@wdio/allure-reporter": "^9.2.14","@wdio/appium-service": "^9.4.1","@wdio/cli": "^9.4.1","@wdio/local-runner": "^9.4.1","@wdio/mocha-framework": "^9.2.8","@wdio/spec-reporter": "^9.2.14","allure-commandline": "^2.32.0","appium-uiautomator2-driver": "^3.9.1"}
}
wdio.conf.js
exports.config = {runner: 'local',specs: ['./test/specs/**/*.js'],maxInstances: 10,services: [['appium', {command: 'appium',logPath: './logs/'}]],capabilities: [{platformName: 'Android','appium:platformVersion': '12','appium:udid': '09714153AO000853','appium:autoGrantPermissions': true,'appium:automationName': 'UiAutomator2','appium:appPackage': 'com.wdiodemoapp','appium:appActivity': 'com.wdiodemoapp.MainActivity',}],reporters: ['spec', ['allure', {outputDir: './allure-results',disableWebdriverStepsReporting: true,disableWebdriverScreenshotsReporting: false,}]],logLevel: 'info',bail: 0,waitforTimeout: 10000,connectionRetryTimeout: 120000,connectionRetryCount: 3,framework: 'mocha',mochaOpts: {ui: 'bdd',timeout: 60000},
}
3.运行测试
运行之前需安装对应库
npm install --save-dev @wdio/appium-service
npm install --save-dev @wdio/spec-reporter
npm install --save-dev @wdio/allure-reporter
安装完成后下载对应apk
apk下载地址:https://github.com/webdriverio/native-demo-app/releases
然后在终端输入命令执行测试
npm test
4.测试报告
执行命令生成报告
npm run report