コード例 #1
0
        /// <summary>
        /// Gets the <see cref = "ILocatable"/> instance of the specified <see cref = "IWebElement"/>.
        /// </summary>
        /// <param name = "element">The <see cref = "IWebElement"/> to get the location of.</param>
        /// <returns>The <see cref = "ILocatable"/> of the <see cref = "IWebElement"/>.</returns>
        protected static ILocatable GetLocatableFromElement(IWebElement element)
        {
            if (element == null)
            {
                return(null);
            }

            ILocatable target = element as ILocatable;

            if (target == null)
            {
                IWrapsElement wrapper = element as IWrapsElement;
                while (wrapper != null)
                {
                    target = wrapper.WrappedElement as ILocatable;
                    if (target != null)
                    {
                        break;
                    }

                    wrapper = wrapper.WrappedElement as IWrapsElement;
                }
            }

            if (target == null)
            {
                throw new ArgumentException("The IWebElement object must implement or wrap an element that implements ILocatable.", "element");
            }

            return(target);
        }
コード例 #2
0
        public override bool Equals(object obj)
        {
            IWebElement webElement = obj as IWebElement;

            if (webElement == null)
            {
                return(false);
            }
            IWrapsElement wrapsElement = obj as IWrapsElement;

            if (wrapsElement != null)
            {
                webElement = wrapsElement.WrappedElement;
            }
            RemoteWebElement remoteWebElement = webElement as RemoteWebElement;

            if (remoteWebElement == null)
            {
                return(false);
            }
            if (this.elementId == remoteWebElement.Id)
            {
                return(true);
            }
            Dictionary <string, object> dictionary = new Dictionary <string, object>();

            dictionary.Add("id", this.elementId);
            dictionary.Add("other", remoteWebElement.Id);
            Response response = this.Execute(DriverCommand.ElementEquals, dictionary);
            object   value    = response.Value;

            return(value != null && value is bool && (bool)value);
        }
コード例 #3
0
        /// <summary>
        /// Compares if two elements are equal
        /// </summary>
        /// <param name="obj">Object to compare against</param>
        /// <returns>A boolean if it is equal or not</returns>
        public override bool Equals(object obj)
        {
            IWebElement other = obj as IWebElement;

            if (other == null)
            {
                return(false);
            }

            IWrapsElement objAsWrapsElement = obj as IWrapsElement;

            if (objAsWrapsElement != null)
            {
                other = objAsWrapsElement.WrappedElement;
            }

            WebElement otherAsElement = other as WebElement;

            if (otherAsElement == null)
            {
                return(false);
            }

            if (this.elementId == otherAsElement.Id)
            {
                // For drivers that implement ID equality, we can check for equal IDs
                // here, and expect them to be equal. There is a potential danger here
                // where two different elements are assigned the same ID.
                return(true);
            }

            return(false);
        }
コード例 #4
0
        /// <summary>
        /// Compares if two elements are equal
        /// </summary>
        /// <param name="obj">Object to compare against</param>
        /// <returns>A boolean if it is equal or not</returns>
        public override bool Equals(object obj)
        {
            IWebElement other = obj as IWebElement;

            if (other == null)
            {
                return(false);
            }

            IWrapsElement objAsWrapsElement = obj as IWrapsElement;

            if (objAsWrapsElement != null)
            {
                other = objAsWrapsElement.WrappedElement;
            }

            RemoteWebElement otherAsElement = other as RemoteWebElement;

            if (otherAsElement == null)
            {
                return(false);
            }

            Dictionary <string, object> parameters = new Dictionary <string, object>();

            parameters.Add("id", this.elementId);
            parameters.Add("other", otherAsElement.Id);

            Response response = this.Execute(DriverCommand.ElementEquals, parameters);
            object   value    = response.Value;

            return(value != null && value is bool && (bool)value);
        }
コード例 #5
0
        private object GetSerializableRoot(object toSerialize)
        {
            if (toSerialize == null)
            {
                throw new ArgumentNullException(nameof(toSerialize), "object to serialize must not be null");
            }

            By asBy = toSerialize as By;

            if (asBy != null)
            {
                return(asBy);
            }

            IWebElement element = toSerialize as IWebElement;

            if (element != null)
            {
                return(element);
            }

            IWrapsElement wrapper = toSerialize as IWrapsElement;

            if (wrapper != null)
            {
                return(wrapper.WrappedElement);
            }

            throw new WebDriverException("Serializable locator must be a By, an IWebElement, or a wrapped element using IWrapsElement");
        }
コード例 #6
0
        /// <summary>
        /// Move to a frame element.
        /// </summary>
        /// <param name="frameElement">a previously found FRAME or IFRAME element.</param>
        /// <returns>A WebDriver instance that is currently in use.</returns>
        public IWebDriver Frame(IWebElement frameElement)
        {
            if (frameElement == null)
            {
                throw new ArgumentNullException("frameElement", "Frame element cannot be null");
            }

            IWebElementReference elementReference = frameElement as IWebElementReference;

            if (elementReference == null)
            {
                IWrapsElement elementWrapper = frameElement as IWrapsElement;
                if (elementWrapper != null)
                {
                    elementReference = elementWrapper.WrappedElement as IWebElementReference;
                }
            }

            if (elementReference == null)
            {
                throw new ArgumentException("frameElement cannot be converted to IWebElementReference", "frameElement");
            }

            // TODO: Remove "ELEMENT" addition when all remote ends are spec-compliant.
            Dictionary <string, object> elementDictionary = elementReference.ToDictionary();

            elementDictionary.Add("ELEMENT", elementReference.ElementReferenceId);

            Dictionary <string, object> parameters = new Dictionary <string, object>();

            parameters.Add("id", elementDictionary);
            this.driver.InternalExecute(DriverCommand.SwitchToFrame, parameters);
            return(this.driver);
        }
コード例 #7
0
        private object GetSerializableObject(object toSerialize)
        {
            if (toSerialize == null)
            {
                throw new ArgumentNullException(nameof(toSerialize), "object to serialize must not be null");
            }

            By asBy = toSerialize as By;

            if (asBy != null)
            {
                Dictionary <string, object> serializedBy = new Dictionary <string, object>();
                serializedBy[asBy.Mechanism] = asBy.Criteria;
                return(serializedBy);
            }

            IWebElement element = toSerialize as IWebElement;

            if (element != null)
            {
                return(element);
            }

            IWrapsElement wrapper = toSerialize as IWrapsElement;

            if (wrapper != null)
            {
                return(wrapper.WrappedElement);
            }

            throw new WebDriverException("Serializable locator must be a By, an IWebElement, or a wrapped element using IWrapsElement");
        }
コード例 #8
0
        /// <summary>
        /// Move to a frame element.
        /// </summary>
        /// <param name="frameElement">a previously found FRAME or IFRAME element.</param>
        /// <returns>A WebDriver instance that is currently in use.</returns>
        public IWebDriver Frame(IWebElement frameElement)
        {
            if (frameElement == null)
            {
                throw new ArgumentNullException("frameElement", "Frame element cannot be null");
            }

            RemoteWebElement convertedElement = frameElement as RemoteWebElement;

            if (convertedElement == null)
            {
                IWrapsElement elementWrapper = frameElement as IWrapsElement;
                if (elementWrapper != null)
                {
                    convertedElement = elementWrapper.WrappedElement as RemoteWebElement;
                }
            }

            if (convertedElement == null)
            {
                throw new ArgumentException("frameElement cannot be converted to RemoteWebElement", "frameElement");
            }

            Dictionary <string, object> elementDictionary = new Dictionary <string, object>();

            elementDictionary.Add("ELEMENT", convertedElement.InternalElementId);
            elementDictionary.Add("element-6066-11e4-a52e-4f735466cecf", convertedElement.InternalElementId);

            Dictionary <string, object> parameters = new Dictionary <string, object>();

            parameters.Add("id", elementDictionary);
            this.driver.InternalExecute(DriverCommand.SwitchToFrame, parameters);
            return(this.driver);
        }
コード例 #9
0
        public IWebDriver Frame(IWebElement frameElement)
        {
            if (frameElement == null)
            {
                throw new ArgumentNullException("frameElement", "Frame element cannot be null");
            }
            RemoteWebElement remoteWebElement = frameElement as RemoteWebElement;

            if (remoteWebElement == null)
            {
                IWrapsElement wrapsElement = frameElement as IWrapsElement;
                if (wrapsElement != null)
                {
                    remoteWebElement = (wrapsElement.WrappedElement as RemoteWebElement);
                }
            }
            if (remoteWebElement == null)
            {
                throw new ArgumentException("frameElement cannot be converted to RemoteWebElement", "frameElement");
            }
            Dictionary <string, object> dictionary = new Dictionary <string, object>();

            dictionary.Add("ELEMENT", remoteWebElement.InternalElementId);
            dictionary.Add("element-6066-11e4-a52e-4f735466cecf", remoteWebElement.InternalElementId);
            Dictionary <string, object> dictionary2 = new Dictionary <string, object>();

            dictionary2.Add("id", dictionary);
            this.driver.InternalExecute(DriverCommand.SwitchToFrame, dictionary2);
            return(this.driver);
        }
コード例 #10
0
        protected static ILocatable GetLocatableFromElement(IWebElement element)
        {
            if (element == null)
            {
                return(null);
            }
            ILocatable locatable = element as ILocatable;

            if (locatable == null)
            {
                for (IWrapsElement wrapsElement = element as IWrapsElement; wrapsElement != null; wrapsElement = (wrapsElement.WrappedElement as IWrapsElement))
                {
                    locatable = (wrapsElement.WrappedElement as ILocatable);
                    if (locatable != null)
                    {
                        break;
                    }
                }
            }
            if (locatable == null)
            {
                throw new ArgumentException("The IWebElement object must implement or wrap an element that implements ILocatable.", "element");
            }
            return(locatable);
        }
コード例 #11
0
        /// <summary>
        /// Compares current element against another
        /// </summary>
        /// <param name="obj">element to compare against</param>
        /// <returns>A value indicating whether they are the same</returns>
        public override bool Equals(object obj)
        {
            IWebElement other = obj as IWebElement;

            if (other == null)
            {
                return(false);
            }

            IWrapsElement elementWrapper = other as IWrapsElement;

            if (elementWrapper != null)
            {
                other = elementWrapper.WrappedElement;
            }

            ChromeWebElement otherChromeWebElement = other as ChromeWebElement;

            if (otherChromeWebElement == null)
            {
                return(false);
            }

            return(Id.Equals(otherChromeWebElement.Id));
        }
コード例 #12
0
        public static void SwitchToIframe(IWebElement iframe)
        {
            WebDriver.Driver.SwitchTo().DefaultContent();

            IWrapsElement wrapped = iframe as IWrapsElement;

            WebDriver.Driver.SwitchTo().Frame(wrapped.WrappedElement);
        }
コード例 #13
0
        private static object ConvertObjectToJavaScriptObject(object arg)
        {
            IWrapsElement    argAsWrapsElement = arg as IWrapsElement;
            RemoteWebElement argAsElement      = arg as RemoteWebElement;
            IEnumerable      argAsEnumerable   = arg as IEnumerable;
            IDictionary      argAsDictionary   = arg as IDictionary;

            if (argAsElement == null && argAsWrapsElement != null)
            {
                argAsElement = argAsWrapsElement.WrappedElement as RemoteWebElement;
            }

            object converted = null;

            if (arg is string || arg is float || arg is double || arg is int || arg is long || arg is bool || arg == null)
            {
                converted = arg;
            }
            else if (argAsElement != null)
            {
                // TODO: Remove addition of 'id' key when spec is changed.
                Dictionary <string, object> elementDictionary = new Dictionary <string, object>();
                elementDictionary.Add("ELEMENT", argAsElement.InternalElementId);
                elementDictionary.Add("id", argAsElement.InternalElementId);
                converted = elementDictionary;
            }
            else if (argAsDictionary != null)
            {
                // Note that we must check for the argument being a dictionary before
                // checking for IEnumerable, since dictionaries also implement IEnumerable.
                // Additionally, JavaScript objects have property names as strings, so all
                // keys will be converted to strings.
                Dictionary <string, object> dictionary = new Dictionary <string, object>();
                foreach (var key in argAsDictionary.Keys)
                {
                    dictionary.Add(key.ToString(), ConvertObjectToJavaScriptObject(argAsDictionary[key]));
                }

                converted = dictionary;
            }
            else if (argAsEnumerable != null)
            {
                List <object> objectList = new List <object>();
                foreach (object item in argAsEnumerable)
                {
                    objectList.Add(ConvertObjectToJavaScriptObject(item));
                }

                converted = objectList.ToArray();
            }
            else
            {
                throw new ArgumentException("Argument is of an illegal type" + arg.ToString(), "arg");
            }

            return(converted);
        }
コード例 #14
0
        private static object ConvertObjectToJavaScriptObject(object arg)
        {
            IWrapsElement    wrapsElement     = arg as IWrapsElement;
            RemoteWebElement remoteWebElement = arg as RemoteWebElement;
            IEnumerable      enumerable       = arg as IEnumerable;
            IDictionary      dictionary       = arg as IDictionary;

            if (remoteWebElement == null && wrapsElement != null)
            {
                remoteWebElement = (wrapsElement.WrappedElement as RemoteWebElement);
            }
            object result;

            if (arg is string || arg is float || arg is double || arg is int || arg is long || arg is bool || arg == null)
            {
                result = arg;
            }
            else if (remoteWebElement != null)
            {
                result = new Dictionary <string, object>
                {
                    {
                        "ELEMENT",
                        remoteWebElement.InternalElementId
                    },
                    {
                        "element-6066-11e4-a52e-4f735466cecf",
                        remoteWebElement.InternalElementId
                    }
                };
            }
            else if (dictionary != null)
            {
                Dictionary <string, object> dictionary2 = new Dictionary <string, object>();
                foreach (object current in dictionary.Keys)
                {
                    dictionary2.Add(current.ToString(), RemoteWebDriver.ConvertObjectToJavaScriptObject(dictionary[current]));
                }
                result = dictionary2;
            }
            else
            {
                if (enumerable == null)
                {
                    throw new ArgumentException("Argument is of an illegal type" + arg.ToString(), "arg");
                }
                List <object> list = new List <object>();
                foreach (object current2 in enumerable)
                {
                    list.Add(RemoteWebDriver.ConvertObjectToJavaScriptObject(current2));
                }
                result = list.ToArray();
            }
            return(result);
        }
コード例 #15
0
        private static object ConvertObjectToJavaScriptObject(object arg)
        {
            IWrapsElement             argAsWrapsElement    = arg as IWrapsElement;
            IWebDriverObjectReference argAsObjectReference = arg as IWebDriverObjectReference;
            IEnumerable argAsEnumerable = arg as IEnumerable;
            IDictionary argAsDictionary = arg as IDictionary;

            if (argAsObjectReference == null && argAsWrapsElement != null)
            {
                argAsObjectReference = argAsWrapsElement.WrappedElement as IWebDriverObjectReference;
            }

            object converted = null;

            if (arg is string || arg is float || arg is double || arg is int || arg is long || arg is bool || arg == null)
            {
                converted = arg;
            }
            else if (argAsObjectReference != null)
            {
                // TODO: Remove "ELEMENT" addition when all remote ends are spec-compliant.
                Dictionary <string, object> webDriverObjectReferenceDictionary = argAsObjectReference.ToDictionary();
                converted = webDriverObjectReferenceDictionary;
            }
            else if (argAsDictionary != null)
            {
                // Note that we must check for the argument being a dictionary before
                // checking for IEnumerable, since dictionaries also implement IEnumerable.
                // Additionally, JavaScript objects have property names as strings, so all
                // keys will be converted to strings.
                Dictionary <string, object> dictionary = new Dictionary <string, object>();
                foreach (var key in argAsDictionary.Keys)
                {
                    dictionary.Add(key.ToString(), ConvertObjectToJavaScriptObject(argAsDictionary[key]));
                }

                converted = dictionary;
            }
            else if (argAsEnumerable != null)
            {
                List <object> objectList = new List <object>();
                foreach (object item in argAsEnumerable)
                {
                    objectList.Add(ConvertObjectToJavaScriptObject(item));
                }

                converted = objectList.ToArray();
            }
            else
            {
                throw new ArgumentException("Argument is of an illegal type" + arg.ToString(), nameof(arg));
            }

            return(converted);
        }
コード例 #16
0
        public void SetForaward(string smth)
        {
            buttonAddForward.Click();
            setForward.SendKeys(smth);
            nextStep.Click();
            IWrapsElement wrap = secondWindow as IWrapsElement;

            driver.SwitchTo().Frame(wrap.WrappedElement);
            acceptForward.Click();
            driver.SwitchTo().DefaultContent();
            okay.Click();
        }
コード例 #17
0
        public static T SwitchToFrameByWebElement <T>(this T webDriver) where T : IWebDriver
        {
            IWrapsElement wrapsElement = webDriver as IWrapsElement;

            if (wrapsElement == null)
            {
                throw new ArgumentException("element", "Element must wrap a web driver");
            }
            IWebElement webElement = wrapsElement.WrappedElement;

            webDriver.SwitchTo().Frame(webElement);
            return(webDriver);
        }
コード例 #18
0
        /// <summary>
        /// Compares if two elements are equal
        /// </summary>
        /// <param name="obj">Object to compare against</param>
        /// <returns>A boolean if it is equal or not</returns>
        public override bool Equals(object obj)
        {
            IWebElement other = obj as IWebElement;

            if (other == null)
            {
                return(false);
            }

            IWrapsElement objAsWrapsElement = obj as IWrapsElement;

            if (objAsWrapsElement != null)
            {
                other = objAsWrapsElement.WrappedElement;
            }

            RemoteWebElement otherAsElement = other as RemoteWebElement;

            if (otherAsElement == null)
            {
                return(false);
            }

            if (this.elementId == otherAsElement.Id)
            {
                // For drivers that implement ID equality, we can check for equal IDs
                // here, and expect them to be equal. There is a potential danger here
                // where two different elements are assigned the same ID.
                return(true);
            }

            if (!driver.IsSpecificationCompliant)
            {
                try
                {
                    Dictionary <string, object> parameters = new Dictionary <string, object>();
                    parameters.Add("id", this.Id);
                    parameters.Add("other", otherAsElement.Id);

                    Response response = this.Execute(DriverCommand.ElementEquals, parameters);
                    object   value    = response.Value;
                    return(value != null && value is bool && (bool)value);
                }
                catch (NotImplementedException)
                {
                }
            }

            return(false);
        }
コード例 #19
0
            public IWebDriver Frame(IWebElement frameElement)
            {
                IWebDriver result = null;

                try
                {
                    IWrapsElement wrapsElement = frameElement as IWrapsElement;
                    result = this.wrappedLocator.Frame(wrapsElement.WrappedElement);
                }
                catch (Exception thrownException)
                {
                    this.parentDriver.OnException(new WebDriverExceptionEventArgs(this.parentDriver, thrownException));
                    throw;
                }
                return(result);
            }
コード例 #20
0
        private static object ConvertObjectToJavaScriptObject(object arg)
        {
            IWrapsElement    argAsWrapsElement = arg as IWrapsElement;
            RemoteWebElement argAsElement      = arg as RemoteWebElement;
            IEnumerable      argAsEnumerable   = arg as IEnumerable;

            if (argAsElement == null && argAsWrapsElement != null)
            {
                argAsElement = argAsWrapsElement.WrappedElement as RemoteWebElement;
            }

            object converted = null;

            if (arg is string || arg is float || arg is double || arg is int || arg is long || arg is bool || arg == null)
            {
                converted = arg;
            }
            else if (argAsElement != null)
            {
                Dictionary <string, object> elementDictionary = new Dictionary <string, object>();
                elementDictionary.Add("ELEMENT", argAsElement.InternalElementId);
                converted = elementDictionary;
            }
            else if (argAsEnumerable != null)
            {
                List <object> objectList = new List <object>();
                foreach (object item in argAsEnumerable)
                {
                    if (item is string || item is float || item is double || item is int || item is long || item is bool || item == null)
                    {
                        objectList.Add(item);
                    }
                    else
                    {
                        throw new ArgumentException("Only primitives may be used as elements in collections used as arguments for JavaScript functions.");
                    }
                }

                converted = objectList.ToArray();
            }
            else
            {
                throw new ArgumentException("Argument is of an illegal type" + arg.ToString(), "arg");
            }

            return(converted);
        }
コード例 #21
0
        public void BrowserElementImplementsIWrapsElement()
        {
            const string pageSource = @"
<html>
<body>
<span>Hello</span>
</body>
</html>";

            using (var browser = OpenBrowserWithPage(pageSource))
            {
                IWrapsElement span         = browser.WaitForElement(By.TagName("span"), "span");
                var           expectedType = browser.GetWebDriver().FindElement(By.TagName("span")).GetType();
                Assert.IsInstanceOfType(span.WrappedElement, expectedType);
                Assert.AreEqual("Hello", span.WrappedElement.Text);
            }
        }
コード例 #22
0
        private string GetIdForElement(IWebElement el)
        {
            RemoteWebElement remoteWebElement = el as RemoteWebElement;

            if (remoteWebElement != null)
            {
                return((string)typeof(OpenQA.Selenium.Remote.RemoteWebElement).GetField("elementId",
                                                                                        BindingFlags.NonPublic | BindingFlags.Instance).GetValue(el));
            }

            IWrapsElement elementWrapper = el as IWrapsElement;

            if (elementWrapper != null)
            {
                return(GetIdForElement(elementWrapper.WrappedElement));
            }

            return(null);
        }
コード例 #23
0
            private Dictionary<string, object> ConvertElement()
            {
                IWebElementReference elementReference = this.target as IWebElementReference;
                if (elementReference == null)
                {
                    IWrapsElement elementWrapper = this.target as IWrapsElement;
                    if (elementWrapper != null)
                    {
                        elementReference = elementWrapper.WrappedElement as IWebElementReference;
                    }
                }

                if (elementReference == null)
                {
                    throw new ArgumentException("Target element cannot be converted to IWebElementReference");
                }

                Dictionary<string, object> elementDictionary = elementReference.ToDictionary();
                return elementDictionary;
            }
コード例 #24
0
ファイル: Program.cs プロジェクト: ArisAgnew/Caesar
 static void Main(string[] args)
 {
     IWrapsDriver  wrapsDriver  = default;
     IWrapsElement wrapsElement = default;
 }