Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Handle two browsers concurrently with WebDriver or WebDriverJS

I am planning to test a simple realtime web app. This app is written in JavaScript and it shows the "presence status" of the logged in user. If the userA logs his status is being modified from "Inactive" to "Active". This action is reflected to all other users that are logged in to the application. UserB that is logged in shows realtime the presence of userA to change.

I would like to test this scenario. A test handler opens one browsers, does a write action and at the same time a second browser gets updated.

What tools are available? Can this be done with WebDriver/WebDriverJs? Can I have two threads/sessions handled via WebDriver? Any examples?

like image 593
cateof Avatar asked Oct 21 '25 19:10

cateof


2 Answers

You can use WebdriverJS to do that. Just create two Webdriver instances and navigate them, for example:

var WebdriverJS = require('webdriverjs'),
    assert      = require('assert'),

browser1 = new WebdriverJS({
    desiredCapabilities: {browserName:'chrome'}
}).init().url('http://github.com'),

browser2 = new WebdriverJS({
    desiredCapabilities: {browserName:'chrome'}
}).init().url('http://github.com');

browser1
    .setValue('#js-command-bar-field',['webdriverjs','Enter'])
    .getText('.sort-bar h3',function(err,text) {
        assert(text.indexOf('found 24 repository results') >= 0);
    })
    .end();


browser2
    .setValue('#js-command-bar-field',['linux','Enter'])
    .getText('.sort-bar h3',function(err,text) {
        assert(text.indexOf('We\'ve found 22,466 repository results') >= 0);
    })
    .end();

Two Chrome windows get opened and will execute your instructions independently.

like image 120
ChristianB Avatar answered Oct 23 '25 09:10

ChristianB


With the later versions of WebDriver.js, all asynchronous operations are managed in control flows. Because the control flow will serialize all operations in the correct order, simply creating multiple driver instances may not be enough. All of the operations on one driver will occur before the operations on the other.

For true parallelization, create multiple control flows. Here is an excerpt from the documentation https://code.google.com/p/selenium/wiki/WebDriverJs#Defining_Multiple_Flows:

var terms = [
   'javascript',
   'selenium',
   'webdriver'
];

var flows = terms.map(function(term) {
 return webdriver.promise.createFlow(function() {
   var driver = new webdriver.Builder().build();

   driver.get('http://www.google.com');
   driver.findElement(webdriver.By.name('q')).sendKeys(term);
   driver.findElement(webdriver.By.name('btnG')).click();
   driver.getTitle().then(function(title) {
     if (title !== (term + ' - Google Search')) {
       throw Error('Unexpected title: ' + title);
     }
   });
 });
});

webdriver.promise.fullyResolved(flows).then(function() {
 console.log('All tests passed!');
});
like image 30
orirawlings Avatar answered Oct 23 '25 08:10

orirawlings