Custom Servlet Invoke in Selenium GRID

1,012 views
Skip to first unread message

Tanmay Ghorai

unread,
Feb 21, 2015, 10:27:45 AM2/21/15
to seleniu...@googlegroups.com
Hi,

Is anyone know how to invoke Custom Servlet when a a Browser request comes to GRID. Is there any WEB.xml with the server Jar which I need to modify? I cant locate any.

Regards
Tanmay

Krishnan Mahadevan

unread,
Feb 22, 2015, 10:28:24 AM2/22/15
to Selenium Users
Tanmay,
The only thing you can do with the Grid's custom servlets is to add additional behavior to the Grid.
The Grid's servlet wouldnt be the right way of getting to know of every browser request that is fired. You would ideally speaking want to use a Proxy server such as BrowserMob Proxy or Fiddler for those sort of things.

If this is not what you are looking for, can you please help elaborate your use case ?



Thanks & Regards
Krishnan Mahadevan

"All the desirable things in life are either illegal, expensive, fattening or in love with someone else!"
My Scribblings @ http://wakened-cognition.blogspot.com/
My Technical Scribbings @ http://rationaleemotions.wordpress.com/

--
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-user...@googlegroups.com.
To post to this group, send email to seleniu...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/selenium-users/02b3571b-edcc-4338-890c-98e93ea8019e%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Tanmay Ghorai

unread,
Feb 22, 2015, 9:55:33 PM2/22/15
to seleniu...@googlegroups.com
Hi Krishnan,

Thank you so much for responding in my question.
 you suggested to use : BrowserMob Proxy or Fiddler.

I am new to this area. I need little more help. Appreciate your help and patience .

I am building a Selenium Grid in my company. what we want to do is : we want to control how many requests one app team can do at any given time. The way we are thinking is we should give a unique token to each app team and when they request they will provide their token and ask for browser session. we will authenticate whether the app and token matching then we will allow the session and at max 2 or 3 we might want to restrict, so that no one can abuse our grid system by sending many request session.

Any help is appreciated.

Regards

krishnan.ma...@gmail.com

unread,
Feb 22, 2015, 11:40:00 PM2/22/15
to seleniu...@googlegroups.com
Tanmay

Thanks for elaborating your use case. This can definitely be done with a custom remote proxy. 

Take a look at what we did with SeLion  (http://selion.io) for a custom proxy 


You basically need to refer to the getNewSesssion method. 


you might also want to take a look at this blog post of mine to learn how to go about plugging in your custom proxy into the grid. 



- Krishnan

This message may include free added typos, which you may use for your entertainment.
Courtesy : my SmartPhone!
_____________________________
From:Tanmay Ghorai
Subject:Re: [selenium-users] Custom Servlet Invoke in Selenium GRID
To:seleniu...@googlegroups.com

Tanmay Ghorai

unread,
Feb 24, 2015, 1:09:43 AM2/24/15
to seleniu...@googlegroups.com
Hi Krishnan,

I think it is getting little overwhelming to me.  Let me try to explain my use case. My apologies as I was not very clear at first place.

The grid is going to be used by many dev team. we want to restrict from mis-use. we are thinking we will provision a unique token id for each app. and allow max 5 browser sessions( as example) at a time. So when a Browser request comes to the Grid..we want to invoke our custom Servlet which will first validate whether the app id and token id are matching and will count whether the app is exhausted 5 browser sessions already, if not we will provision. Something like this.

I am in a very basic confusion - how I can invoke my custom servlet before the GRID gives the session, how I can customize the GRID? I tried to test your MyCustomServlet.java when I try to launch, I am getting below error :

tghorai$ java -cp *:. org.openqa.grid.selenium.GridLauncher -role hub -servlets org.openqa.demo.MyConsoleServlet
22:05:58.863 INFO - Launching a selenium grid server
Exception in thread "main" java.lang.NoClassDefFoundError: org/json/JSONException
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Class.java:260)
    at org.openqa.grid.web.utils.ExtraServletUtil.createServlet(ExtraServletUtil.java:38)
    at org.openqa.grid.web.Hub.<init>(Hub.java:94)
    at org.openqa.grid.selenium.GridLauncher.main(GridLauncher.java:103)
Caused by: java.lang.ClassNotFoundException: org.json.JSONException
    at java.net.URLClassLoader$1.run(URLClassLoader.java:372)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:360)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
    ... 5 more

Appreciate any help.

Regards
Tanmay

On Sunday, February 22, 2015 at 8:40:00 PM UTC-8, Krishnan wrote:
Tanmay

Thanks for elaborating your use case. This can definitely be done with a custom remote proxy. 

Take a look at what we did with SeLion  (http://selion.io) for a custom proxy 


You basically need to refer to the getNewSesssion method. 


you might also want to take a look at this blog post of mine to learn how to go about plugging in your custom proxy into the grid. 



- Krishnan

This message may include free added typos, which you may use for your entertainment.
Courtesy : my SmartPhone!
_____________________________
From:Tanmay Ghorai
Subject:Re: [selenium-users] Custom Servlet Invoke in Selenium GRID

Tanmay Ghorai

unread,
Feb 24, 2015, 1:14:21 AM2/24/15
to seleniu...@googlegroups.com
Hi Krishnan,

Please ignore the last part of my query - I could fix the exception by fixing classpath. But still my other questions are still outstanding. Appreciate your help.

Regards
Tanmay

Krishnan Mahadevan

unread,
Feb 24, 2015, 4:50:33 AM2/24/15
to Selenium Users
Tanmay,

As of Selenium 2.44, the codebase has stopped using org.json for its needs and instead relies on using google gson libraries for doing the same. So you may have to refactor the sample code snippet i shared in my blogs to get it to work.

Now coming back to the second part of your query.

On a high level here's what you would need to do :

1. Create a servlet at the Grid level that is going to have all the checks and balances in place wherein if it is queried with a token it would either return true (if the number of concurrent sessions for the given token is within the permissible value) and false if otherwise.
2. Inject this servlet into the grid as mentioned in my blog.
3. Now create the custom proxy wherein the proxy's newSession() would basically query the servlet for the current status [ depending on whether your servlet responds to a GET or a POST ] and if the servlet responds with a true, it would proceed further with a session creation and if false it would return back a null session. Invoking the servlet would be merely doing a GET/POST to http://localhost:4444/grid/admin/<ServletName>

You actually dont need a custom servlet and you can achieve the same thing using a utility class that step (3) can query as well.







Thanks & Regards
Krishnan Mahadevan

"All the desirable things in life are either illegal, expensive, fattening or in love with someone else!"
My Scribblings @ http://wakened-cognition.blogspot.com/
My Technical Scribbings @ http://rationaleemotions.wordpress.com/

Tanmay Ghorai

unread,
Feb 24, 2015, 1:02:37 PM2/24/15
to seleniu...@googlegroups.com
Hi Krishnan,

Thanks a lot.
I added a CustomProxy. But while I am running : java -cp *:. org.openqa.grid.selenium.GridLauncher -role hub -servlets org.openqa.demo.CustomRemoteProxy


I am getting below error:

09:45:33.329 INFO - Launching a selenium grid server
Exception in thread "main" java.lang.ClassCastException: class org.openqa.demo.CustomRemoteProxy
    at java.lang.Class.asSubclass(Class.java:3396)

    at org.openqa.grid.web.utils.ExtraServletUtil.createServlet(ExtraServletUtil.java:38)
    at org.openqa.grid.web.Hub.<init>(Hub.java:94)

I think it cant be -servlets option..is there anything I am missing ?

Truly appreciate your help.

Regards
Tanmay

Tanmay Ghorai

unread,
Feb 24, 2015, 1:11:06 PM2/24/15
to seleniu...@googlegroups.com
Please never mind :) I could figure out..Thanks a lot !!!!


On Saturday, February 21, 2015 at 7:27:45 AM UTC-8, Tanmay Ghorai wrote:

Tanmay Ghorai

unread,
Feb 24, 2015, 3:38:31 PM2/24/15
to seleniu...@googlegroups.com
Hi Krishnan,

I have injected the Proxy. it worked fine. Thank you so much  :)

Now my challenge is how to pass my custom parameters like 'app id' and 'Token id'.  Can I do like : driver = new RemoteWebDriver(new URL("http://localhost:4444/wd/hub?param1=value1"),browser); wondering if this is the approach and also how to capture the parameters in my Proxy class ?

Regards
Tanmay

Krishnan Mahadevan

unread,
Feb 24, 2015, 11:22:46 PM2/24/15
to Selenium Users
Tanmay,
I dont think you can pass it via the URL for the hub. One of the ways in which you can pass this custom parameters is via the DesiredCapabilities object because it is internally a Map.
So in the newSession() method you would have access to the capabilities that were requested by the test client and thus have access to all of this tokens etc.,

So in a nutshell you would do something like this :

URL url = new URL("http://localhost:4444/wd/hub");
DesiredCapabilities dc = new DesiredCapabilities();
dc.setCapability("authToken", "foo");





Thanks & Regards
Krishnan Mahadevan

"All the desirable things in life are either illegal, expensive, fattening or in love with someone else!"
My Scribblings @ http://wakened-cognition.blogspot.com/
My Technical Scribbings @ http://rationaleemotions.wordpress.com/

--
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-user...@googlegroups.com.
To post to this group, send email to seleniu...@googlegroups.com.

Tanmay Ghorai

unread,
Feb 25, 2015, 6:45:08 PM2/25/15
to seleniu...@googlegroups.com
Hi Krishnan,

Thank you so much. It truly did worked very well.  Now it is validating against app id and token id from  a master data stored in Database and I am passing session null for invalid app/token.

But I am noticing that even though we are returning session as null for invalid app and token combination, it keeps trying to get new session - (getNewSession() method is getting called indefinite times)

Also how to capture start time and end time of a given session as we want to log every session's life into our Database so that at any point of time we want to restrict by giving 5-6 active sessions per app from the grid.

Regards
Tanmay





Now I am trying to restrict number of parallel sessions can be allowed to a specific Appid.  What we are thinking is we want to allow session and log in a DB ( how long a session was active - start time, end time ). 

On Saturday, February 21, 2015 at 7:27:45 AM UTC-8, Tanmay Ghorai wrote:

Tanmay Ghorai

unread,
Feb 26, 2015, 8:42:56 AM2/26/15
to seleniu...@googlegroups.com
Hi Friends,

Appreciate any experience or suggestion on this.

Regards
Tanmay


On Saturday, February 21, 2015 at 7:27:45 AM UTC-8, Tanmay Ghorai wrote:
Reply all
Reply to author
Forward
0 new messages