|
| 1 | +--- |
| 2 | +title: 'boundingBox([options])' |
| 3 | +description: 'Browser module: locator.boundingBox method' |
| 4 | +--- |
| 5 | + |
| 6 | +# boundingBox([options]) |
| 7 | + |
| 8 | +Returns the bounding box of the element. The bounding box is calculated with respect to the position of the [Frame](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/k6-browser/frame) of the current element, which is usually the [Page](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/k6-browser/page)'s main frame. |
| 9 | + |
| 10 | +<TableWithNestedRows> |
| 11 | + |
| 12 | +| Parameter | Type | Default | Description | |
| 13 | +| --------------- | ------ | ------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | |
| 14 | +| options | object | `null` | | |
| 15 | +| options.timeout | number | `30000` | Maximum time in milliseconds. Pass `0` to disable the timeout. Default is overridden by the `setDefaultTimeout` option on [BrowserContext](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/k6-browser/browsercontext/) or [Page](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/k6-browser/page/). | |
| 16 | + |
| 17 | +</TableWithNestedRows> |
| 18 | + |
| 19 | +### Returns |
| 20 | + |
| 21 | +| Type | Description | |
| 22 | +| ----------------------- | ---------------------------------------------------------------------------------------------------------------------------- | |
| 23 | +| `Promise<null \| Rect>` | A Promise that fulfills with the bounding box of the element as a [Rect](#rect). If the element is not visible, the Promise resolves to `null`. | |
| 24 | + |
| 25 | + |
| 26 | +### Rect |
| 27 | + |
| 28 | +The `Rect` object represents the bounding box of an element. |
| 29 | + |
| 30 | +| Property | Type | Description | |
| 31 | +| -------- | -------- | ------------------------------------------ | |
| 32 | +| x | `number` | The x-coordinate of the element in pixels. | |
| 33 | +| y | `number` | The y-coordinate of the element in pixels. | |
| 34 | +| width | `number` | The width of the element in pixels. | |
| 35 | +| height | `number` | The height of the element in pixels. | |
| 36 | + |
| 37 | +### Example |
| 38 | + |
| 39 | +{{< code >}} |
| 40 | + |
| 41 | +```javascript |
| 42 | +import { browser } from 'k6/browser'; |
| 43 | + |
| 44 | +export const options = { |
| 45 | + scenarios: { |
| 46 | + browser: { |
| 47 | + executor: 'shared-iterations', |
| 48 | + options: { |
| 49 | + browser: { |
| 50 | + type: 'chromium', |
| 51 | + }, |
| 52 | + }, |
| 53 | + }, |
| 54 | + }, |
| 55 | +}; |
| 56 | + |
| 57 | +export default async function () { |
| 58 | + const page = await browser.newPage(); |
| 59 | + await page.goto('https://test.k6.io/browser.php'); |
| 60 | + |
| 61 | + const textbox = page.locator('#text1'); |
| 62 | + const boundingBox = await textbox.boundingBox(); |
| 63 | + console.log(`x: ${boundingBox.x}, y: ${boundingBox.y}, width: ${boundingBox.width}, height: ${boundingBox.height}`); |
| 64 | + |
| 65 | + await page.close(); |
| 66 | +} |
| 67 | +``` |
| 68 | + |
| 69 | +{{< /code >}} |
0 commit comments