Get Session ID for a Selenium RemoteWebDriver in C#
Asked Answered
F

5

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();
    }
} 
Frequentative answered 2/4, 2013 at 14:42 Comment(8)
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 postedMordent
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 testingbot.com/support/getting-started/csharp.htmlMordent
So, should I just create a derived class to cast my RemoteWebDriver to and then access the session id with a getter method?Frequentative
Something like this public CustomRemoteDriver(Uri uri, DesiredCapabilities capabilities) : base(uri, capabilities) { } public SessionId getExecutionID() { return ((CustomRemoteDriver)Driver.Browser.driver).SessionId; } }Mordent
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.Frequentative
Whilst I am glad you got your answer, why do you need access to the session ID?Justin
Cool I will add the exampleMordent
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.Frequentative
M
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; 
   } 
}
Mordent answered 2/4, 2013 at 15:22 Comment(0)
I
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());
                }
            }
Illume answered 26/6, 2013 at 7:39 Comment(0)
M
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; 
   } 
}
Mordent answered 2/4, 2013 at 15:22 Comment(0)
S
2

Simply get sessionId: by this

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

here

WebDriver webDriver = null; 

already declared.

Squinch answered 12/9, 2013 at 10:24 Comment(1)
This doesn't work, as the RemoteWebDriver doesn't expose this methodMelesa
T
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");
Torrential answered 30/11, 2015 at 18:31 Comment(0)
P
0

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

string sessionId = ((RemoteWebDriver)Driver).SessionId.ToString();
Promptitude answered 23/11, 2020 at 2:9 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.