@@ -1051,19 +1051,70 @@ Internally, this method calls `.elements` and wraps every element using [`page.e
10511051
10521052- [See ` locator.elements() ` ](#elements )
10531053
1054+ ### serialize
1055+
1056+ ` ` ` ts
1057+ function serialize(): SerializedLocator
1058+ ` ` `
1059+
1060+ Returns a JSON - serializable representation of the locator . The returned object has two fields :
1061+
1062+ - [` selector ` ](#selector ): the provider - specific selector string used to query the element at runtime .
1063+ - ` locator ` : a human - readable description of the locator (e .g . ` getByRole('button') ` ), used for error messages and tracing . Equivalent to calling [` asLocator() ` ](#aslocator ).
1064+
1065+ This is primarily intended for forwarding a locator to a [browser command ](/ api / browser / commands ), which runs in Node and cannot receive a live ` Locator ` instance :
1066+
1067+ ` ` ` ts
1068+ import { commands, page } from 'vitest/browser'
1069+
1070+ await commands.myCommand(page.getByRole('button').serialize())
1071+ ` ` `
1072+
1073+ ::: tip
1074+ Vitest automatically serializes any ` Locator ` argument passed to a command , so calling ` serialize() ` explicitly is rarely necessary . You can also use ` JSON.stringify(locator) ` (it calls [` toJSON ` ](#tojson ) internally ), which produces the same result .
1075+ :::
1076+
1077+ ### toJSON
1078+
1079+ ` ` ` ts
1080+ function toJSON(): SerializedLocator
1081+ ` ` `
1082+
1083+ Alias of [` serialize ` ](#serialize ). Defined so that ` JSON.stringify(locator) ` and structured - clone - based transports return a ` SerializedLocator ` object .
1084+
1085+ ### asLocator
1086+
1087+ ` ` ` ts
1088+ function asLocator(): string
1089+ ` ` `
1090+
1091+ Returns a human - readable description of the locator using the JavaScript locator syntax (e .g . ` getByRole('button', { name: 'Submit' }) ` ). This is the same string exposed as the ` locator ` field of [` serialize() ` ](#serialize ) and is used in error messages and traces .
1092+
1093+ ` ` ` ts
1094+ import { page } from 'vitest/browser'
1095+
1096+ const button = page.getByRole('button', { name: 'Submit' })
1097+ button.asLocator() // "getByRole('button', { name: 'Submit' })"
1098+ ` ` `
1099+
1100+ ::: tip
1101+ Use [` selector ` ](#selector ) when you need the provider - specific string to forward to a [browser command ](/ api / browser / commands ). Use ` asLocator() ` only for diagnostic output . The returned string is not meant to be re - used to query elements .
1102+ :::
1103+
10541104## Properties
10551105
10561106### selector
10571107
1058- The ` selector ` is a string that will be used to locate the element by the browser provider . Playwright will use a ` playwright ` locator syntax while ` preview ` and ` webdriverio ` will use CSS .
1108+ The ` selector ` is a string that will be used to locate the element by the browser provider . Playwright will use a ` playwright ` locator syntax , and ` preview ` and ` webdriverio ` will use CSS .
10591109
10601110::: danger
10611111You should not use this string in your test code . The ` selector ` string should only be used when working with the Commands API :
10621112
10631113` ` ` ts [commands.ts]
10641114import type { BrowserCommand } from 'vitest/node'
1115+ import type { SerializedLocator } from '@vitest/browser'
10651116
1066- const test: BrowserCommand<string > = function test(context, selector) {
1117+ const test: BrowserCommand<SerializedLocator > = function test(context, { selector } ) {
10671118 // playwright
10681119 await context.iframe.locator(selector).click()
10691120 // webdriverio
@@ -1076,8 +1127,8 @@ import { test } from 'vitest'
10761127import { commands, page } from 'vitest/browser'
10771128
10781129test('works correctly', async () => {
1079- await commands.test(page.getByText('Hello').selector ) // ✅
1080- // vitest will automatically unwrap it to a string
1130+ await commands.test(page.getByText('Hello').serialize() ) // ✅
1131+ // vitest will automatically unwrap it to a SerializedLocator
10811132 await commands.test(page.getByText('Hello')) // ✅
10821133})
10831134` ` `
0 commit comments