-
Notifications
You must be signed in to change notification settings - Fork 46
/
index.js
84 lines (74 loc) · 2.02 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
import puppeteer from 'puppeteer';
import chain from './helpers/proxyChain';
import { sanitiseGlobalConfiguration, sanitiseTestConfiguration } from './sanitiser';
import Target from './target';
import logger from './utils/logger';
export default class Differencify {
constructor(conf) {
if (conf && conf.debug === true) {
logger.enable();
}
this.configuration = sanitiseGlobalConfiguration(conf);
this.browser = null;
this.testId = 0;
}
async launchBrowser(options) {
if (!this.browser) {
logger.log('Launching browser...');
try {
this.browser = await puppeteer.launch(options);
} catch (error) {
logger.trace(error);
}
} else {
logger.log('Using existing browser instance');
}
}
static executablePath() {
return puppeteer.executablePath();
}
static chromeExecutablePath() {
return puppeteer.executablePath();
}
async launch(options) {
this.launchBrowser(options);
}
async connectBrowser(options) {
if (!this.browser) {
logger.log('Launching browser...');
try {
this.browser = await puppeteer.connect(options);
} catch (error) {
logger.trace(error);
}
} else {
logger.log('Using existing browser instance');
}
}
async connect(options) {
this.connectBrowser(options);
}
init(config) {
this.testId += 1;
const testConfig = sanitiseTestConfiguration(config, this.testId);
if (testConfig.isUpdate) {
logger.warn('Your tests are running on update mode. Test screenshots will be updated');
}
const target = new Target(this.browser, this.configuration, testConfig);
target.isJest();
const chainedTarget = chain(target, testConfig.chain);
target.chainedTarget = chainedTarget;
return chainedTarget;
}
async cleanup() {
logger.log('Closing browser...');
try {
if (this.browser) {
await this.browser.close();
}
} catch (error) {
logger.trace(error);
}
}
}
module.exports = Differencify;