Running multiple browser instances in the same tes

2019-05-21 09:19发布

问题:

If I have a single spec that is using page object model, how do I run multiple browser instance for that same spec?

For example I have spec:

it('should run multi browser', function() {
    browser.get('http://example.com/searchPage');

    var b2 = browser.forkNewDriverInstance();
    b2.get('http://example.com/searchPage');

    var b3 = browser.forkNewDriverInstance();
    b3.get('http://example.com/searchPage');

    SearchPage.searchButton.click();
    b2.SearchPage.searchButton.click(); //fails here
    b3.SearchPage.searchButton.click();
});

How do I reuse vars declared in the SearchPage page object for the other browser instances?

回答1:

This is a really interesting question that is not covered in Using Multiple Browsers in the Same Test or in the interaction_spec.js.

The problem with page objects is that page object fields are usually defined with a globally available element or browser which in your case would always point to the first browser instance. But you basically need to call element() using a specific browser:

b2.element(by.id('searchInput'));

instead of just:

element(by.id('searchInput'));

FYI, element is just a shortcut for browser.element.


I am really not sure whether this is a reliable solution and would actually work, but you can redefine global element this way. Think about it as switching the search context to different browser instances:

SearchPage.searchButton.click();

global.element = b2.element;
SearchPage.searchButton.click();

global.element = b3.element;
SearchPage.searchButton.click();

global.element = browser.element;