ExecuteScript() публичный Метод

Executes JavaScript in the context of the currently selected frame or window
public ExecuteScript ( string script ) : object
script string The JavaScript code to execute.
Результат object
 private static void WaitUnitJavascriptTrue(RemoteWebDriver driver, string javascript)
 {
     Func<IWebDriver, bool> condition = delegate
                                            {
                                                var scriptResult = driver.ExecuteScript(javascript);
                                                var isScriptActive = (bool)scriptResult;
                                                return isScriptActive;
                                            };
     driver.Wait().Until(condition);
 }
Пример #2
0
        public static void ExecuteTest(this RemoteWebDriver driver, string testScript, params object[] args)
        {
            if (driver == null)
            {
                throw new ArgumentNullException(nameof(driver));
            }

            if (testScript == null)
            {
                throw new ArgumentNullException(nameof(testScript));
            }

            IWebElement testsConsole = driver.GetElementById("testsConsole");

            const string success = "Success";

            string consoleValue = testsConsole.GetAttribute("value");

            string arguments = JsonConvert.SerializeObject(args, new JsonSerializerSettings
            {
                DateFormatHandling    = DateFormatHandling.IsoDateFormat,
                ReferenceLoopHandling = ReferenceLoopHandling.Ignore
            });

            string finalTestScript = $"executeTest({testScript},'{arguments}');";

            driver.WaitForControlReady();

            driver.ExecuteScript(finalTestScript);

            int triesCount = 0;

            do
            {
                if (testsConsole.GetAttribute("value") != consoleValue)
                {
                    break;
                }
                Thread.Sleep(200);
                triesCount++;
            } while (triesCount < 50);

            consoleValue = testsConsole.GetAttribute("value");

            bool testIsPassed = consoleValue == success;

            Console.WriteLine("Test console value: " + consoleValue);
            Console.WriteLine("Url: " + driver.Url);
            Console.WriteLine("Final test script: " + finalTestScript);

            if (testIsPassed != true)
            {
                throw new InvalidOperationException(consoleValue);
            }
        }
Пример #3
0
        public void SmokeTestSetup()
        {
            driver = new FirefoxDriver();

            _runner = new NzbDroneRunner(LogManager.GetCurrentClassLogger());
            _runner.KillAll();
            _runner.Start();

            driver.Url = "http://localhost:8989";

            var page = new PageBase(driver);
            page.WaitForNoSpinner();

            driver.ExecuteScript("window.NzbDrone.NameViews = true;");

            GetPageErrors().Should().BeEmpty();
        }
Пример #4
0
 public bool Connect()
 {
     try
     {
         Dictionary<string, object> capsDef = new Dictionary<string, object>();
         capsDef.Add("device", _Model.Settings.InspectorDeviceCapability.ToString());
         ICapabilities capabilities = new DesiredCapabilities(capsDef);
         _Driver = new ScreenshotRemoteWebDriver(new Uri("http://" + this._Model.IPAddress + ":" + this._Model.Port.ToString() + "/wd/hub"), capabilities);
         // add increased timeout for inspector connection
         Dictionary<string, int> args = new Dictionary<string, int>();
         args.Add("timeout", 900);
         _Driver.ExecuteScript("mobile: setCommandTimeout", new object[] { args });
     }
     catch (Exception e)
     {
         LastMessage = e.Message;
         return false;
     }
     return true;
 }
 private string GetUserAgent(RemoteWebDriver driver)
 {
     return driver.ExecuteScript("return window.navigator.userAgent;").ToString();
 }
 private static void ReportCapabilities(RemoteWebDriver driver)
 {
     Log.InfoFormat("Driver Capabilities: {0}", driver.Capabilities);
     Log.InfoFormat("User agent: {0}", driver.ExecuteScript("return navigator.userAgent;"));
 }
Пример #7
0
 private static void SaveHtmlAndScreenShot(Uri uri, RemoteWebDriver _driver)
 {
     try
     {
         var removeScriptTag =
             "Array.prototype.slice.call(document.getElementsByTagName('script')).forEach(function(item) { item.parentNode.removeChild(item);});";
         var addClassToBody = "document.getElementsByTagName('body')[0].className += ' seoPrerender';";
         
         _driver.ExecuteScript(removeScriptTag + addClassToBody);   
         //uri.AbsolutePath is relative url
         var result = _driver.PageSource;
         string filenameWithPath = _options.FolderPath + uri.AbsolutePath + MakeValidFileName(uri.Query);
         Directory.CreateDirectory(Path.GetDirectoryName(filenameWithPath));
         File.WriteAllText(filenameWithPath + ".html", result);
         logger.Info("SaveHtmlAndScreenShot to {0}.html", filenameWithPath);
         _driver.GetScreenshot().SaveAsFile(filenameWithPath + ".jpg", ImageFormat.Jpeg);
     }
     catch (Exception ex)
     {
         logger.Error(ex);
     }
 }
Пример #8
0
 public static void SetDateValueForJQueryDatepicker(RemoteWebDriver browser, string elementSelector, DateTime value)
 {
     var formattedDate = value.ToString("d MMMM yyyy");
     browser.FindElementByCssSelector(elementSelector);
     browser.ExecuteScript("$(arguments[0]).datepicker('setDate', arguments[1]);", elementSelector, formattedDate);
 }
Пример #9
0
 private void BlockUntilElementIsAvailable(RemoteWebDriver driver, string elementSelector)
 {
     while (true)
     {
         var len = driver.ExecuteScript("return $(\"" + elementSelector + "\").length");
         if (Convert.ToInt32(len) > 0)
             break;
         Thread.Sleep(100);
     }
 }