1

我正在开发测试套件,现在大部分时间都在通过,但并非总是如此!这是 5 个非常基本的测试(与索引相关的测试)的示例。在这些测试中,最后两个(断言 url 测试)随机通过(有时都失败,有时都成功,有时一个有效,另一个失败)。即使在调试模式下,我也找不到他们通过/失败重新产生工作条件的方式的任何模式。我的第一个虽然是竞争条件,但似乎不是,因为即使使用 wait() 计时器,问题仍然存在。所以我虽然,也许我对 casper 的测试/理解的实现是不正确的。

随时向我提供有关此测试实施的反馈,或任何允许最后两个测试以 100% 的时间通过的潜在改进。当前的 MMVC 框架是主干,测试在运行之前进行编译,如果需要,我可以提供测试的编译(js)版本。

测试:

{t} = require '../helpers'
{baseUrl} = require '../config'

mouse = require('mouse').create(casper)

casper.options.loadImages = false
casper.options.verbose = true
casper.options.logLevel = 'debug'

casper.test.begin 'Home page loads correctly', 3, (test) ->
  casper.start "#{baseUrl}page/log-out"
  casper.thenOpen baseUrl, ->
    test.assertHttpStatus 200, 'Status code is 200'
  casper.then ->
    test.assertTitle t('siteName'), "Title is '#{t 'siteName'}'"
  casper.then ->
    test.assertTextExists t('home:featureWhatIsBitcoinTitle'), 'Page body contains intro text'
  casper.run ->
    test.done()

casper.test.begin 'Home page links to sign up page', 1, (test) ->
  casper.start "#{baseUrl}page/log-out"
  casper.thenOpen baseUrl, ->
    @evaluate -> document.querySelector('#signup').click()
  casper.then ->
    test.assertUrlMatch /\/page\/sign-up/, 'Url matches sign up page'
  casper.run ->
    test.done()

casper.test.begin 'Home page links to log in page', 1, (test) ->
  casper.start "#{baseUrl}page/log-out"
  casper.thenOpen baseUrl, ->
    @evaluate -> document.querySelector('a[href="/page/log-in"]').click()
  casper.then ->
    test.assertUrlMatch /\/page\/log-in/, 'Url matches log in page'
  casper.run ->
    test.done()

堆栈跟踪:

casperjs test tests/integration/compiled/tests/home.js
Test file: tests/integration/compiled/tests/home.js
# Home page loads correctly
[info] [phantom] Starting...
[info] [phantom] Running suite: 6 steps
PASS Home page loads correctly (3 tests)
[debug] [phantom] opening url: http://something.dev/page/log-out, HTTP GET
[debug] [phantom] Navigation requested: url=http://something.dev/page/log-out, type=Other, willNavigate=true, isMainFrame=true
[debug] [phantom] url changed to "http://something.dev/page/log-out"
[debug] [phantom] Navigation requested: url=https://platform.twitter.com/widgets/follow_button.html?screen_name=something&show_screen_name=false, type=Other, willNavigate=true, isMainFrame=false
[debug] [phantom] Navigation requested: url=https://www.facebook.com/plugins/like.php?href=https%3A%2F%2Fwww.facebook.com%2Fsomething&width&layout=button_count&action=like&show_faces=false&share=false&height=35&appId=000000000000, type=Other, willNavigate=true, isMainFrame=false
[debug] [phantom] Navigation requested: url=https://www.google.com/recaptcha/api/fallback?k=LOLILOLILOLILOLILOLILOLILOLILOL&hl=en&v=r000000000&t=0&ff=true, type=Other, willNavigate=true, isMainFrame=false
[debug] [phantom] Successfully injected Casper client-side utilities
[debug] [phantom] start page is loaded
[debug] [phantom] opening url: http://something.dev/, HTTP GET
[debug] [phantom] Navigation requested: url=http://something.dev/, type=Other, willNavigate=true, isMainFrame=true
[debug] [phantom] url changed to "http://something.dev/"
[debug] [phantom] Successfully injected Casper client-side utilities
[info] [phantom] Step anonymous 4/6 http://something.dev/ (HTTP 200)
PASS Status code is 200
[info] [phantom] Step anonymous 4/6: done in 3149ms.
[info] [phantom] Step anonymous 5/6 http://something.dev/ (HTTP 200)
PASS Title is 'NVM | SOMETHING'
[info] [phantom] Step anonymous 5/6: done in 3469ms.
[debug] [phantom] Navigation requested: url=https://platform.twitter.com/widgets/follow_button.html?screen_name=something&show_screen_name=false, type=Other, willNavigate=true, isMainFrame=false
[debug] [phantom] Navigation requested: url=https://www.facebook.com/plugins/like.php?href=https%3A%2F%2Fwww.facebook.com%2Fsomething&width&layout=button_count&action=like&show_faces=false&share=false&height=35&appId=000000000000, type=Other, willNavigate=true, isMainFrame=false
[info] [phantom] Step anonymous 6/6 http://something.dev/ (HTTP 200)
PASS Page body contains intro text
[info] [phantom] Step anonymous 6/6: done in 3666ms.
[debug] [phantom] Navigation requested: url=about:blank, type=Other, willNavigate=true, isMainFrame=false
[info] [phantom] Done 6 steps in 3685ms
# Home page links to sign up page
[info] [phantom] Starting...
[info] [phantom] Running suite: 5 steps
PASS Home page links to sign up page (1 test)
[debug] [phantom] opening url: http://something.dev/page/log-out, HTTP GET
[debug] [phantom] Navigation requested: url=http://something.dev/page/log-out, type=Other, willNavigate=true, isMainFrame=true
[debug] [phantom] url changed to "http://something.dev/page/log-out"
[debug] [phantom] Successfully injected Casper client-side utilities
[debug] [phantom] start page is loaded
[debug] [phantom] opening url: http://something.dev/, HTTP GET
[debug] [phantom] Navigation requested: url=http://something.dev/, type=Other, willNavigate=true, isMainFrame=true
[debug] [phantom] Navigation requested: url=https://platform.twitter.com/widgets/follow_button.html?screen_name=something&show_screen_name=false, type=Other, willNavigate=true, isMainFrame=false
[debug] [phantom] Navigation requested: url=https://www.facebook.com/plugins/like.php?href=https%3A%2F%2Fwww.facebook.com%2Fsomething&width&layout=button_count&action=like&show_faces=false&share=false&height=35&appId=000000000000, type=Other, willNavigate=true, isMainFrame=false
[debug] [phantom] url changed to "http://something.dev/"
[debug] [phantom] Successfully injected Casper client-side utilities
[info] [phantom] Step anonymous 4/5 http://something.dev/ (HTTP 200)
[info] [phantom] Step anonymous 4/5: done in 524ms.
[info] [phantom] Step anonymous 5/5 http://something.dev/ (HTTP 200)
FAIL Url matches sign up page
#    type: assertUrlMatch
#    file: tests/integration/compiled/tests/home.js
#    subject: false
#    currentUrl: "http://something.dev/"
#    pattern: "/\\/page\\/sign-up/"
[info] [phantom] Step anonymous 5/5: done in 651ms.
[debug] [phantom] Navigation requested: url=https://platform.twitter.com/widgets/follow_button.html?screen_name=something&show_screen_name=false, type=Other, willNavigate=true, isMainFrame=false
[debug] [phantom] Navigation requested: url=https://www.facebook.com/plugins/like.php?href=https%3A%2F%2Fwww.facebook.com%2Fsomething&width&layout=button_count&action=like&show_faces=false&share=false&height=35&appId=000000000000, type=Other, willNavigate=true, isMainFrame=false
[debug] [phantom] Navigation requested: url=about:blank, type=Other, willNavigate=true, isMainFrame=false
[info] [phantom] Done 5 steps in 866ms
# Home page links to log in page
[info] [phantom] Starting...
[info] [phantom] Running suite: 5 steps
PASS Home page links to log in page (1 test)
[debug] [phantom] opening url: http://something.dev/page/log-out, HTTP GET
[debug] [phantom] Navigation requested: url=http://something.dev/page/log-out, type=Other, willNavigate=true, isMainFrame=true
[debug] [phantom] url changed to "http://something.dev/page/log-out"
[debug] [phantom] Successfully injected Casper client-side utilities
[debug] [phantom] start page is loaded
[debug] [phantom] opening url: http://something.dev/, HTTP GET
[debug] [phantom] Navigation requested: url=http://something.dev/, type=Other, willNavigate=true, isMainFrame=true
[debug] [phantom] Navigation requested: url=https://platform.twitter.com/widgets/follow_button.html?screen_name=something&show_screen_name=false, type=Other, willNavigate=true, isMainFrame=false
[debug] [phantom] Navigation requested: url=https://www.facebook.com/plugins/like.php?href=https%3A%2F%2Fwww.facebook.com%2Fsomething&width&layout=button_count&action=like&show_faces=false&share=false&height=35&appId=000000000000, type=Other, willNavigate=true, isMainFrame=false
[debug] [phantom] url changed to "http://something.dev/"
[debug] [phantom] Successfully injected Casper client-side utilities
[info] [phantom] Step anonymous 4/5 http://something.dev/ (HTTP 200)
[debug] [phantom] url changed to "http://something.dev/page/log-in"
[info] [phantom] Step anonymous 4/5: done in 717ms.
[debug] [phantom] Navigation requested: url=https://platform.twitter.com/widgets/follow_button.html?screen_name=something&show_screen_name=false, type=Other, willNavigate=true, isMainFrame=false
[debug] [phantom] Navigation requested: url=https://www.facebook.com/plugins/like.php?href=https%3A%2F%2Fwww.facebook.com%2Fsomething&width&layout=button_count&action=like&show_faces=false&share=false&height=35&appId=000000000000, type=Other, willNavigate=true, isMainFrame=false
[info] [phantom] Step anonymous 5/5 http://something.dev/page/log-in (HTTP 200)
PASS Url matches log in page
[info] [phantom] Step anonymous 5/5: done in 761ms.
[debug] [phantom] Navigation requested: url=https://www.google.com/recaptcha/api/fallback?k=LOLILOLILOLILOLILOLILOLILOL=en&v=r000000000&t=0&ff=true, type=Other, willNavigate=true, isMainFrame=false
[debug] [phantom] Navigation requested: url=about:blank, type=Other, willNavigate=true, isMainFrame=false
[info] [phantom] Done 5 steps in 949ms
FAIL 5 tests executed in 5.344s, 4 passed, 1 failed, 0 dubious, 0 skipped.

Details for the 1 failed test:

In tests/integration/compiled/tests/home.js
  Home page links to sign up page
    assertUrlMatch: Url matches sign up page
[debug] [phantom] Navigation requested: url=about:blank, type=Other, willNavigate=true, isMainFrame=true
[debug] [phantom] url changed to "about:blank"
4

0 回答 0