public virtual void TestCleanUp() { var testResult = TestContext.CurrentTestOutcome; bool isTestFailed = !(testResult == UnitTestOutcome.Passed || testResult == UnitTestOutcome.Inconclusive); if (isTestFailed) { try { var myUniqueFileName = string.Format(@"screenshot_{0}.png", Guid.NewGuid()); var fullPath = Path.Combine(Path.GetTempPath(), myUniqueFileName); var screenshot = SwdBrowser.TakeScreenshot(); screenshot.SaveAsFile(fullPath, ImageFormat.Png); // Attach image to the log file TestContext.AddResultFile(fullPath); } catch (Exception e) { Console.WriteLine("Unable to take screenshot:" + e.Message); } } bool shouldRestartBrowser = isTestFailed; if (shouldRestartBrowser) { SwdBrowser.CloseDriver(); } }
public void VisualSearch_UpdateSearchResult() { try { MyLog.Write("VisualSearch_UpdateSearchResult: Started"); while (stopVisualSearch == false) { try { if (!SwdBrowser.IsVisualSearchScriptInjected()) { MyLog.Write("VisualSearch_UpdateSearchResult: Found the Visual search is not injected. Injecting"); SwdBrowser.InjectVisualSearch(); } ProcessCommands(); } catch (Exception e) { StopVisualSearch(); MyLog.Exception(e); } Thread.Sleep(VisualSearchQueryDelayMs); } } finally { StopVisualSearch(); MyLog.Write("VisualSearch_UpdateSearchResult: Finished"); } }
public void StopDriver() { MyLog.Write("StopDriver - Entered"); view.DisableDriverStartButton(); view.DriverIsStopping(); Exception threadException; UIActions.PerformSlowOperation( "Operation: Stop WebDriver instance", () => { Presenters.SwdMainPresenter.StopVisualSearch(); SwdBrowser.CloseDriver(); }, out threadException, null, TimeSpan.FromMinutes(10) ); if (threadException != null) { MyLog.Exception(threadException); } view.EnableDriverStartButton(); wasBrowserStarted = false; MyLog.Write("StopDriver - Exited"); }
public void CloseDriver_should_close_the_opened_browser_instance() { WebDriverOptions options = new WebDriverOptions() { BrowserName = WebDriverOptions.browser_Firefox, IsRemote = false, }; string specialTitle = "SwdBrowser.CloseDriver TEST TEST"; string[] specialWindows = new string[] { }; specialWindows = GetDesktopWindowsWithSpecialTitle(specialTitle); specialWindows.Length.Should().Be(0, "Expected no windows with title <{0}> at the beginning", specialTitle); SwdBrowser.Initialize(options); string changeTitleScript = string.Format("document.title = '{0}'", specialTitle); SwdBrowser.ExecuteJavaScript(changeTitleScript); specialWindows = GetDesktopWindowsWithSpecialTitle(specialTitle); specialWindows.Length.Should().Be(1, "Expected only 1 window with title <{0}> after new driver was created", specialTitle); SwdBrowser.CloseDriver(); specialWindows = GetDesktopWindowsWithSpecialTitle(specialTitle); specialWindows.Length.Should().Be(0, "Expected no windows with title <{0}> after the driver was closed", specialTitle); }
internal void RunScript() { string scriptFromEditor = view.GetJavaScriptCodeFromEditor(); Exception outException; bool isOk = false; object result = null; isOk = UIActions.PerformSlowOperation( "Operation: RunScript() / Executing JavaScript Snippet", () => { result = SwdBrowser.ExecuteJavaScript(scriptFromEditor); }, out outException, null, TimeSpan.FromMinutes(1) ); if (!isOk) { MyLog.Error("RunScript() Failed to execute JavaScript snippet"); MyLog.Exception(outException); if (outException != null) { throw outException; } } string consoleOut = DumpObject(result); view.AppendConsole(consoleOut); }
internal void HighLightElementFromNode(TreeNode treeNode) { string xpath = GetXPathFromTreeNode(treeNode); var by = Utils.ByFromLocatorSearchMethod(LocatorSearchMethod.XPath, xpath); SwdBrowser.HighlightElement(by); }
public void Get_Frames_Tree() { string[] expectedTitles = new string[] { "DefaultContent", "firstFrame", "secondFrame", "secondFrame.idIframeInsideSecondFrame", "thirdFrame", "thirdFrame.0" }; Helper.RunDefaultBrowser(); Helper.LoadTestFile("page_with_frames.html"); BrowserPageFrame rootFrame = SwdBrowser.GetPageFramesTree(); List <BrowserPageFrame> allFrames = rootFrame.ToList(); string[] actualTitles = allFrames.Select(i => i.ToString()).ToArray(); actualTitles.Should().Equal(expectedTitles); SwdBrowser.CloseDriver(); }
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(); }
public void RefreshWindowsList() { Exception outException; bool isOk = false; isOk = UIActions.PerformSlowOperation( "Operation: Refresh All Windows List", () => { BrowserWindow[] currentWindows = SwdBrowser.GetBrowserWindows(); string currentWindowHandle = SwdBrowser.GetCurrentWindowHandle(); view.UpdateBrowserWindowsList(currentWindows, currentWindowHandle); }, out outException, null, TimeSpan.FromMinutes(1) ); if (!isOk) { MyLog.Error("Failed to refresh All Windows List"); if (outException != null) { throw outException; } } }
public void Enumerate_Windows_Popup() { Helper.RunDefaultBrowser(); Helper.LoadTestFile("page_opens_several_tabs.html"); Helper.ClickId("openTab2"); Helper.ClickId("openTab3"); Helper.ClickId("openJavaScriptPopup"); BrowserWindow[] actualWindows = SwdBrowser.GetBrowserWindows(); string[] expectedWindowTitles = new string[] { "Page Tab1", "JavaScript Popup", "Page Tab3", "Page Tab2" }; string[] actualTitles = actualWindows.Select(w => w.Title).ToArray(); actualTitles.Should().Contain(expectedWindowTitles); SwdBrowser.CloseDriver(); }
public void RefreshFramesList() { Exception outException; bool isOk = false; isOk = UIActions.PerformSlowOperation( "Operation: Refresh All Frames List", () => { BrowserPageFrame rootFrame = SwdBrowser.GetPageFramesTree(); BrowserPageFrame[] currentPageFrames = rootFrame.ToList().ToArray(); SwdBrowser.SwitchToDefaultContent(); view.UpdatePageFramesList(currentPageFrames); }, out outException, null, TimeSpan.FromMinutes(1) ); if (!isOk) { MyLog.Error("Failed to refresh All Frames List"); MyLog.Exception(outException); if (outException != null) { throw outException; } } }
public static object JS(string script) { var driver = SwdBrowser.GetDriver(); IJavaScriptExecutor jsExec = driver as IJavaScriptExecutor; return(jsExec.ExecuteScript(script)); }
private static void DrawMarker(int absoluteX, int absoluteY) { string script = @"(function showRectangle(x, y) { var rectangle = document.createElement('div'); rectangle.style.background = 'red'; rectangle.style.position = 'absolute'; rectangle.style.borderRadius = '50%'; rectangle.style.left = (x - 5) + 'px'; rectangle.style.top = (y - 5) + 'px'; rectangle.style.width = '10px'; rectangle.style.height = '10px'; document.body.appendChild(rectangle); setTimeout(function cbRemoveRectangle() { if (rectangle) { rectangle.remove(); } }, 2000); })(arguments[0], arguments[1]);"; SwdBrowser.ExecuteJavaScript(script, absoluteX, absoluteY); }
public void StartDriver(WebDriverOptions browserOptions) { wasBrowserStarted = false; bool startFailure = false; view.DisableDriverStartButton(); try { SwdBrowser.Initialize(browserOptions); wasBrowserStarted = true; } catch { startFailure = true; throw; } finally { if (!startFailure) { SetDesiredCapabilities(browserOptions); view.DriverWasStarted(); } view.EnableDriverStartButton(); } }
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); }
internal void DisplayHtmlPageSource() { string singleLineSource = SwdBrowser.GetTidyHtml(); string[] htmlLines = Utils.SplitSingleLineToMultyLine(singleLineSource); view.FillHtmlCodeBox(htmlLines); }
public void StopDriver() { view.DisableDriverStartButton(); SwdBrowser.CloseDriver(); view.EnableDriverStartButton(); view.DriverWasStopped(); wasBrowserStarted = false; }
private void btnBrowser_Go_Click(object sender, EventArgs e) { presenter.SetBrowserUrl(txtBrowserUrl.Text); SwdBrowser.TakeScreenshot(); // \todo Remove this line: // MyPresenters.BrowserScreenButtonPresenter.UpdateScreenshot(); }
public void ProcessCommands() { var command = SwdBrowser.GetNextCommand(); if (command is GetXPathFromElement) { var getXPathCommand = command as GetXPathFromElement; view.UpdateVisualSearchResult(getXPathCommand.XPathValue); } else if (command is AddElement) { var addElementCommand = command as AddElement; SimpleFrame simpleFrame; BrowserPageFrame browserPageFrame = view.getCurrentlyChosenFrame(); if (browserPageFrame != null) { List <string> childs = new List <string>(); string parentTitle; if (browserPageFrame.ParentFrame != null) { parentTitle = browserPageFrame.ParentFrame.GetTitle(); } else { parentTitle = "none"; } if (browserPageFrame.ChildFrames != null) { foreach (BrowserPageFrame b in browserPageFrame.ChildFrames) { childs.Add(b.GetTitle()); } } simpleFrame = new SimpleFrame(browserPageFrame.Index, browserPageFrame.LocatorNameOrId, browserPageFrame.GetTitle(), parentTitle, childs); } else { simpleFrame = new SimpleFrame(-1, "noFrameChosen", "noFrameChosen", "noFrameChosen", null); } bool emptyHtmlId = String.IsNullOrEmpty(addElementCommand.ElementId); var element = new WebElementDefinition() { Name = addElementCommand.ElementCodeName, HtmlId = addElementCommand.ElementId, Xpath = addElementCommand.ElementXPath, HowToSearch = (emptyHtmlId) ? LocatorSearchMethod.XPath: LocatorSearchMethod.Id, Locator = (emptyHtmlId) ? addElementCommand.ElementXPath: addElementCommand.ElementId, CssSelector = addElementCommand.ElementCssSelector, frame = simpleFrame, }; bool addNew = true; Presenters.SelectorsEditPresenter.UpdateWebElementWithAdditionalProperties(element); Presenters.PageObjectDefinitionPresenter.UpdatePageDefinition(element, addNew); } }
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 void ShowElementInTree(ResultElement element) { IWebElement webElement = element.WebElement; string xPath = SwdBrowser.GetElementXPath(webElement); var travelNodes = GetTreeTravelDataFromXPath(xPath); FindTreeNode(travelNodes); }
public void EndTest() { Console.WriteLine("Start EndTest"); SwdBrowser.CloseDriver(); //SwdBrowser.Driver.Close(); //SwdBrowser.Driver.Quit(); Console.WriteLine("End EndTest"); }
public void PageTest <PAGE>(PAGE page) where PAGE : MyPageBase, new() { // Implement Dispose inside page object in order to do cleanup using (page) { page.Invoke(); page.VerifyExpectedElementsAreDisplayed(); SwdBrowser.CloseDriver(); } }
public static void RunDefaultBrowser() { WebDriverOptions options = new WebDriverOptions() { BrowserName = WebDriverOptions.browser_Firefox //BrowserName = WebDriverOptions.browser_PhantomJS, }; SwdBrowser.Initialize(options); }
public void UpdateScreenshot() { var screenshot = SwdBrowser.TakeScreenshot(); using (var ms = new MemoryStream(screenshot.AsByteArray)) { var image = Image.FromStream(ms); view.UpdateImage(image); } }
internal void HighLightWebElement(WebElementDefinition element) { if (!IsValidForm()) { return; } var by = SwdBrowser.ConvertLocatorSearchMethodToBy(element.HowToSearch, element.Locator); SwdBrowser.HighlightElement(by); }
public static void RunDefaultBrowser() { var browserProfile = new Profile(); browserProfile.ProfileConfig.activation.browserName = "Firefox"; WebDriverOptions options = new WebDriverOptions() { BrowserProfile = browserProfile, }; SwdBrowser.Initialize(options); }
public void WebElementExplorer_Test() { Helper.RunDefaultBrowser(); Helper.LoadTestFile("page_with_frames.html"); Helper.ToFrame(secondFrame); SwdBrowser.InjectVisualSearch(); //Helper.ClickId(); throw new NotImplementedException(); }
private void button1_Click(object sender, EventArgs e) { var screenshot = SwdBrowser.TakeScreenshot(); //screenshot.AsByteArray //imgBox.Zoom = 100; using (var ms = new MemoryStream(screenshot.AsByteArray)) { imgBox.Image = Image.FromStream(ms); } }
internal void SwitchToFrame(BrowserPageFrame frame) { PauseWebElementExplorerProcessing(); try { SwdBrowser.DestroyVisualSearch(); SwdBrowser.GoToFrame(frame); MyLog.Write("FRAME: Switched to frame with Index= " + frame.Index + "; and Full Name:" + frame.ToString()); } finally { ResumeWebElementExplorerProcessing(); } }