撰寫測試
測試執行器框架支援
@wdio/visual-service
與測試執行器框架無關,這表示您可以使用它和 WebdriverIO 支援的所有框架,例如
在您的測試中,您可以儲存螢幕截圖,或將正在測試的應用程式的目前視覺狀態與基準進行比對。為此,此服務提供了自訂匹配器,以及檢查方法
- Mocha
- Jasmine
- CucumberJS
describe('Mocha Example', () => {
beforeEach(async () => {
await browser.url('https://webdriverio.dev.org.tw')
})
it('using visual matchers to assert against baseline', async () => {
// Check screen to exactly match with baseline
await expect(browser).toMatchScreenSnapshot('partialPage')
// check an element to have a mismatch percentage of 5% with the baseline
await expect(browser).toMatchScreenSnapshot('partialPage', 5)
// check an element with options for `saveScreen` command
await expect(browser).toMatchScreenSnapshot('partialPage', {
/* some options */
})
// Check an element to exactly match with baseline
await expect($('#element-id')).toMatchElementSnapshot('firstButtonElement')
// check an element to have a mismatch percentage of 5% with the baseline
await expect($('#element-id')).toMatchElementSnapshot('firstButtonElement', 5)
// check an element with options for `saveElement` command
await expect($('#element-id')).toMatchElementSnapshot('firstButtonElement', {
/* some options */
})
// Check a full page screenshot match with baseline
await expect(browser).toMatchFullPageSnapshot('fullPage')
// Check a full page screenshot to have a mismatch percentage of 5% with the baseline
await expect(browser).toMatchFullPageSnapshot('fullPage', 5)
// Check a full page screenshot with options for `checkFullPageScreen` command
await expect(browser).toMatchFullPageSnapshot('fullPage', {
/* some options */
})
// Check a full page screenshot with all tab executions
await expect(browser).toMatchTabbablePageSnapshot('check-tabbable')
// Check a full page screenshot to have a mismatch percentage of 5% with the baseline
await expect(browser).toMatchTabbablePageSnapshot('check-tabbable', 5)
// Check a full page screenshot with options for `checkTabbablePage` command
await expect(browser).toMatchTabbablePageSnapshot('check-tabbable', {
/* some options */
})
})
it('should save some screenshots', async () => {
// Save a screen
await browser.saveScreen('examplePage', {
/* some options */
})
// Save an element
await browser.saveElement(
await $('#element-id'),
'firstButtonElement',
{
/* some options */
}
)
// Save a full page screenshot
await browser.saveFullPageScreen('fullPage', {
/* some options */
})
// Save a full page screenshot with all tab executions
await browser.saveTabbablePage('save-tabbable', {
/* some options, use the same options as for saveFullPageScreen */
})
})
it('should compare successful with a baseline', async () => {
// Check a screen
await expect(
await browser.checkScreen('examplePage', {
/* some options */
})
).toEqual(0)
// Check an element
await expect(
await browser.checkElement(
await $('#element-id'),
'firstButtonElement',
{
/* some options */
}
)
).toEqual(0)
// Check a full page screenshot
await expect(
await browser.checkFullPageScreen('fullPage', {
/* some options */
})
).toEqual(0)
// Check a full page screenshot with all tab executions
await expect(
await browser.checkTabbablePage('check-tabbable', {
/* some options, use the same options as for checkFullPageScreen */
})
).toEqual(0)
})
})
describe('Jasmine Example', () => {
beforeEach(async () => {
await browser.url('https://webdriverio.dev.org.tw')
})
it('using visual matchers to assert against baseline', async () => {
// Check screen to exactly match with baseline
await expect(browser).toMatchScreenSnapshot('partialPage')
// check an element to have a mismatch percentage of 5% with the baseline
await expect(browser).toMatchScreenSnapshot('partialPage', 5)
// check an element with options for `saveScreen` command
await expect(browser).toMatchScreenSnapshot('partialPage', {
/* some options */
})
// Check an element to exactly match with baseline
await expect($('#element-id')).toMatchElementSnapshot('firstButtonElement')
// check an element to have a mismatch percentage of 5% with the baseline
await expect($('#element-id')).toMatchElementSnapshot('firstButtonElement', 5)
// check an element with options for `saveElement` command
await expect($('#element-id')).toMatchElementSnapshot('firstButtonElement', {
/* some options */
})
// Check a full page screenshot match with baseline
await expect(browser).toMatchFullPageSnapshot('fullPage')
// Check a full page screenshot to have a mismatch percentage of 5% with the baseline
await expect(browser).toMatchFullPageSnapshot('fullPage', 5)
// Check a full page screenshot with options for `checkFullPageScreen` command
await expect(browser).toMatchFullPageSnapshot('fullPage', {
/* some options */
})
// Check a full page screenshot with all tab executions
await expect(browser).toMatchTabbablePageSnapshot('check-tabbable')
// Check a full page screenshot to have a mismatch percentage of 5% with the baseline
await expect(browser).toMatchTabbablePageSnapshot('check-tabbable', 5)
// Check a full page screenshot with options for `checkTabbablePage` command
await expect(browser).toMatchTabbablePageSnapshot('check-tabbable', {
/* some options */
})
})
it('should save some screenshots', async () => {
// Save a screen
await browser.saveScreen('examplePage', {
/* some options */
})
// Save an element
await browser.saveElement(
await $('#element-id'),
'firstButtonElement',
{
/* some options */
}
)
// Save a full page screenshot
await browser.saveFullPageScreen('fullPage', {
/* some options */
})
// Save a full page screenshot with all tab executions
await browser.saveTabbablePage('save-tabbable', {
/* some options, use the same options as for saveFullPageScreen */
})
})
it('should compare successful with a baseline', async () => {
// Check a screen
await expect(
await browser.checkScreen('examplePage', {
/* some options */
})
).toEqual(0)
// Check an element
await expect(
await browser.checkElement(
await $('#element-id'),
'firstButtonElement',
{
/* some options */
}
)
).toEqual(0)
// Check a full page screenshot
await expect(
await browser.checkFullPageScreen('fullPage', {
/* some options */
})
).toEqual(0)
// Check a full page screenshot with all tab executions
await expect(
await browser.checkTabbablePage('check-tabbable', {
/* some options, use the same options as for checkFullPageScreen */
})
).toEqual(0)
})
})
import { When, Then } from '@wdio/cucumber-framework'
When('I save some screenshots', async function () {
// Save a screen
await browser.saveScreen('examplePage', {
/* some options */
})
// Save an element
await browser.saveElement(await $('#element-id'), 'firstButtonElement', {
/* some options */
})
// Save a full page screenshot
await browser.saveFullPageScreen('fullPage', {
/* some options */
})
// Save a full page screenshot with all tab executions
await browser.saveTabbablePage('save-tabbable', {
/* some options, use the same options as for saveFullPageScreen */
})
})
Then('I should be able to match some screenshots with a baseline', async function () {
// Check screen to exactly match with baseline
await expect(browser).toMatchScreenSnapshot('partialPage')
// check an element to have a mismatch percentage of 5% with the baseline
await expect(browser).toMatchScreenSnapshot('partialPage', 5)
// check an element with options for `saveScreen` command
await expect(browser).toMatchScreenSnapshot('partialPage', {
/* some options */
})
// Check an element to exactly match with baseline
await expect($('#element-id')).toMatchElementSnapshot('firstButtonElement')
// check an element to have a mismatch percentage of 5% with the baseline
await expect($('#element-id')).toMatchElementSnapshot('firstButtonElement', 5)
// check an element with options for `saveElement` command
await expect($('#element-id')).toMatchElementSnapshot('firstButtonElement', {
/* some options */
})
// Check a full page screenshot match with baseline
await expect(browser).toMatchFullPageSnapshot('fullPage')
// Check a full page screenshot to have a mismatch percentage of 5% with the baseline
await expect(browser).toMatchFullPageSnapshot('fullPage', 5)
// Check a full page screenshot with options for `checkFullPageScreen` command
await expect(browser).toMatchFullPageSnapshot('fullPage', {
/* some options */
})
// Check a full page screenshot with all tab executions
await expect(browser).toMatchTabbablePageSnapshot('check-tabbable')
// Check a full page screenshot to have a mismatch percentage of 5% with the baseline
await expect(browser).toMatchTabbablePageSnapshot('check-tabbable', 5)
// Check a full page screenshot with options for `checkTabbablePage` command
await expect(browser).toMatchTabbablePageSnapshot('check-tabbable', {
/* some options */
})
})
Then('I should be able to compare some screenshots with a baseline', async function () {
// Check a screen
await expect(
await browser.checkScreen('examplePage', {
/* some options */
})
).toEqual(0)
// Check an element
await expect(
await browser.checkElement(
await $('#element-id'),
'firstButtonElement',
{
/* some options */
}
)
).toEqual(0)
// Check a full page screenshot
await expect(
await browser.checkFullPageScreen('fullPage', {
/* some options */
})
).toEqual(0)
// Check a full page screenshot with all tab executions
await expect(
await browser.checkTabbablePage('check-tabbable', {
/* some options, use the same options as for checkFullPageScreen */
})
).toEqual(0)
})
重要
此服務提供 save
和 check
方法。如果您是第一次執行測試,則 不應 合併 save
和 compare
方法,check
方法會自動為您建立基準影像
#####################################################################################
INFO:
Autosaved the image to
/Users/wswebcreation/sample/baselineFolder/desktop_chrome/examplePage-chrome-latest-1366x768.png
#####################################################################################
當您停用自動儲存基準影像時,Promise 將會以以下警告遭到拒絕。
#####################################################################################
Baseline image not found, save the actual image manually to the baseline.
The image can be found here:
/Users/wswebcreation/sample/.tmp/actual/desktop_chrome/examplePage-chrome-latest-1366x768.png
#####################################################################################
這表示目前的螢幕截圖會儲存在實際資料夾中,並且您需要手動將其複製到您的基準。如果您使用 autoSaveBaseline: true
實例化 @wdio/visual-service
,則影像會自動儲存到基準資料夾中。