Skip to content

Commit

Permalink
fix(commands): fix addCommand and overwriteCommand examples
Browse files Browse the repository at this point in the history
  • Loading branch information
sipayRT committed Nov 7, 2024
1 parent d012d1d commit dfdb862
Show file tree
Hide file tree
Showing 4 changed files with 111 additions and 50 deletions.
43 changes: 29 additions & 14 deletions docs/commands/browser/addCommand.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -14,28 +14,43 @@ Use the `addCommand` command to add your own command to the browser or to an ele
## Usage {#usage}

```javascript
await browser.addCommand(name, callback, elementScope);
browser.addCommand(name, callback, elementScope);
```

## Command Parameters {#parameters}

<table>
<thead>
<tr><td>**Name**</td><td>**Type**</td><td>**Description**</td></tr>
</thead>
<tbody>
<tr><td>name</td><td>String</td><td>Custom command name.</td></tr>
<tr><td>callback</td><td>Function</td><td>Command implementation function.</td></tr>
<tr><td>elementScope</td><td>Boolean</td><td>If the value is _true_, add the command to the element instead of the browser. Default: _false_.</td></tr>

</tbody>
<thead>
<tr>
<td>**Name**</td>
<td>**Type**</td>
<td>**Description**</td>
</tr>
</thead>
<tbody>
<tr>
<td>name</td>
<td>String</td>
<td>Custom command name.</td>
</tr>
<tr>
<td>callback</td>
<td>Function</td>
<td>Command implementation function.</td>
</tr>
<tr>
<td>elementScope</td>
<td>Boolean</td>
<td>If the value is _true_, add the command to the element instead of the browser. Default: _false_.</td>
</tr>
</tbody>
</table>

## Usage Examples {#examples}

```javascript
// add the getUrlAndTitle command
await browser.addCommand("getUrlAndTitle", async function (customParam) {
browser.addCommand("getUrlAndTitle", async function (customParam) {
return {
url: await this.getUrl(), // `this` here and below refers to the "browser" object
title: await this.getTitle(),
Expand All @@ -45,14 +60,14 @@ await browser.addCommand("getUrlAndTitle", async function (customParam) {

// use the new getUrlAndTitle command
it("should use my add command", async ({ browser }) => {
await browser.url("https://webdriver.io");
await browser.url("https://testplane.io");

const result = await browser.getUrlAndTitle("foobar");

assert.strictEqual(result.url, "https://webdriver.io");
assert.strictEqual(result.url, "https://testplane.io");
assert.strictEqual(
result.title,
"WebdriverIO · Next-gen browser and mobile automation test framework for Node.js",
"Testplane Docs | Testplane Docs",
);
assert.strictEqual(result.customParam, "foobar");
});
Expand Down
38 changes: 27 additions & 11 deletions docs/commands/browser/overwriteCommand.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -19,25 +19,41 @@ await browser.overwriteCommand(name, callback, elementScope);
## Command Parameters {#parameters}

<table>
<thead>
<tr><td>**Name**</td><td>**Type**</td><td>**Description**</td></tr>
</thead>
<tbody>
<tr><td>name</td><td>String</td><td>The name of the custom command.</td></tr>
<tr><td>callback</td><td>Function</td><td>The function implementation of the command.</td></tr>
<tr><td>elementScope</td><td>Boolean</td><td>If the value is _true_, add the command to the element instead of the browser. Default: _false_.</td></tr>

</tbody>
<thead>
<tr>
<td>**Name**</td>
<td>**Type**</td>
<td>**Description**</td>
</tr>
</thead>
<tbody>
<tr>
<td>name</td>
<td>String</td>
<td>The name of the custom command.</td>
</tr>
<tr>
<td>callback</td>
<td>Function</td>
<td>The function implementation of the command.</td>
</tr>
<tr>
<td>elementScope</td>
<td>Boolean</td>
<td>If the value is _true_, add the command to the element instead of the browser. Default: _false_.</td>
</tr>

</tbody>
</table>

## Usage Examples {#examples}

```javascript
// log the pause duration in ms before the pause and then return the value
await browser.overwriteCommand("pause", function (origPauseFunction, ms) {
await browser.overwriteCommand("pause", async function (origPauseFunction, ms) {
console.log(`Sleeping for ${ms}`);

origPauseFunction(ms);
await origPauseFunction(ms);

return ms;
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,28 +14,43 @@ import Admonition from "@theme/Admonition";
## Использование {#usage}

```javascript
await browser.addCommand(name, callback, elementScope);
browser.addCommand(name, callback, elementScope);
```

## Параметры команды {#parameters}

<table>
<thead>
<tr><td>**Имя**</td><td>**Тип**</td><td>**Описание**</td></tr>
</thead>
<tbody>
<tr><td>name</td><td>String</td><td>Имя кастомной команды.</td></tr>
<tr><td>callback</td><td>Function</td><td>Функция-реализация команды.</td></tr>
<tr><td>elementScope</td><td>Boolean</td><td>Если значение _true_, то добавить команду к элементу, а не к браузеру. По умолчанию: _false_.</td></tr>

</tbody>
<thead>
<tr>
<td>**Имя**</td>
<td>**Тип**</td>
<td>**Описание**</td>
</tr>
</thead>
<tbody>
<tr>
<td>name</td>
<td>String</td>
<td>Имя кастомной команды.</td>
</tr>
<tr>
<td>callback</td>
<td>Function</td>
<td>Функция-реализация команды.</td>
</tr>
<tr>
<td>elementScope</td>
<td>Boolean</td>
<td>Если значение _true_, то добавить команду к элементу, а не к браузеру. По умолчанию: _false_.</td>
</tr>
</tbody>
</table>

## Примеры использования {#examples}

```javascript
// добавляем команду getUrlAndTitle
await browser.addCommand("getUrlAndTitle", async function (customParam) {
browser.addCommand("getUrlAndTitle", async function (customParam) {
return {
url: await this.getUrl(), // `this` здесь и ниже относится к объекту "browser"
title: await this.getTitle(),
Expand All @@ -45,14 +60,14 @@ await browser.addCommand("getUrlAndTitle", async function (customParam) {

// используем новую команду getUrlAndTitle
it("should use my add command", async ({ browser }) => {
await browser.url("https://webdriver.io");
await browser.url("https://testplane.io");

const result = await browser.getUrlAndTitle("foobar");

assert.strictEqual(result.url, "https://webdriver.io");
assert.strictEqual(result.url, "https://testplane.io");
assert.strictEqual(
result.title,
"WebdriverIO · Next-gen browser and mobile automation test framework for Node.js",
"Testplane Docs | Testplane Docs",
);
assert.strictEqual(result.customParam, "foobar");
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,25 +19,40 @@ await browser.overwriteCommand(name, callback, elementScope);
## Параметры команды {#parameters}

<table>
<thead>
<tr><td>**Имя**</td><td>**Тип**</td><td>**Описание**</td></tr>
</thead>
<tbody>
<tr><td>name</td><td>String</td><td>Имя кастомной команды.</td></tr>
<tr><td>callback</td><td>Function</td><td>Функция-реализация команды.</td></tr>
<tr><td>elementScope</td><td>Boolean</td><td>Если значение _true_, то добавить команду к элементу, а не к браузеру. По умолчанию: _false_.</td></tr>

</tbody>
<thead>
<tr>
<td>**Имя**</td>
<td>**Тип**</td>
<td>**Описание**</td>
</tr>
</thead>
<tbody>
<tr>
<td>name</td>
<td>String</td>
<td>Имя кастомной команды.</td>
</tr>
<tr>
<td>callback</td>
<td>Function</td>
<td>Функция-реализация команды.</td>
</tr>
<tr>
<td>elementScope</td>
<td>Boolean</td>
<td>Если значение _true_, то добавить команду к элементу, а не к браузеру. По умолчанию: _false_.</td>
</tr>
</tbody>
</table>

## Примеры использования {#examples}

```javascript
// вывести время паузы в мс перед самой паузой и вернуть потом это значение
await browser.overwriteCommand("pause", function (origPauseFunction, ms) {
await browser.overwriteCommand("pause", async function (origPauseFunction, ms) {
console.log(`Sleeping for ${ms}`);

origPauseFunction(ms);
await origPauseFunction(ms);

return ms;
});
Expand Down

0 comments on commit dfdb862

Please sign in to comment.