Esempio n. 1
0
        /// <summary>
        /// Handles the command.
        /// </summary>
        /// <param name="driver">The driver used to execute the command.</param>
        /// <param name="locator">The first parameter to the command.</param>
        /// <param name="value">The second parameter to the command.</param>
        /// <returns>The result of the command.</returns>
        protected override object HandleSeleneseCommand(IWebDriver driver, string locator, string value)
        {
            this.alertOverride.ReplaceAlertMethod();
            if (this.state.ControlKeyDown || this.state.AltKeyDown || this.state.MetaKeyDown)
            {
                throw new SeleniumException("type not supported immediately after call to controlKeyDown() or altKeyDown() or metaKeyDown()");
            }

            string stringToType = this.state.ShiftKeyDown ? value.ToUpperInvariant() : value;

            IWebElement element = this.finder.FindElement(driver, locator);

            // TODO(simon): Log a warning that people should be using "attachFile"
            string tagName     = element.TagName;
            string elementType = element.GetAttribute("type");

            if (tagName.ToLowerInvariant() == "input" && elementType != null && elementType.ToLowerInvariant() == "file")
            {
                element.SendKeys(stringToType);
                return(null);
            }

            IJavaScriptExecutor executor = driver as IJavaScriptExecutor;

            if (executor != null)
            {
                JavaScriptLibrary.ExecuteScript(driver, this.type, element, stringToType);
            }
            else
            {
                element.SendKeys(stringToType);
            }

            return(null);
        }
        /// <summary>
        /// Handles the command.
        /// </summary>
        /// <param name="driver">The driver used to execute the command.</param>
        /// <param name="locator">The first parameter to the command.</param>
        /// <param name="value">The second parameter to the command.</param>
        /// <returns>The result of the command.</returns>
        protected override object HandleSeleneseCommand(IWebDriver driver, string locator, string value)
        {
            IWebElement element = this.finder.FindElement(driver, locator);

            JavaScriptLibrary.ExecuteScript(driver, this.fire, element, this.type);
            return(null);
        }
Esempio n. 3
0
 /// <summary>
 /// Initializes a new instance of the <see cref="Type"/> class.
 /// </summary>
 /// <param name="alertOverride">An <see cref="AlertOverride"/> object used to handle JavaScript alerts.</param>
 /// <param name="elementFinder">An <see cref="ElementFinder"/> used to find the element on which to execute the command.</param>
 /// <param name="keyState">A <see cref="KeyState"/> object tracking the state of modifier keys.</param>
 public Type(AlertOverride alertOverride, ElementFinder elementFinder, KeyState keyState)
 {
     this.alertOverride = alertOverride;
     this.finder        = elementFinder;
     this.state         = keyState;
     this.type          = "return (" + JavaScriptLibrary.GetSeleniumScript("type.js") + ").apply(null, arguments);";
 }
Esempio n. 4
0
        /// <summary>
        /// Handles the command.
        /// </summary>
        /// <param name="driver">The driver used to execute the command.</param>
        /// <param name="locator">The first parameter to the command.</param>
        /// <param name="value">The second parameter to the command.</param>
        /// <returns>The result of the command.</returns>
        protected override object HandleSeleneseCommand(IWebDriver driver, string locator, string value)
        {
            string script = JavaScriptLibrary.GetSeleniumScript("isTextPresent.js");

            object result = ((IJavaScriptExecutor)driver).ExecuteScript("return (" + script + ")(arguments[0]);", locator);

            // Handle the null case
            return(result is bool && (bool)result);
        }
Esempio n. 5
0
        /// <summary>
        /// Initializes a new instance of the <see cref="ElementFinder"/> class.
        /// </summary>
        public ElementFinder()
        {
            string rawScript = JavaScriptLibrary.GetSeleniumScript("findElement.js");

            this.findElement = "return (" + rawScript + ")(arguments[0]);";

            string linkTextLocator = "return (" + JavaScriptLibrary.GetSeleniumScript("linkLocator.js") + ").call(null, arguments[0], document)";

            this.AddStrategy("link", linkTextLocator);
        }
        /// <summary>
        /// Initializes a new instance of the <see cref="SeleniumSelect"/> class.
        /// </summary>
        /// <param name="finder">An <see cref="ElementFinder"/> used in finding options.</param>
        /// <param name="driver">An <see cref="IWebDriver"/> used to drive the browser.</param>
        /// <param name="locator">A locator used to find options.</param>
        public SeleniumSelect(ElementFinder finder, IWebDriver driver, string locator)
        {
            this.driver = driver;

            this.findOption = "return (" + JavaScriptLibrary.GetSeleniumScript("findOption.js") + ").apply(null, arguments)";

            this.select = finder.FindElement(driver, locator);
            if (this.select.TagName.ToLowerInvariant() != "select")
            {
                throw new SeleniumException("Element is not a select element: " + locator);
            }
        }
Esempio n. 7
0
        /// <summary>
        /// Handles the command.
        /// </summary>
        /// <param name="driver">The driver used to execute the command.</param>
        /// <param name="locator">The first parameter to the command.</param>
        /// <param name="value">The second parameter to the command.</param>
        /// <returns>The result of the command.</returns>
        protected override object HandleSeleneseCommand(IWebDriver driver, string locator, string value)
        {
            object[] parameters = new object[] 
            { 
                value, 
                this.keyState.ControlKeyDown, 
                this.keyState.AltKeyDown, 
                this.keyState.ShiftKeyDown, 
                this.keyState.MetaKeyDown 
            };

            JavaScriptLibrary.CallEmbeddedSelenium(driver, this.eventName, this.finder.FindElement(driver, locator), parameters);

            return null;
        }
        /// <summary>
        /// Initializes a new instance of the <see cref="CompoundMutator"/> class.
        /// </summary>
        /// <param name="basePath">The URL to use in mutating the script.</param>
        public CompoundMutator(string basePath)
        {
            this.AddMutator(new VariableDeclaration("selenium", "var selenium = {};"));
            this.AddMutator(new VariableDeclaration("selenium.browserbot", "selenium.browserbot = {};"));
            this.AddMutator(new VariableDeclaration("selenium.browserbot.baseUrl", "selenium.browserbot.baseUrl = '" + basePath + "';"));

            this.AddMutator(new FunctionDeclaration("selenium.page", "if (!selenium.browserbot) { selenium.browserbot = {} }; return selenium.browserbot;"));
            this.AddMutator(new FunctionDeclaration("selenium.browserbot.getCurrentWindow", "return window;"));
            this.AddMutator(new FunctionDeclaration("selenium.page().getCurrentWindow", "return window;"));
            this.AddMutator(new FunctionDeclaration("selenium.browserbot.getDocument", "return document;"));
            this.AddMutator(new FunctionDeclaration("selenium.page().getDocument", "return document;"));

            this.AddMutator(new SeleniumMutator("selenium.isElementPresent", JavaScriptLibrary.GetSeleniumScript("isElementPresent.js")));
            this.AddMutator(new SeleniumMutator("selenium.isTextPresent", JavaScriptLibrary.GetSeleniumScript("isTextPresent.js")));
            this.AddMutator(new SeleniumMutator("selenium.isVisible", JavaScriptLibrary.GetSeleniumScript("isVisible.js")));
            this.AddMutator(new SeleniumMutator("selenium.browserbot.findElement", JavaScriptLibrary.GetSeleniumScript("findElement.js")));
        }
Esempio n. 9
0
        /// <summary>
        /// Handles the command.
        /// </summary>
        /// <param name="driver">The driver used to execute the command.</param>
        /// <param name="locator">The first parameter to the command.</param>
        /// <param name="value">The second parameter to the command.</param>
        /// <returns>The result of the command.</returns>
        protected override object HandleSeleneseCommand(IWebDriver driver, string locator, string value)
        {
            string getText = JavaScriptLibrary.GetSeleniumScript("getText.js");

            try
            {
                return((string)((IJavaScriptExecutor)driver).ExecuteScript("return (" + getText + ")(arguments[0]);", locator));
            }
            catch (WebDriverException)
            {
                // TODO(simon): remove fall back for IE driver
                IWebElement element = this.finder.FindElement(driver, locator);
                return(element.Text);
            }
            catch (InvalidOperationException)
            {
                // TODO(simon): remove fall back for IE driver
                IWebElement element = this.finder.FindElement(driver, locator);
                return(element.Text);
            }
        }
Esempio n. 10
0
        /// <summary>
        /// Handles the command.
        /// </summary>
        /// <param name="driver">The driver used to execute the command.</param>
        /// <param name="locator">The first parameter to the command.</param>
        /// <param name="value">The second parameter to the command.</param>
        /// <returns>The result of the command.</returns>
        protected override object HandleSeleneseCommand(IWebDriver driver, string locator, string value)
        {
            IWebElement one = this.finder.FindElement(driver, locator);
            IWebElement two = this.finder.FindElement(driver, value);

            string ordered =
                "    if (arguments[0] === arguments[1]) return false;\n" +
                "\n" +
                "    var previousSibling;\n" +
                "    while ((previousSibling = arguments[1].previousSibling) != null) {\n" +
                "        if (previousSibling === arguments[0]) {\n" +
                "            return true;\n" +
                "        }\n" +
                "        arguments[1] = previousSibling;\n" +
                "    }\n" +
                "    return false;\n";

            bool?result = (bool)JavaScriptLibrary.ExecuteScript(driver, ordered, one, two);

            return(result != null && result.Value);
        }
Esempio n. 11
0
        /// <summary>
        /// Handles the command.
        /// </summary>
        /// <param name="driver">The driver used to execute the command.</param>
        /// <param name="locator">The first parameter to the command.</param>
        /// <param name="value">The second parameter to the command.</param>
        /// <returns>The result of the command.</returns>
        protected override object HandleSeleneseCommand(IWebDriver driver, string locator, string value)
        {
            string tableString = string.Empty;

            if (!TableParts.IsMatch(locator))
            {
                throw new SeleniumException("Invalid target format. Correct format is tableName.rowNum.columnNum");
            }

            Match  tableMatch = TableParts.Match(locator);
            string tableName  = tableMatch.Groups[1].Value;
            long   row        = int.Parse(tableMatch.Groups[2].Value, CultureInfo.InvariantCulture);
            long   col        = int.Parse(tableMatch.Groups[3].Value, CultureInfo.InvariantCulture);

            IWebElement table = this.finder.FindElement(driver, tableName);

            string script =
                "var table = arguments[0]; var row = arguments[1]; var col = arguments[2];" +
                "if (row > table.rows.length) { return \"Cannot access row \" + row + \" - table has \" + table.rows.length + \" rows\"; }" +
                "if (col > table.rows[row].cells.length) { return \"Cannot access column \" + col + \" - table row has \" + table.rows[row].cells.length + \" columns\"; }" +
                "return table.rows[row].cells[col];";

            object      returnValue     = JavaScriptLibrary.ExecuteScript(driver, script, table, row, col);
            IWebElement elementReturned = returnValue as IWebElement;

            if (elementReturned != null)
            {
                tableString = elementReturned.Text.Trim();
            }
            else
            {
                throw new SeleniumException(returnValue.ToString());
            }

            return(tableString);
        }
Esempio n. 12
0
 /// <summary>
 /// Initializes a new instance of the <see cref="MouseEvent"/> class.
 /// </summary>
 /// <param name="elementFinder">An <see cref="ElementFinder"/> used to find the element on which to execute the command.</param>
 /// <param name="eventType">Type of event to trigger.</param>
 public MouseEvent(ElementFinder elementFinder, string eventType)
 {
     this.finder = elementFinder;
     this.type   = eventType;
     this.fire   = "return (" + JavaScriptLibrary.GetSeleniumScript("fireEvent.js") + ").apply(null, arguments);";
 }
 /// <summary>
 /// Initializes a new instance of the <see cref="IsSomethingSelected"/> class.
 /// </summary>
 public IsSomethingSelected()
 {
     this.script = "return (" + JavaScriptLibrary.GetSeleniumScript("isSomethingSelected.js") + ").apply(null, arguments)";
 }
        /// <summary>
        /// Handles the command.
        /// </summary>
        /// <param name="driver">The driver used to execute the command.</param>
        /// <param name="locator">The first parameter to the command.</param>
        /// <param name="value">The second parameter to the command.</param>
        /// <returns>The result of the command.</returns>
        protected override object HandleSeleneseCommand(IWebDriver driver, string locator, string value)
        {
            object returnValue = JavaScriptLibrary.ExecuteScript(driver, this.script, locator);

            return(returnValue is bool && (bool)returnValue);
        }
Esempio n. 15
0
 /// <summary>
 /// Initializes a new instance of the <see cref="FireEvent"/> class.
 /// </summary>
 /// <param name="elementFinder">An <see cref="ElementFinder"/> used to find the element on which to execute the command.</param>
 public FireEvent(ElementFinder elementFinder)
 {
     this.finder = elementFinder;
     this.fire   = "return (" + JavaScriptLibrary.GetSeleniumScript("fireEvent.js") + ").apply(null, arguments);";
 }
Esempio n. 16
0
 /// <summary>
 /// Initializes a new instance of the <see cref="FireNamedEvent"/> class.
 /// </summary>
 /// <param name="elementFinder">An <see cref="ElementFinder"/> used to find the element on which to execute the command.</param>
 /// <param name="eventName">The name of the event to fire.</param>
 public FireNamedEvent(ElementFinder elementFinder, string eventName)
 {
     this.finder = elementFinder;
     this.name   = eventName;
     this.fire   = "return (" + JavaScriptLibrary.GetSeleniumScript("fireEvent.js") + ").apply(null, arguments);";
 }
Esempio n. 17
0
 /// <summary>
 /// Handles the command.
 /// </summary>
 /// <param name="driver">The driver used to execute the command.</param>
 /// <param name="locator">The first parameter to the command.</param>
 /// <param name="value">The second parameter to the command.</param>
 /// <returns>The result of the command.</returns>
 protected override object HandleSeleneseCommand(IWebDriver driver, string locator, string value)
 {
     JavaScriptLibrary.ExecuteScript(driver, "arguments[0].id = arguments[1]", this.finder.FindElement(driver, locator), value);
     return(null);
 }
        /// <summary>
        /// Handles the command.
        /// </summary>
        /// <param name="driver">The driver used to execute the command.</param>
        /// <param name="locator">The first parameter to the command.</param>
        /// <param name="value">The second parameter to the command.</param>
        /// <returns>The result of the command.</returns>
        protected override object HandleSeleneseCommand(IWebDriver driver, string locator, string value)
        {
            IWebElement element = this.finder.FindElement(driver, locator);

            return(JavaScriptLibrary.ExecuteScript(driver, ElementIndexFinderScript, element));
        }
Esempio n. 19
0
        /// <summary>
        /// Handles the command.
        /// </summary>
        /// <param name="driver">The driver used to execute the command.</param>
        /// <param name="locator">The first parameter to the command.</param>
        /// <param name="value">The second parameter to the command.</param>
        /// <returns>The result of the command.</returns>
        protected override object HandleSeleneseCommand(IWebDriver driver, string locator, string value)
        {
            JavaScriptLibrary.CallEmbeddedHtmlUtilities(driver, "highlight", this.finder.FindElement(driver, locator));

            return(null);
        }