cypress-io / cypress

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

TypeError: Cannot read property 'replace' of undefined at stripCustomProtocol #7915

Closed ghost closed 3 years ago

ghost commented 4 years ago

When my automated script lands on a new page, the script fails and when I print the error to console, I get the error in the subject line.

image

Desired behavior:

For the test to continue on and finish successfully like it has in the past.

Test code to reproduce

describe('Edit a job', () => {
  beforeEach(() => {
    Cypress.on('window:before:load', (win: any) => { win.usabilla_live = win.usabilla_live || function () { }; });

  });
  it('Edit a job Subscriptions', function () {
    const env = Cypress.env();
    cy.clearCookies();
    Cypress.config('60000');
    Cypress.config('60000');

    cy.request({
      method: "POST",
      url: "https://www.dice.com//app/manage-jobs/active/1?pageSize=25&order=job_mod_date_desc/ajaxLogin",
      body: {
        username: 'essamtest2@dice.com',
        password: 'xxxxxxxx'
      },
      headers: {
        Accept: "application/json",
        "Content-Type": "application/x-www-form-urlencoded"
      }
    });
cy.visit('https://www.dice.com//app/manage-jobs/active/1?pageSize=25&order=job_mod_date_desc');

    cy.wait(10000);

    cy.get('body').then(($body) => {

      if ($body.find('.usabilla__overlay > iframe').length == 0) {
        //cy.wait(1000);
        cy.log('usabilla not found');
        cy.log('inside if');
      } else {
        cy.get(".usabilla__overlay > iframe").then(frame => {
          const iframe = frame.contents();
          const close = iframe.find("#close");
          cy.wrap(close).click();
        });
      }
    });
  }
  pageSize = 25 & order=job_mod_date_desc');

  cy.get('[data-cy=active]').click();
  cy.get('.job-list-item:first > .mx-auto > .ml-1 > .w-100').click();
  cy.get('.col-md-4 > .actions-contianer > .btn-secondary').click();
  cy.get('.border-bottom > :nth-child(1) > .btn').click();

Versions

Cypress version: 4.10.0, Window 10.

jennifer-shehane commented 4 years ago

There's something about the file path that we're unable to read in your example.

I'm not able to run the test as given - the cy.request() just times out and you did not provide the url variable. Additionally the pasted code is malformed.

Please provide completely runnable test code so that we can reproduce and track down where this is throwing.

ghost commented 4 years ago

@jennifer-shehane the base URL in the cypress.json file looks like this: "baseUrl": "https://www.dice.com".

I edited my code above to update the cy.visit url, please take a look and let me know if I missed anything else.

Any idea what the strip custom protocol error might be about?

ghost commented 4 years ago

@jennifer-shehane I just verified the issue only happens in the pilot environment not in production, you will not be able to access our pilot environment since it operates under our vpn network so not sure how you'll be able to re-create it. Can you shed any light on why that might be happening in one environment but not in another? what is strip custom protocol?

I put a bunch of x's in the password field above since I realized it was open to the general public.

ghost commented 4 years ago

ok putting the below code at the top of my tests got things working for me again.

Cypress.on('uncaught:exception', (err, runnable) => {
    // returning false here prevents Cypress from
    // failing the test
    return false
})
prodriguez-shok commented 4 years ago

@eabusharkh0 bro you literally saved my life, I've been with the same error all day! Thanks

jennifer-shehane commented 4 years ago

Unfortunately we have to close this issue as there is not enough information to reproduce the problem. This does not mean that your issue is not happening - it just means that we do not have a path to move forward.

Please comment in this issue with a reproducible example and we will consider reopening the issue.

Measurity commented 4 years ago

I'm also getting this error sometimes (as in, random on any of the tests that make a cy.request call). Using Cypress 5.0.0

TypeError: Cannot read property 'replace' of undefined

Because this error occurred during a `before all` hook we are skipping the remaining tests in the current suite: `Data import`
    at stripCustomProtocol (http://localhost:51599/__cypress/runner/cypress_runner.js:176381:19)
    at getSourceDetailsForLine (http://localhost:51599/__cypress/runner/cypress_runner.js:176397:22)
    at apply (http://localhost:51599/__cypress/runner/cypress_runner.js:24739:17)
    at wrapper (http://localhost:51599/__cypress/runner/cypress_runner.js:29598:16)
    at arrayMap (http://localhost:51599/__cypress/runner/cypress_runner.js:24900:23)
    at Function.map (http://localhost:51599/__cypress/runner/cypress_runner.js:33841:14)
    at Object.getSourceStack (http://localhost:51599/__cypress/runner/cypress_runner.js:176437:18)
    at Object.enhanceStack (http://localhost:51599/__cypress/runner/cypress_runner.js:171468:43)
    at fail (http://localhost:51599/__cypress/runner/cypress_runner.js:168556:21)
    at http://localhost:51599/__cypress/runner/cypress_runner.js:168432:14
From previous event:
    at run (http://localhost:51599/__cypress/runner/cypress_runner.js:168426:16)
    at $Cy.cy.<computed> [as login] (http://localhost:51599/__cypress/runner/cypress_runner.js:168852:11)
    at __stackReplacementMarker (http://localhost:51599/__cypress/runner/cypress_runner.js:167942:13)
    at Context.runnable.fn (http://localhost:51599/__cypress/runner/cypress_runner.js:169078:21)
    at callFn (http://localhost:51599/__cypress/runner/cypress_runner.js:103957:21)
    at Hook.../driver/node_modules/mocha/lib/runnable.js.Runnable.run (http://localhost:51599/__cypress/runner/cypress_runner.js:103944:7)
    at http://localhost:51599/__cypress/runner/cypress_runner.js:174727:28
From previous event:
    at Object.onRunnableRun (http://localhost:51599/__cypress/runner/cypress_runner.js:174715:20)
    at $Cypress.action (http://localhost:51599/__cypress/runner/cypress_runner.js:165051:61)
    at Hook.Runnable.run (http://localhost:51599/__cypress/runner/cypress_runner.js:173050:13)
    at next (http://localhost:51599/__cypress/runner/cypress_runner.js:104459:10)
    at http://localhost:51599/__cypress/runner/cypress_runner.js:104503:5
    at timeslice (http://localhost:51599/__cypress/runner/cypress_runner.js:98429:27)
logError @ cypress_runner.js:199987

image

Would it be an idea to just validate the if originalFile is not undefined and then just putting 'unknown' for it. At least until the actual origin of the issue has been found?

bahmutov commented 3 years ago

Reliably can reproduce this in branch https://github.com/cypress-io/cypress-example-todomvc/tree/problem

Cypress v5.5.0

Install dependencies and run with npm run dev. Click the spec or app_spec, fails immediately

Screen Shot 2020-10-28 at 6 35 35 PM Screen Shot 2020-10-28 at 6 35 18 PM

Setting the breakpoint at the failing line

first stop

Screen Shot 2020-10-28 at 6 34 54 PM

second stop

Screen Shot 2020-10-28 at 6 35 05 PM

and then it crashes

bahmutov commented 3 years ago

Funnily enough, I think there is some kind of syntax error that originates first, and then is hidden by our strip custom protocol logic

When in the js/ files I have replaced

let app = app || {};

with

/* eslint-disable-next-line no-var */
var app = app || {}

then the application started working. See https://github.com/cypress-io/cypress-example-todomvc/tree/fix-eslint

bahmutov commented 3 years ago

Ok, yes, here are the screenshots. First the app does not load, so the Cypress command looking for the element fails

Screen Shot 2020-10-28 at 6 35 35 PM Screen Shot 2020-10-28 at 6 44 45 PM Screen Shot 2020-10-28 at 6 45 05 PM

Then "pause on caught exceptions" gives me the actual error, and this is a JSX file we include as a template, but it gets transformed at the runtime

<script src="node_modules/todomvc-common/base.js"></script>
<script src="node_modules/react/dist/react-with-addons.js"></script>
<script src="node_modules/react/dist/JSXTransformer.js"></script>
<script src="node_modules/director/build/director.js"></script>

<script src="js/utils.js"></script>
<script src="js/todoModel.js"></script>
<!-- jsx is an optional syntactic sugar that transforms methods in React's
`render` into an HTML-looking format. Since the two models above are
unrelated to React, we didn't need those transforms. -->
<script type="text/jsx" src="js/todoItem.jsx"></script>
<script type="text/jsx" src="js/footer.jsx"></script>
<script type="text/jsx" src="js/app.jsx"></script>

Then we hit our error

Screen Shot 2020-10-28 at 6 46 23 PM

and we can see the stack - there are some anonymous lines there, and probably no filename

Screen Shot 2020-10-28 at 6 46 40 PM
cypress-bot[bot] commented 3 years ago

The code for this is done in cypress-io/cypress#9081, but has yet to be released. We'll update this issue and reference the changelog when it's released.

cypress-bot[bot] commented 3 years ago

Released in 5.6.0.

This comment thread has been locked. If you are still experiencing this issue after upgrading to Cypress v5.6.0, please open a new issue.