-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
171 lines (143 loc) · 4.3 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
const path = require('path');
const fs = require('fs');
const axios = require('axios');
const FormData = require('form-data');
const puppeteer = require('puppeteer');
const debug = require('debug')('diffity:screenshoter');
class Diffity {
constructor(options = {}) {
this.setOptions(options);
}
async setOptions(opts = {}) {
const options = {
os: 'linux',
browser: 'chrome',
deviceName: '',
viewPort: {width: 1024, height: 768},
device: 'desktop',
apiBaseUrl: 'https://diffity.herokuapp.com',
...opts
}
this.viewPort = options.viewPort;
this.apiKey = options.apiKey;
this.os = options.os;
this.browser = options.browser;
this.deviceName = options.deviceName;
this.device = options.device;
this.projectName = options.projectName;
this.apiBaseUrl = options.apiBaseUrl;
this.currentRunId = null;
this.uploadQueue = [];
}
async start() {
this.headlessBrowser = await puppeteer.launch();
this.page = await this.headlessBrowser.newPage();
debug( 'browser launched ');
this.page.setViewport(this.viewPort);
debug( 'view port set to ', this.viewPort);
return this;
}
end() {
this.headlessBrowser.close();
this._updateStatus();
}
_updateStatus(status = "completed") {
return axios.put(this.apiBaseUrl + `/api/v1/runs/${this.currentRunId}/status`, { status }, {
auth: {
username: this.apiKey,
password: 'X'
}
})
.then((response) => {
if(response.data.id) {
debug('Run status updated successfully :: ', response.data.screenshot_progress);
return response.data;
}
debug('Run status updated failed :: ', response.data);
return response.data;
})
}
_validateProps() {
if (!this.apiKey) {
throw new Error('Missing mandatory property :: apiKey');
}
if (!this.projectName) {
throw new Error('Missing mandatory property :: projectName');
}
}
async loadUrl(url) {
if (!this.page) {
await this.start();
}
debug( 'navigating to ', url);
return this.page.goto(url);
}
async screenshot(identifier) {
this._validateProps();
const outputPath = path.join(__dirname, 'tmp', identifier +'.png');
await this.page.screenshot({path: outputPath, fullPage: true});
debug( 'screenshot saved ', outputPath);
return this.upload(outputPath, identifier);
}
uploadAll() {
this.uploadQueue.forEach(uploadImage => {
this.upload(uploadImage.outputPath, uploadImage.identifier);
});
}
createRun(details = {}) {
const runDetails = {
project: this.projectName,
name: `${details.project || this.projectName}-${new Date()}`,
js_driver: 'diffity-node',
group: 'group',
author: 'author',
...details,
}
debug( 'creating run with name ', runDetails.name);
return axios
.post(this.apiBaseUrl + `/api/v1/runs`, runDetails, {
auth: {
username: this.apiKey,
password: 'X'
}
})
.then((response) => {
if(response.data.id) {
debug('Run created :: ', response.data.id);
return response.data;
}
})
}
async upload(screenshotPath, identifier) {
if (!this.currentRunId) {
const { id } = await this.createRun();
this.currentRunId = id;
}
const formData = new FormData();
formData.append("identifier", identifier);
formData.append("browser", this.browser);
formData.append("device", this.device);
formData.append("os", this.os);
formData.append("device_name", this.deviceName);
formData.append("image", fs.createReadStream(screenshotPath));
debug( 'uploading screenshot ', screenshotPath);
return axios
.post(this.apiBaseUrl + `/api/v1/runs/${this.currentRunId}/run_images`, formData, {
headers: formData.getHeaders(),
auth: {
username: this.apiKey,
password: 'X'
}
})
.then(response => {
const repsonseData = response.data.data;
if(repsonseData.run_id) {
debug('Upload successful! Server responded with:', repsonseData.run_id);
return repsonseData;
}
debug('Upload failed! Server responded with:', repsonseData);
return repsonseData;
});
}
}
module.exports = Diffity;