மாதிரியாக்கம்
சோதனைகளை எழுதும்போது, ஒரு உள் — அல்லது வெளி — சேவையின் "போலி" பதிப்பை உருவாக்க வேண்டிய நேரம் வரும். இது பொதுவாக மாதிரியாக்கம் (mocking) என்று குறிப்பிடப்படுகிறது. WebdriverIO உங்களுக்கு உதவ பயன்பாட்டு செயல்பாடுகளை வழங்குகிறது. நீங்கள் import { fn, spyOn, mock, unmock } from '@wdio/browser-runner'
மூலம் அதை அணுகலாம். கிடைக்கக்கூடிய மாதிரியாக்க பயன்பாடுகள் பற்றிய கூடுதல் தகவலுக்கு API ஆவணங்களைப் பார்க்கவும்.
செயல்பாடுகள்
உங்கள் கூறு சோதனைகளின் ஒரு பகுதியாக சில செயல்பாட்டு கையாளுநர்கள் அழைக்கப்படுகின்றனவா என்பதை சரிபார்க்க, @wdio/browser-runner
தொகுதி மாதிரியாக்க முறைகளை ஏற்றுமதி செய்கிறது, இவற்றை நீங்கள் பயன்படுத்தி, இந்த செயல்பாடுகள் அழைக்கப்பட்டுள்ளனவா என்பதை சோதிக்கலாம். பின்வரும் முறையில் இந்த முறைகளை இறக்குமதி செய்யலாம்:
import { fn, spyOn } from '@wdio/browser-runner'
fn
ஐ இறக்குமதி செய்வதன் மூலம் நீங்கள் அதன் ச ெயல்பாட்டைக் கண்காணிக்க ஒரு உளவு செயல்பாட்டை (மாதிரி) உருவாக்கலாம் மற்றும் spyOn
மூலம் ஏற்கனவே உருவாக்கப்பட்ட பொருளின் முறையைக் கண்காணிக்கலாம்.
- Mocks
- Spies
முழு எடுத்துக்காட்டையும் கூறு சோதனை எடுத்துக்காட்டு களஞ்சியத்தில் காணலாம்.
import React from 'react'
import { $, expect } from '@wdio/globals'
import { fn } from '@wdio/browser-runner'
import { Key } from 'webdriverio'
import { render } from '@testing-library/react'
import LoginForm from '../components/LoginForm'
describe('LoginForm', () => {
it('should call onLogin handler if username and password was provided', async () => {
const onLogin = fn()
render(<LoginForm onLogin={onLogin} />)
await $('input[name="username"]').setValue('testuser123')
await $('input[name="password"]').setValue('s3cret')
await browser.keys(Key.Enter)
/**
* verify the handler was called
*/
expect(onLogin).toBeCalledTimes(1)
expect(onLogin).toBeCalledWith(expect.equal({
username: 'testuser123',
password: 's3cret'
}))
})
})
முழு எடுத்துக்காட்டையும் எடுத்துக்காட்டுகள் அடை வில் காணலாம்.
import { expect, $ } from '@wdio/globals'
import { spyOn } from '@wdio/browser-runner'
import { html, render } from 'lit'
import { SimpleGreeting } from './components/LitComponent.ts'
const getQuestionFn = spyOn(SimpleGreeting.prototype, 'getQuestion')
describe('Lit Component testing', () => {
it('should render component', async () => {
render(
html`<simple-greeting name="WebdriverIO" />`,
document.body
)
const innerElem = await $('simple-greeting').$('p')
expect(await innerElem.getText()).toBe('Hello, WebdriverIO! How are you today?')
})
it('should render with mocked component function', async () => {
getQuestionFn.mockReturnValue('Does this work?')
render(
html`<simple-greeting name="WebdriverIO" />`,
document.body
)
const innerElem = await $('simple-greeting').$('p')
expect(await innerElem.getText()).toBe('Hello, WebdriverIO! Does this work?')
})
})
WebdriverIO இங்கே @vitest/spy
ஐ மீண்டும் ஏற்றுமதி செய்கிறது, இது WebdriverIOs expect
பொருத்திகளுடன் பயன்படுத்தக்கூடிய ஒரு இலகுரக Jest இணக்கமான உளவு செயல்படுத்தலாகும். இந்த மாதிரி செயல்பாடுகளில் மேலும் ஆவணங்களை Vitest திட்ட பக்கத்தில் காணலாம்.
நிச்சயமாக, நீங்கள் வேறு எந்த உளவு கட்டமைப்பையும் நிறுவலாம் மற்றும் இறக்குமதி செய்யலாம், எ.கா. SinonJS, அது உலாவி சூழலை ஆதரிக்கும் வரை.