nathanboktae / mocha-casperjs

Write CasperJS tests using Mocha
MIT License
120 stars 30 forks source link

Problem with steps counter #107

Closed evgenijponomarev closed 7 years ago

evgenijponomarev commented 7 years ago

If test fails with timeout, first then section of next test will be skipped.

describe('Google searching', function() {
  before(function() {
    casper.start('http://www.google.com/')
  })

  it('Expect #qwerty element', function() {
    casper.waitForSelector('#qwerty')
  })

  it('Expect body element', function() {
    casper.then(function() {
      console.log('THEN 1')
    });
    casper.then(function() {
      console.log('THEN 2')
    })
    casper.then(function() {
      console.log('THEN 3')
    })
  })
})

Result in console:

  Google searching
    1) Expect #qwerty element
THEN 2
THEN 3
    ✓ Expect body element (61ms)

  1 passing (6s)
  1 failing

  1) Google searching Expect #qwerty element:
     "#qwerty" still did not exist 5000ms

Is it a bug? Or I'm doing something wrong? I think, it's problem with steps counter, because if I remove line 51 in node_modules/mocha-casperjs/mocha-casperjs.js, all then sections are running.

https://github.com/nathanboktae/mocha-casperjs/blob/master/mocha-casperjs.js#L51

  Google searching
    1) Expect #qwerty element
THEN 1
THEN 2
THEN 3
    ✓ Expect body element (81ms)

  1 passing (16s)
  1 failing

  1) Google searching Expect #qwerty element:
     "#qwerty" still did not exist 5000ms
nathanboktae commented 7 years ago

You always need to wrap Casper logic in a step. waitForSelector needs to be in a casper.then. otherwise mocha sees that as synchronous and succeeds.

nathanboktae commented 7 years ago

The step counter is a critical piece to how mocha-casperjs works. If you had real logic in there you would see race conditions everywhere removing it.

goganchic commented 7 years ago

Sorry, but on the main page of mocha-casperjs I see such example:

describe('Google searching', function() {
  before(function() {
    casper.start('http://www.google.fr/')
  })

  it('should retrieve 10 or more results', function() {
    casper.then(function() {
      'Google'.should.matchTitle
      'form[action="/search"]'.should.be.inDOM.and.be.visible
      this.fill('form[action="/search"]', {
        q: 'casperjs'
      }, true)
    })

    casper.waitForUrl(/q=casperjs/, function() {
      (/casperjs/).should.matchTitle
    })
  })
})

I see waitForUrl outside of casper.then section. Is it a bug in documentation?

goganchic commented 7 years ago

waitForSelector calls waitFor internally and waitFor creates separate step, so I think it is not necessary to put waitForSelector into casper.then

nathanboktae commented 7 years ago

Ah ok, yeah that incrementing of the step counter came in with #56. Did casper functionality change since 1.1.0-beta3? what version of casperjs are you on? It could be if casper moves on to the next step after timeouts now automatically.

evgenijponomarev commented 7 years ago

I wrap waitForSelector in a step:

casper.then(function() {
  casper.waitForSelector('#qwerty')
})

The result is the same. Version of casperjs is 1.1.3, mocha - 3.2.0, mocha-casperjs - 0.5.8.

nathanboktae commented 7 years ago

I agree with you I think it's a bug and removing those lines make sense. Would you mind to submit a PR with this test case too?

evgenijponomarev commented 7 years ago

Yes. I'll do it this weekend.

evgenijponomarev commented 7 years ago

https://github.com/nathanboktae/mocha-casperjs/pull/108