Running multiple browser instances in the same test spec

﹥>﹥吖頭↗ 提交于 2019-12-22 11:34:12

问题


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;


来源:https://stackoverflow.com/questions/29157099/running-multiple-browser-instances-in-the-same-test-spec

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!