-
Notifications
You must be signed in to change notification settings - Fork 238
Add getBy
docs pages
#2023
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
ankur22
wants to merge
12
commits into
main
Choose a base branch
from
add/getBy
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Add getBy
docs pages
#2023
Changes from 9 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
fd1bf0b
Fix $ and $$ in page index
ankur22 8a5ca50
Add page.getByAltText doc
ankur22 e77d217
Add page.getByLabel doc page
ankur22 bc37784
Add page.getByPlaceholder docs page
ankur22 a9741c2
Add page.getByRole docs page
ankur22 d98026b
Add page.getByTestId docs page
ankur22 b96d88c
Add page.getByText docs page
ankur22 6413818
Add page.getByTitle docs page
ankur22 1a0009d
Add getBy* APIs to page's index page
ankur22 a9804cd
Add skips on code examples to avoid running them
ankur22 7edf609
Add md-k6:skip on some more examples
ankur22 78fe77b
Remove {{< code >}} since we're only working...
ankur22 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
11 changes: 9 additions & 2 deletions
11
docs/sources/k6/next/javascript-api/k6-browser/page/_index.md
Large diffs are not rendered by default.
Oops, something went wrong.
189 changes: 189 additions & 0 deletions
189
docs/sources/k6/next/javascript-api/k6-browser/page/getbyalttext.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,189 @@ | ||
--- | ||
title: 'getByAltText(altText[, options])' | ||
description: 'Browser module: page.getByAltText(altText[, options]) method' | ||
--- | ||
|
||
# getByAltText(altText[, options]) | ||
|
||
Returns a locator for elements with the specified alt text. This method is useful for locating images and other elements that have an `alt` attribute, making your tests more accessible and user-focused. | ||
|
||
<TableWithNestedRows> | ||
|
||
| Parameter | Type | Default | Description | | ||
| ------------- | -------------- | ------- | ---------------------------------------------------------------------------------------------------------- | | ||
| altText | string\|RegExp | - | Required. The alt text to search for. Can be a string for exact match or a RegExp for pattern matching. | | ||
| options | object | `null` | | | ||
| options.exact | boolean | `false` | Whether to match the alt text exactly with case sensitivity. When `true`, performs a case-sensitive match. | | ||
|
||
</TableWithNestedRows> | ||
|
||
### Returns | ||
|
||
| Type | Description | | ||
| -------------------------------------------------------------------------------------- | -------------------------------------------------------------------------------------------------- | | ||
| [Locator](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/k6-browser/locator/) | A locator object that can be used to interact with the element(s) matching the specified alt text. | | ||
|
||
### Examples | ||
|
||
#### Basic alt text matching | ||
|
||
Find and click an image by its alt text: | ||
|
||
{{< code >}} | ||
|
||
```javascript | ||
import { browser } from 'k6/browser'; | ||
|
||
export const options = { | ||
scenarios: { | ||
ui: { | ||
executor: 'shared-iterations', | ||
options: { | ||
browser: { | ||
type: 'chromium', | ||
}, | ||
}, | ||
}, | ||
}, | ||
}; | ||
|
||
export default async function () { | ||
const page = await browser.newPage(); | ||
|
||
try { | ||
await page.goto('https://quickpizza.grafana.com'); | ||
|
||
const logo = page.getByAltText('LOGO'); | ||
await logo.waitFor(); | ||
|
||
await logo.click(); | ||
await page.waitForLoadState(); | ||
} finally { | ||
await page.close(); | ||
} | ||
} | ||
``` | ||
|
||
{{< /code >}} | ||
|
||
#### Exact alt text matching | ||
|
||
Use exact matching for precise alt text: | ||
|
||
{{< code >}} | ||
|
||
```javascript | ||
import { browser } from 'k6/browser'; | ||
|
||
export const options = { | ||
scenarios: { | ||
ui: { | ||
executor: 'shared-iterations', | ||
options: { | ||
browser: { | ||
type: 'chromium', | ||
}, | ||
}, | ||
}, | ||
}, | ||
}; | ||
|
||
export default async function () { | ||
const page = await browser.newPage(); | ||
|
||
try { | ||
await page.goto('https://quickpizza.grafana.com'); | ||
|
||
const logo = page.getByAltText('logo', { exact: true }); | ||
await logo.waitFor(); | ||
|
||
await logo.click(); | ||
await page.waitForLoadState(); | ||
} finally { | ||
await page.close(); | ||
} | ||
} | ||
``` | ||
|
||
{{< /code >}} | ||
|
||
#### Using regular expressions | ||
|
||
Find images using pattern matching: | ||
|
||
{{< code >}} | ||
|
||
```javascript | ||
import { browser } from 'k6/browser'; | ||
|
||
export const options = { | ||
scenarios: { | ||
ui: { | ||
executor: 'shared-iterations', | ||
options: { | ||
browser: { | ||
type: 'chromium', | ||
}, | ||
}, | ||
}, | ||
}, | ||
}; | ||
|
||
export default async function () { | ||
const page = await browser.newPage(); | ||
|
||
try { | ||
await page.goto('https://quickpizza.grafana.com'); | ||
|
||
const logo = page.getByAltText(/logo/s); | ||
await logo.waitFor(); | ||
|
||
await logo.click(); | ||
await page.waitForLoadState(); | ||
} finally { | ||
await page.close(); | ||
} | ||
} | ||
``` | ||
|
||
{{< /code >}} | ||
|
||
### Common use cases | ||
|
||
**Image testing:** | ||
|
||
- Testing image alt text for accessibility compliance | ||
- Interacting with clickable images/banners | ||
|
||
**Accessibility testing:** | ||
|
||
- Ensuring all images have meaningful alt text | ||
- Testing screen reader compatibility | ||
- Validating WCAG compliance | ||
|
||
**UI interaction:** | ||
|
||
- Clicking on logo images to navigate home | ||
- Selecting images in galleries or carousels | ||
- Interacting with image-based buttons | ||
|
||
### Best practices | ||
|
||
1. **Use descriptive alt text**: When creating tests, ensure your application uses meaningful alt text that describes the image content or function. | ||
|
||
2. **Prefer exact matches**: Use `exact: true` when you need precise matching to avoid false positives. | ||
|
||
3. **Accessibility-first**: Using `getByAltText()` encourages better accessibility practices by ensuring images have proper alt attributes. | ||
|
||
4. **Regular expressions for patterns**: Use RegExp for flexible matching when dealing with dynamic or numbered content. | ||
|
||
5. **Combine with assertions**: Always verify that the located elements behave as expected using assertions. | ||
|
||
### Related | ||
|
||
- [page.getByRole()](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/k6-browser/page/getbyrole/) - Locate by ARIA role | ||
- [page.getByLabel()](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/k6-browser/page/getbylabel/) - Locate by form labels | ||
- [page.getByPlaceholder()](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/k6-browser/page/getbyplaceholder/) - Locate by placeholder text | ||
- [page.getByTestId()](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/k6-browser/page/getbytestid/) - Locate by test ID | ||
- [page.getByTitle()](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/k6-browser/page/getbytitle/) - Locate by title attribute | ||
- [page.getByText()](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/k6-browser/page/getbytext/) - Locate by visible text |
189 changes: 189 additions & 0 deletions
189
docs/sources/k6/next/javascript-api/k6-browser/page/getbylabel.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,189 @@ | ||
--- | ||
title: 'getByLabel(text[, options])' | ||
description: 'Browser module: page.getByLabel(text[, options]) method' | ||
--- | ||
|
||
# getByLabel(text[, options]) | ||
|
||
Returns a locator for form controls associated with the specified label text. This method is ideal for interacting with form elements in an accessible and user-focused way, as it mirrors how users typically identify form fields. | ||
|
||
<TableWithNestedRows> | ||
|
||
| Parameter | Type | Default | Description | | ||
| ------------- | -------------- | ------- | ------------------------------------------------------------------------------------------------------------ | | ||
| text | string\|RegExp | - | Required. The label text to search for. Can be a string for exact match or a RegExp for pattern matching. | | ||
| options | object | `null` | | | ||
| options.exact | boolean | `false` | Whether to match the label text exactly with case sensitivity. When `true`, performs a case-sensitive match. | | ||
|
||
</TableWithNestedRows> | ||
|
||
### Returns | ||
|
||
| Type | Description | | ||
| -------------------------------------------------------------------------------------- | -------------------------------------------------------------------------------------------------------- | | ||
| [Locator](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/k6-browser/locator/) | A locator object that can be used to interact with the form control associated with the specified label. | | ||
|
||
### Examples | ||
|
||
#### Basic form interaction | ||
|
||
Fill form fields using their labels: | ||
|
||
{{< code >}} | ||
|
||
```javascript | ||
import { browser } from 'k6/browser'; | ||
|
||
export const options = { | ||
scenarios: { | ||
ui: { | ||
executor: 'shared-iterations', | ||
options: { | ||
browser: { | ||
type: 'chromium', | ||
}, | ||
}, | ||
}, | ||
}, | ||
}; | ||
|
||
export default async function () { | ||
const page = await browser.newPage(); | ||
|
||
try { | ||
await page.setContent(` | ||
<label for="username">Username (hint: default)</label> | ||
<input type="text" id="username" name="username"> | ||
<label for="password">Password (hint: 12345678)</label> | ||
<input type="password" id="password" name="password"> | ||
`); | ||
|
||
const username = page.getByLabel('Username (hint: default)', { exact: true }); | ||
const password = page.getByLabel(/^Password.*$/); | ||
|
||
await username.fill('default'); | ||
await password.fill('12345678'); | ||
} finally { | ||
await page.close(); | ||
} | ||
} | ||
``` | ||
|
||
{{< /code >}} | ||
|
||
#### Working with different input types | ||
|
||
Handle various form control types in various label association patterns: | ||
|
||
{{< code >}} | ||
|
||
```javascript | ||
import { browser } from 'k6/browser'; | ||
|
||
export const options = { | ||
scenarios: { | ||
browser: { | ||
executor: 'shared-iterations', | ||
options: { | ||
browser: { | ||
type: 'chromium', | ||
}, | ||
}, | ||
}, | ||
}, | ||
}; | ||
|
||
export default async function () { | ||
const page = await browser.newPage(); | ||
|
||
try { | ||
await page.setContent(` | ||
<label for="username">Username (hint: default)</label> | ||
<input type="text" id="username" name="username"> | ||
<span id="password-label">Password (hint: 12345678)</span> | ||
<input type="password" aria-labelledby="password-label"> | ||
<label for="subscribe">Subscribe to newsletter</label> | ||
<input type="checkbox" id="subscribe" name="subscribe"> | ||
<label for="email">Email</label> | ||
<input type="radio" id="email" value="email"> | ||
<label for="sms">Text message</label> | ||
<input type="radio" id="sms" value="sms"> | ||
<label for="theme">Theme</label> | ||
<select id="theme" name="theme"> | ||
<option value="light">Light</option> | ||
<option value="dark">Dark</option> | ||
<option value="system">System</option> | ||
</select> | ||
<textarea aria-label="Comments"></textarea> | ||
`); | ||
|
||
// Inputs | ||
page.getByLabel('Username (hint: default)', { exact: true }).fill('default'); | ||
page.getByLabel(/^Password.*$/).fill('12345678'); | ||
|
||
// Checkbox | ||
await page.getByLabel('Subscribe to newsletter').check(); | ||
|
||
// Radio button | ||
await page.getByLabel('Email', { exact: true }).check(); | ||
|
||
// Select dropdown | ||
await page.getByLabel('Theme').selectOption('light'); | ||
|
||
// Textarea | ||
await page.getByLabel('Comments').fill('This is a test comment'); | ||
} finally { | ||
await page.close(); | ||
} | ||
} | ||
``` | ||
|
||
{{< /code >}} | ||
|
||
### Label association patterns | ||
|
||
The `getByLabel()` method works with several HTML patterns for associating labels with form controls: | ||
|
||
**1. Explicit association with `for` attribute:** | ||
|
||
```html | ||
<label for="username">Username</label> <input type="text" id="username" name="username" /> | ||
``` | ||
|
||
**2. ARIA labeling:** | ||
|
||
```html | ||
<span id="username-label">Username</span> <input type="text" aria-labelledby="username-label" /> | ||
``` | ||
|
||
**3. ARIA label attribute:** | ||
|
||
```html | ||
<input type="text" aria-label="Username" /> | ||
``` | ||
|
||
### Common use cases | ||
|
||
- **Form testing**: Login forms, registration forms, contact forms | ||
- **E-commerce**: Checkout forms, shipping information, payment details | ||
- **Settings pages**: User preferences, account settings, configuration forms | ||
- **Accessibility testing**: Ensuring proper label association and screen reader compatibility | ||
|
||
### Best practices | ||
|
||
1. **Accessibility-first approach**: Using `getByLabel()` ensures your tests work the same way users with assistive technology interact with forms. | ||
|
||
2. **Meaningful labels**: Encourage developers to use descriptive, unique label text that clearly identifies the form control's purpose. | ||
|
||
3. **Required field indicators**: When testing required fields, include any visual indicators (like asterisks) in your label text matching. | ||
|
||
4. **Form validation testing**: Use labels to test form validation scenarios, as they provide a stable way to identify fields regardless of styling changes. | ||
|
||
### Related | ||
|
||
- [page.getByRole()](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/k6-browser/page/getbyrole/) - Locate by ARIA role | ||
- [page.getByAltText()](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/k6-browser/page/getbyalttext/) - Locate by alt text | ||
- [page.getByPlaceholder()](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/k6-browser/page/getbyplaceholder/) - Locate by placeholder text | ||
- [page.getByTestId()](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/k6-browser/page/getbytestid/) - Locate by test ID | ||
- [page.getByTitle()](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/k6-browser/page/getbytitle/) - Locate by title attribute | ||
- [page.getByText()](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/k6-browser/page/getbytext/) - Locate by text content |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this following the same pattern as other pages? It looks like we could move the headings up for all of these (from h3 to h2, etc.)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we have this issue in most of the browser docs (maybe elsewhere too) where the heading is one level too low. Would you rather I fix all of them in a new PR?