跳到主要内容

保存截图

将当前浏览上下文的截图保存为操作系统上的PNG文件。请注意, 一些浏览器驱动程序会截取整个文档的截图(例如Firefox的Geckodriver), 而其他驱动程序只截取当前视口的截图(例如Chrome的Chromedriver)。

用法
browser.saveScreenshot(filepath, { fullPage, format, quality, clip })
参数
名称类型详情
filepathString生成图片的路径(需要.png后缀)相对于执行目录
optionsObject截图选项
options.fullPage=false
可选
Boolean是否截取整个页面或仅当前视口
options.format='png'
可选
String截图的格式(pngjpeg
options.quality=100
可选
NumberJPEG格式的截图质量,范围为0-100百分比
options.clip
可选
Object截图的矩形裁剪区域
示例
saveScreenshot.js
it('should save a screenshot of the browser viewport', async () => {
await browser.saveScreenshot('./some/path/screenshot.png');
});

it('should save a screenshot of the full page', async () => {
await browser.saveScreenshot('./some/path/screenshot.png', { fullPage: true });
});

it('should save a screenshot of a specific rectangle', async () => {
await browser.saveScreenshot('./some/path/screenshot.png', { clip: { x: 0, y: 0, width: 100, height: 100 } });
});

it('should save a screenshot of the full page in JPEG format', async () => {
await browser.saveScreenshot('./some/path/screenshot.jpeg', { fullPage: true, format: 'jpeg' });
});

it('should save a screenshot of the full page in JPEG format with quality 50', async () => {
await browser.saveScreenshot('./some/path/screenshot.jpeg', { fullPage: true, format: 'jpeg', quality: 50 });
});

running from a hook, make sure to explicitly define the hook as async:

wdio.conf.js
afterTest: async function(test) {
await browser.saveScreenshot('./some/path/screenshot.png');
}
返回
  • <Buffer> return: 截图缓冲区

Welcome! How can I help?

WebdriverIO AI Copilot