2015-10-30 97 views
1

我有我的代碼:量角器黃瓜:isPresent不發揮作用

this.Then(/^I should see "([^"]*)" link$/, function (callback) { 
    var logoutpath = by.xpath('//div[@id="account_logout"]/a'); 

    browser.wait(function() { 
     return dv.isElementPresent(logoutpath); 
    }, 30000); 

    browser.driver.isElementPresent(logoutpath).then(function(isPresent){ 
     expect(isPresent.isPresent()).toBe(true); 
     browser.driver.findElement(logoutpath).then(function(start){ 
      start.click(); 
     }); 
    }); 
    browser.sleep(2222); 

    console.log(">>>>>>>"+browser.getTitle()); 

    callback(); 
}); 

當我運行,並在控制檯中看到的錯誤:

TypeError: isPresent.isPresent is not a function 
at c:\Users\binhlex\WebstormProjects\untitled\Feature\Steps\login_steps.js:33:30 
at [object Object].promise.ControlFlow.runInFrame_ (c:/Users/binhlex/AppData/Roaming/npm/node_modules/protractor/node_modules/selenium-webdriver/lib/goog/../webdriver/promise.js:1857:20) 
at [object Object].goog.defineClass.notify (c:/Users/binhlex/AppData/Roaming/npm/node_modules/protractor/node_modules/selenium-webdriver/lib/goog/../webdriver/promise.js:2448:25) 
at [object Object].promise.Promise.notify_ (c:/Users/binhlex/AppData/Roaming/npm/node_modules/protractor/node_modules/selenium-webdriver/lib/goog/../webdriver/promise.js:564:12) 
at Array.forEach (native) 

我有一些問題? - 爲什麼我沒有使用isPresent方法? - 當我運行console.log(">>>>>>>"+browser.getTitle());,爲什麼它顯示>>>>>>>Promise::222 {[[PromiseStatus]]: "pending"},我如何使用它來驗證頁面的預期標題?

回答

1

要將最新的問題,因爲browser.getTitle()是一個承諾,如果你想CONSOLE.LOG標題你必須做的:browser.getTitle().then(function(title){console.log(title)});

關於第一個問題,我不知道爲什麼你正在試圖混淆代碼這麼多。在量角器中,您不必在單擊它之前等待元素。 (如果您沒有忽略同步)。

所以這個:

browser.driver.findElement(logoutpath).then(function(start){ 
      start.click(); 

equeals:

logoutpath.click() 
+0

感謝您的回覆,有關問題'isPresent.isPresent不是function'功能,我想檢查元素'logout'存在在頁面上還是沒有。那麼你有什麼建議嗎? – Luasg

+0

我會使用預期條件:https://angular.github.io/protractor/#/api?view=ExpectedConditions '變種EC = protractor.ExpectedConditions;' '//等待利用ID的元素'account_logout'在dom上可見。# 'browser.wait(EC.visibilityOf($('#account_logout')),5000);' – cvakiitho