【问题标题】:Protractor, checking if current Url is equal to string url量角器,检查当前 Url 是否等于字符串 url
【发布时间】:2018-11-11 16:12:00
【问题描述】:

我正在尝试检查单击提交按钮后页面的当前 url 是否更改为正确的 url。 我试图以我在网上找到的许多方式来做到这一点,但没有任何效果。有时问题是不可能为“url object”和“string”做相等性,有时是别的东西。一件事肯定是我启动测试时的错误。你能帮助我吗?谢谢。

这是我的 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: expected { Object (flow_, stack_, ...) } to equal 'https://myurl.com/eng/homepage.html'

【问题讨论】:

标签: javascript url browser protractor chai


【解决方案1】:

browser.getCurrentUrl() 返回一个承诺。您需要使用 .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");

【讨论】:

  • Avinashs15 感谢您的回复。即使在这种情况下,它也会给我一个错误:E/launcher - 无法读取未定义的属性“等于”[16:42:38] E/launcher - TypeError:无法读取未定义的属性“等于”此错误被引用到line: "currentUrl.to.equal("myurl.com/eng/homepage.html");" 其中 currentUrl 未定义
  • @Spicchio 尝试使用expect(currentUrl).to.equal('whatever url you're using');
【解决方案2】:

尝试做:

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

【讨论】:

  • 嗨@BlueScoreMan 谢谢你的回复。不幸的是,我收到此消息错误:E/launcher - Invalid Chai property: toEqual。您指的是 “equal” 吗? [16:02:59] E/launcher - 错误:无效的 Chai 属性:toEqual。你的意思是“平等”吗?
  • 他使用了 Jasmine 内置断言 API(需要 Jamsine 编写测试用例)。但是从您给定的代码来看,您似乎使用 chai 作为断言 libaray。
猜你喜欢
  • 1970-01-01
  • 2015-02-04
  • 1970-01-01
  • 2023-03-20
  • 2021-10-12
  • 1970-01-01
  • 1970-01-01
  • 2014-07-29
  • 2012-09-21
相关资源
最近更新 更多