C# 为什么我可以将IWebDriver转换为IJavaScriptExecutor?

C# 为什么我可以将IWebDriver转换为IJavaScriptExecutor?,c#,javascript,.net,inheritance,selenium,C#,Javascript,.net,Inheritance,Selenium,有人能解释这个“魔法”吗 public interface IWebDriver : ISearchContext, IDisposable { string CurrentWindowHandle { get; } string PageSource { get; } string Title { get; } string Url { get; set; } ReadOnlyCollection<string> WindowHandles

有人能解释这个“魔法”吗

public interface IWebDriver : ISearchContext, IDisposable
{
    string CurrentWindowHandle { get; }
    string PageSource { get; }
    string Title { get; }
    string Url { get; set; }
    ReadOnlyCollection<string> WindowHandles { get; }

    void Close();
    IOptions Manage();
    INavigation Navigate();
    void Quit();
    ITargetLocator SwitchTo();
}

public interface ISearchContext
{
    IWebElement FindElement(By by);
    ReadOnlyCollection<IWebElement> FindElements(By by);
}

public interface IJavaScriptExecutor
{
    object ExecuteAsyncScript(string script, params object[] args);
    object ExecuteScript(string script, params object[] args);
}
工作正常吗?

这是因为IWebDriver的IJavaScriptExecutor提供了实现


您没有提供显示如何实例化驱动程序的代码,但它们都继承自
RemoteWebDriver
:示例是是的,默认实现是这样的,当cast发生时,它实际上是
IWebDriver
->
ChromeDriver
->
RemoteWebDriver
->
IJavaScriptExecutor

public class RemoteWebDriver : IWebDriver, IDisposable, ISearchContext, IJavaScriptExecutor, IFindsById, IFindsByClassName, IFindsByLinkText, IFindsByName, IFindsByTagName, IFindsByXPath, IFindsByPartialLinkText, IFindsByCssSelector, IHasInputDevices, IHasCapabilities, IAllowsFileDetection
{
    protected static readonly TimeSpan DefaultCommandTimeout;

    public RemoteWebDriver(ICapabilities desiredCapabilities);
    public RemoteWebDriver(ICommandExecutor commandExecutor, ICapabilities desiredCapabilities);
    public RemoteWebDriver(Uri remoteAddress, ICapabilities desiredCapabilities);
    public RemoteWebDriver(Uri remoteAddress, ICapabilities desiredCapabilities, TimeSpan commandTimeout);

    public ICapabilities Capabilities { get; }
    protected ICommandExecutor CommandExecutor { get; }
    public string CurrentWindowHandle { get; }
    public virtual IFileDetector FileDetector { get; set; }
    public IKeyboard Keyboard { get; }
    public IMouse Mouse { get; }
    public string PageSource { get; }
    protected SessionId SessionId { get; }
    public string Title { get; }
    public string Url { get; set; }
    public ReadOnlyCollection<string> WindowHandles { get; }

    public void Close();
    protected virtual RemoteWebElement CreateElement(string elementId);
    public void Dispose();
    protected virtual void Dispose(bool disposing);
    protected virtual Response Execute(string driverCommandToExecute, Dictionary<string, object> parameters);
    public object ExecuteAsyncScript(string script, params object[] args);
    public object ExecuteScript(string script, params object[] args);
    public IWebElement FindElement(By by);
    protected IWebElement FindElement(string mechanism, string value);
    public IWebElement FindElementByClassName(string className);
    public IWebElement FindElementByCssSelector(string cssSelector);
    public IWebElement FindElementById(string id);
    public IWebElement FindElementByLinkText(string linkText);
    public IWebElement FindElementByName(string name);
    public IWebElement FindElementByPartialLinkText(string partialLinkText);
    public IWebElement FindElementByTagName(string tagName);
    public IWebElement FindElementByXPath(string xpath);
    public ReadOnlyCollection<IWebElement> FindElements(By by);
    protected ReadOnlyCollection<IWebElement> FindElements(string mechanism, string value);
    public ReadOnlyCollection<IWebElement> FindElementsByClassName(string className);
    public ReadOnlyCollection<IWebElement> FindElementsByCssSelector(string cssSelector);
    public ReadOnlyCollection<IWebElement> FindElementsById(string id);
    public ReadOnlyCollection<IWebElement> FindElementsByLinkText(string linkText);
    public ReadOnlyCollection<IWebElement> FindElementsByName(string name);
    public ReadOnlyCollection<IWebElement> FindElementsByPartialLinkText(string partialLinkText);
    public ReadOnlyCollection<IWebElement> FindElementsByTagName(string tagName);
    public ReadOnlyCollection<IWebElement> FindElementsByXPath(string xpath);
    public IOptions Manage();
    public INavigation Navigate();
    public void Quit();
    protected virtual void StartClient();
    protected void StartSession(ICapabilities desiredCapabilities);
    protected virtual void StopClient();
    public ITargetLocator SwitchTo();
}
公共类RemoteWebDriver:IWebDriver、IDisposable、ISearchContext、IJavaScriptExecutor、IFindsById、IFindsByClassName、iFindsByLink、IFindsByName、IFindsByTagName、iFindsByPath、iFindsByPartialLink、iFindsByCSS选择器、IHasInputDevices、IHasCapabilities、iLowsFileDetection
{
受保护的静态只读TimeSpan DefaultCommandTimeout;
公共RemoteWebDriver(ICapabilities desiredCapabilities);
公共RemoteWebDriver(ICommandExecutor命令执行器,ICapabilities desiredCapabilities);
公共RemoteWebDriver(Uri remoteAddress,ICapabilities desiredCapabilities);
公共RemoteWebDriver(Uri remoteAddress、ICapabilities desiredCapabilities、TimeSpan commandTimeout);
公共ICapabilities功能{get;}
受保护的ICommandExecutor命令执行器{get;}
公共字符串CurrentWindowHandle{get;}
公共虚拟IFileDetector文件检测器{get;set;}
公共IKeyboard键盘{get;}
公共IMouse鼠标{get;}
公共字符串PageSource{get;}
受保护的SessionId SessionId{get;}
公共字符串标题{get;}
公共字符串Url{get;set;}
公共只读集合窗口句柄{get;}
公共空间关闭();
受保护的虚拟RemoteWebElement CreateElement(字符串elementId);
公开无效处置();
受保护的虚拟无效处置(bool处置);
受保护的虚拟响应执行(字符串驱动器CommandToExecute、字典参数);
公共对象ExecuteAsyncScript(字符串脚本,参数对象[]args);
公共对象ExecuteScript(字符串脚本,参数对象[]args);
公共IWebElement FindElement(由);
受保护的IWebElement FindElement(字符串机制、字符串值);
公共IWebElement FindElementByClassName(字符串类名称);
公共IWebElement FindElementByCssSelector(字符串cssSelector);
公共IWebElement FindElementById(字符串id);
公共IWebElement FindElementByLinkText(字符串linkText);
公共IWebElement FindElementByName(字符串名称);
公共IWebElement FindElementByPartialLinkText(字符串partialLinkText);
公共IWebElement FindElementByTagName(字符串标记名);
公共IWebElement FindElementByXPath(字符串xpath);
公共只读集合findelegions(By);
受保护的只读集合FindElements(字符串机制、字符串值);
public ReadOnlyCollection FindElementsByClassName(字符串类名称);
公共只读集合findelementsbycsselector(字符串cssSelector);
public ReadOnlyCollection findelementsbyd(字符串id);
public ReadOnlyCollection FindElementsByLinkText(字符串linkText);
public ReadOnlyCollection FindElementsByName(字符串名称);
public ReadOnlyCollection FindElementsByPartialLinkText(字符串partialLinkText);
public ReadOnlyCollection FindElementsByTagName(字符串标记名);
公共只读集合FindElementsByXPath(字符串xpath);
公共事务管理();
公众缺乏活力;
公开无效退出();
受保护的虚拟void StartClient();
受保护的无效启动会话(ICapabilities desiredCapabilities);
受保护的虚拟void StopClient();
公共ITargetLocator切换到();
}
public class RemoteWebDriver : IWebDriver, IDisposable, ISearchContext, IJavaScriptExecutor, IFindsById, IFindsByClassName, IFindsByLinkText, IFindsByName, IFindsByTagName, IFindsByXPath, IFindsByPartialLinkText, IFindsByCssSelector, IHasInputDevices, IHasCapabilities, IAllowsFileDetection
{
    protected static readonly TimeSpan DefaultCommandTimeout;

    public RemoteWebDriver(ICapabilities desiredCapabilities);
    public RemoteWebDriver(ICommandExecutor commandExecutor, ICapabilities desiredCapabilities);
    public RemoteWebDriver(Uri remoteAddress, ICapabilities desiredCapabilities);
    public RemoteWebDriver(Uri remoteAddress, ICapabilities desiredCapabilities, TimeSpan commandTimeout);

    public ICapabilities Capabilities { get; }
    protected ICommandExecutor CommandExecutor { get; }
    public string CurrentWindowHandle { get; }
    public virtual IFileDetector FileDetector { get; set; }
    public IKeyboard Keyboard { get; }
    public IMouse Mouse { get; }
    public string PageSource { get; }
    protected SessionId SessionId { get; }
    public string Title { get; }
    public string Url { get; set; }
    public ReadOnlyCollection<string> WindowHandles { get; }

    public void Close();
    protected virtual RemoteWebElement CreateElement(string elementId);
    public void Dispose();
    protected virtual void Dispose(bool disposing);
    protected virtual Response Execute(string driverCommandToExecute, Dictionary<string, object> parameters);
    public object ExecuteAsyncScript(string script, params object[] args);
    public object ExecuteScript(string script, params object[] args);
    public IWebElement FindElement(By by);
    protected IWebElement FindElement(string mechanism, string value);
    public IWebElement FindElementByClassName(string className);
    public IWebElement FindElementByCssSelector(string cssSelector);
    public IWebElement FindElementById(string id);
    public IWebElement FindElementByLinkText(string linkText);
    public IWebElement FindElementByName(string name);
    public IWebElement FindElementByPartialLinkText(string partialLinkText);
    public IWebElement FindElementByTagName(string tagName);
    public IWebElement FindElementByXPath(string xpath);
    public ReadOnlyCollection<IWebElement> FindElements(By by);
    protected ReadOnlyCollection<IWebElement> FindElements(string mechanism, string value);
    public ReadOnlyCollection<IWebElement> FindElementsByClassName(string className);
    public ReadOnlyCollection<IWebElement> FindElementsByCssSelector(string cssSelector);
    public ReadOnlyCollection<IWebElement> FindElementsById(string id);
    public ReadOnlyCollection<IWebElement> FindElementsByLinkText(string linkText);
    public ReadOnlyCollection<IWebElement> FindElementsByName(string name);
    public ReadOnlyCollection<IWebElement> FindElementsByPartialLinkText(string partialLinkText);
    public ReadOnlyCollection<IWebElement> FindElementsByTagName(string tagName);
    public ReadOnlyCollection<IWebElement> FindElementsByXPath(string xpath);
    public IOptions Manage();
    public INavigation Navigate();
    public void Quit();
    protected virtual void StartClient();
    protected void StartSession(ICapabilities desiredCapabilities);
    protected virtual void StopClient();
    public ITargetLocator SwitchTo();
}