Get Session ID for a Selenium RemoteWebDriver in C#

二次信任 提交于 2020-02-02 02:25:32

问题


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.

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();
    }
} 

回答1:


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; 
   } 
}



回答2:


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());
                }
            }



回答3:


Simply get sessionId: by this

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

here

WebDriver webDriver = null; 

already declared.




回答4:


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");


来源:https://stackoverflow.com/questions/15767066/get-session-id-for-a-selenium-remotewebdriver-in-c-sharp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!