Skip to content

Commit c2c76f1

Browse files
authored
support playwright helper (#81)
1 parent d163f6d commit c2c76f1

File tree

1 file changed

+13
-11
lines changed

1 file changed

+13
-11
lines changed

index.js

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -77,16 +77,16 @@ class ResembleHelper extends Helper {
7777
if (!data.isSameDimensions) {
7878
let dimensions1 = sizeOf(baseImage);
7979
let dimensions2 = sizeOf(actualImage);
80-
reject(new Error("The base image is of " + dimensions1.height + " X " + dimensions1.width + " and actual image is of " + dimensions2.height + " X " + dimensions2.width + ". Please use images of same dimensions so as to avoid any unexpected results."));
80+
reject(new Error(`The base image is of ${dimensions1.height} X ${dimensions1.width} and actual image is of ${dimensions2.height} X ${dimensions2.width}. Please use images of same dimensions so as to avoid any unexpected results.`));
8181
}
8282
resolve(data);
8383
if (data.misMatchPercentage >= tolerance) {
8484
if (!fs.existsSync(getDirName(this.diffFolder + diffImage))) {
85-
fs.mkdirSync(getDirName(this.diffFolder + diffImage));
85+
fs.mkdirSync(getDirName(this.diffFolder + diffImage));
8686
}
8787
fs.writeFileSync(this.diffFolder + diffImage + '.png', data.getBuffer());
8888
const diffImagePath = path.join(process.cwd(), this.diffFolder + diffImage + '.png');
89-
this.debug("Diff Image File Saved to: " + diffImagePath);
89+
this.debug(`Diff Image File Saved to: ${diffImagePath}`);
9090
}
9191
}
9292
});
@@ -114,15 +114,13 @@ class ResembleHelper extends Helper {
114114
*/
115115
async screenshotElement(selector, name) {
116116
const helper = this._getHelper();
117-
if (this.helpers['Puppeteer']) {
117+
if (this.helpers['Puppeteer'] || this.helpers['Playwright']) {
118118
await helper.waitForVisible(selector);
119119
const els = await helper._locate(selector);
120120
if (!els.length) throw new Error(`Element ${selector} couldn't be located`);
121121
const el = els[0];
122122

123-
await el.screenshot({
124-
path: global.output_dir + "/" + name + '.png'
125-
});
123+
await el.screenshot({path: `${global.output_dir}/${name}.png`});
126124
} else if (this.helpers['WebDriver']) {
127125
await helper.waitForVisible(selector);
128126
const els = await helper._locate(selector);
@@ -137,7 +135,7 @@ class ResembleHelper extends Helper {
137135
const { t } = this.helpers['TestCafe'];
138136

139137
await t.takeElementScreenshot(els, name);
140-
} else throw new Error("Method only works with Puppeteer, WebDriver or TestCafe helpers.");
138+
} else throw new Error("Method only works with Playwright, Puppeteer, WebDriver or TestCafe helpers.");
141139
}
142140

143141
/**
@@ -381,7 +379,7 @@ class ResembleHelper extends Helper {
381379
const helper = this._getHelper();
382380
await helper.waitForVisible(selector);
383381
const els = await helper._locate(selector);
384-
382+
385383
if (this.helpers['TestCafe']) {
386384
if (await els.count != 1) throw new Error(`Element ${selector} couldn't be located or isn't unique on the page`);
387385
}
@@ -391,7 +389,7 @@ class ResembleHelper extends Helper {
391389

392390
let location, size;
393391

394-
if (this.helpers['Puppeteer']) {
392+
if (this.helpers['Puppeteer'] || this.helpers['Playwright']) {
395393
const el = els[0];
396394
const box = await el.boundingBox();
397395
size = location = box;
@@ -447,7 +445,11 @@ class ResembleHelper extends Helper {
447445
return this.helpers['TestCafe'];
448446
}
449447

450-
throw new Error('No matching helper found. Supported helpers: WebDriver/Appium/Puppeteer/TestCafe');
448+
if (this.helpers['Playwright']) {
449+
return this.helpers['Playwright'];
450+
}
451+
452+
throw new Error('No matching helper found. Supported helpers: Playwright/WebDriver/Appium/Puppeteer/TestCafe');
451453
}
452454
}
453455

0 commit comments

Comments
 (0)