11---
2- title : Interactivity API | Browser Mode
2+ title : 交互性 API | 浏览器模式
33---
44
5- # Interactivity API
5+ # 交互性 API
66
77Vitest 使用 [ Chrome DevTools Protocol] ( https://chromedevtools.github.io/devtools-protocol/ ) 或 [ webdriver] ( https://www.w3.org/TR/webdriver/ ) 实现了 [ ` @testing-library/user-event ` ] ( https://testing-library.com/docs/user-event/intro ) 库的子集 API,而不是伪造事件,这使得浏览器行为更加可靠和一致,符合用户与页面交互的方式。
88
@@ -30,11 +30,11 @@ import { userEvent as vitestUserEvent } from 'vitest/browser'
3030import { userEvent as originalUserEvent } from '@testing-library/user-event'
3131import { userEvent as vitestUserEvent } from '@vitest/browser/context'
3232
33- await vitestUserEvent.keyboard('{Shift}') // press shift without releasing
34- await vitestUserEvent.keyboard('{/Shift}') // releases shift
33+ await vitestUserEvent.keyboard('{Shift}') // 按住 shift 键不放
34+ await vitestUserEvent.keyboard('{/Shift}') // 放开 shift 键不放
3535
36- await originalUserEvent.keyboard('{Shift}') // press shift without releasing
37- await originalUserEvent.keyboard('{/Shift}') // DID NOT release shift because the state is different
36+ await originalUserEvent.keyboard('{Shift}') // 按住 shift 键不放
37+ await originalUserEvent.keyboard('{/Shift}') // 没有放开 shift 键,因为状态不同
3838` ` `
3939
4040这种行为更有用,因为我们并没有模拟键盘,而是实际按下了 Shift 键,所以保留原来的行为会在字段中键入时造成意想不到的问题。
@@ -58,7 +58,7 @@ test('clicks on an element', async () => {
5858 const logo = page.getByRole('img', { name: /logo/ })
5959
6060 await userEvent.click(logo)
61- // or you can access it directly on the locator
61+ // 或直接从定位器上访问
6262 await logo.click()
6363})
6464` ` `
@@ -89,7 +89,7 @@ test('triggers a double click on an element', async () => {
8989 const logo = page.getByRole('img', { name: /logo/ })
9090
9191 await userEvent.dblClick(logo)
92- // or you can access it directly on the locator
92+ // 或直接从定位器上访问
9393 await logo.dblClick()
9494})
9595` ` `
@@ -126,7 +126,7 @@ test('triggers a triple click on an element', async () => {
126126 })
127127
128128 await userEvent.tripleClick(logo)
129- // or you can access it directly on the locator
129+ // 或直接从定位器上访问
130130 await logo.tripleClick()
131131
132132 expect(tripleClickFired).toBe(true)
@@ -160,7 +160,7 @@ test('update input', async () => {
160160 await userEvent.fill(input, '{{a[[') // input.value == {{a[[
161161 await userEvent.fill(input, '{Shift}') // input.value == {Shift}
162162
163- // or you can access it directly on the locator
163+ // 或直接从定位器上访问
164164 await input.fill('foo') // input.value == foo
165165})
166166` ` `
@@ -193,11 +193,11 @@ function keyboard(text: string): Promise<void>
193193import { userEvent } from 'vitest/browser'
194194
195195test('trigger keystrokes', async () => {
196- await userEvent.keyboard('foo') // translates to: f, o, o
197- await userEvent.keyboard('{{a[[') // translates to: {, a, [
198- await userEvent.keyboard('{Shift}{f}{o}{o}') // translates to: Shift, f, o, o
199- await userEvent.keyboard('{a>5}') // press a without releasing it and trigger 5 keydown
200- await userEvent.keyboard('{a>5/}') // press a for 5 keydown and then release it
196+ await userEvent.keyboard('foo') // 转化成: f, o, o
197+ await userEvent.keyboard('{{a[[') // 转化成: {, a, [
198+ await userEvent.keyboard('{Shift}{f}{o}{o}') // 转化成: Shift, f, o, o
199+ await userEvent.keyboard('{a>5}') // 按 a 不松开,触发 5 次按键
200+ await userEvent.keyboard('{a>5/}') // 按 a 键 5 次,然后松开
201201})
202202` ` `
203203
@@ -299,7 +299,7 @@ test('clears input', async () => {
299299 expect(input).toHaveValue('foo')
300300
301301 await userEvent.clear(input)
302- // or you can access it directly on the locator
302+ // 或直接从定位器上访问
303303 await input.clear()
304304
305305 expect(input).toHaveValue('')
@@ -343,7 +343,7 @@ test('clears input', async () => {
343343 const select = page.getByRole('select')
344344
345345 await userEvent.selectOptions(select, 'Option 1')
346- // or you can access it directly on the locator
346+ // 或直接从定位器上访问
347347 await select.selectOptions('Option 1')
348348
349349 expect(select).toHaveValue('option-1')
@@ -393,7 +393,7 @@ test('hovers logo element', async () => {
393393 const logo = page.getByRole('img', { name: /logo/ })
394394
395395 await userEvent.hover(logo)
396- // or you can access it directly on the locator
396+ // 或直接从定位器上访问
397397 await logo.hover()
398398})
399399` ` `
@@ -426,7 +426,7 @@ test('unhover logo element', async () => {
426426 const logo = page.getByRole('img', { name: /logo/ })
427427
428428 await userEvent.unhover(logo)
429- // or you can access it directly on the locator
429+ // 或直接从定位器上访问
430430 await logo.unhover()
431431})
432432` ` `
@@ -458,10 +458,10 @@ test('can upload a file', async () => {
458458 const file = new File(['file'], 'file.png', { type: 'image/png' })
459459
460460 await userEvent.upload(input, file)
461- // or you can access it directly on the locator
461+ // 或直接从定位器上访问
462462 await input.upload(file)
463463
464- // you can also use file paths relative to the root of the project
464+ // 也可以使用相对于项目根目录的文件路径
465465 await userEvent.upload(input, './fixtures/file.png')
466466})
467467` ` `
@@ -496,7 +496,7 @@ test('drag and drop works', async () => {
496496 const target = page.getByTestId('logo-target')
497497
498498 await userEvent.dragAndDrop(source, target)
499- // or you can access it directly on the locator
499+ // 或直接从定位器上访问
500500 await source.dropTo(target)
501501
502502 await expect.element(target).toHaveTextContent('Logo is processed')
@@ -524,15 +524,15 @@ function copy(): Promise<void>
524524import { page, userEvent } from 'vitest/browser'
525525
526526test('copy and paste', async () => {
527- // write to 'source'
527+ // 写入 'source'
528528 await userEvent.click(page.getByPlaceholder('source'))
529529 await userEvent.keyboard('hello')
530530
531- // select and copy 'source'
531+ // 选择并复制 'source'
532532 await userEvent.dblClick(page.getByPlaceholder('source'))
533533 await userEvent.copy()
534534
535- // paste to 'target'
535+ // 粘贴到 'target'
536536 await userEvent.click(page.getByPlaceholder('target'))
537537 await userEvent.paste()
538538
@@ -557,15 +557,15 @@ function cut(): Promise<void>
557557import { page, userEvent } from 'vitest/browser'
558558
559559test('copy and paste', async () => {
560- // write to 'source'
560+ // 写入 'source'
561561 await userEvent.click(page.getByPlaceholder('source'))
562562 await userEvent.keyboard('hello')
563563
564- // select and cut 'source'
564+ // 选择并剪切 'source'
565565 await userEvent.dblClick(page.getByPlaceholder('source'))
566566 await userEvent.cut()
567567
568- // paste to 'target'
568+ // 粘贴到 'target'
569569 await userEvent.click(page.getByPlaceholder('target'))
570570 await userEvent.paste()
571571
0 commit comments