Skip to content
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

Cypress commands don't work inside an origin block #221

Open
warpdesign opened this issue May 17, 2022 · 19 comments
Open

Cypress commands don't work inside an origin block #221

warpdesign opened this issue May 17, 2022 · 19 comments

Comments

@warpdesign
Copy link

warpdesign commented May 17, 2022

  • cypress-testing-library version: 7.0.6
  • node version: 14.17.5
  • npm (or yarn) version: 6.14.15
  • Cypress 9.6.1 (9.6.0 min needed for experimental cy.origin function)

Relevant code or config

"experimentalSessionAndOrigin": true,
cy.visit('/')
// works as expected
cy.findByText('foo')

cy.origin('google.com', () => {
  cy.visit('/')
  // will crash with "cy.findByText is not a function"
  cy.findByText('foo')
})

What you did:

  • called cy.findByText within a cy.origin callback

What happened:

  • crashed with "cy.findByText is not a function" error

Capture d’écran 2022-05-17 à 16 36 12

@the-ult
Copy link

the-ult commented Jun 23, 2022

Indeed.
Same problem here with cv.findByRole() etc

@pakhilov
Copy link

I have the same problem with cypress-testing-library and real-events

@luke-sensei
Copy link

HI, same issue here with cypress 10.4

Without this fix, the cy.origin() feature is totally unusable for my project...

@craigsumner
Copy link

Hit the same issue on my first time out with cy.origin. I wonder if the same problem exists in cy.session.

@csky-ryangibson
Copy link

This is a known limitation with cy.origin (that custom commands don't work). There is a workaround I was pointed to by Gleb which is to make another cy.origin block in the beforeEach block. This origin block would be pointed to the same origin that you are trying to use in your main test. You would setup your custom command in this first origin block and you will be able to use it within the origin block in your test. https://docs.cypress.io/api/commands/origin#Callback-restrictions

@luke-sensei
Copy link

Cypress 10.9 with Cypress.require('../support/myCustomCommands') does solve this issue for me

@DariaTsypkina
Copy link

Got the same issue using custom command in cy.origin() block. @luke-sensei solution helped. Note you should insert Cypress.require('../support/myCustomCommands') directly in cy.origin() block.

@leschdom
Copy link
Contributor

Got the same issue using custom command in cy.origin() block. @luke-sensei solution helped. Note you should insert Cypress.require('../support/myCustomCommands') directly in cy.origin() block.

Just an additional info:
I just read in the release notes for @cypress/[email protected] that you can directly use import()/require() without Cypress.require()

Features
Enable requiring cy.origin dependencies with require() and import() (cypress-io/cypress#24294) (1b29ce7)

see https://github.com/cypress-io/cypress/blob/develop/npm/webpack-preprocessor/CHANGELOG.md#cypresswebpack-preprocessor-v5150-2022-10-19

@anthonyhastings
Copy link

Still an issue with Cypress 10.11 😢

@craigsumner
Copy link

When I use:

Cypress.require("../../support/myCustomCommands") 

I see:

Cypress.require is not a function

When I use:

require("../../support/myCustomCommands") 

I see:

webpack_require is not defined

I'm using Cypress 9.7.0.
What am I missing?

@preetibellad
Copy link

preetibellad commented Nov 11, 2022

When I use:

Cypress.require("../../support/myCustomCommands") 

I see:

Cypress.require is not a function

When I use:

require("../../support/myCustomCommands") 

I see:

webpack_require is not defined

I'm using Cypress 9.7.0. What am I missing?

Upgrade Cypress to 10.9.0 and @cypress/webpack-preprocessor to 5.15.0. It worked for me. Thanks @luke-sensei :)

@mlberkow
Copy link

mlberkow commented Dec 3, 2022

I'm using 10.10 and it waits for the new page to load, but neither the cy.url command or cy.log appear to do anything. I purposefully spelled google wrong in the validation, but the test passes, and the cy.log results don't show up anywhere that I can find.

cy.origin('https://www.google.com', () => { cy.url({ timeout: 60000 }).should('contain', 'gogle.com') cy.log('%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%') })

@GeorgeXCV
Copy link

When I use:

Cypress.require("../../support/myCustomCommands") 

I see:

Cypress.require is not a function

When I use:

require("../../support/myCustomCommands") 

I see:

webpack_require is not defined

I'm using Cypress 9.7.0. What am I missing?

Same problem on Cypress 12.1.0 and "@cypress/webpack-preprocessor": "^5.16.0"

@charliedodds
Copy link

When I use:

Cypress.require("../../support/myCustomCommands") 

I see:

Cypress.require is not a function

When I use:

require("../../support/myCustomCommands") 

I see:

webpack_require is not defined

I'm using Cypress 9.7.0. What am I missing?

Same problem on Cypress 12.1.0 and "@cypress/webpack-preprocessor": "^5.16.0"

Adding experimentalOriginDependencies: true to the e2e config in cypress.config.json fixed this for me
(cypress: ^12.1.0, @cypress/webpack-preprocessor: ^5.16.1, webpack: ^5.75.0)

@XavierChevalier
Copy link

Cypress origin and custom commands doesn't work with TypeScript ?

cy.origin(
  Cypress.env('BASE_URL_SHOP'), () => {
    Cypress.require('../support/commands')
    cy.checkIfLinksAreDeadOnPage('/')
  }
)
cy.origin() failed to run the callback function due to the following error:

Processing the origin callback errored:
Error: Cannot find module 'typescript'

Require stack:

- /Users/X/Library/Caches/Cypress/12.7.0/Cypress.app/Contents/Resources/app/packages/server/lib/cross-origin/process-callback.ts
    at Module._resolveFilename (node:internal/modules/cjs/loader:940:15)
    at n._resolveFilename (node:electron/js2c/browser_init:249:1105)
    at PackherdModuleLoader._tryResolveFilename (evalmachine.<anonymous>:1:735006)
    at PackherdModuleLoader._resolvePaths (evalmachine.<anonymous>:1:731827)
    at PackherdModuleLoader.tryResolve (evalmachine.<anonymous>:1:727083)
    at resolve (evalmachine.<anonymous>:1:741891)
    at Function.resolve (evalmachine.<anonymous>:1:752340)
    at customRequire.resolve (<embedded>:5233:1354109)
    at _ (<embedded>:5174:22380)
    at <embedded>:5213:67851
    at a.handle_request (<embedded>:3012:1038)
    at i (<embedded>:3027:1285)
    at <embedded>:2627:2101
    at n (<embedded>:2608:1794)
    at p (<embedded>:2608:1813)
    at IncomingMessage.f (<embedded>:2608:2273)
    at IncomingMessage.emit (node:events:539:35)
    at endReadableNT (node:internal/streams/readable:1345:12)
    at process.processTicksAndRejections (node:internal/process/task_queues:83:21)

Note: I'm using Lerna with PNPM for a monorepository architecture. Could this be a problem?
This is the package.json of the e2e tests package:

{
  "name": "@my/e2e",
  "description": "E2E tests",
  "version": "0.0.0",
  "private": true,
  "sideEffects": false,
  "scripts": {
    "cypress:open": "cypress open",
    "cypress:run": "cypress run"
  },
  "dependencies": {
    "cypress": "^12.7.0"
  },
  "devDependencies": {
    "start-server-and-test": "^1.15.2",
    "typescript": "^4.9.4"
  }
}

@luke-sensei
Copy link

@XavierChevalier see cypress-io/cypress#25885 should be fixed with next cypress release (in 4 days)

@SuprunO
Copy link

SuprunO commented Dec 8, 2023

Still an issue for me
cy.origin("https://creditcube.qa.creditsense.ai/", () => { const _ = Cypress.require("../../support/commands"); cy.visit("/"); });

ERROR
process is not defined

Variables must either be defined within the cy.origin() command or passed in using the args option.
Using require() or import() within the cy.origin() callback is not supported. Use Cypress.require() to include dependencies instead, but note that it currently requires enabling the experimentalOriginDependencies flag.
This error occurred while creating the session. Because the session setup failed, we failed the test.

@draganabodiroga23
Copy link

I am also getting process is not defined error.. I have experimentalOriginDependencies set to true in cypress config file.
Within origin I am using custom command for login(enter username, password, click login button).. Variables are passed in using the args option.
Cypress version is 12.17.4
Is this issue going to be fixed soon or it is fixed in some version above the one I am using (haven't found anything related to the issue in change log)?

ERROR
process is not defined

Variables must either be defined within the cy.origin() command or passed in using the args option.

Using require() or import() within the cy.origin() callback is not supported. Use Cypress.require() to include dependencies instead, but note that it currently requires enabling the experimentalOriginDependencies flag.

@katQA
Copy link

katQA commented Apr 11, 2024

Hi, I'm experiencing the same issue with "cypress": "^13.6.1". The commands .type('') and .click() are yelling the same error for me when they are situated inside of the cy.origin() block. experimentalOriginDependencies is set to true.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests