任何人都可以解释这个'魔术'?
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?')");
工作正常吗?
答案 0 :(得分:2)
这是因为IWebDriver
main implementation IJavaScriptExecutor
implements因此提供了实施of those methods。
您尚未提供显示实例化驱动程序的代码,但它们都是从RemoteWebDriver
继承的:示例是ChromeDriver
答案 1 :(得分:0)
是的默认实现就是这样,当发生强制转换时,它实际上是IWebDriver
- &gt; ChromeDriver
- &gt; RemoteWebDriver
- &gt; 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();
}