|
| 1 | +import Admonition from "@theme/Admonition"; |
| 2 | +import Version from "../../_partials/specs/version-en.mdx"; |
| 3 | + |
| 4 | +# restoreState |
| 5 | + |
| 6 | +<Version version="8.33.0" /> |
| 7 | + |
| 8 | +## Overview {#overview} |
| 9 | + |
| 10 | +Browser command that restores session state (cookies, local and session storages) from a file or variable. |
| 11 | + |
| 12 | +## Usage {#usage} |
| 13 | + |
| 14 | +You can restore the browser state from either a file (using the `path` parameter) or directly from an object (using the `data` parameter). |
| 15 | + |
| 16 | +**Important:** If you provide both `path` and `data` parameters, the file specified in `path` will take priority. |
| 17 | + |
| 18 | +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. |
| 19 | + |
| 20 | +The state data for restoration can be obtained from the [saveState](../saveState) command. |
| 21 | + |
| 22 | +<Admonition type="warning"> |
| 23 | + You must be on the exact same page from which the cookies were originally saved. You need to |
| 24 | + navigate to the page first, use the [url](../url) command before restoring state. |
| 25 | +</Admonition> |
| 26 | + |
| 27 | +```typescript |
| 28 | +await browser.restoreState({ |
| 29 | + path: "./stateDump.json", |
| 30 | + data: stateDump, |
| 31 | + cookies: true, |
| 32 | + localStorage: true, |
| 33 | + sessionStorage: true, |
| 34 | +}); |
| 35 | +``` |
| 36 | + |
| 37 | +## Command Parameters {#parameters} |
| 38 | + |
| 39 | +<table> |
| 40 | + <thead> |
| 41 | + <tr> |
| 42 | + <td>**Name**</td> |
| 43 | + <td>**Type**</td> |
| 44 | + <td>**Default**</td> |
| 45 | + <td>**Description**</td> |
| 46 | + </tr> |
| 47 | + </thead> |
| 48 | + <tbody> |
| 49 | + <tr> |
| 50 | + <td>path</td> |
| 51 | + <td>`string`</td> |
| 52 | + <td>`-`</td> |
| 53 | + <td>Path to file with state.</td> |
| 54 | + </tr> |
| 55 | + <tr> |
| 56 | + <td>data</td> |
| 57 | + <td>`SaveStateData`</td> |
| 58 | + <td>`-`</td> |
| 59 | + <td>Object with state.</td> |
| 60 | + </tr> |
| 61 | + <tr> |
| 62 | + <td>cookies</td> |
| 63 | + <td>`boolean`</td> |
| 64 | + <td>`true`</td> |
| 65 | + <td>Enable restore cookies.</td> |
| 66 | + </tr> |
| 67 | + <tr> |
| 68 | + <td>localStorage</td> |
| 69 | + <td>`boolean`</td> |
| 70 | + <td>`true`</td> |
| 71 | + <td>Enable restore localStorage.</td> |
| 72 | + </tr> |
| 73 | + <tr> |
| 74 | + <td>sessionStorage</td> |
| 75 | + <td>`boolean`</td> |
| 76 | + <td>`true`</td> |
| 77 | + <td>Enable restore sessionStorage.</td> |
| 78 | + </tr> |
| 79 | + <tr> |
| 80 | + <td>cookieFilter</td> |
| 81 | + <td>`(cookie: Cookie) => boolean`</td> |
| 82 | + <td>`-`</td> |
| 83 | + <td> |
| 84 | + Function for filtering cookies, receiving cookie objects, and returning boolean. |
| 85 | + </td> |
| 86 | + </tr> |
| 87 | + </tbody> |
| 88 | +</table> |
| 89 | + |
| 90 | +## Usage Examples {#examples} |
| 91 | + |
| 92 | +Restore state from file. |
| 93 | + |
| 94 | +```typescript |
| 95 | +it("test", async ({ browser }) => { |
| 96 | + await browser.url("https://github.com/gemini-testing/testplane"); |
| 97 | + |
| 98 | + await browser.restoreState({ |
| 99 | + path: "./stateDump.json", |
| 100 | + cookieFilter: ({ domain }) => domain === ".example.com", |
| 101 | + }); |
| 102 | + |
| 103 | + // Reload page for see auth result. |
| 104 | + await browser.refresh(); |
| 105 | +}); |
| 106 | +``` |
| 107 | + |
| 108 | +Example of implementing authentication in tests with saveState/restoreState and beforeAll hook. |
| 109 | + |
| 110 | +```typescript |
| 111 | +import { ConfigInput, WdioBrowser } from "testplane"; |
| 112 | +import { launchBrowser } from "testplane/unstable"; |
| 113 | + |
| 114 | +export default { |
| 115 | + gridUrl: "local", |
| 116 | + beforeAll: async ({ config }) => { |
| 117 | + const b = await launchBrowser(config.browsers["chrome"]!); |
| 118 | + |
| 119 | + await b.url("https://our-site.com"); |
| 120 | + await b. $( "input.login"). setValue( "[email protected]"); |
| 121 | + await b.$("input.password").setValue("password123"); |
| 122 | + |
| 123 | + await b.saveState({ path: "./.testplane/state.json" }); |
| 124 | + await b.deleteSession(); |
| 125 | + }, |
| 126 | + sets: { |
| 127 | + /* ... */ |
| 128 | + }, |
| 129 | + browsers: { |
| 130 | + chrome: { |
| 131 | + headless: false, |
| 132 | + desiredCapabilities: { |
| 133 | + browserName: "chrome", |
| 134 | + }, |
| 135 | + }, |
| 136 | + }, |
| 137 | + plugins: { |
| 138 | + /* ... */ |
| 139 | + "@testplane/global-hook": { |
| 140 | + enabled: true, |
| 141 | + beforeEach: async ({ browser }: { browser: WdioBrowser }) => { |
| 142 | + await browser.url("https://our-site.com"); |
| 143 | + await browser.restoreState({ path: "./.testplane/state.json" }); |
| 144 | + }, |
| 145 | + }, |
| 146 | + }, |
| 147 | +} satisfies ConfigInput; |
| 148 | +``` |
| 149 | + |
| 150 | +## Related Commands {#related} |
| 151 | + |
| 152 | +- [saveState](../saveState) |
| 153 | +- [afterAll](../../../config/after-all) |
| 154 | +- [beforeAll](../../../config/before-all) |
0 commit comments