Search code examples
javaseleniumselenium-webdrivercucumberpojo

Can we use static Pojo methods when running tests concurrently?


Currently, I have set up an automation test framework which performs the following:

  1. Executes specific tests concurrently (i.e. x3 tests at the same time).
  2. The framework uses Cucumber BDD.
  3. I need to store and share data across steps.

I need your advice in relation to using static methods and variables when running tests concurrently, will the data be stored correctly even when running the tests concurrently?

public class Chat_Pojo {

    private static String messageToSend;

    public static void storeUniqueMessage(String message) {
        messageToSend = message;
    }

    public static String getUniqueMessage() {
        return messageToSend;
    }

    public static void wipeMessage() {
        messageToSend = null;
    }
    ...
}


Solution

  • private static String messageToSend; belongs to Chat_Pojo class only, so if you do not want to share that value, you should either modify it with private static ThreadLocal<String> messageToSend. In this case each thread will have its own messageToSend value. Respectively if you want to get that value, you need to call set() or get() If you want to share that value between threads, you need to synchronize modification methods ( public static void storeUniqueMessage(String message) and public static void wipeMessage() )