在同一个测试规范中运行多个浏览器实例

在同一个测试规范中运行多个浏览器实例

本文介绍了在同一个测试规范中运行多个浏览器实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我有一个使用页面对象模型的规范,我该如何为同一个规范运行多个浏览器实例?

If I have a single spec that is using page object model, how do I run multiple browser instance for that same 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();
});

如何为其他浏览器实例重用 SearchPage 页面对象中声明的变量?

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

推荐答案

这是一个非常有趣的问题,在同一测试中使用多个浏览器interaction_spec.js.

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

页面对象的问题在于页面对象字段通常使用全局可用的elementbrowser 定义,在您的情况下,它们始终指向第一个浏览器实例.但是您基本上需要使用特定浏览器调用 element() :

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'));

而不仅仅是:

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

仅供参考,element 只是 browser.element 的快捷方式.

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

我真的不确定这是否是一个可靠的解决方案并且是否真的有效,但是您可以重新定义 全局 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;

这篇关于在同一个测试规范中运行多个浏览器实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-28 03:58