cypress icon indicating copy to clipboard operation
cypress copied to clipboard

Cypress.require() works only if give absolute path

Open neerajlad opened this issue 1 year ago • 4 comments

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

neerajlad avatar Mar 12 '24 16:03 neerajlad

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

jennifer-shehane avatar Mar 12 '24 19:03 jennifer-shehane

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.

  • node v20.15.1 (npm v10.7.0)
  • cypress 13.13.1
  • Mac OSX 14.5
// 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 avatar Jul 22 '24 11:07 GazEdge

@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"

GazEdge avatar Aug 14 '24 11:08 GazEdge

This issue has not had any activity in 180 days. Cypress evolves quickly and the reported behavior should be tested on the latest version of Cypress to verify the behavior is still occurring. It will be closed in 14 days if no updates are provided.

cypress-app-bot avatar Mar 04 '25 02:03 cypress-app-bot

This issue has been closed due to inactivity.

cypress-app-bot avatar Mar 18 '25 02:03 cypress-app-bot