ExecuteScript() public method

Executes JavaScript in the context of the currently selected frame or window.

The ExecuteScriptmethod executes JavaScript in the context of the currently selected frame or window. This means that "document" will refer to the current document. If the script has a return value, then the following steps will be taken:

For an HTML element, this method returns a IWebElement For a number, a System.Int64 is returned For a boolean, a System.Boolean is returned For all other cases a System.String is returned. For an array,we check the first element, and attempt to return a List{T} of that type, following the rules above. Nested lists are not supported. If the value is null or there is no return value, is returned.

Arguments must be a number (which will be converted to a System.Int64), a System.Boolean, a System.String or a IWebElement. An exception will be thrown if the arguments do not meet these criteria. The arguments will be made available to the JavaScript via the "arguments" magic variable, as if the function were called via "Function.apply"

public ExecuteScript ( string script ) : object
script string The JavaScript code to execute.
return object
Esempio n. 1
0
        public void ShouldUnwrapElementArgsWhenCallingScripts()
        {
            Mock <IExecutingDriver> executingDriver = mocks.CreateMock <IExecutingDriver>();

            executingDriver.Expects.One.Method(_ => _.FindElement(null)).With(By.Id("foo")).WillReturn(mockElement.MockObject);
            executingDriver.Expects.One.Method(_ => _.ExecuteScript(null, null)).With("foo", new[] { mockElement.MockObject }).WillReturn("foo");

            EventFiringWebDriver testedDriver = new EventFiringWebDriver(executingDriver.MockObject);

            IWebElement element = testedDriver.FindElement(By.Id("foo"));

            try
            {
                testedDriver.ExecuteScript("foo", element);
            }
            catch (Exception e)
            {
                // This is the error we're trying to fix
                throw e;
            }
        }
Esempio n. 2
0
        public void ShouldUnwrapElementArgsWhenCallingScripts()
        {
            Mock <IExecutingDriver> executingDriver = new Mock <IExecutingDriver>();

            executingDriver.Setup(_ => _.FindElement(It.Is <By>(x => x.Equals(By.Id("foo"))))).Returns(mockElement.Object);
            executingDriver.Setup(_ => _.ExecuteScript(It.IsAny <string>(), It.IsAny <object[]>())).Returns("foo");

            EventFiringWebDriver testedDriver = new EventFiringWebDriver(executingDriver.Object);

            IWebElement element = testedDriver.FindElement(By.Id("foo"));

            try
            {
                testedDriver.ExecuteScript("foo", element);
            }
            catch (Exception e)
            {
                // This is the error we're trying to fix
                throw e;
            }
        }
        public void ShouldUnwrapElementArgsWhenCallingScripts()
        {
            IExecutingDriver executingDriver = mocks.NewMock <IExecutingDriver>();

            Expect.Once.On(executingDriver).Method("FindElement").With(By.Id("foo")).Will(Return.Value(mockElement));
            Expect.Once.On(executingDriver).Method("ExecuteScript").With("foo", new[] { mockElement }).Will(Return.Value("foo"));

            EventFiringWebDriver testedDriver = new EventFiringWebDriver(executingDriver);

            IWebElement element = testedDriver.FindElement(By.Id("foo"));

            try
            {
                testedDriver.ExecuteScript("foo", element);
            }
            catch (Exception e)
            {
                // This is the error we're trying to fix
                throw e;
            }
        }
        public void ShouldUnwrapElementArgsWhenCallingScripts()
        {
            IExecutingDriver executingDriver = mocks.NewMock<IExecutingDriver>();
            Expect.Once.On(executingDriver).Method("FindElement").With(By.Id("foo")).Will(Return.Value(mockElement));
            Expect.Once.On(executingDriver).Method("ExecuteScript").With("foo", new[] { mockElement }).Will(Return.Value("foo"));

            EventFiringWebDriver testedDriver = new EventFiringWebDriver(executingDriver);

            IWebElement element = testedDriver.FindElement(By.Id("foo"));
            try
            {
                testedDriver.ExecuteScript("foo", element);
            }
            catch (Exception e)
            {
                // This is the error we're trying to fix
                throw e;
            }
        }