Skip to content

Commit 64b1340

Browse files
authored
Merge pull request #273 from maksym-moshniaha/WAT-5355
WAT-5355: Add possibility to take screenshots of elements
2 parents 47932ee + 81127eb commit 64b1340

6 files changed

Lines changed: 39 additions & 10 deletions

File tree

core/types/src/browser-proxy/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ export interface IBrowserProxyPlugin {
175175

176176
makeScreenshot(applicant: string): Promise<string | void>;
177177

178-
makeElementScreenshot(applicant: string, selector: Selector, scroll?: boolean): Promise<string | void>;
178+
makeElementScreenshot(applicant: string, selector: Selector): Promise<string | void>;
179179

180180
uploadFile(applicant: string, filePath: string): Promise<string | void>;
181181

packages/e2e-test-app/static-fixtures/screenshot.html

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,8 @@
99
</head>
1010

1111
<body data-test-automation-id="root" style="padding:0;">
12-
<div style="position: relative; width: 100px; height: 100px;padding:0;background-color: red;">
13-
12+
<div data-test-automation-id="testElement" style="position: relative; width: 100px; height: 100px;padding:0;background-color: red;">
1413
</div>
1514
</body>
1615

17-
</html>
16+
</html>
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import {run} from 'testring';
2+
import {assert} from 'chai';
3+
import {getTargetUrl} from './utils';
4+
5+
run(async (api) => {
6+
const app = api.application;
7+
await app.url(getTargetUrl(api, 'screenshot.html'));
8+
9+
const base64String = await app.makeElementScreenshot(app.root.testElement);
10+
11+
assert.ok(
12+
typeof base64String === 'string' && base64String.length > 0,
13+
'makeElementScreenshot should return a non-empty string',
14+
);
15+
16+
assert.doesNotThrow(
17+
() => Buffer.from(base64String, 'base64'),
18+
'returned string should be valid base64 encoded',
19+
);
20+
});

packages/plugin-selenium-driver/src/plugin/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1247,12 +1247,12 @@ export class SeleniumPlugin implements IBrowserProxyPlugin {
12471247
return client.takeScreenshot();
12481248
}
12491249

1250-
public async makeElementScreenshot(applicant: string, selector: Selector, scroll: boolean = true): Promise<string | void> {
1250+
public async makeElementScreenshot(applicant: string, selector: Selector): Promise<string | void> {
12511251
await this.createClient(applicant);
12521252
const client = this.getBrowserClient(applicant);
12531253
const element = await this.getElement(applicant, selector);
12541254

1255-
return client.takeElementScreenshot(await element.elementId, scroll);
1255+
return client.takeElementScreenshot(await element.elementId);
12561256
}
12571257

12581258
public async uploadFile(

packages/web-application/src/web-application.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ export class WebApplication extends PluggableModule {
199199
if (typeof selector === 'string') {
200200
return { type: 'xpath', xpath: selector } as XpathSelector;
201201
}
202-
202+
203203
if (this.isShadowElementPathProxy(selector)) {
204204
return { type: 'shadow-css', css: selector.toShadowCSSSelector(), parentSelectors: selector.getParentSelectors(), isShadowElement: true } as ShadowCssSelector;
205205
}
@@ -458,7 +458,7 @@ export class WebApplication extends PluggableModule {
458458
return result;
459459
}
460460

461-
461+
462462
@stepLog(function (this: WebApplication, xpath: ElementPath, timeout: number = this.WAIT_TIMEOUT) {
463463
return `Checking if ${this.formatXpath(xpath)} is visible for ${timeout}`;
464464
})
@@ -1714,6 +1714,16 @@ export class WebApplication extends PluggableModule {
17141714
return null;
17151715
}
17161716

1717+
@stepLog(function (this: WebApplication, xpath: ElementPath) {
1718+
return `Making screenshot of ${this.formatXpath(xpath)}`;
1719+
})
1720+
public async makeElementScreenshot(xpath: ElementPath, timeout: number = this.WAIT_TIMEOUT): Promise<string | null> {
1721+
await this.waitForExist(xpath, timeout);
1722+
1723+
const normalizedXPath = this.normalizeSelector(xpath);
1724+
return await this.client.makeElementScreenshot(normalizedXPath);
1725+
}
1726+
17171727
@stepLog(function (this: WebApplication, fullPath: string) {
17181728
return `Uploading file ${fullPath}`;
17191729
})

packages/web-application/src/web-client.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -307,8 +307,8 @@ export class WebClient implements IWebApplicationClient {
307307
return this.makeRequest(BrowserProxyActions.makeScreenshot, []);
308308
}
309309

310-
public makeElementScreenshot(selector: Selector, scroll?: boolean) {
311-
return this.makeRequest(BrowserProxyActions.makeElementScreenshot, [selector, scroll]);
310+
public makeElementScreenshot(selector: Selector) {
311+
return this.makeRequest(BrowserProxyActions.makeElementScreenshot, [selector]);
312312
}
313313

314314
public uploadFile(path: string) {

0 commit comments

Comments
 (0)