2

This is what needs to be achieved: we create a driver instance. start two threads/processes - one will execute the signup (or any other) testcase and the second will execute another testcase (a trigger, change network to slow, or otherwise)

  • 2
    I don't think a single browser instance was meant to be shared by more than one thread. You will likely need to spawn one browser window per thread, which means one web driver object per thread. – Greg Burghardt Mar 06 '20 at 15:03

4 Answers4

1

I've read that python itself wasn't really built with multi-threading in mind like other languages (although there are packages that claim to do this), so I would lean towards multi-processing. You should be able to pass necessary data to corresponding parts of the code (the different processes) to do what you wish. If you're doing things that involve the same driver in both tests, you may want to consider either not running the tests at the same time (order them), or make two driver instances and pass one to each process (although I've seen some problems trying to do this with the requests package trying to make too many simultaneous connections, this was without multi-processing though). If this is a problem you run into with multi-processing, it would be interesting to see if multi-threading handles this, but you may have to run the tests simultaneously on different machines or VM's if neither provides a solution. Hope this helps

0

I suggest make the tests with selenium Grid.

Selenium Grid - StackOF Question

I think you can figure out how run Different test cases in parallel.

JaviPako
  • 56
  • 3
0

As per the documentation WebDriver is not thread-safe. Having said that, if you can serialise access to the underlying driver instance, you can share a reference in more than one thread. This is not advisable. But you can always instantiate one WebDriver instance for each thread.

Ideally the issue of thread-safety isn't in your code but in the actual browser bindings. They all assume there will only be one command at a time (e.g. like a real user). But on the other hand you can always instantiate one WebDriver instance for each thread which will launch multiple browsing tabs/windows. Till this point it seems your program is perfect.

Now, different threads can be run on same Webdriver, but then the results of the tests would not be what you expect. The reason behind is, when you use multi-threading to run different tests on different tabs/windows a little bit of thread safety coding is required or else the actions you will perform like click() or send_keys() will go to the opened tab/window that is currently having the focus regardless of the thread you expect to be running. Which essentially means all the test will run simultaneously on the same tab/window that has focus but not on the intended tab/window.


Reference

You can find a relevant detailed discussion in:

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
0

We can achieve this with testng parallel tests+Selenium Grid. you can mention in testng.xml file

    <suite name="regression" thread-count="2" parallel="tests">

        <test name="sanity">
            <classes>
                <class name="com.orangehrm.tests.Test1"></class>
            </classes>
        </test>

        <test name="smoke pack" >
            <classes>
                <class name="com.orangehrm.tests.Test2"></class>
            </classes>
        </test>
    </suite>

In java there is ThreadLocal class which allows you to create variables that can be read or written by same thread. So even if two threads are accessing same code but two threads can't see each other.

You can use synchronized method which will instantiate the driver and launch browser. Something like this

public class WDBase {
    private static ThreadLocal<RemoteWebDriver> th=new ThreadLocal<RemoteWebDriver>();


public static synchronized void setDriver(String browserName) throws MalformedURLException {
        System.setProperty("webdriver.chrome.driver", "././drivers/chromedriver.exe");
        ChromeOptions coptions=new ChromeOptions();
        th.set(new RemoteWebDriver(new URL("http://localhost:4444/wd/hub"), coptions));
}   
    public static synchronized RemoteWebDriver getDriver(){
        return th.get();
    }

    public static synchronized void launchBrowser(String browserName) throws IOException {
        setDriver(browserName);
        prop=PropertyReader.readProperties();
        getDriver().manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
        getDriver().manage().timeouts().pageLoadTimeout(30, TimeUnit.SECONDS);
        getDriver().manage().window().maximize();

        getDriver().get(prop.getProperty("url"));
    }

    public static synchronized void killBrowser() {
        if (driver!=null)
            th.get().close();
            th.get().quit();
    }
}
nikhil udgirkar
  • 367
  • 1
  • 7
  • 23