Skip to content

Commit 5dc650f

Browse files
authored
Merge pull request #25 from Kartikeya99/ssElement
Added screenshotElement function
2 parents a4ab7fd + cebb963 commit 5dc650f

File tree

2 files changed

+34
-6
lines changed

2 files changed

+34
-6
lines changed

Diff for: README.md

+8-2
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,12 @@ It is exactly same as `seeVisualDiff` function, only an additional `selector` CS
6969
> Note:
7070
`seeVisualDiffForElement` only works when the page for baseImage is open in the browser, so that webdriver can fetch coordinates of the provided selector
7171

72-
72+
Third one is the `screenshotElement` which basically takes screenshot of the element. Selector for the element must be provided.
73+
It saves the image in the output directory as mentioned in the config folder.
74+
This method only works with puppeteer.
75+
```
76+
I.screenshotElement("selectorForElement", "nameForImage");
77+
```
7378

7479
Finally to use the helper in your test, you can write something like this:
7580

@@ -95,4 +100,5 @@ Scenario('Compare CPU Usage Images', async (I) => {
95100
adminPage.navigateToDashboard("OS", "System Overview");
96101
I.seeVisualDiffForElement("//div[@class='panel-container']", "Complete_Dashboard_Image.png", {prepareBaseImage: false, tolerance: 3});
97102
});
98-
```
103+
```
104+

Diff for: index.js

+26-4
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,29 @@ class ResembleHelper extends Helper {
6868
return data.misMatchPercentage;
6969
}
7070

71+
/**
72+
* Take screenshot of individual element.
73+
* @param selector selector of the element to be screenshotted
74+
* @param name name of the image
75+
* @returns {Promise<void>}
76+
*/
77+
async screenshotElement(selector, name) {
78+
const helper = this._getHelper();
79+
if(this.helpers['Puppeteer']){
80+
const configuration = this.config;
81+
82+
await helper.waitForVisible(selector);
83+
const els = await helper._locate(selector);
84+
if (!els.length) throw new Error(`Element ${selector} couldn't be located`);
85+
const el = els[0];
86+
87+
await el.screenshot({
88+
path: configuration.screenshotFolder + name + '.png'
89+
});
90+
}
91+
else throw new Error("Method only works with Puppeteer");
92+
}
93+
7194
/**
7295
* Check Visual Difference for Base and Screenshot Image
7396
* @param baseImage Name of the Base Image (Base Image path is taken from Configuration)
@@ -172,8 +195,8 @@ class ResembleHelper extends Helper {
172195
}
173196

174197
if (this.helpers['WebDriver'] || this.helpers['Appium']) {
175-
location = await ele.getLocation();
176-
size = await ele.getSize();
198+
location = await el.getLocation();
199+
size = await el.getSize();
177200
}
178201

179202
if (this.helpers['WebDriverIO']) {
@@ -213,5 +236,4 @@ class ResembleHelper extends Helper {
213236
throw new Error('No matching helper found. Supported helpers: WebDriver/Appium/Puppeteer');
214237
}
215238
}
216-
217-
module.exports = ResembleHelper;
239+
module.exports = ResembleHelper;

0 commit comments

Comments
 (0)