延长符:catch断言错误

Protractor: catch AssertionError

本文关键字:断言 错误 catch      更新时间:2023-09-26

我使用ProtractorChai作为承诺来创建一个基于javascript的测试工具,我得到了错误

AssertionError: expected 'http://localhost:8888/test/homepage.php' to equal 'http://localhost:8888/test/my_homepage.php'

当我用这个步骤定义检查url时:

this.Then(/^The url of the page should be "([^"]*)"$/, function(myUrl, callback){
    expect(browser.getCurrentUrl()).to.eventually.equal(myUrl);
    callback();
  });

我想捕捉这个错误以便使用不同的回调函数,我该怎么做?我试过用一个试抓块,但似乎不起作用。我甚至不明白AssertionErrors是否是由Protractor生成的,你能温和地解释一下吗?

提前感谢

我找不到任何可以捕获预期错误并执行其他操作的东西。如果@alexe从评论中提出的建议有效,那应该是你的答案,否则为什么不直接做呢

browser.getCurrentUrl().then(function(url) {
    if(url === myUrl) {
       callback();
    } else {
       callback('something went wrong'); 
    }
});

或者这不会奏效?

try {
  expect(browser.getCurrentUrl()).to.eventually.equal(myUrl);
  callback();
} catch(e) {
  callback('something went wrong ')); 
}