I have the following code, which uses selenium to open 10 google search pages and do a search all 10 searches at the same time.
I think it runs correctly (in parallel), but I'm not sure, because it actually seems to open 5 instances of the browser at a time (however, this might be to do with just the gui opening I guess).
Would love to know if this is truly running in parallel, or whether I need to use the cores in the CPU as well (if this can be done?)
test.py
import unittest
from testsss import Testsss
from concurrent.futures import ThreadPoolExecutor
class Runner():
def parallel_execution(self, *name):
suite = unittest.TestSuite()
for method in dir(Testsss):
if (method.startswith('test_selenium')):
print('testing')
suite.addTest(Testsss(method))
with ThreadPoolExecutor(max_workers=10) as executor:
list_of_suites = list(suite)
for test in range(len(list_of_suites)):
test_name = str(list_of_suites[test])
executor.submit(unittest.TextTestRunner().run, list_of_suites[test])
Runner().parallel_execution(Testsss)
Testsss.py
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import Select
from selenium.common.exceptions import NoSuchElementException
import unittest
import time
class Testsss(unittest.TestCase):
print('ok3')
def tearDown(self):
self.driver.quit()
@staticmethod
def selenium_test(self, testno):
print('ok4')
self.driver = webdriver.Firefox()
self.driver.get("http://google.com")
search_field = self.driver.find_element_by_id("lst-ib")
search_field.send_keys("Test " + str(testno) )
search_field.submit()
print("ok1")
def test_selenium_1(self):
Testsss.selenium_test(self,1)
def test_selenium_2(self):
Testsss.selenium_test(self, 2)
def test_selenium_3(self):
Testsss.selenium_test(self, 3)
def test_selenium_4(self):
Testsss.selenium_test(self, 4)
def test_selenium_5(self):
Testsss.selenium_test(self, 5)
def test_selenium_6(self):
Testsss.selenium_test(self, 6)
def test_selenium_7(self):
Testsss.selenium_test(self, 7)
def test_selenium_8(self):
Testsss.selenium_test(self, 8)
def test_selenium_9(self):
Testsss.selenium_test(self, 9)
def test_selenium_10(self):
Testsss.selenium_test(self, 10)
If you're running this on Linux you could use the top
shell command to view the running processes. When open, if you press Shift-H
it will show you the threads. Run the program whilst watching top
, and you should see several python threads running under one parent thread. If so, it's working.
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.