简体   繁体   中英

Parallel testing using tesng in java using selenium

Actually, I was creating one framework but while creating it I just want it for serial execution, but now I want to do parallel testing for methods. Problem is that I have declared the driver instance as static and because of the static 2nd thread is not able to change its value.

I am creating the driver in a separate class and fetching it using getter method.

Now the problem is if I make Webdrive to non-static then I am not able to use the driver in other classes.

Even if I try extending the class (where I am creating the driver instance) it passes a null value.

So, basically I want to isolate all the instance but I can't make instance locally to class. I tried removing static variables but while doing parallel execution, 2 browser instance opens up but execution happens in one browser for all the test cases and that too parallel

How can I achieve this?

You have to create something like Driver Pool for your application. And you can start a bunch of your drivers from this pool for your test cases.

Also, consider how to unmake your driver instance static.

I have tried something similar in the past:

public class DriverPool {

    public static final int MAX_NUMBER = 5;

    private static final Object waitObj = new Object();
    private static AtomicInteger counter = new AtomicInteger(0);
    private static Logger log = Logger.getLogger(DriverPool.class);

    private static volatile ThreadLocal<WebDriver> instance = ThreadLocal
            .withInitial(DriverManager::getInstance);

    public static synchronized WebDriver getDriver() {
        try {
            while (counter.get() > MAX_NUMBER) {
                synchronized (waitObj) {
                    waitObj.wait();
                }
            }
            counter.getAndIncrement();
        } catch (InterruptedException e) {
            log.error(e);
        }

        return instance.get();
    }

    public static synchronized void closeDriver() {
        WebDriver driver = instance.get();

        driver.close();
        driver.quit();

        instance.remove();
        counter.decrementAndGet();
        synchronized (waitObj) {
            waitObj.notifyAll();
        }
    }
}

I hope it would be helpful.

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