12

I have done some research and the consensus appears to state that this is impossible without a lot of knowledge and work. However:

  • Would it be possible to run the same test in different tabs simultaneously?

If so, how would I go about that? I'm using python and attempting to run 3-5 of the same test at once.

This is not a generic test, hence I do not care if it interrupts a clean testing environment.

imbr
  • 6,226
  • 4
  • 53
  • 65
Colin
  • 151
  • 1
  • 1
  • 8
  • Have you looked at setting up several virtual machines to run the tests simultaneously? – Rescis Apr 02 '18 at 19:30
  • 1
    Yes, look up `multiprocessing`, it will take **knowledge and work**, unless you are looking to hire someone to do the job for you, then it just takes *money*. – PixelEinstein Apr 02 '18 at 19:35
  • Do you need them to be in different tabs? or different windows is ok as well? I presume you don't really need them to be diffferent tabs but multiple browsers sessions on your box? Do you need to do this yourself or you have a budget? – Ali Rad Apr 02 '18 at 21:44
  • @AliRad it could be multiple windows as well. I'd prefer to do it myself but could devote some money to it – Colin Apr 03 '18 at 03:56
  • Look at TestNG, you should be able to find frameworks that achieve this. Else look at https://butlerthing.io/products#demovideo. Drop us a message and will be happy to discuss this with you – Ali Rad Apr 03 '18 at 10:15
  • look at [seleniumbase](https://seleniumbase.io/) or other comercial alternatives – imbr Oct 10 '22 at 12:59

3 Answers3

14

I think you can do that. But I feel the better or easier way to do that is using different windows. Having said that we can use either multithreading or multiprocessing or subprocess module to trigger the task in parallel (near parallel).

Multithreading example

Let me show you a simple example as to how to spawn multiple tests using threading module.

from selenium import webdriver
import threading
import time


def test_logic():
    driver = webdriver.Firefox()
    url = 'https://www.google.co.in'
    driver.get(url)
    # Implement your test logic
    time.sleep(2)
    driver.quit()

N = 5   # Number of browsers to spawn
thread_list = list()

# Start test
for i in range(N):
    t = threading.Thread(name='Test {}'.format(i), target=test_logic)
    t.start()
    time.sleep(1)
    print(t.name + ' started!')
    thread_list.append(t)

# Wait for all threads to complete
for thread in thread_list:
    thread.join()

print('Test completed!')

Here I am spawning 5 browsers to run test cases at one time. Instead of implementing the test logic I have put sleep time of 2 seconds for the purpose of demonstration. The code will fire up 5 firefox browsers (tested with python 2.7), open google and wait for 2 seconds before quitting.

Logs:

Test 0 started!
Test 1 started!
Test 2 started!
Test 3 started!
Test 4 started!
Test completed!

Process finished with exit code 0
imbr
  • 6,226
  • 4
  • 53
  • 65
Swadhikar
  • 2,152
  • 1
  • 19
  • 32
3

Python 3.2+

Threads with their own webdriver instances (different windows)

Threads can solve your problem with a good performance boost (some explanation here) on different windows. Also threads are lighter than processes.

The example bellow uses a chrome-webdriver. To exemplify uses integer as argument url_test for the test function selenium_test 6 times.

from concurrent import futures
from selenium import webdriver

def selenium_test(test_url):
    chromeOptions = webdriver.ChromeOptions()
    #chromeOptions.add_argument("--headless") # make it not visible
    driver = webdriver.Chrome(options=chromeOptions)  
    print("testing url {:0} started".format(test_url))        
    driver.get("https://www.google.com")  # replace here by driver.get(test_url)
    #<actual work that needs to be done be selenium>      
    driver.quit()  

# default number of threads is optimized for cpu cores 
# but you can set with `max_workers` like `futures.ThreadPoolExecutor(max_workers=...)`
with futures.ThreadPoolExecutor() as executor: 
  future_test_results = [ executor.submit(selenium_test, i)  
        for i in range(6) ] # running same test 6 times, using test number as url
  for future_test_result in future_test_results: 
    try:        
        test_result = future_test_result.result() # can use `timeout` to wait max seconds for each thread               
        #... do something with the test_result
    except Exception as exc: # can give a exception in some thread, but 
        print('thread generated an exception: {:0}'.format(exc))

Outputs:

testing url 1 started
testing url 5 started
testing url 3 started
testing url 4 started
testing url 0 started
testing url 2 started
imbr
  • 6,226
  • 4
  • 53
  • 65
0

Look at TestNG, you should be able to find frameworks that achieve this.

I did a brief check and here are a couple of links to get you started:

Parallel Execution & Session Handling in Selenium

Parallel Execution using Selenium Webdriver and TestNG

If you want a reliable, rebost framework that can do parallel execution as well as load testing at scale then look at TurboSelenium : https://butlerthing.io/products#demovideo. Drop us a message and will be happy to discuss this with you.

Ali Rad
  • 159
  • 1
  • 6