public static object JS(string script) { var driver = WebSpyBrowser.GetDriver(); IJavaScriptExecutor jsExec = driver as IJavaScriptExecutor; return(jsExec.ExecuteScript(script)); }
internal Task TakeAndSaveScreenshot() { var task = new Task(() => { EnsureScreenshotsDirectoryExists(); MyLog.Write("Action: TakeAndSaveScreenshot()"); try { var pageUrl = new Uri(WebSpyBrowser.GetDriver().Url); var host = pageUrl.Host; string newFileName = DateTime.Now.ToString("yyyy-dd-M__HH-mm-ss") + "_" + host + ".png"; string newFilePath = Path.Combine(ScreenshotsLocation, newFileName); Screenshot screenshot = WebSpyBrowser.TakeScreenshot(); screenshot.SaveAsFile(newFilePath, ImageFormat.Png); } catch (Exception ex) { MyLog.Write("Action: TakeAndSaveScreenshot() FAILED"); MyLog.Exception(ex); throw; } }); task.Start(); return(task); }
public static void LoadTestFile(string pageRelativePath) { string fullPath = Path.Combine(AssemblyDirectory(), "TestResource", pageRelativePath); Uri uri = new Uri(fullPath); string uriPath = uri.AbsoluteUri; WebSpyBrowser.GetDriver().Url = uriPath; }
internal async void RunScript(string code) { Presenters.WebSpyMainPresenter.DisplayLoadingIndicator(true); Task <string> t = new Task <string>(() => { using (var engine = new JScriptEngine()) { engine.AddHostObject("driver", WebSpyBrowser.GetDriver()); ImportTypes(engine); var uiPageObject = Presenters.PageObjectDefinitionPresenter.GetWebElementDefinitionFromTree(); foreach (var element in uiPageObject.Items) { IWebElement proxyElement = WebSpyBrowser.CreateWebElementProxy(element); string name = element.Name; engine.AddHostObject(name, proxyElement); } var result = engine.Evaluate(code) ?? "(none)"; return(result.ToString()); } }); string logLine = "done"; try { t.Start(); logLine = await t; } catch (Exception ex) { MyLog.Exception(ex); logLine = "ERROR: " + ex.Message; // TODO: FIX message --> Exception has been thrown by the target of invocation // \TODO: FIX message --> Exception has been thrown by the target of invocation } finally { view.AppendConsole(logLine + "\r\n"); Presenters.WebSpyMainPresenter.DisplayLoadingIndicator(false); } }
public void Initialize_should_be_able_to_start_new_browser() { Profile browserProfile = new Profile(); browserProfile.ProfileConfig.activation.browserName = WebDriverOptions.browser_HtmlUnitWithJavaScript; WebDriverOptions options = new WebDriverOptions() { BrowserProfile = browserProfile, IsRemote = true, RemoteUrl = "http://localhost:4444/wd/hub/", }; bool isSeleniumServerAvailable = true; try { WebSpyBrowser.TestRemoteHub(options.RemoteUrl); } catch (Exception e) { isSeleniumServerAvailable = false; Console.WriteLine("FAILED: " + e.Message); } if (!isSeleniumServerAvailable) { WebSpyBrowser.RunStandaloneServer("start_selenium_server.bat"); } WebSpyBrowser.Initialize(options); var rempteDriver = (RemoteWebDriver)WebSpyBrowser.GetDriver(); rempteDriver.Capabilities.BrowserName.Should().Be("htmlunit"); WebSpyBrowser.CloseDriver(); }
internal void LoadCapabilities() { var remoteDriver = (RemoteWebDriver)WebSpyBrowser.GetDriver(); foreach (var prop in _desiredCapabilitiesdata.GetType().GetProperties()) { string capabilityName = prop.Name.Replace("__", "."); if (!remoteDriver.Capabilities.HasCapability(capabilityName)) { continue; } object driverValue = remoteDriver.Capabilities.GetCapability(capabilityName); if (driverValue == null) { continue; } if (prop.PropertyType == typeof(bool?)) { bool?boolValue = GetValueOrNull <bool>(driverValue.ToString()); prop.SetValue(_desiredCapabilitiesdata, boolValue, null); } else if (prop.PropertyType == typeof(int?)) { int?intValue = GetValueOrNull <int>(driverValue.ToString()); prop.SetValue(_desiredCapabilitiesdata, intValue, null); } else { prop.SetValue(_desiredCapabilitiesdata, driverValue.ToString(), null); } } InitDesiredCapabilities(); }
public static void ToFrame(int index) { var driver = WebSpyBrowser.GetDriver(); driver.SwitchTo().Frame(index); }
public static void ClickId(string elementId) { var element = WebSpyBrowser.GetDriver().FindElement(By.Id(elementId)); element.Click(); }