|
| 1 | +import Tabs from "@theme/Tabs"; |
| 2 | +import TabItem from "@theme/TabItem"; |
| 3 | +import Admonition from "@theme/Admonition"; |
| 4 | +import AddElementCommandExample from "@site/docs/config/_partials/examples/_prepare-browser-add-element-command.mdx"; |
| 5 | + |
| 6 | +# Custom Commands |
| 7 | + |
| 8 | +Testplane allows you to enhance your test suites by adding custom commands. These commands can streamline repetitive tasks, encapsulate complex actions, and improve code readability. |
| 9 | + |
| 10 | +<Admonition title="What you'll learn"> |
| 11 | + |
| 12 | +- How to add custom commands to the browser object. |
| 13 | +- How to add custom commands to elements. |
| 14 | +- Ways to overwrite existing commands. |
| 15 | +- Make custom commands work in a TypeScript environment. |
| 16 | + |
| 17 | +</Admonition> |
| 18 | + |
| 19 | +## Adding Custom Commands to the Browser Object |
| 20 | + |
| 21 | +<Admonition type="tip"> |
| 22 | + A good place to add all of your custom commands is |
| 23 | + [`prepareBrowser`](../config/prepare-browser.mdx) hook. |
| 24 | +</Admonition> |
| 25 | + |
| 26 | +To add a custom command to the `browser` object, use the `addCommand` method. Here's how you can define a command that retrieves both the URL and title of the current page: |
| 27 | + |
| 28 | +```typescript |
| 29 | +browser.addCommand("getUrlAndTitle", async function () { |
| 30 | + return { |
| 31 | + url: await this.getUrl(), |
| 32 | + title: await this.getTitle(), |
| 33 | + }; |
| 34 | +}); |
| 35 | +``` |
| 36 | + |
| 37 | +You can then use this custom command in your tests: |
| 38 | + |
| 39 | +```typescript |
| 40 | +it("should fetch URL and title", async function () { |
| 41 | + const result = await browser.getUrlAndTitle(); |
| 42 | + console.log(result); // { url: 'https://example.com', title: 'Example Domain' } |
| 43 | +}); |
| 44 | +``` |
| 45 | + |
| 46 | +## Adding Custom Commands to Elements |
| 47 | + |
| 48 | +Custom commands can also be added to element instances. For instance, to create a command that gets element position: |
| 49 | + |
| 50 | +<AddElementCommandExample /> |
| 51 | + |
| 52 | +Note the last, third parameter is set to `true` — it means that this command should be added to element instances. |
| 53 | + |
| 54 | +## Overwriting Existing Commands |
| 55 | + |
| 56 | +There might be scenarios where you need to modify the behavior of existing commands. Testplane allows you to overwrite these commands using the `overwriteCommand` method. For example, to add logging to the `click` command: |
| 57 | + |
| 58 | +```typescript |
| 59 | +browser.overwriteCommand("click", async function (this: WebdriverIO.Element, origClick, options) { |
| 60 | + console.log("Element clicked:", this.selector); |
| 61 | + await origClick(options); |
| 62 | +}); |
| 63 | +``` |
| 64 | + |
| 65 | +In this example, before executing the original `click` function, a message is logged to the console. |
| 66 | + |
| 67 | +## Using Custom Commands with TypeScript |
| 68 | + |
| 69 | +To ensure TypeScript recognizes your custom commands, you need to augment the WebdriverIO types. To do that, follow these simple steps: |
| 70 | + |
| 71 | +1. Create a `custom-commands.d.ts` file in your project. |
| 72 | + |
| 73 | +2. Make sure TypeScript compiler "sees" this file during build: you might need to include it in your `tsconfig.json`. |
| 74 | + |
| 75 | +3. Write your type definitions as follows. Note that there are two file contexts in TypeScript: module and ambient. If you have at least one import/export in your file, the file is in "module" mode, otherwise it's in ambient. Choose declaration style accordingly. |
| 76 | + |
| 77 | + <Tabs> |
| 78 | + <TabItem value="module" label="Module mode" default> |
| 79 | + |
| 80 | + ```typescript |
| 81 | + import 'webdriverio'; // Can be any import, not necessarily webdriverio |
| 82 | + |
| 83 | + declare global { |
| 84 | + declare namespace WebdriverIO { |
| 85 | + interface Browser { |
| 86 | + customCommand: (arg: any) => Promise<void> |
| 87 | + } |
| 88 | + |
| 89 | + interface Element { |
| 90 | + elementCustomCommand: (arg: any) => Promise<void> |
| 91 | + } |
| 92 | + } |
| 93 | + } |
| 94 | + ``` |
| 95 | + |
| 96 | + </TabItem> |
| 97 | + <TabItem value="ambient" label="Ambient mode"> |
| 98 | + |
| 99 | + ```typescript |
| 100 | + declare namespace WebdriverIO { |
| 101 | + interface Browser { |
| 102 | + customCommand: (arg: any) => Promise<void> |
| 103 | + } |
| 104 | + |
| 105 | + interface Element { |
| 106 | + elementCustomCommand: (arg: any) => Promise<void> |
| 107 | + } |
| 108 | + } |
| 109 | + ``` |
| 110 | + |
| 111 | + </TabItem> |
| 112 | + |
| 113 | + </Tabs> |
| 114 | + |
| 115 | +This declaration extends the `Browser` and `Element` interfaces to include your custom commands, allowing TypeScript to recognize them without errors. |
0 commit comments