Inheritance: IDisposable
Esempio n. 1
0
        /// <summary>
        /// Converts the Collection to a list
        /// </summary>
        /// <returns>A list of strings </returns>
        public List<string> ToList()
        {
            int elementCount = 0;
            WebDriverResult result = NativeDriverLibrary.Instance.GetStringCollectionLength(handle, ref elementCount);
            if (result != WebDriverResult.Success)
            {
                Dispose();
                throw new WebDriverException(string.Format(CultureInfo.InvariantCulture, "Cannot extract strings from collection: {0}", result));
            }

            List<string> toReturn = new List<string>();
            for (int i = 0; i < elementCount; i++)
            {
                SafeStringWrapperHandle stringHandle = new SafeStringWrapperHandle();
                result = NativeDriverLibrary.Instance.GetStringAtIndex(handle, i, ref stringHandle);
                if (result != WebDriverResult.Success)
                {
                    stringHandle.Dispose();
                    Dispose();
                    throw new WebDriverException(string.Format(CultureInfo.InvariantCulture, "Cannot extract string from collection at index: {0} ({1})", i, result));
                }

                using (StringWrapper wrapper = new StringWrapper(stringHandle))
                {
                    toReturn.Add(wrapper.Value);
                }
            }

            // TODO(andre.nogueira): from the java code (elementcollection.java)... "Free memory from the collection"
            // Dispose();
            return toReturn;
        }
            /// <summary>
            /// Method for getting a Collection of Cookies that are present in the browser.
            /// </summary>
            /// <returns>ReadOnlyCollection of Cookies in the browser.</returns>
            public ReadOnlyCollection <Cookie> GetCookies()
            {
                Uri currentUri = GetCurrentUri();

                SafeStringWrapperHandle stringHandle = new SafeStringWrapperHandle();
                WebDriverResult         result       = NativeDriverLibrary.Instance.GetCookies(driver.handle, ref stringHandle);

                ResultHandler.VerifyResultCode(result, "Getting Cookies");
                string allDomainCookies = string.Empty;

                using (StringWrapper wrapper = new StringWrapper(stringHandle))
                {
                    allDomainCookies = wrapper.Value;
                }

                List <Cookie> toReturn = new List <Cookie>();

                string[] cookies = allDomainCookies.Split(new string[] { "; " }, StringSplitOptions.RemoveEmptyEntries);
                foreach (string cookie in cookies)
                {
                    string[] parts = cookie.Split(new string[] { "=" }, StringSplitOptions.RemoveEmptyEntries);
                    if (parts.Length != 2)
                    {
                        continue;
                    }

                    toReturn.Add(new ReturnedCookie(parts[0], parts[1], currentUri.Host, string.Empty, null, false, currentUri));
                }

                return(new ReadOnlyCollection <Cookie>(toReturn));
            }
Esempio n. 3
0
        /// <summary>
        /// Converts the Collection to a list
        /// </summary>
        /// <returns>A list of strings </returns>
        public List <string> ToList()
        {
            int             elementCount = 0;
            WebDriverResult result       = NativeDriverLibrary.Instance.GetStringCollectionLength(handle, ref elementCount);

            if (result != WebDriverResult.Success)
            {
                Dispose();
                throw new WebDriverException(string.Format(CultureInfo.InvariantCulture, "Cannot extract strings from collection: {0}", result));
            }

            List <string> toReturn = new List <string>();

            for (int i = 0; i < elementCount; i++)
            {
                SafeStringWrapperHandle stringHandle = new SafeStringWrapperHandle();
                result = NativeDriverLibrary.Instance.GetStringAtIndex(handle, i, ref stringHandle);
                if (result != WebDriverResult.Success)
                {
                    stringHandle.Dispose();
                    Dispose();
                    throw new WebDriverException(string.Format(CultureInfo.InvariantCulture, "Cannot extract string from collection at index: {0} ({1})", i, result));
                }

                using (StringWrapper wrapper = new StringWrapper(stringHandle))
                {
                    toReturn.Add(wrapper.Value);
                }
            }

            // TODO(andre.nogueira): from the java code (elementcollection.java)... "Free memory from the collection"
            // Dispose();
            return(toReturn);
        }
Esempio n. 4
0
        /// <summary>
        /// Method to return the value of a CSS Property
        /// </summary>
        /// <param name="propertyName">CSS property key</param>
        /// <returns>string value of the CSS property</returns>
        public string GetValueOfCssProperty(string propertyName)
        {
            SafeStringWrapperHandle stringHandle = new SafeStringWrapperHandle();
            WebDriverResult         result       = NativeDriverLibrary.Instance.GetElementValueOfCssProperty(elementHandle, propertyName, ref stringHandle);

            ResultHandler.VerifyResultCode(result, string.Format(CultureInfo.InvariantCulture, "get the value of CSS property '{0}'", propertyName));
            string returnValue = string.Empty;

            using (StringWrapper wrapper = new StringWrapper(stringHandle))
            {
                returnValue = wrapper.Value;
            }

            return(returnValue);
        }
        /// <summary>
        /// Returns the Name of Window that the driver is working in.
        /// </summary>
        /// <returns>Returns the name of the Window.</returns>
        /// <example>
        /// IWebDriver driver = new InternetExplorerDriver();
        /// string windowName = driver.GetWindowHandles();
        /// </example>
        public string GetWindowHandle()
        {
            SafeStringWrapperHandle stringHandle = new SafeStringWrapperHandle();
            WebDriverResult         result       = NativeDriverLibrary.Instance.GetCurrentWindowHandle(handle, ref stringHandle);

            ResultHandler.VerifyResultCode(result, "Unable to obtain current window handle");
            string handleValue = string.Empty;

            using (StringWrapper wrapper = new StringWrapper(stringHandle))
            {
                handleValue = wrapper.Value;
            }

            return(handleValue);
        }
Esempio n. 6
0
        /// <summary>
        /// If this current element is a form, or an element within a form, then this will be submitted to the remote server. If this causes the current page to change, then this method will block until the new page is loaded.
        /// </summary>
        /// <param name="attributeName">Attribute you wish to get details of.</param>
        /// <returns>The attribute's current value or null if the value is not set.</returns>
        public string GetAttribute(string attributeName)
        {
            SafeStringWrapperHandle          stringHandle = new SafeStringWrapperHandle();
            SafeInternetExplorerDriverHandle driverHandle = driver.GetUnderlayingHandle();
            WebDriverResult result = NativeDriverLibrary.Instance.GetElementAttribute(driverHandle, elementHandle, attributeName, ref stringHandle);

            ResultHandler.VerifyResultCode(result, string.Format(CultureInfo.InvariantCulture, "getting attribute '{0}' of the element", attributeName));
            string returnValue = null;

            using (StringWrapper wrapper = new StringWrapper(stringHandle))
            {
                returnValue = wrapper.Value;
            }

            return(returnValue);
        }
        /// <summary>
        /// Gets a <see cref="Screenshot"/> object representing the image of the page on the screen.
        /// </summary>
        /// <returns>A <see cref="Screenshot"/> object containing the image.</returns>
        public Screenshot GetScreenshot()
        {
            Screenshot currentScreenshot         = null;
            SafeStringWrapperHandle stringHandle = new SafeStringWrapperHandle();
            WebDriverResult         result       = NativeDriverLibrary.Instance.CaptureScreenshotAsBase64(handle, ref stringHandle);

            ResultHandler.VerifyResultCode(result, "Unable to get screenshot");
            string screenshotValue = string.Empty;

            using (StringWrapper wrapper = new StringWrapper(stringHandle))
            {
                screenshotValue = wrapper.Value;
            }

            if (!string.IsNullOrEmpty(screenshotValue))
            {
                currentScreenshot = new Screenshot(screenshotValue);
            }

            return(currentScreenshot);
        }
        /// <summary>
        /// Method to return the value of a CSS Property
        /// </summary>
        /// <param name="propertyName">CSS property key</param>
        /// <returns>string value of the CSS property</returns>
        public string GetValueOfCssProperty(string propertyName)
        {
            SafeStringWrapperHandle stringHandle = new SafeStringWrapperHandle();
            WebDriverResult result = NativeDriverLibrary.Instance.GetElementValueOfCssProperty(elementHandle, propertyName, ref stringHandle);
            ResultHandler.VerifyResultCode(result, string.Format(CultureInfo.InvariantCulture, "get the value of CSS property '{0}'", propertyName));
            string returnValue = string.Empty;
            using (StringWrapper wrapper = new StringWrapper(stringHandle))
            {
                returnValue = wrapper.Value;
            }

            return returnValue;
        }
        /// <summary>
        /// If this current element is a form, or an element within a form, then this will be submitted to the remote server. If this causes the current page to change, then this method will block until the new page is loaded.
        /// </summary>
        /// <param name="attributeName">Attribute you wish to get details of.</param>
        /// <returns>The attribute's current value or null if the value is not set.</returns>
        public string GetAttribute(string attributeName)
        {
            SafeStringWrapperHandle stringHandle = new SafeStringWrapperHandle();
            SafeInternetExplorerDriverHandle driverHandle = driver.GetUnderlayingHandle();
            WebDriverResult result = NativeDriverLibrary.Instance.GetElementAttribute(driverHandle, elementHandle, attributeName, ref stringHandle);
            ResultHandler.VerifyResultCode(result, string.Format(CultureInfo.InvariantCulture, "getting attribute '{0}' of the element", attributeName));
            string returnValue = null;
            using (StringWrapper wrapper = new StringWrapper(stringHandle))
            {
                returnValue = wrapper.Value;
            }

            return returnValue;
        }
Esempio n. 10
0
            /// <summary>
            /// Method for getting a Collection of Cookies that are present in the browser.
            /// </summary>
            /// <returns>ReadOnlyCollection of Cookies in the browser.</returns>
            public ReadOnlyCollection<Cookie> GetCookies()
            {
                Uri currentUri = GetCurrentUri();

                SafeStringWrapperHandle stringHandle = new SafeStringWrapperHandle();
                WebDriverResult result = NativeDriverLibrary.Instance.GetCookies(driver.handle, ref stringHandle);
                ResultHandler.VerifyResultCode(result, "Getting Cookies");
                string allDomainCookies = string.Empty;
                using (StringWrapper wrapper = new StringWrapper(stringHandle))
                {
                    allDomainCookies = wrapper.Value;
                }

                List<Cookie> toReturn = new List<Cookie>();

                string[] cookies = allDomainCookies.Split(new string[] { "; " }, StringSplitOptions.RemoveEmptyEntries);
                foreach (string cookie in cookies)
                {
                    string[] parts = cookie.Split(new string[] { "=" }, StringSplitOptions.RemoveEmptyEntries);
                    if (parts.Length != 2)
                    {
                        continue;
                    }

                    toReturn.Add(new ReturnedCookie(parts[0], parts[1], currentUri.Host, string.Empty, null, false, currentUri));
                }

                return new ReadOnlyCollection<Cookie>(toReturn);
            }
Esempio n. 11
0
        private object ExtractReturnValue(SafeScriptResultHandle scriptResult)
        {
            WebDriverResult result;

            int type;
            result = NativeDriverLibrary.Instance.GetScriptResultType(handle, scriptResult, out type);

            ResultHandler.VerifyResultCode(result, "Cannot determine result type");

            object toReturn = null;
            try
            {
                switch (type)
                {
                    case 1:
                        SafeStringWrapperHandle stringHandle = new SafeStringWrapperHandle();
                        result = NativeDriverLibrary.Instance.GetStringScriptResult(scriptResult, ref stringHandle);
                        ResultHandler.VerifyResultCode(result, "Cannot extract string result");
                        using (StringWrapper wrapper = new StringWrapper(stringHandle))
                        {
                            toReturn = wrapper.Value;
                        }

                        break;

                    case 2:
                        long longVal;
                        result = NativeDriverLibrary.Instance.GetNumberScriptResult(scriptResult, out longVal);
                        ResultHandler.VerifyResultCode(result, "Cannot extract number result");
                        toReturn = longVal;
                        break;

                    case 3:
                        int boolVal;
                        result = NativeDriverLibrary.Instance.GetBooleanScriptResult(scriptResult, out boolVal);
                        ResultHandler.VerifyResultCode(result, "Cannot extract boolean result");
                        toReturn = boolVal == 1 ? true : false;
                        break;

                    case 4:
                        SafeInternetExplorerWebElementHandle element;
                        result = NativeDriverLibrary.Instance.GetElementScriptResult(scriptResult, handle, out element);
                        ResultHandler.VerifyResultCode(result, "Cannot extract element result");
                        toReturn = new InternetExplorerWebElement(this, element);
                        break;

                    case 5:
                        toReturn = null;
                        break;

                    case 6:
                        SafeStringWrapperHandle messageHandle = new SafeStringWrapperHandle();
                        result = NativeDriverLibrary.Instance.GetStringScriptResult(scriptResult, ref messageHandle);
                        ResultHandler.VerifyResultCode(result, "Cannot extract string result");
                        string message = string.Empty;
                        using (StringWrapper wrapper = new StringWrapper(messageHandle))
                        {
                            message = wrapper.Value;
                        }

                        throw new WebDriverException(message);

                    case 7:
                        double doubleVal;
                        result = NativeDriverLibrary.Instance.GetDoubleScriptResult(scriptResult, out doubleVal);
                        ResultHandler.VerifyResultCode(result, "Cannot extract number result");
                        toReturn = doubleVal;
                        break;

                    case 8:
                        bool allArrayItemsAreElements = true;
                        int arrayLength = 0;
                        result = NativeDriverLibrary.Instance.GetArrayLengthScriptResult(handle, scriptResult, out arrayLength);
                        ResultHandler.VerifyResultCode(result, "Cannot extract array length.");
                        List<object> list = new List<object>();
                        for (int i = 0; i < arrayLength; i++)
                        {
                            // Get reference to object
                            SafeScriptResultHandle currItemHandle = new SafeScriptResultHandle();
                            WebDriverResult getItemResult = NativeDriverLibrary.Instance.GetArrayItemFromScriptResult(handle, scriptResult, i, out currItemHandle);
                            if (getItemResult != WebDriverResult.Success)
                            {
                                // Note about memory management: Usually memory for this item
                                // will be released during the recursive call to
                                // ExtractReturnValue. It is freed explicitly here since a
                                // recursive call will not happen.
                                currItemHandle.Dispose();
                                throw new WebDriverException(string.Format(CultureInfo.InvariantCulture, "Cannot extract element from collection at index: {0} ({1})", i, result));
                            }

                            object arrayItem = ExtractReturnValue(currItemHandle);
                            if (allArrayItemsAreElements && !(arrayItem is IWebElement))
                            {
                                allArrayItemsAreElements = false;
                            }

                            // Call ExtractReturnValue with the fetched item (recursive)
                            list.Add(arrayItem);
                        }

                        if (allArrayItemsAreElements)
                        {
                            List<IWebElement> elementList = new List<IWebElement>();
                            foreach (object item in list)
                            {
                                elementList.Add((IWebElement)item);
                            }

                            toReturn = elementList.AsReadOnly();
                        }
                        else
                        {
                            toReturn = list.AsReadOnly();
                        }

                        break;

                    default:
                        throw new WebDriverException("Cannot determine result type");
                }
            }
            finally
            {
                scriptResult.Dispose();
            }

            return toReturn;
        }
Esempio n. 12
0
        /// <summary>
        /// Returns the Name of Window that the driver is working in.
        /// </summary>
        /// <returns>Returns the name of the Window.</returns>
        /// <example>
        /// IWebDriver driver = new InternetExplorerDriver();
        /// string windowName = driver.GetWindowHandles();
        /// </example>
        public string GetWindowHandle()
        {
            SafeStringWrapperHandle stringHandle = new SafeStringWrapperHandle();
            WebDriverResult result = NativeDriverLibrary.Instance.GetCurrentWindowHandle(handle, ref stringHandle);
            ResultHandler.VerifyResultCode(result, "Unable to obtain current window handle");
            string handleValue = string.Empty;
            using (StringWrapper wrapper = new StringWrapper(stringHandle))
            {
                handleValue = wrapper.Value;
            }

            return handleValue;
        }
Esempio n. 13
0
        /// <summary>
        /// Gets a <see cref="Screenshot"/> object representing the image of the page on the screen.
        /// </summary>
        /// <returns>A <see cref="Screenshot"/> object containing the image.</returns>
        public Screenshot GetScreenshot()
        {
            Screenshot currentScreenshot = null;
            SafeStringWrapperHandle stringHandle = new SafeStringWrapperHandle();
            WebDriverResult result = NativeDriverLibrary.Instance.CaptureScreenshotAsBase64(handle, ref stringHandle);
            ResultHandler.VerifyResultCode(result, "Unable to get screenshot");
            string screenshotValue = string.Empty;
            using (StringWrapper wrapper = new StringWrapper(stringHandle))
            {
                screenshotValue = wrapper.Value;
            }

            if (!string.IsNullOrEmpty(screenshotValue))
            {
                currentScreenshot = new Screenshot(screenshotValue);
            }

            return currentScreenshot;
        }
        private object ExtractReturnValue(SafeScriptResultHandle scriptResult)
        {
            WebDriverResult result;

            int type;

            result = NativeDriverLibrary.Instance.GetScriptResultType(handle, scriptResult, out type);

            ResultHandler.VerifyResultCode(result, "Cannot determine result type");

            object toReturn = null;

            try
            {
                switch (type)
                {
                case 1:
                    SafeStringWrapperHandle stringHandle = new SafeStringWrapperHandle();
                    result = NativeDriverLibrary.Instance.GetStringScriptResult(scriptResult, ref stringHandle);
                    ResultHandler.VerifyResultCode(result, "Cannot extract string result");
                    using (StringWrapper wrapper = new StringWrapper(stringHandle))
                    {
                        toReturn = wrapper.Value;
                    }

                    break;

                case 2:
                    long longVal;
                    result = NativeDriverLibrary.Instance.GetNumberScriptResult(scriptResult, out longVal);
                    ResultHandler.VerifyResultCode(result, "Cannot extract number result");
                    toReturn = longVal;
                    break;

                case 3:
                    int boolVal;
                    result = NativeDriverLibrary.Instance.GetBooleanScriptResult(scriptResult, out boolVal);
                    ResultHandler.VerifyResultCode(result, "Cannot extract boolean result");
                    toReturn = boolVal == 1 ? true : false;
                    break;

                case 4:
                    SafeInternetExplorerWebElementHandle element;
                    result = NativeDriverLibrary.Instance.GetElementScriptResult(scriptResult, handle, out element);
                    ResultHandler.VerifyResultCode(result, "Cannot extract element result");
                    toReturn = new InternetExplorerWebElement(this, element);
                    break;

                case 5:
                    toReturn = null;
                    break;

                case 6:
                    SafeStringWrapperHandle messageHandle = new SafeStringWrapperHandle();
                    result = NativeDriverLibrary.Instance.GetStringScriptResult(scriptResult, ref messageHandle);
                    ResultHandler.VerifyResultCode(result, "Cannot extract string result");
                    string message = string.Empty;
                    using (StringWrapper wrapper = new StringWrapper(messageHandle))
                    {
                        message = wrapper.Value;
                    }

                    throw new WebDriverException(message);

                case 7:
                    double doubleVal;
                    result = NativeDriverLibrary.Instance.GetDoubleScriptResult(scriptResult, out doubleVal);
                    ResultHandler.VerifyResultCode(result, "Cannot extract number result");
                    toReturn = doubleVal;
                    break;

                case 8:
                    bool allArrayItemsAreElements = true;
                    int  arrayLength = 0;
                    result = NativeDriverLibrary.Instance.GetArrayLengthScriptResult(handle, scriptResult, out arrayLength);
                    ResultHandler.VerifyResultCode(result, "Cannot extract array length.");
                    List <object> list = new List <object>();
                    for (int i = 0; i < arrayLength; i++)
                    {
                        // Get reference to object
                        SafeScriptResultHandle currItemHandle = new SafeScriptResultHandle();
                        WebDriverResult        getItemResult  = NativeDriverLibrary.Instance.GetArrayItemFromScriptResult(handle, scriptResult, i, out currItemHandle);
                        if (getItemResult != WebDriverResult.Success)
                        {
                            // Note about memory management: Usually memory for this item
                            // will be released during the recursive call to
                            // ExtractReturnValue. It is freed explicitly here since a
                            // recursive call will not happen.
                            currItemHandle.Dispose();
                            throw new WebDriverException(string.Format(CultureInfo.InvariantCulture, "Cannot extract element from collection at index: {0} ({1})", i, result));
                        }

                        object arrayItem = ExtractReturnValue(currItemHandle);
                        if (allArrayItemsAreElements && !(arrayItem is IWebElement))
                        {
                            allArrayItemsAreElements = false;
                        }

                        // Call ExtractReturnValue with the fetched item (recursive)
                        list.Add(arrayItem);
                    }

                    if (allArrayItemsAreElements)
                    {
                        List <IWebElement> elementList = new List <IWebElement>();
                        foreach (object item in list)
                        {
                            elementList.Add((IWebElement)item);
                        }

                        toReturn = elementList.AsReadOnly();
                    }
                    else
                    {
                        toReturn = list.AsReadOnly();
                    }

                    break;

                default:
                    throw new WebDriverException("Cannot determine result type");
                }
            }
            finally
            {
                scriptResult.Dispose();
            }

            return(toReturn);
        }