macaca对元素选择器,提供了很多接口,诸如:elementById、elementByClassName、elementByCss等元素定位选择器、对于操作事件也封装了很多方便的方法,诸如:click()模拟点击、sendKeys模拟键入,等事件、下面是今天看这个api简单编写的测试程序。
实例非常简单:用例:打开页面,,输入用户名,输入密码,点击登录按钮
'use strict'
const {assert} = require('chai')
const {opn} = require('macaca-utils')
const path = require('path')
const wd = require('macaca-wd')
var browser = 'electron'
describe('login-macaca-test', function() {
this.timeout(5 * 60 * 1000)
//定义webdriver对象,用于提供macaca服务
var driver = wd.promiseChainRemote({
host: 'localhost',
port: process.env.MACACA_SERVER_PORT || 3456
})
//before实例化webdriver,定义执行情况的视口
before(() => {
return driver
.init({
platformName: 'desktop',
browserName: browser,
userAgent: `Mozilla/5.0 AppleWebKit/537.36 (KHTML, like Gecko) Chrome/59.0 Safari/537.36 Macaca Custom UserAgent`,
deviceScaleFactor: 2
})
.setWindowSize(1280, 800);
})
describe('macaca desktop sample', function() {
it('#1 should login success', function() {
const initialURL = 'https://bq150623.wornhole1.cn:38043/gz_opinion_test/pc_login'
return driver
.get(initialURL)
.sleep(3000)
.elementByClassName('ACP_USR')
.sendKeys('cqsj_shangbao1')
.sleep(2000)
.elementByClassName('ACP_PSW')
.sendKeys('cqsj1234')
.sleep(2000)
.elementByCss('input[value]')
.click()
.sleep(5000)
.source()
.then(function() {
console.log('success!')
})
})
after(() => {
return driver
})
})
})