TransWikia.com

Error in Launching 2 chrome browsers using MultiThreading and perform same functionality simultaneouly

Software Quality Assurance & Testing Asked by Apeksha Agarwal on February 16, 2021

I am trying to launch two chrome browsers Simultaneously using Selenium ad cucumber, and perform same operations using multiThreading, but on one browser, operations get complete, whereas on another browser, browser is launched, but nothing else is performed, not even window Maximize, or navigation to endpoint URL

below is my code Snippet:

public class TraderViewStepDef extends Thread implements Runnable {

WebDriver driver = utils.driver;
UIGeneral uIG = new UIGeneral();
Common_Step cs =new Common_Step();

@When("Open event simultaneously to check message {string} {string}")
public void openEvent(String user1, String user2) throws InterruptedException {

    Thread t1 = new Thread(new TraderViewStepDef());
    Thread t2 = new Thread(new TraderViewStepDef());
    t1.setName(user1);
    t2.setName(user2);
    t1.start();
    Thread.sleep(6000);
    t2.start();
    t1.join();
    t2.join();
}
@Override
public void run() {
    try {
        System.out.println(Thread.currentThread().getName());
        uIG.userLoginAsIn(Thread.currentThread().getName());
        
    }
    catch(Exception e)
    {
        e.printStackTrace();
    }

}

public void userLoginAsIn(String userName)
{
    getDriver();
    driver.manage().window().maximize();
    driver.get(endpoint);
}

}

One Answer

Unless seeing the code properly its hard to say what's going on, just a random guess would be that you are just starting two threads that are pointing to the same identifier called the driver

In the first thread you are creating a driver object A

and on the second thread, you are creating driver object B

but there is only one variable (Identifier) called the driver that stores this object.

so let assume thread 2 starts after thread A, so driver variable start pointing to the driver Object B,

That's why you have two browsers opened ( Object A and Object B) but the action in only one ( because driver now points to address location of Object B )

try defining driver as thread local

public static ThreadLocal<WebDriver> driver = new ThreadLocal<>();
driver.set(new ChromeDriver());

Now instead of driver use driver.get()

example:

driver.get().get("https://www.google.com");

Answered by PDHide on February 16, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP