|
| 1 | +# Within |
| 2 | + |
| 3 | +`Within` narrows the execution context to a specific element or iframe on the page. All actions called inside a `Within` block are scoped to the matched element. |
| 4 | + |
| 5 | +```js |
| 6 | +import { Within } from 'codeceptjs/effects' |
| 7 | +``` |
| 8 | + |
| 9 | +## Begin / End Pattern |
| 10 | + |
| 11 | +The simplest way to use `Within` is the begin/end pattern. Call `Within` with a locator to start, perform actions, then call `Within()` with no arguments to end: |
| 12 | + |
| 13 | +```js |
| 14 | +Within('.signup-form') |
| 15 | +I.fillField('Email', 'user@example.com') |
| 16 | +I.fillField('Password', 'secret') |
| 17 | +I.click('Sign Up') |
| 18 | +Within() |
| 19 | +``` |
| 20 | + |
| 21 | +Steps between `Within('.signup-form')` and `Within()` are scoped to `.signup-form`. After `Within()`, the context resets to the full page. |
| 22 | + |
| 23 | +### Auto-end previous context |
| 24 | + |
| 25 | +Starting a new `Within` automatically ends the previous one: |
| 26 | + |
| 27 | +```js |
| 28 | +Within('.sidebar') |
| 29 | +I.click('Dashboard') |
| 30 | + |
| 31 | +Within('.main-content') // ends .sidebar, begins .main-content |
| 32 | +I.see('Welcome') |
| 33 | +Within() |
| 34 | +``` |
| 35 | + |
| 36 | +### Forgetting to close |
| 37 | + |
| 38 | +If you forget to call `Within()` at the end, the context is automatically cleaned up when the test finishes. However, it is good practice to always close it explicitly. |
| 39 | + |
| 40 | +## Callback Pattern |
| 41 | + |
| 42 | +The callback pattern wraps actions in a function. The context is automatically closed when the function returns: |
| 43 | + |
| 44 | +```js |
| 45 | +Within('.signup-form', () => { |
| 46 | + I.fillField('Email', 'user@example.com') |
| 47 | + I.fillField('Password', 'secret') |
| 48 | + I.click('Sign Up') |
| 49 | +}) |
| 50 | +I.see('Account created') |
| 51 | +``` |
| 52 | + |
| 53 | +### Returning values |
| 54 | + |
| 55 | +The callback pattern supports returning values. Use `await` on both the `Within` call and the inner action: |
| 56 | + |
| 57 | +```js |
| 58 | +const text = await Within('#sidebar', async () => { |
| 59 | + return await I.grabTextFrom('h1') |
| 60 | +}) |
| 61 | +I.fillField('Search', text) |
| 62 | +``` |
| 63 | + |
| 64 | +## When to use `await` |
| 65 | + |
| 66 | +**Begin/end pattern** does not need `await`: |
| 67 | + |
| 68 | +```js |
| 69 | +Within('.form') |
| 70 | +I.fillField('Name', 'John') |
| 71 | +Within() |
| 72 | +``` |
| 73 | + |
| 74 | +**Callback pattern** needs `await` when: |
| 75 | + |
| 76 | +- The callback is `async` |
| 77 | +- You need a return value from `Within` |
| 78 | + |
| 79 | +```js |
| 80 | +// async callback — await required |
| 81 | +await Within('.form', async () => { |
| 82 | + await I.click('Submit') |
| 83 | + await I.waitForText('Done') |
| 84 | +}) |
| 85 | +``` |
| 86 | + |
| 87 | +```js |
| 88 | +// sync callback — no await needed |
| 89 | +Within('.form', () => { |
| 90 | + I.fillField('Name', 'John') |
| 91 | + I.click('Submit') |
| 92 | +}) |
| 93 | +``` |
| 94 | + |
| 95 | +## Working with IFrames |
| 96 | + |
| 97 | +Use the `frame` locator to scope actions inside an iframe: |
| 98 | + |
| 99 | +```js |
| 100 | +// Begin/end |
| 101 | +Within({ frame: 'iframe' }) |
| 102 | +I.fillField('Email', 'user@example.com') |
| 103 | +I.click('Submit') |
| 104 | +Within() |
| 105 | + |
| 106 | +// Callback |
| 107 | +Within({ frame: '#editor-frame' }, () => { |
| 108 | + I.see('Page content') |
| 109 | +}) |
| 110 | +``` |
| 111 | + |
| 112 | +### Nested IFrames |
| 113 | + |
| 114 | +Pass an array of selectors to reach nested iframes: |
| 115 | + |
| 116 | +```js |
| 117 | +Within({ frame: ['.wrapper', '#content-frame'] }, () => { |
| 118 | + I.fillField('Name', 'John') |
| 119 | + I.see('Sign in!') |
| 120 | +}) |
| 121 | +``` |
| 122 | + |
| 123 | +Each selector in the array navigates one level deeper into the iframe hierarchy. |
| 124 | + |
| 125 | +### switchTo auto-disables Within |
| 126 | + |
| 127 | +If you call `I.switchTo()` while inside a `Within` context, the within context is automatically ended. This prevents conflicts between the two scoping mechanisms: |
| 128 | + |
| 129 | +```js |
| 130 | +Within('.sidebar') |
| 131 | +I.click('Open editor') |
| 132 | +I.switchTo('#editor-frame') // automatically ends Within('.sidebar') |
| 133 | +I.fillField('content', 'Hello') |
| 134 | +I.switchTo() // exits iframe |
| 135 | +``` |
| 136 | + |
| 137 | +## Usage in Page Objects |
| 138 | + |
| 139 | +In page objects, import `Within` directly: |
| 140 | + |
| 141 | +```js |
| 142 | +// pages/Login.js |
| 143 | +import { Within } from 'codeceptjs/effects' |
| 144 | + |
| 145 | +export default { |
| 146 | + loginForm: '.login-form', |
| 147 | + |
| 148 | + fillCredentials(email, password) { |
| 149 | + Within(this.loginForm) |
| 150 | + I.fillField('Email', email) |
| 151 | + I.fillField('Password', password) |
| 152 | + Within() |
| 153 | + }, |
| 154 | + |
| 155 | + submitLogin(email, password) { |
| 156 | + this.fillCredentials(email, password) |
| 157 | + I.click('Log In') |
| 158 | + }, |
| 159 | +} |
| 160 | +``` |
| 161 | + |
| 162 | +```js |
| 163 | +// tests/login_test.js |
| 164 | +Scenario('user can log in', ({ I, loginPage }) => { |
| 165 | + I.amOnPage('/login') |
| 166 | + loginPage.submitLogin('user@example.com', 'password') |
| 167 | + I.see('Dashboard') |
| 168 | +}) |
| 169 | +``` |
| 170 | + |
| 171 | +The callback pattern also works in page objects: |
| 172 | + |
| 173 | +```js |
| 174 | +// pages/Checkout.js |
| 175 | +import { Within } from 'codeceptjs/effects' |
| 176 | + |
| 177 | +export default { |
| 178 | + async getTotal() { |
| 179 | + return await Within('.order-summary', async () => { |
| 180 | + return await I.grabTextFrom('.total') |
| 181 | + }) |
| 182 | + }, |
| 183 | +} |
| 184 | +``` |
| 185 | + |
| 186 | +## Deprecated: lowercase `within` |
| 187 | + |
| 188 | +The lowercase `within()` is still available as a global function for backward compatibility, but it is deprecated: |
| 189 | + |
| 190 | +```js |
| 191 | +// deprecated — still works, shows a one-time warning |
| 192 | +within('.form', () => { |
| 193 | + I.fillField('Name', 'John') |
| 194 | +}) |
| 195 | + |
| 196 | +// recommended |
| 197 | +import { Within } from 'codeceptjs/effects' |
| 198 | +Within('.form', () => { |
| 199 | + I.fillField('Name', 'John') |
| 200 | +}) |
| 201 | +``` |
| 202 | + |
| 203 | +The global `within` only supports the callback pattern. For the begin/end pattern, you must import `Within`. |
| 204 | + |
| 205 | +## Output |
| 206 | + |
| 207 | +When running steps inside a `Within` block, the output shows them indented under the context: |
| 208 | + |
| 209 | +``` |
| 210 | + Within ".signup-form" |
| 211 | + I fill field "Email", "user@example.com" |
| 212 | + I fill field "Password", "secret" |
| 213 | + I click "Sign Up" |
| 214 | + I see "Account created" |
| 215 | +``` |
| 216 | + |
| 217 | +## Tips |
| 218 | + |
| 219 | +- Prefer the begin/end pattern for simple linear flows — it's more readable. |
| 220 | +- Use the callback pattern when you need return values or want guaranteed cleanup. |
| 221 | +- Avoid deeply nesting `Within` blocks. If you find yourself needing nested contexts, consider restructuring your test. |
| 222 | +- `Within` cannot be used inside a `session`. Use `session` at the top level and `Within` inside it, not the other way around. |
0 commit comments