简体   繁体   中英

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

test problem

as seen in test problem pic, there are two browsers that run different test cases. i want to run spec 7 on both running browsers( browser1 and browser2) with interaction like chat application. after running specs 1 to 6 on both browser, spec 7 should run on both browser with specified tests. for example:

spec7.js file:

  • user1 sends hello msg from browser1 to user2 in browser2.
  • user2 recieve hello msg in browser2 and send msg hi from browser2 to user1 in browser1.

These both actions should be done in same spec7.js file and this file should be run in both browser that are running already for other specs file. i am seeking a help.

Protractor allows you to solve this problem using the browser.forkNewDriverInstance(opt_useSameUrl, opt_copyMockModules) method, which will return an independent browser object. Quoting the Protractor API overview from this link, here is the official documentation for this-

If you are testing apps where two browsers need to interact with each other (eg 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);`

I suppose this is what you are looking for. As per the problem you have mentioned, you can specifically mention the browser object that you want to perform you action in- for eg- you can verify your message on the second instance using method like

 //get element text in second browser
  var element2=browser2.element;
  element2.getText().then(function(){
     //use an expect to verify the text
 }); 

Please go through the link given above for further explanation.

You could use two file to solve your issues.

I try to explain it with a sample chat application

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

And so on...

Maybe you need to increase the default timeout of jasmine in your protractor-config.js :

jasmineNodeOpts: {
    ...
    defaultTimeoutInterval: 90000 // 90s timeout
}

I hope this helps, otherwise please tell me so i can improve my answer ;-)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM