-
-
Notifications
You must be signed in to change notification settings - Fork 753
feat: Within() with begin/end pattern #5481
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
Open
DavertMik
wants to merge
5
commits into
4.x
Choose a base branch
from
feat/focus-on
base: 4.x
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+395
−28
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
29af2d9
feat: add context parameter to appendField, clearField, attachFile
7851d21
test: add tests for context parameter on appendField, clearField, att…
551ab94
refactor: simplify Playwright clearField signature to match other hel…
7cb9681
feat: refactor within to Within with begin/end pattern
aa41e81
fix: resolve merge conflicts with 4.x and use WithinContext.endCurren…
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,222 @@ | ||
| # Within | ||
|
|
||
| `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. | ||
|
|
||
| ```js | ||
| import { Within } from 'codeceptjs/effects' | ||
| ``` | ||
|
|
||
| ## Begin / End Pattern | ||
|
|
||
| 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: | ||
|
|
||
| ```js | ||
| Within('.signup-form') | ||
| I.fillField('Email', 'user@example.com') | ||
| I.fillField('Password', 'secret') | ||
| I.click('Sign Up') | ||
| Within() | ||
| ``` | ||
|
|
||
| Steps between `Within('.signup-form')` and `Within()` are scoped to `.signup-form`. After `Within()`, the context resets to the full page. | ||
|
|
||
| ### Auto-end previous context | ||
|
|
||
| Starting a new `Within` automatically ends the previous one: | ||
|
|
||
| ```js | ||
| Within('.sidebar') | ||
| I.click('Dashboard') | ||
|
|
||
| Within('.main-content') // ends .sidebar, begins .main-content | ||
| I.see('Welcome') | ||
| Within() | ||
| ``` | ||
|
|
||
| ### Forgetting to close | ||
|
|
||
| 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. | ||
|
|
||
| ## Callback Pattern | ||
|
|
||
| The callback pattern wraps actions in a function. The context is automatically closed when the function returns: | ||
|
|
||
| ```js | ||
| Within('.signup-form', () => { | ||
| I.fillField('Email', 'user@example.com') | ||
| I.fillField('Password', 'secret') | ||
| I.click('Sign Up') | ||
| }) | ||
| I.see('Account created') | ||
| ``` | ||
|
|
||
| ### Returning values | ||
|
|
||
| The callback pattern supports returning values. Use `await` on both the `Within` call and the inner action: | ||
|
|
||
| ```js | ||
| const text = await Within('#sidebar', async () => { | ||
| return await I.grabTextFrom('h1') | ||
| }) | ||
| I.fillField('Search', text) | ||
| ``` | ||
|
|
||
| ## When to use `await` | ||
|
|
||
| **Begin/end pattern** does not need `await`: | ||
|
|
||
| ```js | ||
| Within('.form') | ||
| I.fillField('Name', 'John') | ||
| Within() | ||
| ``` | ||
|
|
||
| **Callback pattern** needs `await` when: | ||
|
|
||
| - The callback is `async` | ||
| - You need a return value from `Within` | ||
|
|
||
| ```js | ||
| // async callback — await required | ||
| await Within('.form', async () => { | ||
| await I.click('Submit') | ||
| await I.waitForText('Done') | ||
| }) | ||
| ``` | ||
|
|
||
| ```js | ||
| // sync callback — no await needed | ||
| Within('.form', () => { | ||
| I.fillField('Name', 'John') | ||
| I.click('Submit') | ||
| }) | ||
| ``` | ||
|
|
||
| ## Working with IFrames | ||
|
|
||
| Use the `frame` locator to scope actions inside an iframe: | ||
|
|
||
| ```js | ||
| // Begin/end | ||
| Within({ frame: 'iframe' }) | ||
| I.fillField('Email', 'user@example.com') | ||
| I.click('Submit') | ||
| Within() | ||
|
|
||
| // Callback | ||
| Within({ frame: '#editor-frame' }, () => { | ||
| I.see('Page content') | ||
| }) | ||
| ``` | ||
|
|
||
| ### Nested IFrames | ||
|
|
||
| Pass an array of selectors to reach nested iframes: | ||
|
|
||
| ```js | ||
| Within({ frame: ['.wrapper', '#content-frame'] }, () => { | ||
| I.fillField('Name', 'John') | ||
| I.see('Sign in!') | ||
| }) | ||
| ``` | ||
|
|
||
| Each selector in the array navigates one level deeper into the iframe hierarchy. | ||
|
|
||
| ### switchTo auto-disables Within | ||
|
|
||
| If you call `I.switchTo()` while inside a `Within` context, the within context is automatically ended. This prevents conflicts between the two scoping mechanisms: | ||
|
|
||
| ```js | ||
| Within('.sidebar') | ||
| I.click('Open editor') | ||
| I.switchTo('#editor-frame') // automatically ends Within('.sidebar') | ||
| I.fillField('content', 'Hello') | ||
| I.switchTo() // exits iframe | ||
| ``` | ||
|
|
||
| ## Usage in Page Objects | ||
|
|
||
| In page objects, import `Within` directly: | ||
|
|
||
| ```js | ||
| // pages/Login.js | ||
| import { Within } from 'codeceptjs/effects' | ||
|
|
||
| export default { | ||
| loginForm: '.login-form', | ||
|
|
||
| fillCredentials(email, password) { | ||
| Within(this.loginForm) | ||
| I.fillField('Email', email) | ||
| I.fillField('Password', password) | ||
| Within() | ||
| }, | ||
|
|
||
| submitLogin(email, password) { | ||
| this.fillCredentials(email, password) | ||
| I.click('Log In') | ||
| }, | ||
| } | ||
| ``` | ||
|
|
||
| ```js | ||
| // tests/login_test.js | ||
| Scenario('user can log in', ({ I, loginPage }) => { | ||
| I.amOnPage('/login') | ||
| loginPage.submitLogin('user@example.com', 'password') | ||
| I.see('Dashboard') | ||
| }) | ||
| ``` | ||
|
|
||
| The callback pattern also works in page objects: | ||
|
|
||
| ```js | ||
| // pages/Checkout.js | ||
| import { Within } from 'codeceptjs/effects' | ||
|
|
||
| export default { | ||
| async getTotal() { | ||
| return await Within('.order-summary', async () => { | ||
| return await I.grabTextFrom('.total') | ||
| }) | ||
| }, | ||
| } | ||
| ``` | ||
|
|
||
| ## Deprecated: lowercase `within` | ||
|
|
||
| The lowercase `within()` is still available as a global function for backward compatibility, but it is deprecated: | ||
|
|
||
| ```js | ||
| // deprecated — still works, shows a one-time warning | ||
| within('.form', () => { | ||
| I.fillField('Name', 'John') | ||
| }) | ||
|
|
||
| // recommended | ||
| import { Within } from 'codeceptjs/effects' | ||
| Within('.form', () => { | ||
| I.fillField('Name', 'John') | ||
| }) | ||
| ``` | ||
|
|
||
| The global `within` only supports the callback pattern. For the begin/end pattern, you must import `Within`. | ||
|
|
||
| ## Output | ||
|
|
||
| When running steps inside a `Within` block, the output shows them indented under the context: | ||
|
|
||
| ``` | ||
| Within ".signup-form" | ||
| I fill field "Email", "user@example.com" | ||
| I fill field "Password", "secret" | ||
| I click "Sign Up" | ||
| I see "Account created" | ||
| ``` | ||
|
|
||
| ## Tips | ||
|
|
||
| - Prefer the begin/end pattern for simple linear flows — it's more readable. | ||
| - Use the callback pattern when you need return values or want guaranteed cleanup. | ||
| - Avoid deeply nesting `Within` blocks. If you find yourself needing nested contexts, consider restructuring your test. | ||
| - `Within` cannot be used inside a `session`. Use `session` at the top level and `Within` inside it, not the other way around. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -8,6 +8,7 @@ import promiseRetry from 'promise-retry' | |
| import Locator from '../locator.js' | ||
| import recorder from '../recorder.js' | ||
| import store from '../store.js' | ||
| import WithinContext from '../step/within.js' | ||
| import { includes as stringIncludes } from '../assert/include.js' | ||
| import { urlEquals, equals } from '../assert/equal.js' | ||
| import { empty } from '../assert/empty.js' | ||
|
|
@@ -2674,6 +2675,8 @@ class Puppeteer extends Helper { | |
| * {{> switchTo }} | ||
| */ | ||
| async switchTo(locator) { | ||
| WithinContext.endCurrent() | ||
|
|
||
|
Collaborator
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. WithinContext.endCurrent() ? to properly clear the context. |
||
| if (Number.isInteger(locator)) { | ||
| // Select by frame index of current context | ||
| let frames = [] | ||
|
|
||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
WithinContext.endCurrent() ? to properly clear the context.