-1

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);
}

}
  • Why use threads? You can just start two drivers after each other. Even your code example of t1 and t2 is running after each other and not at the same time. I think you are over complicating it :) Make sure your utils.driver returns an unique driver and not a copy of an existing one. – Niels van Reijmersdal Nov 12 '20 at 8:41
1

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");

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.