2014-01-20 42 views
6

我的測試失敗:簡單量角器測試與不支持的定位策略

it('should allow login', function() { 
    browser.get('index.html'); 

    $('#username').sendKeys('administrator'); 
    $('#password').sendKeys('password'); 
    $('#login').click(); 

    var logout = $('#logout'); 
    expect($p.isElementPresent(logout)).to.eventually.be.true; 
}); 

但這個錯誤出具有:

Error: Unsupported locator strategy: click 
    at Error (<anonymous>) 
    at Function.webdriver.Locator.createFromObj (/usr/local/lib/node_modules/protractor/node_modules/selenium-webdriver/lib/webdriver/locators.js:97:9) 
    at Function.webdriver.Locator.checkLocator (/usr/local/lib/node_modules/protractor/node_modules/selenium-webdriver/lib/webdriver/locators.js:111:33) 
    at webdriver.WebDriver.findElements (/usr/local/lib/node_modules/protractor/node_modules/selenium-webdriver/lib/webdriver/webdriver.js:805:31) 
    at webdriver.WebDriver.isElementPresent (/usr/local/lib/node_modules/protractor/node_modules/selenium-webdriver/lib/webdriver/webdriver.js:787:29) 
    at Protractor.isElementPresent (/usr/local/lib/node_modules/protractor/lib/protractor.js:476:22) 
    at /Users/pschuegr/wt/client/e2e/login_test.js:26:15 

奇怪的是,它指向isElementPresent行,而不是用線點擊。我對webdriver相當陌生,所以如果我錯過了某些明顯的道歉,我很抱歉。我正在使用摩卡框架(這意味着量角器的金絲雀版本),fwiw。

任何想法讚賞。

+0

什麼是$ p,它來自哪裏? – wlingke

+0

$ p是量角器實例(即ptor) – pschuegr

+3

現在'$ p'和'ptor'已被'browser'有效替換 –

回答

5

$('#logout')是一個WebElement。 isElementPresent需要一個定位,如by.css

$('#username').sendKeys('administrator'); 
$('#password').sendKeys('password'); 
$('#login').click(); 

var logout = by.css('#logout'); 
browser.wait(function() { return $p.isElementPresent(logout); }, 8000); 
expect($p.isElementPresent(logout)).toBeTruthy(); 
17

採用最新量角器構建,可以縮短上面的回答以下:

expect(element(by.css('#logout')).isPresent()).toBeTruthy(); 

這樣,您就不必執行瀏覽器。等待並減少對isElementPresent的調用次數。

+2

這裏等待的地方在哪裏,爲什麼?我不明白爲什麼你的任何修改會導致'browser.wait'調用不必要,所以我認爲量角器內部發生了一些變化,使得在沒有顯式等待的情況下可以做到這一點。如果是這樣,那有什麼變化? –

+0

@MarkAmery對我來說,似乎還沒有,這可能適用於已經可用的元素,而不是你正在等待的元素。 – MrYellow

+2

@MarkAmery量角器在其所有函數中使用promise,並等待$ http請求在解析之前完成(如果有的話)。因此,在這裏的答案,期望將1)得到元素2),當它被解決時,它將檢查元素是否存在3)當解決這個問題時,將會觸發對真實結果的期望。等待發生在引擎蓋下,您不需要手動等待,步驟1,2和3中的每個承諾都會根據需要等待每個待解決的承諾。 – inolasco

-1

這應該工作:

var logout = $('#logout'); 
expect(logout.isPresent()).to.eventually.be.true; 
+2

isPresent()不返回承諾,因此您不能使用'最終'! 我已經犯了同樣的錯誤...... -.- – Sentenza

+0

Thx @Sentenza這個評論是有幫助的。 – miphe

0

我會採取下面的代碼片段描述了最安全的方法:

it('should return true when element is present', function() { 
var logout; 
logout = $('#logout'); 

    browser.driver.isElementPresent(logout).then(function (isPresent) { 
    isPresent = (isPresent) ? true : browser.wait(function() { 
     return browser.driver.isElementPresent(logout); 
    }, 15000); //timeout after 15s 
    expect(isPresent).toBeTruthy(); 
    }); 
}); 

以上並承諾檢查元素是否存在的代碼開始,如果屬實,則將其分配給true,否則等待並保持在接下來的15秒中查看是否存在元素,並且在這兩種情況下,我們都認爲它是真實的。