How to design custom action method in Selenium Grid architecture

41 views
Skip to first unread message

Abhijit Biradar

unread,
May 15, 2018, 5:45:08 AM5/15/18
to Selenium Users
Hi All,

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

In current implementation of selenium framework in my project we have created a action class which includes all selenium WebElelement actions in 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 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");
}

Jonathan Herbaut

unread,
May 15, 2018, 5:53:28 AM5/15/18
to Selenium Users
Hi
Implementing a static method with parallel script is not a problem, the problem is about shared objects. And with your code, the only problem I can eventually see is your log object. Depending on how he is created and deleted, you can have some surprises.
I have already writen a method that looks like the same than yours.

Abhijit Biradar

unread,
May 15, 2018, 7:33:37 AM5/15/18
to Selenium Users
Thanks for your reply Jonathan.

Could you please share how you implemented this in your code & how you handled log object.

Thanks in advance.

Regards,
Abhijit

Jonathan Herbaut

unread,
May 15, 2018, 9:19:56 AM5/15/18
to seleniu...@googlegroups.com
Here is my code :

private static void SetDataSelectIndex(WebElementValue webElementValue)
{
var element = webElementValue.Element;
var index = (int) webElementValue.Value;
try
{
var select = new SelectElement(element);
if (select.Options.IndexOf(select.SelectedOption) != index)
{
select.SelectByIndex(index);
}
else
{
SeleniumLogger.Instance.Info("SetDataSelect", $"L'index {index} n'impose pas d'action sur le champ [{element.GetId()}]");
}
}
catch (Exception e)
{
SeleniumLogger.Instance.Fatal("SetDataSelect", $"Anomalie sur le champ [{element.GetId()}] sur sélection de l'index {index}", e);
#if DEBUG
if (System.Diagnostics.Debugger.IsAttached) System.Diagnostics.Debugger.Break();
#endif
throw;
}
}

And here is a sample of my SeleniumLogger : 

public class SeleniumLogger
{
private static readonly SiouxSink Sioux = new SiouxSink("TEST", "Selenium");
private static volatile SeleniumLogger s_instance;
private static readonly object s_syncRoot = new Object();
[NotNull]
public static SeleniumLogger Instance
{
get => s_instance ?? CreateLoggerInstance();
set => s_instance = value;
}
[Pure, NotNull]
private static SeleniumLogger CreateLoggerInstance()
{
lock (s_syncRoot)
{
return s_instance ?? (s_instance = new SeleniumLogger());
}
}

public void Fatal(string methodName, string message)
{
Sioux.Fatal(BasicMessage(methodName, message));
}
....
}

--
You received this message because you are subscribed to the Google Groups "Selenium Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to selenium-users+unsubscribe@googlegroups.com.
To post to this group, send email to selenium-users@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/selenium-users/9a51b1a9-b51c-4ee8-ab77-addedc1ff02d%40googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

Abhijit Biradar

unread,
May 16, 2018, 12:13:31 AM5/16/18
to Selenium Users
Thanks a Ton Jonathan. I will try to implement this approach.

Regards,
Abhijit
To unsubscribe from this group and stop receiving emails from it, send an email to selenium-user...@googlegroups.com.
To post to this group, send email to seleniu...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages