【问题标题】:How come I can cast IWebDriver to IJavaScriptExecutor?为什么我可以将 IWebDriver 转换为 IJavaScriptExecutor?
【发布时间】:2014-05-31 11:30:37
【问题描述】:

谁能解释一下这个“魔法”?

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

怎么来的

(this.Driver as IJavaScriptExecutor).ExecuteScript("console.log('wtf?')");

工作正常吗?

【问题讨论】:

    标签: c# javascript .net inheritance selenium


    【解决方案1】:

    是的,默认实现是这样的,当演员发生时它实际上是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();
    }
    

    【讨论】:

      【解决方案2】:

      这是因为IWebDrivermain implementation implements IJavaScriptExecutor 提供了实现of those methods

      您尚未提供显示如何实例化驱动程序的代码,但它们都继承自 RemoteWebDriver:示例是 ChromeDriver

      【讨论】:

        猜你喜欢
        • 2011-03-31
        • 1970-01-01
        • 1970-01-01
        • 2011-08-11
        • 1970-01-01
        • 1970-01-01
        • 2012-12-30
        • 1970-01-01
        相关资源
        最近更新 更多