2017-04-11 57 views
0
  • 黃瓜測試方案

量角器+黃瓜 - 如果斷言失敗,測試執行突然停止

@login 
 
Scenario: Test signin link 
 
Given the user goes to "example.com" 
 
When the user clicks on login button 
 
Then the current page is the login page

嗨,每當柴/「柴作爲承諾」斷言失敗我測試停止執行突然,而不是使相應的黃瓜步驟失敗。如果方案有5個黃瓜DSL一步,如果斷言在第2步測試執行失敗,我希望測試結果應該是

  • 1方案(1失敗)
  • 5個步驟(1失敗,3跳過1後)

,但我得到的測試結果如下圖所示,錯誤代碼199

  • 步驟定義
    this.When(/^the user clicks on login button$/, function() { 
     
         browser.ignoreSynchronization = false; 
     
         return browser.wait(wagHomePage.elements.signIn.isDisplayed().then(function(visible) { 
     
          if (visible) { 
     
           wagHomePage.elements.signIn.click().then(function() { 
     
            expect(visible).to.be.true; 
     
           }); 
     
          } 
     
          else { 
     
           chai.assert.isTrue(false); 
     
          } 
     
         })); 
     
        }); 
     
    
     
        this.Then(/^the current page is the login page$/, function() {   
     
         expect(wagLoginPage.elements.pageIdentifier.isDisplayed()).to.eventually.be.true; 
     
        });

@login 
 
    Scenario: Test signin link 
 
    √ Given the user goes to "example.com" 
 
[19:58:02] E/launcher - expected false to be true 
 
[19:58:02] E/launcher - AssertionError: expected false to be true 
 
    at doAsserterAsyncAndAddThen (C:\JS_UIAutomation\node 
 
_modules\chai-as-promised\lib\chai-as-promised.js:293:29) 
 
    at .<anonymous> (C:\JS_UIAutomation\node_modules\chai 
 
-as-promised\lib\chai-as-promised.js:283:21) 
 
    at get (C:\JS_UIAutomation\node_modules\chai\lib\chai 
 
\utils\overwriteProperty.js:50:37) 
 
    at Function.assert.isTrue (C:\JS_UIAutomation\node_mo 
 
dules\chai\lib\chai\interface\assert.js:332:31) 
 
    at C:\JS_UIAutomation\example_site_tests\step_defin 
 
itions\wagLogin_definition.js:23:29 
 
    at elementArrayFinder_.then (C:\Users\username\AppData\Roaming\npm\node_modules\protractor\lib\element.ts:840: 
 
22) 
 
    at ManagedPromise.invokeCallback_ (C:\Users\username\AppData\Roaming\npm\node_modules\protractor\node_modules\ 
 
selenium-webdriver\lib\promise.js:1366:14) 
 
    at TaskQueue.execute_ (C:\Users\username\AppData\Roaming\npm\node_modules\protractor\node_modules\selenium-web 
 
driver\lib\promise.js:2970:14) 
 
    at TaskQueue.executeNext_ (C:\Users\username\AppData\Roaming\npm\node_modules\protractor\node_modules\selenium 
 
-webdriver\lib\promise.js:2953:27) 
 
    at asyncRun (C:\Users\username\AppData\Roaming\npm\node_modules\protractor\node_modules\selenium-webdriver\lib 
 
\promise.js:2813:27) 
 
[19:58:02] E/launcher - Process exited with error code 199

請幫我得到正確的測試結果類似

  • 1方案(1失敗)
  • 5個步驟(1失敗,3跳過,1通過)
+0

我沒有得到這段代碼'return browser.wait(wagHomePage.elements.signIn.isDisplayed()。然後(函數(可視){ 如果(可視){ wagHomePage.elements.signIn.click(),然後(函數(){ 預期(可見).to.be.true; });} 其他 { chai.assert.isTrue(false); } }));' 您首先等待顯示元素,如果它是可見的,您點擊某個東西(解析爲承諾),然後在預期中使用以前的結果。你能解釋一下嗎? – wswebcreation

+0

有時候,點擊操作甚至無法執行 - 雖然元素是可見的。所以爲了確保執行點擊操作,我添加了一個promise期望(可見).to.be.true;但我可以刪除它。不管這條線我面對這個問題。請幫助 –

+0

失敗的確切代碼行是什麼,它在日誌中顯示'C:\ JS_UIAutomation \ example_site_tests \ step_definitions \ wagLogin_definition.js:23:29'。你能添加一個字符串到你希望的期望像這樣'expect(visible).to.equal(true,'Descriptive message:');' – wswebcreation

回答

1

我想我看到了問題,看起來您沒有正確實施browser.wait()。根據該文件,應該由出來的:

  1. 條件:條件上等待,定義爲一個承諾,條件對象,或作爲評估條件的功能。
  2. opt_timeout:等待條件成立的時間需要多長時間。

你的代碼是這樣

return browser.wait(wagHomePage.elements.signIn.isDisplayed().then(function(visible) { 
    if (visible) { 
     wagHomePage.elements.signIn.click().then(function() { 
      expect(visible).to.be.true; 
     }); 
    } 
    else { 
     chai.assert.isTrue(false); 
    } 
})); 

它應該更像這個

// Wait 3 seconds for the element to appear and click on it 
// If not the wait wail fail by rejecting the promise with the custom message 
return browser.wait(function(){ 
    return wagHomePage.elements.signIn.isDisplayed() 
    .then(function(visible){ 
     if (visible) { 
      // click on the element 
      wagHomePage.elements.signIn.click(); 
      return true; 
     } 
     // Not visible yet, but it is in the DOM, then try again 
     return false; 
    }).catch(function(notFound){ 
     // Element not found in the DOM, try again 
     return false; 
    }); 
}, 3000, 'Element not found within 3 seconds'); 

記住isPresent()檢查該元素存在於DOM,isDisplayed()檢查元素存在於DOM中並且可見。如果您在isDisplayed()上進行檢查,則需要執行catch();

希望這會有所幫助。

+1

非常感謝你:)。這解決了我的問題。現在,如果該元素不可見,則會拋出錯誤消息「在3秒內未找到元素」,並使相應的DSL步驟失敗並繼續執行下一個測試用例。測試執行沒有突然停止。謝謝 !!! :) –

+0

@ThangakumarD,不客氣。是[這](http://stackoverflow.com/questions/43333350/protractor-cucumber-element-not-visible-even-though-element-is-visible/43333484?noredirect=1#comment73767697_43333484)也相關,如果所以,你能鏈接到這個答案?然後圓圈再次變圓;-) – wswebcreation

+0

是的。兩者都有關係。我複製了一個鏈接[here](http://stackoverflow.com/questions/43333350/protractor-cucumber-element-not-visible-even-though-element-is-visible/43333484?noredirect=1#comment73775395_43333484)。這夠了嗎 ?或者是以任何其他方式鏈接答案? –