From 9fcaf5bba14880aa158202cd868968ee7e94e0c7 Mon Sep 17 00:00:00 2001 From: Teun Duynstee Date: Wed, 14 Jan 2015 08:33:21 +0100 Subject: [PATCH] Reintroduced static ClearWindows Had to rename instance scoped ClearWindows to ClearWindowsInContext Added unit tests --- .../OfflineTests/WindowsAndFrames.cs | 326 +++++++++--------- SimpleBrowser/Browser.cs | 14 +- 2 files changed, 185 insertions(+), 155 deletions(-) mode change 100644 => 100755 SimpleBrowser.UnitTests/OfflineTests/WindowsAndFrames.cs mode change 100644 => 100755 SimpleBrowser/Browser.cs diff --git a/SimpleBrowser.UnitTests/OfflineTests/WindowsAndFrames.cs b/SimpleBrowser.UnitTests/OfflineTests/WindowsAndFrames.cs old mode 100644 new mode 100755 index 2e41fb6..6bb5582 --- a/SimpleBrowser.UnitTests/OfflineTests/WindowsAndFrames.cs +++ b/SimpleBrowser.UnitTests/OfflineTests/WindowsAndFrames.cs @@ -1,154 +1,172 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using NUnit.Framework; - -namespace SimpleBrowser.UnitTests.OfflineTests -{ - [TestFixture] - public class WindowsAndFrames - { - [Test] - public void Clicking_Target_Blank() - { - Browser b = new Browser(Helper.GetMoviesRequestMocker()); - HttpRequestLog lastRequest = null; - b.RequestLogged += (br, l) => - { - lastRequest = l; - }; - b.Navigate("http://localhost/movies/"); - Assert.That(b.Url == new Uri("http://localhost/movies/")); - var link = b.Find(ElementType.Anchor, FindBy.Text, "About us"); - link.Click(); - Assert.That(b.Url == new Uri("http://localhost/movies/")); - Assert.That(b.Windows.Count() == 2); - link.Click(); - Assert.That(b.Windows.Count() == 3); - var newBrowserWindow = b.Windows.First(br => br.WindowHandle != b.WindowHandle); - Assert.That(newBrowserWindow.Url == new Uri("http://localhost/movies/About")); - } - - [Test] - public void Holding_Ctrl_Shft_Opens_New_Window() - { - Browser b = new Browser(Helper.GetMoviesRequestMocker()); - HttpRequestLog lastRequest = null; - b.RequestLogged += (br, l) => - { - lastRequest = l; - }; - b.Navigate("http://localhost/movies/"); - Assert.That(b.Url == new Uri("http://localhost/movies/")); - var link = b.Find(ElementType.Anchor, FindBy.Text, "Home"); - link.Click(); - Assert.That(b.Windows.Count() == 1); - link = b.Find(ElementType.Anchor, FindBy.Text, "Home"); - b.KeyState = KeyStateOption.Ctrl; - link.Click(); - Assert.That(b.Windows.Count() == 2); - link = b.Find(ElementType.Anchor, FindBy.Text, "Home"); - b.KeyState = KeyStateOption.Shift; - link.Click(); - Assert.That(b.Windows.Count() == 3); - link = b.Find(ElementType.Anchor, FindBy.Text, "Home"); - b.KeyState = KeyStateOption.Alt; - link.Click(); - Assert.That(b.Windows.Count() == 3); // alt does not open new browser - } - - - [Test] - public void Accessing_New_Windows_Using_Event() - { - Browser b = new Browser(Helper.GetMoviesRequestMocker()); - Browser newlyOpened = null; - b.NewWindowOpened += (b1, b2) => - { - newlyOpened = b2; - }; - b.Navigate("http://localhost/movies/"); - Assert.That(b.Url == new Uri("http://localhost/movies/")); - var link = b.Find(ElementType.Anchor, FindBy.Text, "Details"); - b.KeyState = KeyStateOption.Ctrl; - link.Click(); - Assert.That(b.Windows.Count() == 2); - Assert.NotNull(newlyOpened); - Assert.That(b.Url.ToString() == "http://localhost/movies/"); - Assert.That(newlyOpened.Url.ToString() == "http://localhost/movies/Movies/Details/1"); - } - - - [Test] - public void ClosingBrowsers() - { - Browser b = new Browser(Helper.GetMoviesRequestMocker()); - HttpRequestLog lastRequest = null; - b.RequestLogged += (br, l) => - { - lastRequest = l; - }; - b.Navigate("http://localhost/movies/"); - Assert.That(b.Url == new Uri("http://localhost/movies/")); - var link = b.Find(ElementType.Anchor, FindBy.Text, "About us"); - link.Click(); - Assert.That(b.Url == new Uri("http://localhost/movies/")); - Assert.That(b.Windows.Count() == 2); - b.Close(); - Assert.That(b.Windows.Count() == 1); - b.Windows.First().Close(); - Assert.That(b.Windows.Count() == 0); - Assert.Throws(typeof(ObjectDisposedException), () => { Uri s = b.Url; }); - } - [Test] - public void Page_With_IFrames() - { - Browser b = new Browser(Helper.GetFramesMock()); - HttpRequestLog lastRequest = null; - b.RequestLogged += (br, l) => - { - lastRequest = l; - }; - b.Navigate("http://localhost/"); - Assert.That(b.Frames.Count() == 2); - - // now navigate away to a page without frames - b.Navigate("http://localhost/bla"); - Assert.That(b.Frames.Count() == 0); - Assert.That(b.Windows.Count() == 1); - } - [Test] - public void GetAttribute_Backdoor_FrameHandle() - { - Browser b = new Browser(Helper.GetFramesMock()); - HttpRequestLog lastRequest = null; - b.RequestLogged += (br, l) => - { - lastRequest = l; - }; - b.Navigate("http://localhost/"); - var elm = b.Select("iframe"); - string handle = elm.GetAttribute("SimpleBrowser.WebDriver:frameWindowHandle"); - Assert.AreEqual(handle, "frame1"); - } - [Test] - public void Navigating_IFrames_Using_Target() - { - Browser b = new Browser(Helper.GetFramesMock()); - HttpRequestLog lastRequest = null; - b.RequestLogged += (br, l) => - { - lastRequest = l; - }; - b.Navigate("http://localhost/"); - Assert.That(b.Frames.Count() == 2); - Assert.That(b.Frames.First().Url == new Uri("http://localhost/subdirectory/frame.htm")); - - b.Find("framelink").Click(); - Assert.That(b.Frames.Count() == 2); - Assert.That(b.Url == new Uri("http://localhost/")); - Assert.That(b.Frames.First().Url == new Uri("http://localhost/bla.htm")); - - } - } -} +using System; +using System.Collections.Generic; +using System.Linq; +using NUnit.Framework; + +namespace SimpleBrowser.UnitTests.OfflineTests +{ + [TestFixture] + public class WindowsAndFrames + { + [Test] + public void Clicking_Target_Blank() + { + Browser b = new Browser(Helper.GetMoviesRequestMocker()); + HttpRequestLog lastRequest = null; + b.RequestLogged += (br, l) => + { + lastRequest = l; + }; + b.Navigate("http://localhost/movies/"); + Assert.That(b.Url == new Uri("http://localhost/movies/")); + var link = b.Find(ElementType.Anchor, FindBy.Text, "About us"); + link.Click(); + Assert.That(b.Url == new Uri("http://localhost/movies/")); + Assert.That(b.Windows.Count() == 2); + link.Click(); + Assert.That(b.Windows.Count() == 3); + var newBrowserWindow = b.Windows.First(br => br.WindowHandle != b.WindowHandle); + Assert.That(newBrowserWindow.Url == new Uri("http://localhost/movies/About")); + } + + [Test] + public void Holding_Ctrl_Shft_Opens_New_Window() + { + Browser b = new Browser(Helper.GetMoviesRequestMocker()); + HttpRequestLog lastRequest = null; + b.RequestLogged += (br, l) => + { + lastRequest = l; + }; + b.Navigate("http://localhost/movies/"); + Assert.That(b.Url == new Uri("http://localhost/movies/")); + var link = b.Find(ElementType.Anchor, FindBy.Text, "Home"); + link.Click(); + Assert.That(b.Windows.Count() == 1); + link = b.Find(ElementType.Anchor, FindBy.Text, "Home"); + b.KeyState = KeyStateOption.Ctrl; + link.Click(); + Assert.That(b.Windows.Count() == 2); + link = b.Find(ElementType.Anchor, FindBy.Text, "Home"); + b.KeyState = KeyStateOption.Shift; + link.Click(); + Assert.That(b.Windows.Count() == 3); + link = b.Find(ElementType.Anchor, FindBy.Text, "Home"); + b.KeyState = KeyStateOption.Alt; + link.Click(); + Assert.That(b.Windows.Count() == 3); // alt does not open new browser + } + + + [Test] + public void Accessing_New_Windows_Using_Event() + { + Browser b = new Browser(Helper.GetMoviesRequestMocker()); + Browser newlyOpened = null; + b.NewWindowOpened += (b1, b2) => + { + newlyOpened = b2; + }; + b.Navigate("http://localhost/movies/"); + Assert.That(b.Url == new Uri("http://localhost/movies/")); + var link = b.Find(ElementType.Anchor, FindBy.Text, "Details"); + b.KeyState = KeyStateOption.Ctrl; + link.Click(); + Assert.That(b.Windows.Count() == 2); + Assert.NotNull(newlyOpened); + Assert.That(b.Url.ToString() == "http://localhost/movies/"); + Assert.That(newlyOpened.Url.ToString() == "http://localhost/movies/Movies/Details/1"); + } + + + [Test] + public void ClosingBrowsers() + { + Browser b = new Browser(Helper.GetMoviesRequestMocker()); + HttpRequestLog lastRequest = null; + b.RequestLogged += (br, l) => + { + lastRequest = l; + }; + b.Navigate("http://localhost/movies/"); + Assert.That(b.Url == new Uri("http://localhost/movies/")); + var link = b.Find(ElementType.Anchor, FindBy.Text, "About us"); + link.Click(); + Assert.That(b.Url == new Uri("http://localhost/movies/")); + Assert.That(b.Windows.Count() == 2); + b.Close(); + Assert.That(b.Windows.Count() == 1); + b.Windows.First().Close(); + Assert.That(b.Windows.Count() == 0); + Assert.Throws(typeof(ObjectDisposedException), () => { Uri s = b.Url; }); + } + [Test] + public void Page_With_IFrames() + { + Browser b = new Browser(Helper.GetFramesMock()); + HttpRequestLog lastRequest = null; + b.RequestLogged += (br, l) => + { + lastRequest = l; + }; + b.Navigate("http://localhost/"); + Assert.That(b.Frames.Count() == 2); + + // now navigate away to a page without frames + b.Navigate("http://localhost/bla"); + Assert.That(b.Frames.Count() == 0); + Assert.That(b.Windows.Count() == 1); + } + [Test] + public void GetAttribute_Backdoor_FrameHandle() + { + Browser b = new Browser(Helper.GetFramesMock()); + HttpRequestLog lastRequest = null; + b.RequestLogged += (br, l) => + { + lastRequest = l; + }; + b.Navigate("http://localhost/"); + var elm = b.Select("iframe"); + string handle = elm.GetAttribute("SimpleBrowser.WebDriver:frameWindowHandle"); + Assert.AreEqual(handle, "frame1"); + } + [Test] + public void Navigating_IFrames_Using_Target() + { + Browser b = new Browser(Helper.GetFramesMock()); + HttpRequestLog lastRequest = null; + b.RequestLogged += (br, l) => + { + lastRequest = l; + }; + b.Navigate("http://localhost/"); + Assert.That(b.Frames.Count() == 2); + Assert.That(b.Frames.First().Url == new Uri("http://localhost/subdirectory/frame.htm")); + + b.Find("framelink").Click(); + Assert.That(b.Frames.Count() == 2); + Assert.That(b.Url == new Uri("http://localhost/")); + Assert.That(b.Frames.First().Url == new Uri("http://localhost/bla.htm")); + + } + [Test] + public void Static_scoped_clear_works() + { + Browser b1 = new Browser(Helper.GetFramesMock()); + Browser b2 = new Browser(Helper.GetFramesMock()); + Browser.ClearWindows(); + Assert.Throws(typeof(ObjectDisposedException), () => b1.Navigate("http://localhost/")); + } + [Test] + public void Instance_scoped_clear_works() + { + Browser b1 = new Browser(Helper.GetFramesMock()); + Browser b2 = new Browser(Helper.GetFramesMock()); + b2.ClearWindowsInContext(); + b1.Navigate("http://localhost/"); + Assert.That(b1.Url.ToString() == "http://localhost/"); + Assert.Throws(typeof(ObjectDisposedException), () => b2.Navigate("http://localhost/")); + } + } +} diff --git a/SimpleBrowser/Browser.cs b/SimpleBrowser/Browser.cs old mode 100644 new mode 100755 index ed537e3..bedf559 --- a/SimpleBrowser/Browser.cs +++ b/SimpleBrowser/Browser.cs @@ -289,11 +289,19 @@ public void ClearException() LastWebException = null; } - public void ClearWindows() + public void ClearWindowsInContext() { foreach (var window in _allWindows.ToArray()) window.Close(); _allWindows.Clear(); } + public static void ClearWindows() + { + foreach (var list in _allContexts.ToArray()) + { + foreach (var window in list.ToArray()) window.Close(); + } + _allContexts.Clear(); + } public void Close() { @@ -1281,11 +1289,15 @@ private IHttpWebRequest PrepareRequestObject(Uri url, string method, string cont private void Register(Browser browser) { _allWindows.Add(browser); + if(!_allContexts.Contains(_allWindows)){ + _allContexts.Add(_allWindows); + } if (browser.WindowHandle == null) { browser.WindowHandle = Guid.NewGuid().ToString().Substring(0, 8); } } + private static List> _allContexts = new List>(); #endregion private methods end }