public static IWebDriver ToDriver(this ISearchContext context) { IWrapsDriver wrapsDriver = context as IWrapsDriver; if (wrapsDriver == null) { try { return((IWebDriver)context); } catch (InvalidCastException) { FieldInfo field = context.GetType().GetField("underlyingElement", BindingFlags.Instance | BindingFlags.NonPublic); if (field == (FieldInfo)null) { return((IWebDriver)null); } wrapsDriver = field.GetValue((object)context) as IWrapsDriver; if (wrapsDriver == null) { throw new ArgumentException("Element must wrap a web driver", nameof(context)); } } } return(wrapsDriver.WrappedDriver); }
/// <summary> /// Initializes a new instance of the <see cref="TouchActions"/> class. /// </summary> /// <param name="driver">The <see cref="IWebDriver"/> object on which the actions built will be performed.</param> public TouchActions(IWebDriver driver) : base(driver) { IHasTouchScreen touchScreenDriver = driver as IHasTouchScreen; if (touchScreenDriver == null) { IWrapsDriver wrapper = driver as IWrapsDriver; while (wrapper != null) { touchScreenDriver = wrapper.WrappedDriver as IHasTouchScreen; if (touchScreenDriver != null) { break; } wrapper = wrapper.WrappedDriver as IWrapsDriver; } } if (touchScreenDriver == null) { throw new ArgumentException("The IWebDriver object must implement or wrap a driver that implements IHasTouchScreen.", "driver"); } this.touchScreen = touchScreenDriver.TouchScreen; }
// Find many elements public override ReadOnlyCollection <IWebElement> FindElements(ISearchContext context) { // Create script arguments object[] scriptArgs = new object[this.args.Length + 1]; scriptArgs[0] = this.RootElement; Array.Copy(this.args, 0, scriptArgs, 1, this.args.Length); // Get JS executor IJavaScriptExecutor jsExecutor = context as IJavaScriptExecutor; if (jsExecutor == null) { IWrapsDriver wrapsDriver = context as IWrapsDriver; if (wrapsDriver != null) { jsExecutor = wrapsDriver.WrappedDriver as IJavaScriptExecutor; } } if (jsExecutor == null) { throw new NotSupportedException("Could not get an IJavaScriptExecutor instance from the context."); } // Send script, arguments array over the wire https://code.google.com/p/selenium/wiki/JsonWireProtocol#/session/:sessionId/execute ReadOnlyCollection <IWebElement> elements = jsExecutor.ExecuteScript(this.script, scriptArgs) as ReadOnlyCollection <IWebElement>; if (elements == null) { elements = new ReadOnlyCollection <IWebElement>(new List <IWebElement>(0)); } return(elements); }
/// <summary> /// Finds all elements matching the criteria. /// </summary> /// <param name="context"> /// An <see cref="ISearchContext"/> object to use to search for the elements. /// </param> /// <returns> /// A collection of all <see cref="OpenQA.Selenium.IWebElement"/> matching the current criteria, /// or an empty list if nothing matches. /// </returns> public override ReadOnlyCollection <IWebElement> FindElements(ISearchContext context) { // Create script arguments object[] scriptArgs = new object[this.args.Length + 1]; scriptArgs[0] = this.RootElement; Array.Copy(this.args, 0, scriptArgs, 1, this.args.Length); // Get JS executor IJavaScriptExecutor jsExecutor = context as IJavaScriptExecutor; if (jsExecutor == null) { IWrapsDriver wrapsDriver = context as IWrapsDriver; if (wrapsDriver != null) { jsExecutor = wrapsDriver.WrappedDriver as IJavaScriptExecutor; } } if (jsExecutor == null) { throw new NotSupportedException("Could not get an IJavaScriptExecutor instance from the context."); } ReadOnlyCollection <IWebElement> elements = jsExecutor.ExecuteScript(this.script, scriptArgs) as ReadOnlyCollection <IWebElement>; if (elements == null) { elements = new ReadOnlyCollection <IWebElement>(new List <IWebElement>(0)); } return(elements); }
private IJavaScriptExecutor GetExecutor(ISearchContext context) { IJavaScriptExecutor executor = context as IJavaScriptExecutor; if (executor != null) { return(executor); } IWrapsDriver current = context as IWrapsDriver; while (current != null) { IWebDriver driver = current.WrappedDriver; executor = driver as IJavaScriptExecutor; if (executor != null) { break; } current = driver as IWrapsDriver; } if (executor == null) { throw new ArgumentException("Search context must support JavaScript or IWrapsDriver where the wrappted driver supports JavaScript", nameof(context)); } return(executor); }
/// <summary> /// Initializes a new instance of the <see cref = "Actions"/> class. /// </summary> /// <param name = "driver">The <see cref = "IWebDriver"/> object on which the actions built will be performed.</param> public Actions(IWebDriver driver) { this.driver = driver; IHasInputDevices inputDevicesDriver = driver as IHasInputDevices; if (inputDevicesDriver == null) { IWrapsDriver wrapper = driver as IWrapsDriver; while (wrapper != null) { inputDevicesDriver = wrapper.WrappedDriver as IHasInputDevices; if (inputDevicesDriver != null) { this.driver = wrapper.WrappedDriver; break; } wrapper = wrapper.WrappedDriver as IWrapsDriver; } } if (inputDevicesDriver == null) { throw new ArgumentException("The IWebDriver object must implement or wrap a driver that implements IHasInputDevices.", "driver"); } this.keyboard = inputDevicesDriver.Keyboard; this.mouse = inputDevicesDriver.Mouse; }
private static IWebDriver GetIWebDriver(this IWebElement webElement) { IWrapsDriver wrapsDriver = webElement as IWrapsDriver; if (wrapsDriver == null) throw new ArgumentException("element", "Element must wrap a web driver"); IWebDriver webDriver = wrapsDriver.WrappedDriver; return webDriver; }
public static IWrapsDriver GetWrapsDriver(this IWebElement element) { IWrapsDriver wrappedDriver = element as IWrapsDriver; if (wrappedDriver == null) { throw new Exception("Could not retrive IWebDriver from current IWebElement instance"); } return(wrappedDriver); }
//trigger after every step of the scenario to capture screenshot of error public void AfterStep() { IWrapsDriver wrapperAccess = (IWrapsDriver)objectContainer.Resolve <IWebDriver>(); IWebDriver driver = wrapperAccess.WrappedDriver; if (scenarioContext.TestError != null) { ScreenshotHelper screenshotTools = new ScreenshotHelper(driver); screenshotTools.SnapFullScreenshot(featureContext.FeatureInfo.Title, scenarioContext.ScenarioInfo.Title); } }
public void SetupFlutterMobileTest() { IWrapsDriver wrapsDriver = WebDriver as IWrapsDriver; var remoteWebDriver = wrapsDriver?.WrappedDriver as RemoteWebDriver; if (null == remoteWebDriver) { throw new System.InvalidOperationException($"FlutterDriver is intended to wrap AppiumDrivers"); } FlutterDriver = new FlutterDriver(remoteWebDriver, Resolve <ICommandExecutor>(), Resolve <IControlSettings>().WaitUntilTimeoutInSeconds); }
public static Rectangle GetElementScreenRect(RemoteWebElement webElement) { if (webElement == null) { return(new Rectangle()); } IWrapsDriver wrapsDriver = webElement as IWrapsDriver; IJavaScriptExecutor jsExecutor = wrapsDriver.WrappedDriver as IJavaScriptExecutor; Dictionary <string, object> pos = jsExecutor.ExecuteScript(_jsCalcScreenPos, webElement.Location.X, webElement.Location.Y) as Dictionary <string, object>; return(new Rectangle(new Point(Convert.ToInt32(pos["x"]), Convert.ToInt32(pos["y"])), webElement.Size)); }
protected virtual ReadOnlyCollection <IWebElement> FindElementsOnElement(IWrapsDriver wrapsDriver) { IJavaScriptExecutor executor = wrapsDriver.WrappedDriver as IJavaScriptExecutor; if (executor == null) { throw new Exception("IWrapsDriver does not implement IJavaScriptExecutor"); } string script = @" var returnList = []; $(arguments[0]).find('" + _selector + @"').each(function () { returnList.push(this); }); return returnList;"; var objects = (IEnumerable <object>)executor.ExecuteScript(script, wrapsDriver); return(new ReadOnlyCollection <IWebElement>(objects.Cast <IWebElement>().ToList())); }
/// <summary> /// This method use for /// set attribute of element /// </summary> /// <param name="element"></param> /// <param name="attributeName"></param> /// <param name="value"></param> public void SetAttribute(IWebElement element, string attributeName, string value) { IWrapsDriver wrappedElement = element as IWrapsDriver; if (wrappedElement == null) { throw new ArgumentException("element", "Element must wrap a web driver"); } IWebDriver driver = wrappedElement.WrappedDriver; IJavaScriptExecutor javascript = driver as IJavaScriptExecutor; if (javascript == null) { throw new ArgumentException("element", "Element must wrap a web driver that supports javascript execution"); } javascript.ExecuteScript("arguments[0].setAttribute(arguments[1], arguments[2])", element, attributeName, value); }
/// <summary> /// Click on element using java script. it will be hold good if your element is not visible at the time element interactions. /// </summary> /// <param name="webElement">The web element.</param> public static void JavaScriptClick(this IWebElement webElement) { IWrapsDriver wrappedElement = webElement as IWrapsDriver; if (wrappedElement == null) { throw new ArgumentException("element", "Element must wrap a web driver"); } IWebDriver driver = wrappedElement.WrappedDriver; IJavaScriptExecutor javascript = driver as IJavaScriptExecutor; if (javascript == null) { throw new ArgumentException("Element must wrap a web driver that supports javascript execution"); } javascript.ExecuteScript("arguments[0].click();", webElement); }
private static T GetDriverAs <T>(IWebDriver driver) where T : class { T convertedDriver = driver as T; if (convertedDriver == null) { // If the driver doesn't directly implement the desired interface, but does // implement IWrapsDriver, walk up the hierarchy of wrapped drivers until // either we find a class that does implement the desired interface, or is // no longer wrapping a driver. IWrapsDriver driverWrapper = driver as IWrapsDriver; while (convertedDriver == null && driverWrapper != null) { convertedDriver = driverWrapper.WrappedDriver as T; driverWrapper = driverWrapper.WrappedDriver as IWrapsDriver; } } return(convertedDriver); }
public TouchActions(IWebDriver driver) : base(driver) { IHasTouchScreen hasTouchScreen = driver as IHasTouchScreen; if (hasTouchScreen == null) { for (IWrapsDriver wrapsDriver = driver as IWrapsDriver; wrapsDriver != null; wrapsDriver = (wrapsDriver.WrappedDriver as IWrapsDriver)) { hasTouchScreen = (wrapsDriver.WrappedDriver as IHasTouchScreen); if (hasTouchScreen != null) { break; } } } if (hasTouchScreen == null) { throw new ArgumentException("The IWebDriver object must implement or wrap a driver that implements IHasTouchScreen.", "driver"); } this.touchScreen = hasTouchScreen.TouchScreen; }
private static object ExecuteJavaScript(ISearchContext arg, string script) { IJavaScriptExecutor executor = arg as IJavaScriptExecutor; if (executor == null) { IWrapsDriver driverWrapper = arg as IWrapsDriver; if (driverWrapper != null) { executor = driverWrapper.WrappedDriver as IJavaScriptExecutor; } } if (executor == null) { throw new InvalidOperationException("Search context cannot execute JavaScript"); } object scriptResult = executor.ExecuteScript(script); return(scriptResult); }
private T GetDriverAs <T>(IWebDriver driver) where T : class { T driverAsType = driver as T; if (driverAsType == null) { IWrapsDriver wrapper = driver as IWrapsDriver; while (wrapper != null) { driverAsType = wrapper.WrappedDriver as T; if (driverAsType != null) { driver = wrapper.WrappedDriver; break; } wrapper = wrapper.WrappedDriver as IWrapsDriver; } } return(driverAsType); }
public Actions(IWebDriver driver) { IHasInputDevices hasInputDevices = driver as IHasInputDevices; if (hasInputDevices == null) { for (IWrapsDriver wrapsDriver = driver as IWrapsDriver; wrapsDriver != null; wrapsDriver = (wrapsDriver.WrappedDriver as IWrapsDriver)) { hasInputDevices = (wrapsDriver.WrappedDriver as IHasInputDevices); if (hasInputDevices != null) { break; } } } if (hasInputDevices == null) { throw new ArgumentException("The IWebDriver object must implement or wrap a driver that implements IHasInputDevices.", "driver"); } this.keyboard = hasInputDevices.Keyboard; this.mouse = hasInputDevices.Mouse; }
/// <summary> /// Set Attribute of an element /// </summary> /// <param name="driver">driver instance</param> /// <param name="locator">element locator</param> /// <param name="attributeName">attribute name</param> /// <param name="value">attribute text</param> public static void SetAttribute(this IWebDriver driver, By locator, string attributeName, string value) { var element = driver.FindElement(locator); IWrapsDriver wrappedElement = element as IWrapsDriver; string message; if (wrappedElement == null) { message = $"Element must wrap a web driver"; Log.Error(message); throw new ArgumentException("element", $"Element must wrap a web driver"); } driver = wrappedElement.WrappedDriver; IJavaScriptExecutor javascript = driver as IJavaScriptExecutor; if (javascript == null) { message = $"Element must wrap a web driver that supports javascript execution"; Log.Error(message); throw new ArgumentException("element", message); } javascript.ExecuteScript("arguments[0].setAttribute(arguments[1], arguments[2])", element, attributeName, value); }
/// <summary> /// Finds all elements matching the criteria. /// </summary> /// <param name="context"> /// An <see cref="ISearchContext"/> object to use to search for the elements. /// </param> /// <returns> /// A collection of all <see cref="OpenQA.Selenium.IWebElement"/> matching the current criteria, /// or an empty list if nothing matches. /// </returns> public override ReadOnlyCollection <IWebElement> FindElements(ISearchContext context) { // Create script arguments object[] scriptArgs = this.args; if (this.AdditionalScriptArguments != null && this.AdditionalScriptArguments.Length > 0) { // Add additionnal script arguments scriptArgs = new object[this.args.Length + this.AdditionalScriptArguments.Length]; this.args.CopyTo(scriptArgs, 0); this.AdditionalScriptArguments.CopyTo(scriptArgs, this.args.Length); } // Get JS executor IJavaScriptExecutor jsExecutor = context as IJavaScriptExecutor; if (jsExecutor == null) { IWrapsDriver wrapsDriver = context as IWrapsDriver; if (wrapsDriver != null) { jsExecutor = wrapsDriver.WrappedDriver as IJavaScriptExecutor; } } if (jsExecutor == null) { throw new NotSupportedException("Could not get an IJavaScriptExecutor instance from the context."); } ReadOnlyCollection <IWebElement> elements = jsExecutor.ExecuteScript(this.script, scriptArgs) as ReadOnlyCollection <IWebElement>; if (elements == null) { elements = new ReadOnlyCollection <IWebElement>(new List <IWebElement>(0)); } return(elements); }
static void Main(string[] args) { IWrapsDriver wrapsDriver = default; IWrapsElement wrapsElement = default; }
private static IWebDriver GetDriver(IWrapsDriver wrappedDriver) => wrappedDriver.WrappedDriver;
//public static Func<Func<IWebElement>, TResult> ByWebElement<TResult>(this Func<IWebDriver, TResult> condition) //{ // return webElement => condition(webElement().Driver()); //} public static Func <Func <IWebElement>, TResult> ByWebElement <TResult>(this Func <IWebDriver, TResult> condition, IWrapsDriver browser) { return(webElement => condition(browser.WrappedDriver)); }