Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
326 changes: 172 additions & 154 deletions SimpleBrowser.UnitTests/OfflineTests/WindowsAndFrames.cs
100644 → 100755
Original file line number Diff line number Diff line change
@@ -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;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Were there any changes in the tests. 'Merge' did not show changes in the code.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, the bottom two tests are new. I guess this happened because I edited on my mac. Whitespace/line ending issues.

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/"));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

#120 fix

Assert.Throws(typeof(ObjectDisposedException), () => b2.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/"));
}
}
}
14 changes: 13 additions & 1 deletion SimpleBrowser/Browser.cs
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Be sure to add the removal of both ClearWindows () otherwise we will keep all links _allContexts, which will lead to memory leaks.

}
public static void ClearWindows()
{
foreach (var list in _allContexts.ToArray())
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You need to add the removal of the local copies
Maybe ClearWindowsInContext() or similar

{
foreach (var window in list.ToArray()) window.Close();
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

#120 fix: Change 301 line

foreach (var window in list.ToArray()) window.ClearWindowsInContext();

_allContexts.Clear();
}

public void Close()
{
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

#120 fix: Need change Close()

        public void Close()
        {
            _history = null;
            _allWindows.Remove(this);
            if (_allContexts.Contains(_allWindows))
            {
                _allContexts.Remove(_allWindows);
            }
        }

Expand Down Expand Up @@ -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<List<Browser>> _allContexts = new List<List<Browser>>();

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is the semantic meaning of the possibility of access to all your open pages of all application threads working with this class?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, the old paradigm was that SimpleBrowser had only one "name-space". Much like real browsers do. If you open Internet Explorer (window 1) and click a link with target="myName" (opens in window 2) and then you start IE again (window 3), clicking that same link in your new window, the link will open in the same window 2. There is one process-wide list of named windows.

Because we want to be able to run concurrent unit tests that shouldn't interfere with each other, we now create a new name-space when you start a new Browser. When you click a link with a target attribute there, it will open in the same name-space. So your change from Browser.ClearWindows() to myBrowser.ClearWindows() will clear only the current name-space. But others still rely on the static method for clearing all namespaces.

#endregion private methods end
}
Expand Down