admin管理员组

文章数量:1291123

Is it possible to check the location path? I'm new to AngularJS and I'm learning with a book that describes ngScenario. This package is deprecated and I'm trying to update the described test to protractor.

E.g.

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

bees:

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

But I'd like to avoid http://localhost:8080/.

Is it possible to check the location path? I'm new to AngularJS and I'm learning with a book that describes ngScenario. This package is deprecated and I'm trying to update the described test to protractor.

E.g.

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

bees:

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

But I'd like to avoid http://localhost:8080/.

Share Improve this question edited Dec 10, 2015 at 19:48 alecxe 474k127 gold badges1.1k silver badges1.2k bronze badges asked Dec 10, 2015 at 16:59 Thomas SablikThomas Sablik 16.4k7 gold badges37 silver badges66 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 10

Just use the power of jasmine matchers:

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

where toEndWith() is ing from an awesome jasmine-matchers library.

Or, if you want an exact match, get the base URL:

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

The getCurrentUrl() function does not return the whole baseUrl (which is http://localhost:8080/#/books). So, you can try it:

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

"test" is a function of the regex module. In the example above, the text "#/books" is being pared to the current url

本文标签: javascriptProtractor check for location pathStack Overflow