2014-01-20 30 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秒中查看是否存在元素,并且在这两种情况下,我们都认为它是真实的。