Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get Session ID for a Selenium RemoteWebDriver in C#

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();
    }
} 
like image 709
elucid8 Avatar asked Apr 02 '13 14:04

elucid8


People also ask

How do I find my session ID in selenium?

We can get the webdriver session id with Selenium webdriver using the SessionId class. A session id is a distinctive number that is given to the webdriver by the server. This number is utilized by the webdriver to establish communication with the browser.

How do I use selenium RemoteWebDriver?

Selenium RemoteWebDriver Installation Once the Selenium grid is installed, follow the steps below to configure the Selenium RemoteWebdriver. Open a web browser and navigate to http://localhost:4444/grid/console. The grid console page will be visible. Connect to Selenium Hub to execute test cases.

How do I find my Appium session ID?

When you connect to your mobile device, a new Appium session starts. You can get the ID of your Appium session to use it later in your mobile test automation. Use the Module Get Appium Session Id to save the Appium session ID to a buffer.

What is invalid session ID in selenium?

The invalid session ID error is a WebDriver error that occurs when the server does not recognize the unique session identifier. This happens if the session has been deleted or if the session ID is invalid.


2 Answers

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());
                }
            }
like image 137
Kenn Avatar answered Sep 21 '22 18:09

Kenn


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; 
   } 
}
like image 26
MethodMan Avatar answered Sep 23 '22 18:09

MethodMan