Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/command-line/index.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,7 @@ testplane --repl --grep 'my test name' --browser chrome
- `--keep-browser-on-fail` - the same as the `--keep-browser` option, but the browser will only remain open if the tests fail.

Example of console output with connection information to the browser:

```
[15:44:38 +0700] Testplane run has finished, but the browser won't be closed, because you passed the --keep-browser argument.
[15:44:38 +0700] You may attach to this browser using the following capabilities:
Expand Down
70 changes: 70 additions & 0 deletions docs/commands/browser/restoreState.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import Admonition from "@theme/Admonition";

# restoreState

## Overview {#overview}

Browser command that restores session state (cookies, local and session storages) from a file or variable.

<Admonition type="warning">
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this warning still needed?

If you are using the `webdriver` automation protocol, you have to provide `webSocketUrl: true`
in `desiredCapabilities` in config browser settings. For the `devtools` protocol, you don't need
additional settings.
</Admonition>

## Usage {#usage}

You can restore state from file (using the `path` param) or from object (using the `data` param).
But if you provide both, the file will have the highest priority.
Also, you can provide `cookies`, `localStorage` and `sessionStorage` params to restore only what you need.
Data for restore state you can get from the [saveState](../saveState) command.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need a warning block over here, that user must be on the page from which the cookies were saved. If the user's on about:blank or on the other site, the error will be thrown.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FIxed

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

here as well spelling and wording needs to be improved (see comment below). IMO starting sentences from "but" and "also" doesn't quite fit the docs writing style

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed


```typescript
await browser.restoreState({
path: "./stateDump.json",
data: stateDump,
cookies: true,
localStorage: true,
sessionStorage: true,
});
```

## Command Parameters {#parameters}

<table>
<thead>
<tr><td>**Name**</td><td>**Type**</td><td>**Description**</td></tr>
</thead>
<tbody>
<tr><td>path</td><td>String</td><td>Path to file with state.</td></tr>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same thing about typescript-style types

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed

<tr><td>data</td><td>SaveStateData</td><td>Object with state.</td></tr>
<tr><td>cookies</td><td>Boolean</td><td>Enable restore cookies (true by default).</td></tr>
<tr><td>localStorage</td><td>Boolean</td><td>Enable restore localStorage (true by default).</td></tr>
<tr><td>sessionStorage</td><td>Boolean</td><td>Enable restore sessionStorage (true by default).</td></tr>
<tr><td>cookieFilter</td><td>(cookie: Cookie) => boolean</td><td>Function for filtering cookies, receiving cookie objects, and returning boolean.</td></tr>

</tbody>

</table>

## Usage Examples {#examples}

Restore state from file.

```typescript
it("test", async ({ browser }) => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can use example from below here as well. Not necessarily replacing yours, you can just add it as a second one.

await browser.url("https://github.com/gemini-testing/testplane");

await browser.restoreState({
path: "./stateDump.json",
cookieFilter: ({ domain }) => domain === ".example.com",
});

// Reload page for see auth result.
await browser.refresh();
});
```

## Related Commands {#related}

- [saveState](../saveState)
67 changes: 67 additions & 0 deletions docs/commands/browser/saveState.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import Admonition from "@theme/Admonition";

# saveState

## Overview {#overview}

Browser command that saves session state (cookies, local and session storages).

<Admonition type="warning">
If you are using the `webdriver` automation protocol, you have to provide `webSocketUrl: true`
in `desiredCapabilities` in config browser settings. For the `devtools` protocol, you don't need
additional settings.
</Admonition>

## Usage {#usage}

Command return state dump from page; it will include cookie, localStorage, and sessionStorage.
But using params, you can disable some data.
Also, if you provide the `path` param, you can get a dump in a file.
After saving the state, you can use it in the [restoreState](../restoreState) command.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Besides spelling mistakes, the way this section is structured makes it hard to follow.

I strongly suggest to use LLM to improve this section. For example, I used this prompt:

Improve spelling and wording of this documentation section. Make it easy to follow and clear. Respond only with markdown of corrected doc section.

And got:

This command returns a snapshot of the page state, including cookies, localStorage, and sessionStorage.
You can use parameters to exclude specific types of data if needed.

If you provide the path parameter, the state dump will be saved to a file.
The saved state can later be restored using the restoreState command.


```typescript
import type { SaveStateData } from "testplane";

const stateDump: SaveStateData = await browser.saveState({
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be really nice to briefly comment what's exactly inside that SaveStateData object. Or, if the SaveStateData interface is simple enough, we can include it right in the example.

path: "./stateDump.json",
cookies: true,
localStorage: true,
sessionStorage: true,
});
```

## Command Parameters {#parameters}

<table>
<thead>
<tr><td>**Name**</td><td>**Type**</td><td>**Description**</td></tr>
</thead>
<tbody>
<tr><td>path</td><td>String</td><td>Path to file where state will be saved.</td></tr>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's use Typescript-style types here and wrap them in backticks: string, boolean, etc.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In another docs tables we have this style, but ok here I changed to typescript style

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah it's still can be found in a lot of places, but that's legacy style. Typescript style is more widely used in other docs and can be used to express more precise types

<tr><td>cookies</td><td>Boolean</td><td>Enable save cookies (true by default).</td></tr>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Enable saving cookies (true by default). And similar below. Or perhaps it would make sense to create a dedicated "Default" column.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed

<tr><td>localStorage</td><td>Boolean</td><td>Enable save localStorage (true by default).</td></tr>
<tr><td>sessionStorage</td><td>Boolean</td><td>Enable save sessionStorage (true by default).</td></tr>
<tr><td>cookieFilter</td><td>(cookie: Cookie) => boolean</td><td>Function for filtering cookies, receiving cookie objects, and returning boolean.</td></tr>

</tbody>

</table>

## Usage Examples {#examples}

Save state in file.

```typescript
it("test", async ({ browser }) => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think a more comprehensive example would bring much more value. Here's what I needed to do to setup simple save/restore workflow:

import {ConfigInput, WdioBrowser} from "testplane";
import {launchBrowser} from "testplane/unstable";

export default {
    gridUrl: "local",
    beforeAll: async ({config}) => {
        const b = await launchBrowser(config.browsers['chrome']!);

        await b.url('https://our-site.com');
        await b.$('input.login').setValue('[email protected]');
        await b.$('input.password').setValue('password123');

        await b.saveState({path: './.testplane/state.json'});
        await b.deleteSession();
    },
    sets: {/* ... */},
    browsers: {
        chrome: {
            headless: false,
            desiredCapabilities: {
                browserName: "chrome"
            }
        }
    },
    plugins: {
        /* ... */
        "@testplane/global-hook": {
            enabled: true,
            beforeEach: async ({browser}: {browser: WdioBrowser}) => {
                await browser.url('https://our-site.com');
                await browser.restoreState({path: './.testplane/state.json'});
            }
        }
    },
} satisfies ConfigInput;

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed

await browser.url("https://github.com/gemini-testing/testplane");

await browser.saveState({
path: "./stateDump.json",
cookieFilter: ({ domain }) => domain === ".example.com",
});
});
```

## Related Commands {#related}

- [restoreState](../restoreState)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's add references to the new hooks as well.

54 changes: 54 additions & 0 deletions docs/config/after-all.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
# afterAll

## Overview {#overview}

This parameter is a hook. The function specified for this parameter will be automatically called after tests are completed.

The context of the function is the Testplane config. Also function receive config in arguments.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IMHO starting sentences with "Also" or "But" doesn't quite fit the docs style. But thats up to you.

What needs to be fixed is a typo — function receives

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed


## Usage Example {#example}

Here is an example of suing this hook for logout.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

typos

I strongly suggest to use either LLM or some editor to auto-detect and fix them. There are a lot of them

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed


```typescript title="testplane.config.ts"
import { launchBrowser } from "testplane/unstable";
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this example is useless in practice. No one would want to restore tests after all tests are done executing.

A more useful example would be some kind of cleanup, like stopping a server, restoring files, etc.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Change to example where we remove state file


export default {
// ...
browsers: {
chrome: {
headless: true,
desiredCapabilities: {
webSocketUrl: true,
browserName: "chrome",
},
},
firefox: {
headless: true,
desiredCapabilities: {
webSocketUrl: true,
browserName: "firefox",
},
},
},
afterAll: async () => {
// launch a new browser with existing config
const browser = await launchBrowser(this.config.browsers.chrome);

await browser.url("https://example.com");

// login using saved state
await browser.restoreState({
path: "./dump.json",
});

// do logout things (press logout button etc.)

await browser.deleteSession();
},
};
```

## Related {#related}

- [beforeAll](../before-all)
54 changes: 54 additions & 0 deletions docs/config/before-all.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
# beforeAll

## Overview {#overview}

This parameter is a hook. The function specified for this parameter will be automatically called before tests running.

The context of the function is the Testplane config. Also function receive config in arguments.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same over here

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed


## Usage Example {#example}

Here is an example of using this hook for logging in and getting session data for use in tests.

```typescript title="testplane.config.ts"
import { launchBrowser } from "testplane/unstable";

export default {
// ...
browsers: {
chrome: {
headless: true,
desiredCapabilities: {
webSocketUrl: true,
browserName: "chrome",
},
},
firefox: {
headless: true,
desiredCapabilities: {
webSocketUrl: true,
browserName: "firefox",
},
},
},
beforeAll: async () => {
// launch a new browser with existing config
const browser = await launchBrowser(this.config.browsers.chrome);

await browser.url("https://example.com");

// do login things, type username/password etc.

// save dump with state (cookies, localStorage) for using in tests
await browser.saveState({
path: "./dump.json",
});

await browser.deleteSession();
},
};
```

## Related {#related}

- [afterAll](../after-all)
2 changes: 1 addition & 1 deletion docs/reference/testplane-standalone-api.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import { launchBrowser } from "testplane/unstable";
// Launch a new browser with the given settings
const browser = await launchBrowser({
desiredCapabilities: {
browserName: 'chrome',
browserName: "chrome",
},
headless: false,
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,7 @@ testplane --repl --grep 'my test name' --browser chrome
- `--keep-browser-on-fail` - то же, что и опция `--keep-browser`, только браузер останется только в том случае, если тесты завершатся неуспешно

Пример вывода в консоли c информацией для подключения к браузеру

```
[15:44:38 +0700] Testplane run has finished, but the browser won't be closed, because you passed the --keep-browser argument.
[15:44:38 +0700] You may attach to this browser using the following capabilities:
Expand Down
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All comments from english versions of these articles apply to ru localization as well

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed

Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import Admonition from "@theme/Admonition";

# restoreState

## Обзор {#overview}

Команда для восстановления данных (cookies, local and session storages) из файла или объекта.

<Admonition type="warning">
Если вы используете `webdriver` automation protocol вы должны установить `webSocketUrl: true` в
`desiredCapabilities` для браузера в конфиге. Для `devtools` protocol не нужно дополнительных
настроек.
</Admonition>

## Использование {#usage}

Вы можете восстановить данные из файла (используя параметр `path`) или из объекта (используя параметр`data`)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A lot of typos here e.g. "нуд=жные" and I'd use more structured language here. Again, I'd just use LLM to improve spelling and wording here

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed

Но если вы укажете оба параметра то данные восстановятся из файла.
Так же вы можете указать параметры `cookies`, `localStorage` и `sessionStorage` что бы восстановить только нужные данные.
Данные для восстановления вы можете получить из команды [saveState](../saveState).

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

<table>
<thead>
<tr><td>**Имя**</td><td>**Тип**</td><td>**Описание**</td></tr>
</thead>
<tbody>
<tr><td>path</td><td>String</td><td>Путь к файлу с данными.</td></tr>
<tr><td>data</td><td>SaveStateData</td><td>Объект с данными.</td></tr>
<tr><td>cookies</td><td>Boolean</td><td>Включить восстановление кук (true по умолчанию).</td></tr>
<tr><td>localStorage</td><td>Boolean</td><td>Включить восстановление localStorage (true по умолчанию).</td></tr>
<tr><td>sessionStorage</td><td>Boolean</td><td>Включить восстановление sessionStorage (true по умолчанию).</td></tr>
<tr><td>cookieFilter</td><td>(cookie: Cookie) => boolean</td><td>Функция для фильтрации кук, принимает объект куки и возвращает boolean.</td></tr>

</tbody>

</table>

```typescript
await browser.restoreState({
path: "./stateDump.json",
data: stateDump,
cookies: true,
localStorage: true,
sessionStorage: true,
});
```

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

Восстановление данных из файла.

```typescript
it("test", async ({ browser }) => {
await browser.url("https://github.com/gemini-testing/testplane");

await browser.restoreState({
path: "./stateDump.json",
cookieFilter: ({ domain }) => domain === ".example.com",
});

// Reload page for see auth result.
await browser.refresh();
});
```

## Связанные команды {#related}

- [saveState](../saveState)
Loading
Loading