jcollum
jcollum

Reputation: 46599

how do I wait for an element to exist in a casper test?

casper.test.begin "Can open the homepage", (test) ->
  casper.options.viewportSize = {width: 1024, height: 768}
  casper.start("http://localhost:4000/", ->
    @wait(1000, ->
      test.assertHttpStatus 200
      test.assertTitle("XYZ Corp")
      test.assertExists('DIV.links')
      test.assertElementCount('DIV.links', 1)
      test.assertExists('IMG#companyLogo')
      @capture('./test/screencaps/homepage.png')
    )
  ).run ->
    test.done()

OK so that test is passing just fine. The thing I'd like to improve is the @wait call, it seemed smarter to do @waitFor:

@waitFor(
  ->
    document.querySelectorAll('DIV.links').length > 0
  ->
    test.assertHttpStatus 200
    test.assertTitle("XYZ Corp")

However this always times out. The document object is present, it can run the querySelectorAll function, but I never get any results back. The DIV.links object must be present on the page (and I can see it in the screenshot) because the test is passing in the first case. But in the second case, the condition is always false. waitFor documentation is here.

Bonus question: can I debug casper tests?

Upvotes: 1

Views: 400

Answers (1)

jcollum
jcollum

Reputation: 46599

4 days, no answer, figured it out on my own:

  .then( ->
    @open("theUrl")    
    @waitUntilVisible("DIV.alpha",
    ->
      test.assertExists("DIV.alpha") # doesnt hurt to check
      test.assertExists("DIV#beta")
      @capture('./test/screencaps/waitUntilVisible.png')
    ->
      test.fail('failed to find div.alpha')
    3000
    )
  )

Upvotes: 2

Related Questions