简体   繁体   中英

Selenium Multithreading using Python

I have written a python test script to test out functionality of a website. Functionality such as Login into the web page, etc. In order to maximize testing, I have tried to implement multithreading to speed up the test process (so I could run two test cases concurrently). I found out that when I run the scripts, two browser would be open (which is correct), however, only one of the browser would be doing the actions I have scripted (such as clicking an element). I am able to browser.get(link) correctly but browser.find_element_by_xpath(xpath).click() didn't work.

thread1 = threading.Thread(target=runTC, args=(argument1,))
thread2 = threading.Thread(target=runTC, args=(argument2,))
# Will execute both in parallel
thread1.start()
thread2.start()

runTC() consists of the test functions I wrote.

To use benefit of multi-threading each your thread has to work with different WebDriver object since a WebDrider object is a sort of binding to a particular session/instance of your web browser.

A web driver is a service that exposes REST interface (W3C standard) to your PL bindings on one hand and translates the calls from your test script to what a particular browser understands on another hand (that is why you have different web drivers for different browsers).

When you create an instance of WebDriver in your test script it actually establishes a session that is associated with running browser. So if your multiple threads use the same object, they'll be acting within the same session, use the same cookies and hence impact each other.

If your threads use their own instances of WebDriver they'll be running in isolation in parallel each in its own browser.

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