量角器,检查当前Url是否等于字符串url

时间:2018-06-01 13:41:07

标签: javascript url browser protractor chai

我试图检查点击提交按钮后页面的当前网址是否更改为正确的网址。 我尝试以多种方式在网上找到它,但没有任何工作。有时候问题是它不可能为" url object"做同样的事情。和"字符串",有时是别的东西。有一件事是肯定的,我启动测试时的错误。你能帮助我吗?谢谢。

这里是我的stepdefinition.js

expect(browser.getCurrentUrl()).to.equal("https://myurl.com/eng/homepage.html");

这是我的错误:

E/launcher - expected { Object (flow_, stack_, ...) } to equal 'https://myurl.com/eng/homepage.html'

[15:34:28] E / launcher - AssertionError:期望{Object(flow_,stack_,...)}等于' https://myurl.com/eng/homepage.html'

2 个答案:

答案 0 :(得分:3)

browser.getCurrentUrl()返回一个promise。您需要使用.then()解决它,如下所示:

选项1)仅使用chai

// protractor conf.js
onPrepare: function() {
    var chai = require('chai'),
        chaiAsPromised = require('chai-as-promised');

    chai.use(chaiAsPromised);
    global.expect = chai.expect;
    // make `expect`as global, so you can use it anywhere is your code
}

browser.getCurrentUrl().then(function(currentUrl){
  expect(currentUrl).to.equal("https://myurl.com/eng/homepage.html");
})

选项2)同时使用chaichai-as-promised

// protractor conf.js
onPrepare: function() {
    var chai = require('chai'),
        chaiAsPromised = require('chai-as-promised');

    chai.use(chaiAsPromised);
    global.expect = chai.expect;
    // make `expect`as global, so you can use it anywhere is your code
}

expect(browser.getCurrentUrl()).to.eventually
      .equal("https://myurl.com/eng/homepage.html");

答案 1 :(得分:0)

尝试做:

expect(browser.getCurrentUrl()).toEqual("https://myurl.com/eng/homepage.html");