如何在量角器的不同浏览器上将测试用例控制为 运行

how to control test cases to run on different browsers in protractor

test problem

如测试问题图片所示,有两种浏览器 运行 不同的测试用例。我想在两个 运行ning 浏览器(浏览器 1 和浏览器 2)上使用 运行 spec 7 进行交互,例如聊天应用程序。在两个浏览器上 运行ning 规范 1 到 6 之后,规范 7 应该在两个浏览器上 运行 并进行指定测试。例如:

spec7.js 文件:

这两个操作应该在同一个 spec7.js 文件中完成,并且这个文件在两个浏览器中应该是 运行,其他规格文件已经 运行ning。 我正在寻求帮助。

Protractor 允许您使用 browser.forkNewDriverInstance(opt_useSameUrl, opt_copyMockModules) 方法,它将 return 一个独立的浏览器对象。引用 this link 中的 Protractor API 概述,这里是官方文档-

If you are testing apps where two browsers need to interact with each other (e.g. chat systems), you can do that with protractor by dynamically creating browsers on the go in your test. Protractor exposes a function in the browser object to help you achieve this: browser.forkNewDriverInstance(opt_useSameUrl, opt_copyMockModules). Calling this will return a new independent browser object. The first parameter in the function denotes whether you want the new browser to start with the same url as the browser you forked from. The second parameter denotes whether you want the new browser to copy the mock modules from the browser you forked from.

browser.get('http://www.angularjs.org');
browser.addMockModule('moduleA', "angular.module('moduleA', []).value('version', '3');");

// To create a new browser.

var browser2 = browser.forkNewDriverInstance();

// To create a new browser with url as 'http://www.angularjs.org':

var browser3 = browser.forkNewDriverInstance(true);

// To create a new browser with mock modules injected:
var browser4 = browser.forkNewDriverInstance(false, true);

// To create a new browser with url as 'http://www.angularjs.org' and 
mock modules injected:
var browser4 = browser.forkNewDriverInstance(true, true);`

我想这就是您要找的。根据您提到的问题,您可以特别提及要执行操作的浏览器对象 - 例如 - 您可以使用

等方法在第二个实例上验证您的消息
 //get element text in second browser
  var element2=browser2.element;
  element2.getText().then(function(){
     //use an expect to verify the text
 }); 

请仔细阅读上面给出的link以获得进一步的解释。

您可以使用两个文件来解决您的问题。

我试着用一个示例聊天应用程序来解释它

spec7_browser1.js:

...
//Sends own message
element(by.id('messageInput')).sendKeys('hello'); // Enters hello into the message input field

element(by.id('submitMessage')).click(); // Clicks on the submit button to send the message

expect(element(by.id('chat')).getText()).toContain('hello');

// Waits for the message of browser 2
var EC = protractor.ExpectedConditions;
browser.wait(EC.textToBePresentInElement(element(by.id('chat')), 'hi'), 60000); // Waits until the given text is displayed in the element and fails if the text is not displayed in 60 seconds 

expect(element(by.id('chat')).getText()).toContain('hi');
...

spec7_browser2.js:

...
// Waits for the message of browser 1
var EC = protractor.ExpectedConditions;
browser.wait(EC.textToBePresentInElement(element(by.id('chat')), 'hello'), 60000); // Waits until the given text is displayed in the element and fails if the text is not displayed in 60 seconds 

expect(element(by.id('chat')).getText()).toContain('hello');

// Sends own message
element(by.id('messageInput')).sendKeys('hi'); // Enters hello into the message input field

element(by.id('submitMessage')).click();

expect(element(by.id('chat')).getText()).toContain('hi');
...

等等...

也许您需要在 protractor-config.js:

中增加 jasmine 的默认超时时间
jasmineNodeOpts: {
    ...
    defaultTimeoutInterval: 90000 // 90s timeout
}

希望这对您有所帮助,否则请告诉我,以便我改进我的回答;-)