-
Notifications
You must be signed in to change notification settings - Fork 3
Docs for saveState and restoreState #89
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
base: master
Are you sure you want to change the base?
Changes from all commits
7e531b0
5f69c27
a21253e
ba553af
c18d65e
7e48133
fc1ed2b
ca3ba6a
92087ad
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,151 @@ | ||
import Admonition from "@theme/Admonition"; | ||
|
||
# restoreState | ||
|
||
## Overview {#overview} | ||
|
||
Browser command that restores session state (cookies, local and session storages) from a file or variable. | ||
|
||
## Usage {#usage} | ||
|
||
You can restore the browser state from either a file (using the `path` parameter) or directly from an object (using the `data` parameter). | ||
|
||
**Important:** If you provide both `path` and `data` parameters, the file specified in `path` will take priority. | ||
|
||
You can optionally specify which storage types to restore using the `cookies`, `localStorage`, and `sessionStorage` parameters. This allows you to restore only the specific data you need. | ||
|
||
The state data for restoration can be obtained from the [saveState](../saveState) command. | ||
|
||
<Admonition type="warning"> | ||
You must be on the exact same page from which the cookies were originally saved. You need to | ||
navigate to the page first, use the [url](../url) command before restoring state. | ||
</Admonition> | ||
|
||
```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>**Default**</td> | ||
<td>**Description**</td> | ||
</tr> | ||
</thead> | ||
<tbody> | ||
<tr> | ||
<td>path</td> | ||
<td>`string`</td> | ||
<td>`-`</td> | ||
<td>Path to file with state.</td> | ||
</tr> | ||
<tr> | ||
<td>data</td> | ||
<td>`SaveStateData`</td> | ||
<td>`-`</td> | ||
<td>Object with state.</td> | ||
</tr> | ||
<tr> | ||
<td>cookies</td> | ||
<td>`boolean`</td> | ||
<td>`true`</td> | ||
<td>Enable restore cookies (true by default).</td> | ||
</tr> | ||
<tr> | ||
<td>localStorage</td> | ||
<td>`boolean`</td> | ||
<td>`true`</td> | ||
<td>Enable restore localStorage (true by default).</td> | ||
</tr> | ||
<tr> | ||
<td>sessionStorage</td> | ||
<td>`boolean`</td> | ||
<td>`true`</td> | ||
<td>Enable restore sessionStorage (true by default).</td> | ||
</tr> | ||
<tr> | ||
<td>cookieFilter</td> | ||
<td>`(cookie: Cookie) => boolean`</td> | ||
<td>`-`</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 }) => { | ||
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(); | ||
}); | ||
``` | ||
|
||
Example of implementing authentication in tests with saveState/restoreState and beforeAll hook. | ||
|
||
```typescript | ||
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; | ||
``` | ||
|
||
## Related Commands {#related} | ||
|
||
- [saveState](../saveState) | ||
- [afterAll](../../../config/after-all) | ||
- [beforeAll](../../../config/before-all) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
# saveState | ||
|
||
## Overview {#overview} | ||
|
||
Browser command that saves session state (cookies, local and session storages). | ||
|
||
## Usage {#usage} | ||
|
||
This command returns a state 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](../restoreState) command. | ||
|
||
```typescript | ||
import type { SaveStateData } from "testplane"; | ||
|
||
const stateDump: SaveStateData = await browser.saveState({ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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>**Default**</td> | ||
<td>**Description**</td> | ||
</tr> | ||
</thead> | ||
<tbody> | ||
<tr> | ||
<td>path</td> | ||
<td>`string`</td> | ||
<td>`-`</td> | ||
<td>Path to file where state will be saved.</td> | ||
</tr> | ||
<tr> | ||
<td>cookies</td> | ||
<td>`boolean`</td> | ||
<td>`true`</td> | ||
<td>Enable save cookies (true by default).</td> | ||
</tr> | ||
<tr> | ||
<td>localStorage</td> | ||
<td>`boolean`</td> | ||
<td>`true`</td> | ||
<td>Enable save localStorage (true by default).</td> | ||
</tr> | ||
<tr> | ||
<td>sessionStorage</td> | ||
<td>`boolean`</td> | ||
<td>`true`</td> | ||
<td>Enable save sessionStorage (true by default).</td> | ||
</tr> | ||
<tr> | ||
<td>cookieFilter</td> | ||
<td>`(cookie: Cookie) => boolean`</td> | ||
<td>`-`</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 }) => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's add references to the new hooks as well. |
||
- [afterAll](../../../config/after-all) | ||
- [beforeAll](../../../config/before-all) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
# 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. | ||
|
||
## Usage Example {#example} | ||
|
||
Here is an example of using this hook to remove a file with a page state. | ||
|
||
```typescript title="testplane.config.ts" | ||
import * as fs from "fs"; | ||
|
||
export default { | ||
// ... | ||
afterAll: async () => { | ||
await fs.unlink("./dump.json"); | ||
}, | ||
}; | ||
``` | ||
|
||
## Related {#related} | ||
|
||
- [beforeAll](../before-all) |
sonic16x marked this conversation as resolved.
Show resolved
Hide resolved
|
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. | ||
|
||
## 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) |
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.
We can use example from below here as well. Not necessarily replacing yours, you can just add it as a second one.