7

I'm trying to get a session id for a test being run on the SauceLabs cloud, but I can't seem to access it.

I've tried the following approaches:

//Returns null
var sessionId = (string)((RemoteWebDriver)driver).Capabilities.GetCapability("webdriver.remote.sessionid");

//Will not compile
sessionId = ((RemoteWebDriver)driver).SessionId; //This is protected. 

The second approach is particularly confusing. It's a protected property, but if I can only access this from a derived class, then it's essentially useless for what I need.

Session ID

Any help is appreciated.

In order for this to work, I had to create a class derived from RemoteWebDriver and then define a getter method. For example:

class  CustomeRemoteDriver : RemoteWebDriver
{

    public CustomeRemoteDriver(ICapabilities desiredCapabilities):base(desiredCapabilities)
    {
    }

    public CustomeRemoteDriver(ICommandExecutor commandExecutor, ICapabilities desiredCapabilities):base(commandExecutor, desiredCapabilities)
    {
    }

    public CustomeRemoteDriver(Uri remoteAddress, ICapabilities desiredCapabilities):base(remoteAddress, desiredCapabilities)
    {
    }

    public CustomeRemoteDriver(Uri remoteAddress, ICapabilities desiredCapabilities, TimeSpan commandTimeout):base(remoteAddress, desiredCapabilities, commandTimeout)
    {
    }

    public string GetSessionId()
    {
        return base.SessionId.ToString();
    }
} 
elucid8
  • 1,312
  • 2
  • 17
  • 39
  • 1
    it looks almost like a `Casting` issue can you post what the actual error is it's hard to see the little image you have posted – MethodMan Apr 02 '13 at 14:45
  • here is a guide that is online that may help you out .. also can you show how this Class is defined I may be able to provide you with an Idea but need to see the Class and how it's defined for your RemoteWebDriver..how are you inheriting this http://testingbot.com/support/getting-started/csharp.html – MethodMan Apr 02 '13 at 14:54
  • So, should I just create a derived class to cast my RemoteWebDriver to and then access the session id with a getter method? – elucid8 Apr 02 '13 at 14:56
  • Something like this `public CustomRemoteDriver(Uri uri, DesiredCapabilities capabilities) : base(uri, capabilities) { } public SessionId getExecutionID() { return ((CustomRemoteDriver)Driver.Browser.driver).SessionId; } }` – MethodMan Apr 02 '13 at 15:00
  • Yeah, I just did that. It works, but it's needlessly circuitous. Go ahead and add an answer and I'll give you the upvote. – elucid8 Apr 02 '13 at 15:04
  • Whilst I am glad you got your answer, why do you need access to the session ID? – Arran Apr 02 '13 at 15:12
  • Cool I will add the example – MethodMan Apr 02 '13 at 15:20
  • Arran, I'm using SauceLabs and in order to make pass/fail assertions I need to get the session/job id to build requests to submit to their REST API. There are other flags that can only be set using this method as well. – elucid8 Apr 02 '13 at 15:35

5 Answers5

4

Could also reach in using reflection.

            var sessionIdProperty = typeof(RemoteWebDriver).GetProperty("SessionId", BindingFlags.Instance | BindingFlags.NonPublic);
            if (sessionIdProperty != null)
            {
                SessionId sessionId = sessionIdProperty.GetValue(driver, null) as SessionId;
                if (sessionId == null)
                {
                    Trace.TraceWarning("Could not obtain SessionId.");
                }
                else
                {
                    Trace.TraceInformation("SessionId is " + sessionId.ToString());
                }
            }
Kenn
  • 2,269
  • 2
  • 28
  • 36
2

An Example of what you could do

class  CustomeRemoteDriver : RemoteWebDriver
{    
    public CustomRemoteDriver(Uri uri, DesiredCapabilities capabilities) 
    : base(uri, capabilities)
    { 
    } 

    public SessionId getExecutionID() 
   { 
      return ((CustomRemoteDriver)Driver.Browser.driver).SessionId; 
   } 
}
MethodMan
  • 17,665
  • 6
  • 32
  • 52
2

Simply get sessionId: by this

String sessionId = ((RemoteWebDriver)webDriver).getSessionId().toString();

here

WebDriver webDriver = null; 

already declared.

1

I am using selenium-dotnet-2.48.0 and this is working just fine:

string sessionId = ((RemoteWebDriver)driver).Capabilities.GetCapability("webdriver.remote.sessionid").ToString(); 

Make sure you set video to True:

DesiredCapabilities capability = DesiredCapabilities.Firefox();
...
capability.SetCapability("video", "True");
Kris Krause
  • 7,134
  • 2
  • 20
  • 26
0

using Selenium.WebDriver Version="3.141.0", this is working:

string sessionId = ((RemoteWebDriver)Driver).SessionId.ToString();
M.Hassan
  • 7,947
  • 4
  • 45
  • 67