我正在尝试为在 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();
}
}