Skip to content

Commit

Permalink
feat: Support takeScreenshot (#11)
Browse files Browse the repository at this point in the history
* feat: Support take screenshot

* add fullPage check

* add comments
  • Loading branch information
tianfeng92 authored Aug 9, 2024
1 parent e45af5b commit 710d006
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 6 deletions.
10 changes: 10 additions & 0 deletions src/driver.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import wd, { Client } from 'webdriver';
import * as fs from 'fs';
import { isDevice, isSimulator } from './device';
import { CreateSessionError } from './errors';

Expand Down Expand Up @@ -139,4 +140,13 @@ export class SauceDriver {
newWindowSize.height,
);
}

async takeScreenshot(browserId: string, filepath: string) {
const browser = this.sessions.get(browserId);
if (!browser) {
return;
}
const screenBuffer = await browser.takeScreenshot();
fs.writeFileSync(filepath, Buffer.from(screenBuffer, 'base64'));
}
}
29 changes: 23 additions & 6 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { SauceDriver } from './driver.js';
import { SauceDriver } from './driver';
import { AuthError, TunnelNameError, WindowSizeRangeError } from './errors';
import { getPlatforms } from './api';
import { rcompareOses, rcompareVersions } from './sort';
import { isDevice } from './device.js';
import { isDevice } from './device';

type Browser = string;
type Version = string;
Expand Down Expand Up @@ -230,10 +230,27 @@ module.exports = {
* Called by TestCafe to take a screenshot.
*
* https://github.com/DevExpress/testcafe/blob/4a30f1c3b8769ca68c9b7912911f1dd8aa91d62c/src/browser/provider/plugin-host.js#L134
*
* @param {string} browserId - The ID of the browser session.
* @param {string} screenshotPath - The absolute path with .png extension where the screenshot will be saved.
* It also supports path pattern.
* @param {number} pageWidth - The width of the page to capture, currently no use.
* @param {number} pageHeight - The height of the page to capture, currently no use.
* @param {boolean} fullPage - A flag indicating whether to capture a full-page screenshot.
* Currently, full-page screenshots are not supported.
*/
async takeScreenshot(/* id, screenshotPath, pageWidth, pageHeight */) {
this.reportWarning(
'The screenshot functionality is not supported by the Sauce Labs browser provider plugin.',
);
async takeScreenshot(
browserId: string,
screenshotPath: string,
pageWidth: number,
pageHeight: number,
fullPage: boolean,
) {
if (fullPage) {
console.warn(
'Taking a full-page screenshot on the remote browser is not supported.',
);
}
await sauceDriver.takeScreenshot(browserId, screenshotPath);
},
};

0 comments on commit 710d006

Please sign in to comment.