public void Initialize_should_be_able_to_start_new_browser() { WebDriverOptions options = new WebDriverOptions() { BrowserName = WebDriverOptions.browser_HtmlUnitWithJavaScript, IsRemote = true, RemoteUrl = "http://localhost:4444/wd/hub/", }; bool isSeleniumServerAvailable = true; try { SwdBrowser.TestRemoteHub(options.RemoteUrl); } catch (Exception e) { isSeleniumServerAvailable = false; Console.WriteLine("FAILED: " + e.Message); } if (!isSeleniumServerAvailable) { SwdBrowser.RunStandaloneServer("start_selenium_server.bat"); } SwdBrowser.Initialize(options); var rempteDriver = (RemoteWebDriver)SwdBrowser.GetDriver(); rempteDriver.Capabilities.BrowserName.Should().Be("htmlunit"); SwdBrowser.CloseDriver(); }
internal Task TakeAndSaveScreenshot() { var task = new Task(() => { EnsureScreenshotsDirectoryExists(); MyLog.Write("Action: TakeAndSaveScreenshot()"); try { var pageUrl = new Uri(SwdBrowser.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 = SwdBrowser.TakeScreenshot(); screenshot.SaveAsFile(newFilePath, ImageFormat.Png); } catch (Exception ex) { MyLog.Write("Action: TakeAndSaveScreenshot() FAILED"); MyLog.Exception(ex); throw; } }); task.Start(); return(task); }
public static object JS(string script) { var driver = SwdBrowser.GetDriver(); IJavaScriptExecutor jsExec = driver as IJavaScriptExecutor; return(jsExec.ExecuteScript(script)); }
public static void LoadTestFile(string pageRelativePath) { string fullPath = Path.Combine(AssemblyDirectory(), "TestResource", pageRelativePath); Uri uri = new Uri(fullPath); string uriPath = uri.AbsoluteUri; SwdBrowser.GetDriver().Url = uriPath; }
internal async void RunScript(string code) { Presenters.SwdMainPresenter.DisplayLoadingIndicator(true); Task <string> t = new Task <string>(() => { using (var engine = new JScriptEngine()) { engine.AddHostObject("driver", SwdBrowser.GetDriver()); ImportTypes(engine); var uiPageObject = Presenters.PageObjectDefinitionPresenter.GetWebElementDefinitionFromTree(); foreach (var element in uiPageObject.Items) { IWebElement proxyElement = SwdBrowser.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.SwdMainPresenter.DisplayLoadingIndicator(false); } }
internal void LoadCapabilities() { var remoteDriver = (RemoteWebDriver)SwdBrowser.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(); }
void UserCommand_OnMouseClick(UserCommands.MouseClickCommand evt) { MouseEventArgs mouse = evt.MouseEvent; var imageViewPort = imgBox.GetImageViewPort(); int absoluteX = mouse.X - imageViewPort.Left; int absoluteY = mouse.Y - imageViewPort.Top; absoluteX = (Convert.ToInt32(absoluteX / imgBox.ZoomFactor) + Convert.ToInt32(imgBox.HorizontalScroll.Value / imgBox.ZoomFactor)); absoluteY = Convert.ToInt32(absoluteY / imgBox.ZoomFactor) + Convert.ToInt32(imgBox.VerticalScroll.Value / imgBox.ZoomFactor); txtLog.DoInvokeAction(() => { txtLog.Text += "\r\n" + "X " + absoluteX.ToString() + " Y " + absoluteY.ToString(); }); if (!ModifierKeys.HasFlag(WINKeys.Control)) { } ; var clickCommand = @"return (function SWD_GET_ELEMENT() { var absoluteX = {## absoluteX ##}; var absoluteY = {## absoluteY ##}; var view = { Left: window.scrollX, Top: window.scrollY, Right: window.scrollX + window.innerWidth, Bottom: window.scrollY + window.innerHeight }; var elementInsideViewPort = (view.Left <= absoluteX && absoluteX <= view.Right) && (view.Top <= absoluteY && absoluteY <= view.Bottom); if (!elementInsideViewPort) { window.scroll(Math.max(absoluteX - 30, 0), Math.max(absoluteY - 30, 0)); } absoluteX = absoluteX - window.scrollX; absoluteY = absoluteY - window.scrollY; return document.elementFromPoint(absoluteX, absoluteY); })()" .Replace("{## absoluteX ##}", absoluteX.ToString()) .Replace("{## absoluteY ##}", absoluteY.ToString()); IWebElement element = (IWebElement)SwdBrowser.ExecuteJavaScript(clickCommand); // element.Click(); if (element == null) { MyLog.Error("Element is null after performing command " + clickCommand ?? "<null>"); return; } if (element.TagName == "iframe") { try { var frameLocation = element.Location; absoluteX = absoluteX - frameLocation.X; absoluteY = absoluteY - frameLocation.Y; MyLog.Debug("Switching to IFrame"); SwdBrowser.GetDriver().SwitchTo().Frame(element); clickCommand = String.Format(@"return document.elementFromPoint({0}, {1});", absoluteX, absoluteY); element = (IWebElement)SwdBrowser.ExecuteJavaScript(clickCommand); ClickOnElement(absoluteX, absoluteY, element); } finally { SwdBrowser.SwitchToDefaultContent(); } } else { ClickOnElement(absoluteX, absoluteY, element); } }
private void imgBox_KeyPress(object sender, KeyPressEventArgs e) { SwdBrowser.GetDriver().SwitchTo().ActiveElement().SendKeys(e.KeyChar.ToString()); }
public static void ToFrame(int index) { var driver = SwdBrowser.GetDriver(); driver.SwitchTo().Frame(index); }
public static void ClickId(string elementId) { var element = SwdBrowser.GetDriver().FindElement(By.Id(elementId)); element.Click(); }