获取会话ID的硒RemoteWebDriver在C#(Get Session ID for a Se

2019-08-19 01:14发布

我试图获得正对SauceLabs云中运行一个测试会话ID,但我似乎无法访问它。

我尝试以下方法:

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

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

第二种方法是特别混乱。 这是一个受保护的财产,但如果我只能从派生类访问它,那么它是什么,我需要基本上是无用的。

任何帮助表示赞赏。

为了使这一工作,我不得不创建派生的类RemoteWebDriver ,然后定义一个getter方法。 例如:

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

Answer 1:

你可以做一个例子

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

    public SessionId getExecutionID() 
   { 
      return ((CustomRemoteDriver)Driver.Browser.driver).SessionId; 
   } 
}


Answer 2:

也可以达到使用反射。

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


Answer 3:

只是得到的sessionId:此

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

这里

WebDriver webDriver = null; 

已经声明。



Answer 4:

我使用的selenium-dotnet-2.48.0 ,这是工作得很好:

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

请确保您设置视频为True:

DesiredCapabilities capability = DesiredCapabilities.Firefox();
...
capability.SetCapability("video", "True");


文章来源: Get Session ID for a Selenium RemoteWebDriver in C#