Skip to content

Commit

Permalink
Merge pull request #7 from delta-reporter/file-sending-update
Browse files Browse the repository at this point in the history
WIP - Added preliminary version to send files
  • Loading branch information
Jnegrier authored Jun 18, 2020
2 parents a7e9877 + f859b3a commit 03545bc
Show file tree
Hide file tree
Showing 7 changed files with 1,358 additions and 421 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
node_modules
.delta_service
_results_
lib
logs
1,581 changes: 1,227 additions & 354 deletions package-lock.json

Large diffs are not rendered by default.

34 changes: 15 additions & 19 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,15 +1,11 @@
{
"name": "@delta-reporter/wdio-delta-reporter-service",
"version": "0.0.2",
"version": "1.0.0",
"description": "Delta Reporter service for WebdriverIO",
"main": "index.js",
"main": "lib/src/index.js",
"files": [
"index.js",
"compare.js",
"launcher.js",
"*.md",
"src",
"lib"
"lib/src"
],
"scripts": {
"clean": "rimraf lib .tmp",
Expand Down Expand Up @@ -42,14 +38,14 @@
"devDependencies": {
"@types/chai": "^4.2.7",
"@types/mocha": "^5.2.7",
"@types/node": "^10.17.21",
"@wdio/cli": "^5.22.4",
"@types/node": "^10.17.26",
"@wdio/cli": "^5.23.0",
"@wdio/dot-reporter": "^5.22.4",
"@wdio/local-runner": "^5.22.4",
"@wdio/mocha-framework": "^5.16.5",
"@wdio/local-runner": "^5.23.0",
"@wdio/mocha-framework": "^5.23.0",
"@wdio/selenium-standalone-service": "^5.16.10",
"@wdio/spec-reporter": "^5.22.4",
"@wdio/sync": "^5.16.9",
"@wdio/spec-reporter": "^5.23.0",
"@wdio/sync": "^5.23.0",
"chai": "^4.2.0",
"lint-staged": "^8.1.7",
"mocha": "^5.2.0",
Expand All @@ -58,20 +54,20 @@
"ts-node": "^7.0.1",
"tsconfig-paths": "^3.9.0",
"tslint": "^5.16.0",
"typescript": "^3.7.4",
"wdio-docker-service": "^2.3.1",
"webdriverio": "^5.22.4"
"typescript": "^3.9.5",
"wdio-docker-service": "^2.4.0",
"webdriverio": "^5.23.0"
},
"dependencies": {
"axios": "^0.19.2",
"axios-retry": "^3.1.8",
"husky": "^3.0.8",
"wdio-mocha-framework": "^0.6.4"
"wdio-mocha-framework": "^0.6.4",
"wdio-video-reporter": "^3.0.0"
},
"homepage": "https://github.com/delta-reporter/wdio-delta-reporter-service#readme",
"directories": {
"lib": "lib",
"test": "test"
"lib": "lib"
},
"keywords": [
"webdriverio",
Expand Down
18 changes: 18 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const RestClientPro = require('./rest');
const fs = require('fs');
const path = require('path');
const rimraf = require('rimraf');
const FormData = require('form-data');
const log = logger('wdio-delta-reporter-service');

class DeltaService {
Expand Down Expand Up @@ -62,6 +63,15 @@ class DeltaService {
return this.restClient.update(url, data);
}

sendFileToTest(test_history_id: number, type: string, file: any, description?: string) {
const url = ['api/v1/file_receiver_test_history/' + test_history_id];
const form = new FormData();
form.append('type', type);
form.append('file', file);
description ? form.append('description', description) : form.append('description', '');
return this.restClient.create(url, form, { headers: form.getHeaders() });
}

async onPrepare(config, capabilities) {
log.setLevel(config.logLevel || 'info');

Expand Down Expand Up @@ -104,6 +114,14 @@ class DeltaService {
log.info(response);
}

async before(capabilities, specs) {
browser.addCommand('sendFileToTest', async (type, file, description?) => {
const delta_test = JSON.parse(fs.readFileSync('./.delta_service/test.json'));
var response = await this.sendFileToTest(delta_test.test_history_id, type, file, description);
log.info(response);
});
}

async beforeSuite(suite) {
const testRun = JSON.parse(fs.readFileSync('./.delta_service/testRun.json'));

Expand Down
93 changes: 48 additions & 45 deletions src/rest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,51 +4,54 @@ const axiosRetry = require('axios-retry');
axiosRetry(axios, { retryDelay: () => 100, retries: 10, retryCondition: axiosRetry.isRetryableError });

class RestClient {
baseURL: string;
headers: any;

constructor(options) {
this.baseURL = options.baseURL;
}

buildPath(path) {
return [this.baseURL, path].join('/');
}

static request(method, url, data, options: any = {}) {
return axios({
method,
url,
headers: options.headers,
data,
})
.then((response) => response.data)
.catch((error) => {
const errorMessage = error.message;
const responseData = error.response && error.response.data;
throw new Error(`${errorMessage}${
responseData
&& typeof responseData === 'object'
? `: ${JSON.stringify(responseData)}`
: ''}`);
});
}

create(path, data, options = { headers: this.headers }) {
return RestClient.request('POST', this.buildPath(path), data, options);
}

retrieve(path, options = { headers: this.headers }) {
return RestClient.request('GET', this.buildPath(path), {}, options);
}

update(path, data, options = { headers: this.headers }) {
return RestClient.request('PUT', this.buildPath(path), data, options);
}

delete(path, data, options = { headers: this.headers }) {
return RestClient.request('DELETE', this.buildPath(path), data, options);
}
baseURL: string;
headers: any;

constructor(options) {
this.baseURL = options.baseURL;
}

buildPath(path) {
return [this.baseURL, path].join('/');
}

static request(method, url, data, options: any = {}) {
return axios({
method,
url,
headers: options.headers,
data
})
.then(response => response.data)
.catch(error => {
const errorMessage = error.message;
const responseData = error.response && error.response.data;
throw new Error(
`${errorMessage}${
responseData && typeof responseData === 'object' ? `: ${JSON.stringify(responseData)}` : ''
}`
);
});
}

create(path, data, options = { headers: this.headers }) {
console.log('### SEND FILE LOGS ###');
console.log(data);
console.log(options);
return RestClient.request('POST', this.buildPath(path), data, options);
}

retrieve(path, options = { headers: this.headers }) {
return RestClient.request('GET', this.buildPath(path), {}, options);
}

update(path, data, options = { headers: this.headers }) {
return RestClient.request('PUT', this.buildPath(path), data, options);
}

delete(path, data, options = { headers: this.headers }) {
return RestClient.request('DELETE', this.buildPath(path), data, options);
}
}

module.exports = RestClient;
13 changes: 12 additions & 1 deletion test/wdio/DeltaReporterClient.test.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,25 @@
import { expect } from 'chai';

describe('Basic validations', function() {
// this.retries(1);
this.retries(1);

it('Should check that the page is loaded', () => {
browser.url('http://the-internet.herokuapp.com/');
});

it('Should check that the nested frames page is loaded', () => {
browser.url('http://the-internet.herokuapp.com/nested_frames');
browser.pause(1000);
browser.url('http://the-internet.herokuapp.com/slow');
browser.pause(1000);
browser.url('http://the-internet.herokuapp.com/typos');
browser.pause(1000);
browser.url('http://the-internet.herokuapp.com/shifting_content');
browser.pause(1000);
browser.url('http://the-internet.herokuapp.com/jqueryui/menu');
browser.pause(1000);
browser.url('http://the-internet.herokuapp.com/challenging_dom');
browser.pause(1000);
expect($('[name="frameset-middle"]').isDisplayed()).to.be.true;
});

Expand Down
39 changes: 37 additions & 2 deletions test/wdio/wdio.config.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,20 @@
var path = require('path');
const fs = require('fs');
const video = require('wdio-video-reporter');
const DeltaService = require('../../lib/src');

function getLatestFile({ directory, extension }, callback) {
fs.readdir(directory, (_, dirlist) => {
const latest = dirlist
.map(_path => ({ stat: fs.lstatSync(path.join(directory, _path)), dir: _path }))
.filter(_path => _path.stat.isFile())
.filter(_path => (extension ? _path.dir.endsWith(`.${extension}`) : 1))
.sort((a, b) => b.stat.mtime - a.stat.mtime)
.map(_path => _path.dir);
callback(directory + '/' + latest[0]);
});
}

exports.config = {
specs: ['./test/wdio/DeltaReporterClient.test.ts'],
capabilities: [
Expand All @@ -24,7 +38,17 @@ exports.config = {
require: ['tsconfig-paths/register', 'ts-node/register'],
timeout: 60000
},
reporters: ['dot', 'spec'],
reporters: [
'dot',
'spec',
[
video,
{
saveAllVideos: false, // If true, also saves videos for successful test cases
videoSlowdownMultiplier: 3 // Higher to get slower videos, lower for faster videos [Value 1-100]
}
]
],
services: [
'docker',
[
Expand Down Expand Up @@ -54,5 +78,16 @@ exports.config = {
},
// Options for selenium-standalone
// Path where all logs from the Selenium server should be stored.
seleniumLogs: './logs/'
seleniumLogs: './logs/',
afterTest(test) {
if (test.passed === false) {
const file_name = 'screenshot.png';
const outputFile = path.join(__dirname, file_name);
browser.saveScreenshot(outputFile);
browser.sendFileToTest('img', fs.createReadStream(outputFile));
getLatestFile({ directory: browser.options.outputDir + '/_results_', extension: 'mp4' }, (filename = null) => {
browser.sendFileToTest('video', fs.createReadStream(filename), 'Video captured during test execution');
});
}
}
};

0 comments on commit 03545bc

Please sign in to comment.