MCPcopy
hub / github.com/codeceptjs/CodeceptJS / switchTo

Method switchTo

lib/helper/Playwright.js:3609–3679  ·  view source on GitHub ↗

* {{> switchTo }}

(locator)

Source from the content-addressed store, hash-verified

3607 * {{> switchTo }}
3608 */
3609 async switchTo(locator) {
3610 if (Number.isInteger(locator)) {
3611 // Select by frame index of current context
3612
3613 let childFrames = null
3614 if (this.context && typeof this.context.childFrames === 'function') {
3615 childFrames = this.context.childFrames()
3616 } else {
3617 childFrames = this.page.mainFrame().childFrames()
3618 }
3619
3620 if (locator >= 0 && locator < childFrames.length) {
3621 try {
3622 this.context = await Promise.race([this.page.frameLocator('iframe').nth(locator), new Promise((_, reject) => setTimeout(() => reject(new Error('Frame locator timeout')), 5000))])
3623 this.contextLocator = locator
3624 } catch (e) {
3625 console.warn('Warning during frame selection:', e.message)
3626 throw new Error('Element #invalidIframeSelector was not found by text|CSS|XPath')
3627 }
3628 } else {
3629 throw new Error('Element #invalidIframeSelector was not found by text|CSS|XPath')
3630 }
3631 return
3632 }
3633
3634 if (!locator) {
3635 this.context = this.page
3636 this.contextLocator = null
3637 this.frame = null
3638 return
3639 }
3640
3641 // iframe by selector
3642 locator = buildLocatorString(new Locator(locator, 'css'))
3643
3644 let frame
3645 try {
3646 frame = await Promise.race([this._locateElement(locator), new Promise((_, reject) => setTimeout(() => reject(new Error('Locate frame timeout')), 5000))])
3647 } catch (e) {
3648 console.warn('Warning during frame location:', e.message)
3649 frame = null
3650 }
3651
3652 if (!frame) {
3653 throw new Error(`Frame ${JSON.stringify(locator)} was not found by text|CSS|XPath`)
3654 }
3655
3656 try {
3657 // Always create frame locator from page to avoid nested frame paths
3658 this.frame = await Promise.race([this.page.frameLocator(locator), new Promise((_, reject) => setTimeout(() => reject(new Error('Frame locator timeout')), 5000))])
3659 } catch (e) {
3660 console.warn('Warning during frame locator creation:', e.message)
3661 throw new Error(`Frame ${JSON.stringify(locator)} could not be accessed`)
3662 }
3663
3664 const contentFrame = this.frame
3665
3666 if (contentFrame) {

Callers 1

_withinBeginMethod · 0.95

Calls 4

_locateElementMethod · 0.95
buildLocatorStringFunction · 0.85
nthMethod · 0.80
nameMethod · 0.80

Tested by

no test coverage detected