Get the sessionId in c#

已查看 1,113 次
跳至第一个未读帖子

MWQA

未读,
2015年11月25日 05:58:232015/11/25
收件人 Selenium Users
All

I am running tests in saucelabs using RemoteWebDriver and I need to get hold of the sessionId in order to publish the results to my Jenkins job.

I've googled and this seems to be much more difficult than in java, to the point where I can't see that anyone has actually solved it

Can anyone help?

Thanks

Mark

Jim Evans

未读,
2015年11月25日 06:47:222015/11/25
收件人 Selenium Users
This is an example of SauceLabs incorrectly leaking what should be an internal implementation detail. The session ID is deliberately unavailable in the .NET bindings because it's just that, an implementation detail that should not be made available in the abstract case. No, it's not easy, and it's not going to be. SauceLabs is in the wrong here, and should be coming up with an alternative solution.

MWQA

未读,
2015年11月25日 06:49:572015/11/25
收件人 Selenium Users
Accept that Jim - not easy... I have a solution now though in case anyone else needs it...

Actually, I have now solved this using some input from here: http://4byte.cn/question/1068133/get-session-id-for-a-selenium-remotewebdriver-in-c.html


Subclass RemoteWebDriver (seems to be the only way to get the session id):

using OpenQA.Selenium;
using OpenQA.Selenium.Remote;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Specflow_Selenium_PO_Example2.Utils
{
class CustomRemoteDriver : RemoteWebDriver
{
CustomRemoteDriver(ICapabilities desiredCapabilities) : base(desiredCapabilities)
{
}
public CustomRemoteDriver(ICommandExecutor commandExecutor, ICapabilities desiredCapabilities):base(commandExecutor, desiredCapabilities)
{
}
public CustomRemoteDriver(Uri remoteAddress, ICapabilities desiredCapabilities):base(remoteAddress, desiredCapabilities)
{
}
public CustomRemoteDriver(Uri remoteAddress, ICapabilities desiredCapabilities, TimeSpan commandTimeout):base(remoteAddress, desiredCapabilities, commandTimeout)
{
}
public string GetSessionId()
{
return base.SessionId.ToString();
}
}
}

 

I'm using Specflow, so in my Hooks class (and because I execute tests locally as well (this could also just be put into the standard tearDown method for standard selenium tests):

static IWebDriver localDriver;
static CustomRemoteDriver driver;

...

In the teardown method:

...

if (host == "saucelabs")
{
bool passed = TestContext.CurrentContext.Result.Status == TestStatus.Passed;
try
{
// Logs the result to Sauce Labs
((IJavaScriptExecutor)driver).ExecuteScript("sauce:job-result=" + (passed ? "passed" : "failed"));
// Sauce labs results are reported back to the Jenkins job
string message = string.Format("SauceOnDemandSessionID=%1$s job-name=%2$s", driver.GetSessionId().ToString(),"some jobs name");
Console.Write(message);
}
finally
{
driver.Quit();
}

...


@markwinspear 

Krishnan mahadevan

未读,
2015年11月25日 07:31:052015/11/25
收件人 seleniu...@googlegroups.com
Jim

Just curious: Why is this so in .NET ? It would be good if you could help us understand the rationale.

Because as Mark called out its pretty straight forward in Java and this information is useful in finding out other information as well about the tests (for eg., to which node was my test routed to, so that i can get into the box and review the node logs to find out what went wrong ).


-Krishnan Mahadevan

"All the desirable things in life are either illegal, expensive, fattening or in love with someone else"

From: MWQA
Sent: ‎25-‎11-‎2015 17:20
To: Selenium Users
Subject: [selenium-users] Re: Get the sessionId in c#

--
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/c116aad9-817c-4ef1-a499-629ca0665037%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Jim Evans

未读,
2015年11月25日 09:13:372015/11/25
收件人 Selenium Users
I think I already addressed why. The concept of a "session" containing a unique identifier is an implementation detail. It's not required for the primary use case of automating a browser. It's not something the developer should concern themselves with. Exposing the session ID introduces a leak in the abstraction. It is my view that you should not use the session ID, because it ties you to a specific implementation that may change. Thus, it's not exposed. If it were to be exposed by the .NET bindings, I would publicly recommend against using it. If a pull request were to come in exposing it, I would recommend against merging it, and would work to revert it if it were merged by another committer.

Incidentally, as to the "but Java exposes it" argument, .NET is *not* Java. The .NET bindings should not be the Java bindings. There are differing philosophies between the languages. The .NET bindings are intended to be a building block on which to build your solution; not the full end-all-be-all solution itself. This philosophy extends to the base bindings, where things are made available to be subclasses, as well as to the support library (WebDriver.Support.dll), which I from time to time regret creating at all, as users tend to see it as the definitive, canonical solution, instead of suggestions and examples of how to proceed.

Does that answer your question?

MWQA

未读,
2015年11月25日 10:44:462015/11/25
收件人 Selenium Users
Thanks Jim. Is there an alternative solution to mine and Krishnan's use cases?

Krishnan Mahadevan

未读,
2015年11月25日 22:58:152015/11/25
收件人 seleniu...@googlegroups.com
Jim

Thanks for the explanation. 

Yes I agree that :

  • .NET is *not* Java.  
  • The underlying philosophies differ across the different languages. But I was hoping that there would be parity across all of the implementations which is supported by the core WebDriver community, because its still the same product/library/tool/API Toolset, that is going to help end users talk to a browser and run automation tests. Languages can have difference of philosophies, but should the same library built across these different languages also be plagued by the difference of philosophies ? Wouldn’t that cause a diversity in the end-user experience who are trying to consume these APIs ? I would love to hear your outlook on this.
  • The session id is an implementation detail and should not necessarily be a thing of concern to a person who leverages web driver to perform automation. That’s one way of looking at it.

Even though its an implementation detail, I believe this is the basic information that ties a test to a specific browser instance. The JSONWireProtocol is also built around this as a backbone. [ No I am NOT attempting to tell you what you already know and in a much more in-depth level than me. You obviously have a much more in-depth knowledge of both the protocol and the implementation than me ]

The session Id even though is an implementation detail, there is NOT much that an end user can do even if it were to be exposed. An end-user cannot corrupt the state of the web driver by having access to it.  So what is wrong in exposing it ? Even if its implementation detail is going to change, end users are not going to be impacted, because they get only access to the session id, not to the way in which it gets constructed/built etc., Since the JSONWireProtocol seems to have been built with the session Id at its backbone, I believe that a change in the session id is fundamentally going to change a lot of things within the WebDriver world.

Considering the fact that the *Grid* is ONLY available in the Java bindings and not in the .NET bindings [ atleast not to the best of my knowledge ], how do you envision that an end user who is using the .NET bindings and working with the Grid do the following ?

  • Extract information as to which node (Ip and Port) did my test get routed to ?
  • Given that I have the IP and port number of the node to which my test got routed to, I would like to extract the selenium node logs via Http calls.
  • Given that I have the IP and port number of the node to which my test got routed to, I would like to start and stop video recording and even get hold of the recorded video via http calls.

The Grid architecture lets me do all of this, but looks like the .NET bindings is going to hold me back from doing this because I don’t have access to the session id.

How do you propose that we facilitate implementation of all such use cases in the .NET world ? The building block to all the above use cases is that one has to know the session id of one’s current active test.

Please share your thoughts on this and kindly correct me if I have gone wrong anywhere in my understanding.

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.

David

未读,
2015年11月26日 00:46:232015/11/26
收件人 Selenium Users
Perhaps on this discussion, part of the problem lies in extending Selenium Grid beyond its original design intent? Use of nodes in a grid was never to expose to the user which node is running a given test, by design/intent.

If one really wanted that level of control, you sacrifice simple scalability of the grid and just use RemoteWebDriver against standalone server instances (or implement your own version of grid on top of that), or alter the way you define desired capabilities, etc. in conjuction with how you start up nodes so that you will know where the test routes to based on capability specified.

Following the original design/intent, then what Krishnan mentions wanting to do sounds to be out of scope, and require customizations to server and/or client.

Perhaps the better question is how do we make Selenium Grid 2.1+/3.0 formally support those types of use cases without requiring unsupported techniques like extracting session ID, but rather a more natural mechanism that is ok to be exposed/provided by the API.

Those are my thoughts anyhow.

Krishnan Mahadevan

未读,
2015年11月26日 01:20:212015/11/26
收件人 Selenium Users
David

I beg to differ on the intent part.
If Grid was never intended to support any of these things, then those APIs/capabilities would have never existed. 

I believe it was introduced purposefully into the Grid architecture so that the more *advanced* users should be able to do all of this scaling on their own without trying to depend on the Grid codebase to basically provide all of this [ and rightly so because the list of things that a person would want to do with Grid can be endless and it would be a tiring exercise to try and solve everyone's problems without affecting existing functionalities, without bloating the code-base ]

That is why the Grid architecture introduced mechanisms using which a user could :

* Customize how proxies work
* Customize the look and feel of the console itself
* Plug in custom servlets into either the Hub (or) Node for that matter to get various things done.
* Customize how matching should happen [ desired Vs available capabilities ]

What a user does with all of these capabilities is left to the figments of imagination of the users. Remember, none of the capabilities that the Grid exposes requires a user to resorting to hackish ways of getting it done. It's all there made available in a proper manner.

So IMHO, exposing the session Id which is the way using some of this customization can happen doesn't sound that evil, not to forget the fact that by exposing it, none of the internal states of the webdriver implementation gets changed [ which is what one should guard API implementations against ]


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/

Jim Evans

未读,
2015年11月26日 11:46:162015/11/26
收件人 Selenium Users
It seems clear that I should never have gotten involved in open source software. I've discovered over the last five years that I don't like having every design decision I've made be questioned, even after I've provided an explanation. It's also become clear that I expect far too much from people who use my output (It's really *that* hard to create a freaking subclass?! It's really *that* hard to design your own function to pass into WebDriverWait? It's really *that* hard to create your own subclass of DefaultWait<T> so you can use something other than IWebDriver?). If you believe in software designed by "what's popular instead of what's right," elegance and forethought be damned, consider today a victory for your point of view.

I'll add exposing the session ID to the .NET bindings soon, hidden behind a role-based interface (i.e. you'll have to cast your variable). I *won't* be replying to this thread any longer, and my presence on the mailing list will be diminished going forward. I might continue to maintain the .NET bindings and IE driver going forward; I haven't decided on that yet.

To my U.S.-based colleagues on this list, I wish you a Happy Thanksgiving holiday, and I hope you enjoy your time with whomever you decide to spend it. The jury is still out as to whether I will be able to, after this.

Krishnan Mahadevan

未读,
2015年11月26日 21:06:522015/11/26
收件人 Selenium Users
Jim,
My bad. I missed the part wherein you said it can be accessed via Sub Classes. [ Yeah sometimes I tend to get over excited and not read things completely ]. Please take this as one of those instances.

I don't think I am yet there to have notions about software design ! Its a long way to go. So definitely the intentions were not to start an argument. You can imagine it as having to explain something to someone who has problems learning quickly or understanding quickly.

All said and done, I would request you to re-consider your decision on vanishing off from the mailing list. Folks like you are the ones that add a lot of value by sharing not only answers, but also a lot of good insights into the thought process that goes into building something the way it is. Please don't let folks like me stop you from doing that.

Thanks for everything you do for Selenium and my apologies if I irked you! I know pretty well that wasn't my intention. I have a lot of respect for all you folks in the Selenium dev community and aspire to be part of it someday! 

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.

MWQA

未读,
2015年11月27日 03:22:122015/11/27
收件人 Selenium Users
Jim

I echo Krishnan's response.  This thread was more of a discussion on how to best solve a particular problem and was certainly not questioning your work, for which we are all extremely grateful and would be poorer without your valuable contributions, which I sincerely hope will continue.. 

Paul Grandjean - from Home

未读,
2015年11月28日 21:02:592015/11/28
收件人 Selenium Users
Ah Jim, that would be unfortunate, but totally understand one can't do everything.

(yes this is Paul G back on this list after a multi-year hiatus--hope you're well!)
回复全部
回复作者
转发
0 个新帖子