Skip to content

Commit

Permalink
feat: add webdriver wrapper (#89)
Browse files Browse the repository at this point in the history
  • Loading branch information
xudafeng committed Dec 15, 2021
1 parent 06d6cfb commit a954ca6
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 1 deletion.
53 changes: 53 additions & 0 deletions lib/webdriver.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
'use strict';

const fs = require('fs');
const path = require('path');
const { appendToContext } = require('macaca-reporter');
const { sync: mkdir } = require('mkdirp');
const { default: WebDriver } = require('webdriver');

module.exports.buildDriver = wd => {
let client;
const driver = wd.promiseChainRemote();

async function initDriver(options = {}) {
client = await WebDriver.newSession(options);
return driver;
}

async function saveScreenshots(context) {
const reportspath = path.join(process.cwd(), 'report');
const base64 = await client.takeScreenshot();
const img = new Buffer(base64, 'base64');
const filepath = path.join(reportspath, 'screenshots', `${Date.now()}.png`);
mkdir(path.dirname(filepath));
fs.writeFileSync(filepath, img.toString('binary'), 'binary');
appendToContext(context, `${path.relative(reportspath, filepath)}`);
}

async function execute(code) {
return await client.executeScript(code, []);
}

async function quit() {
await client.deleteSession();
}

async function initWindow() {
return driver;
}

async function get(url) {
return await client.navigateTo(url);
}

const { addPromiseChainMethod } = wd;
addPromiseChainMethod('initDriver', initDriver);
addPromiseChainMethod('saveScreenshots', saveScreenshots);
addPromiseChainMethod('execute', execute);
addPromiseChainMethod('quit', quit);
addPromiseChainMethod('initWindow', initWindow);
addPromiseChainMethod('get', get);

return driver;
};
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "macaca-wd",
"version": "3.4.2",
"version": "3.5.0",
"description": "Macaca wd client",
"keywords": [
"macaca",
Expand Down Expand Up @@ -34,6 +34,7 @@
"request": "~2.85.0",
"underscore.string": "~3.0.3",
"vargs": "~0.1.0",
"webdriver": "^7.16.11",
"webdriver-keycode": "^1.0.0",
"xlogger": "^1.0.6",
"xutil": "1"
Expand Down

0 comments on commit a954ca6

Please sign in to comment.