sumitsingh13
sumitsingh13

Reputation: 53

cypress.origin throws error: (uncaught exception)Error: on only accepts instances of Function

I am using Cypress with Cucumber.

I am trying to test cross origin login but the origin method keeps on throwing error:

Code:

Given(/^the user login to the Test Page$/, function () {
  cy.visit("https://example-originalURL");
  cy.get("button").contains("Login").click();

  const credentials = {
    username: "hello",
    password: "user",
  };


  cy.origin("https://example-newURL", { args: credentials }, ({ username, password }) => {
    cy.get("#email", { timeout: 20000 }).type(username);
    cy.get("#password").type(password, { log: false });
    cy.get("button").contains("Login").click();
  });
});

Cypress.config.js

module.exports = defineConfig({
  projectId: "t7unhv",
  e2e: {
    setupNodeEvents(on, config) {
      on("file:preprocessor", cucumber());
      on('task', {
        log(message) {
          console.log(message +'\n\n');
          return null;
        },
      });
    },
    specPattern: "./cypress/e2e/features/*.feature",
    chromeWebSecurity: false,
    experimentalSessionAndOrigin: true,
    defaultCommandTimeout: 15000,
    env: {
      devCentralUrl: "https://***.dev.***.com.au/login",
      testCentralUrl:
        "https://***.test.***.com.au/login",
      test***: "http://***.test.***.com.au",
      dev***: "http://***.dev.***.com.au",
      uat***: "https://***.uat.***.com.au",
      dataSource: "",
      environs: "test",
    },
    retries: {
      runMode: 0,
    },
    pageLoadTimeout: 15000,
    reporter: "mochawesome",
    reporterOptions: {
      reporterEnabled: "mochawesome",
      mochawesomeReporterOptions: {
        reportDir: "cypress/reports/mocha",
        quite: true,
        charts: true,
        overwrite: false,
        html: false,
        json: true,
      },
    },
  },
});

Error:

The following error originated from your test code, not from Cypress.

> on only accepts instances of Function

When Cypress detects uncaught errors originating from your test code it will automatically fail the current test.

I have tried multiple syntax changes like not passing the credentials as optional argument to cy.origin. If someone can provide a quick help, that will be great.

Upvotes: 1

Views: 954

Answers (1)

TesterDick
TesterDick

Reputation: 10535

If the problem is in the test code, it is likely to be that newURL is undefined. The error message suggests the problem is in the app, but that might be a red herring.

Try just adding a fixed string for the cy.origin() key,

cy.origin('login', { args: credentials }, ({ username, password }) => {
  ...
})

Upvotes: 2

Related Questions