cypress-io / cypress

Fast, easy and reliable testing for anything that runs in a browser.
https://cypress.io
MIT License
46.69k stars 3.16k forks source link

Cypress.require() works only if give absolute path #29119

Open neerajlad opened 6 months ago

neerajlad commented 6 months ago

Current behavior

I have two app which open using Okta and then it navigate to actual url . What I am currently facing is that I am using Cypress.require() method with absolute path and it works well but when I change to relative then its shows error as

Cannot find module '../XXX/xxx/xx/pageObjects/XXXPage.js'

Desired behavior

cypress.require() should access relative path

Test code to reproduce

Not working code:

When("I see the XX button", () => {
  cy.origin(xUrl, () => {
    const LoginPage1 = Cypress.require("../XXX/xxx/xx/pageObjects/XXXPage.js");
    const xPageInstance = new XPage1();
    xPageInstance .isXButtonExists();
  });
});

Working code

When("I see the XX button", () => {
  cy.origin(xUrl, () => {
    const LoginPage1 = Cypress.require("C:/user/XXX/xxx/xx/pageObjects/XXXPage.js");
    const xPageInstance = new XPage1();
    xPageInstance .isXButtonExists();
  });
});
module.exports = defineConfig({
  video: false,
  viewportWidth: 1920,
  viewportHeight: 1080,
  e2e: {
    experimentalOriginDependencies: true,
    defaultCommandTimeout: 10000,
    baseUrl:
      "https://XXXXX.com",
    specPattern: "**/*.feature",
    excludeSpecPattern: "*.js",
    setupNodeEvents,
  },
});

Cypress Version

v13.6.6

Node version

Bundled Node version: 18.17.1 & npm version : 10.4.0

Operating System

Windows 10 Enterprise - build version 19045.4046

Debug Logs

at Context.eval (webpack://XXX.cypress-tests/./cypress/e2e/steps/loginSteps.js:27)
    at Registry.runStepDefininition (webpack://XXX.cypress-tests/./node_modules/@badeball/cypress-cucumber-preprocessor/dist/registry.js:160)
    at Object.fn (webpack://XXX.cypress-tests/./node_modules/@badeball/cypress-cucumber-preprocessor/dist/browser-runtime.js:442)
    at runStepWithLogGroup (webpack://XXX.cypress-tests/./node_modules/@badeball/cypress-cucumber-preprocessor/dist/helpers/cypress.js:51)
    at Context.eval (webpack://XXX.cypress-tests/./node_modules/@badeball/cypress-cucumber-preprocessor/dist/browser-runtime.js:438)
From previous event:
    at Promise.longStackTracesCaptureStackTrace [as _captureStackTrace] (https://XXX.okta.com/__cypress/runner/cypress_runner.js:3486:19)
    at Promise._then (https://XXX.okta.com/__cypress/runner/cypress_runner.js:1239:17)
    at Promise._passThrough (https://XXX.okta.com/__cypress/runner/cypress_runner.js:4110:17)
    at Promise.lastly.Promise.finally (https://XXX.okta.com/__cypress/runner/cypress_runner.js:4119:17)
    at Object.onRunnableRun (https://XXX.okta.com/__cypress/runner/cypress_runner.js:163045:53)
    at $Cypress.action (https://XXX.okta.com/__cypress/runner/cypress_runner.js:41042:28)
    at Runnable.run (https://XXX.okta.com/__cypress/runner/cypress_runner.js:145633:13)
    at next (https://XXX.okta.com/__cypress/runner/cypress_runner.js:155418:10)
    at <unknown> (https://XXX.okta.com/__cypress/runner/cypress_runner.js:155462:5)
    at timeslice (https://XXX.okta.com/__cypress/runner/cypress_runner.js:145973:27)

Other

Value seen as XXX or XX is replaced with actual value

jennifer-shehane commented 6 months ago

We have examples in our tests of using a relative path, so this should work. We'll need some way to reproduce it since this is likely influenced by some other component in your test suite.

Screenshot 2024-03-12 at 3 36 09 PM

https://github.com/cypress-io/cypress/blob/develop/packages/driver/cypress/e2e/e2e/origin/cookie_misc.cy.ts

GazEdge commented 1 month ago

i'm having the exact same problem - relative paths do not work, having to use absolute. I tried with a bare bones cypress install and still have the issue.

// this is required as expected
const Pages = require('../support/page-objects')

describe('As an user i can', () => {
  it('login', () => {
    // works as expected, taken to auth page on different domain
    Pages.Auth.login()

    // navigate to different domain
    cy.origin('localhost', () => {
      // works as expected
      const Pages2 = Cypress.require('/REDACTED/myproject/cypress/support/page-objects.js')
      // errors with 'module not found'
      const Pages = Cypress.require('../support/page-objects.js')
    })
  })
})

cypress.config.js

const { defineConfig } = require('cypress')

module.exports = defineConfig({
  e2e: {
    experimentalOriginDependencies: true
  }

})
GazEdge commented 1 month ago

@jennifer-shehane I was able to fix my issue my using:

const Pages = Cypress.require('/cypress/support/page-objects')

is this expected behaviour? Or do i have an error in my setup?

cypress/
 - e2e/
 - support/ 
package.json

i run with npm run cy:local:

"cy": "cypress open --e2e --browser electron"
"cy:local": "dotenvx run -f .env.local -- npm run cy"