0

I have a very basic question related to how to design method for executing selenium GRID.

In the current implementation of selenium framework in my project, we have created an action class which includes all selenium WebElelement actions in a static format. For sequential script execution, there is no issue. But for parallel script execution, I heard that we can't design a method as static as only one copy will be created. Then, how to write custom action method which we can use in other classes.

Could you please advise on this.

Current Implementation:

public class ActionUtil{
public static void selectByVisibleText(WebElement element, String visibleText, String elementName)
   {
    try {
        Select oSelect = new Select(element);
        oSelect.selectByVisibleText(text);
        log.info(text + " text is selected on " + elementName);
    } catch (Exception e) {
        log.error("selectByVisibleText action failed.Exception occured :" + e.toString());
    }
 }

}

Use of 'selectByVisibleText' static method in other page classes:

public void selectMemorableQuestion1(String question) {
    ActionUtil.selectByVisibleText(memorableQuestion1, question, "memorableQuestion1");
}
Yan
  • 433
  • 2
  • 16

1 Answers1

0

If You're trying to make parallel test run, and use methods in that way avoid static methods.

You need to add the synchronized modifier if you are working with objects that require concurrent access.

You may have concurrency issue (and being in non thread-safe situation) as soon as the code of your static method modify static variables.

So bottom line use synchronized modifier, avoid using static modifier due to thread-safe issues.

public class ActionUtil{
public synchronized void selectByVisibleText(WebElement element, String visibleText, String elementName)
   {
    try {
        Select oSelect = new Select(element);
        oSelect.selectByVisibleText(text);
        log.info(text + " text is selected on " + elementName);
    } catch (Exception e) {
        log.error("selectByVisibleText action failed.Exception occured :" + e.toString());
    }
 }

so call would be:

ActionUtil.selectByVisibleText(...);
Kovacic
  • 1,473
  • 6
  • 21
  • Many Thanks Kovacic for your reply. One more thing i forgot to mentioned in above code that i have created log object at class level for eg. static Logger log = Logger.getLogger(ActionUtil.class.getName()); then, do i need to remove the static with synchronized for log object ? Can you please advise on this. – Abhijit Biradar May 16 '18 at 04:19
  • Static should not "bother" your code, but its not thread-safe, and it might cause problems, that afterwards You wouldn't easily debug. Try with mentioned code above. Check this article from quora about static modifier: https://www.quora.com/What-does-static-keyword-do-in-Java. Basically Your log should be ok to leave with static modifier. – Kovacic May 16 '18 at 10:57
  • also check this issue here: https://stackoverflow.com/questions/2120248/how-to-synchronize-a-static-variable-among-threads-running-different-instances-o – Kovacic May 16 '18 at 10:59
  • Thanks Kovacic for your reply. will try to implement your suggestion – Abhijit Biradar May 19 '18 at 19:22