【问题标题】:Protractor check for location path量角器检查位置路径
【发布时间】:2016-03-16 10:09:49
【问题描述】:

是否可以检查位置路径?我是 AngularJS 的新手,我正在学习一本描述 ngScenario 的书。该软件包已弃用,我正在尝试将描述的测试更新为量角器。

例如

expect(browser().location().path()).toEqual('/books'); 

变成:

expect(browser.getCurrentUrl()).toEqual('http://localhost:8080/#/books');

但我想避免http://localhost:8080/

【问题讨论】:

    标签: javascript angularjs selenium automation protractor


    【解决方案1】:

    只需使用茉莉花匹配器的强大功能

    expect(browser.getCurrentUrl()).toMatch(/\/#\/books$/);  // /#/books at the end of url
    expect(browser.getCurrentUrl()).toEndWith("/#/books");
    expect(browser.getCurrentUrl()).toContain("/#/books");
    

    toEndWith() 来自一个很棒的 jasmine-matchers 库。

    或者,如果您想要完全匹配,请获取基本 URL:

    expect(browser.getCurrentUrl()).toEqual(browser.baseUrl + "/#/books");
    

    【讨论】:

    • expect(browser.getCurrentUrl()).toEqual(browser.baseUrl + "#/books"); 有效,但我不知道如何将茉莉花火柴与量角器一起使用。
    • 我必须将onPrepare: function(){ require("jasmine-expect"); } 插入我的量角器配置文件中。现在 botch 变体可以工作了。
    【解决方案2】:

    getCurrentUrl() 函数不会返回整个 baseUrl(即http://localhost:8080/#/books)。 所以,你可以试试:

    browser.driver.getCurrentUrl().then(function(url) {
        return /#\/books/.test(url);
    });
    

    “test”是正则表达式模块的一个函数。在上面的示例中,文本“#/books”正在与当前 url 进行比较

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-08-04
      • 1970-01-01
      • 1970-01-01
      • 2018-07-22
      • 2022-08-17
      • 2016-03-03
      • 2019-11-15
      • 1970-01-01
      相关资源
      最近更新 更多