Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I run multiple instances at once(simultaneously) with selenium-webdriver?

I'm trying to use Selenium to automate file uploading.

I'v already wrote a tiny program with the selenium-webdriver that works.

The problem is, there are thousands of files need to be uploaded, I'd like to run multiple browser instances simultaneously to speed up the automation. So I tried something like this

var i = 0;
while (i < 10) {
    i++;
    var driver = new webdriver.Builder()
            .forBrowser('firefox')
            .build();

    // login and upload files......
}

I expected this would create 10 browser instances at once, and do the automation simultaneously.

But actually... the above code will creates browser instance 'one by one' which means, it won't create another instance until the previous one finishes.

I'v also tried execute the program in multiple shell instances, that will fire up multiple browser instances for me, but I just don't want to do this...

like image 964
yaquawa Avatar asked Nov 16 '15 18:11

yaquawa


People also ask

Can you run multiple instances of Selenium?

If you want to do more than that then you have to use selenium Grid. You don't need selenium grid to execute test in parallel but managing 10 instances with grid would be a piece of cake. Just use the same machine for node and host and point your tests to the hub. Done.

Can we handle multiple windows in Selenium?

The window handle in Selenium helps in handling multiple windows and child windows. Each browser will have a unique window handle value with which we can uniquely identify it.


2 Answers

Well you need to create multiple threads instead of looping, then you can start each upload in parallel threads. You are on the right track. You dont need selenium grid to achieve this.

lookup about multithreading. You can start with this answer

It's not right you need grid for executing multiple browser sessions. You can invoke multiple browser sessions by just creating multiple driver objects, and managing them. Each session will be separate if you want them to be.

Grid is for scaling as there is a limitation on the no of browser instances you can run keeping your machine performance intact and tests stable. Like more than 5 chrome instances in a single machine. If you want to do more than that then you have to use selenium Grid.

like image 130
Shamik Avatar answered Sep 18 '22 16:09

Shamik


You should create a new instance of the WebDriver and it's capabilities for each new browser you want to open.

The following will open Google in five separate instances of Chrome.

import * as webdriver from "selenium-webdriver";
import * as Chrome from 'selenium-webdriver/chrome';

function loadSelenium(){
    let options = new Chrome.Options();
    let capabilities = options.toCapabilities();
    console.log('loading another');
    return new webdriver.Builder()
        .forBrowser('chrome')
        .withCapabilities(capabilities)
        .build();
}

for(let i = 0; i < 5; i++) {
    let driver = loadSelenium();
    driver.get('http://www.google.com');
}
like image 37
moefinley Avatar answered Sep 16 '22 16:09

moefinley