Skip to content

Commit 9fcaf5b

Browse files
author
Teun Duynstee
committed
Reintroduced static ClearWindows
Had to rename instance scoped ClearWindows to ClearWindowsInContext Added unit tests
1 parent 72acea3 commit 9fcaf5b

File tree

2 files changed

+185
-155
lines changed

2 files changed

+185
-155
lines changed
Lines changed: 172 additions & 154 deletions
Original file line numberDiff line numberDiff line change
@@ -1,154 +1,172 @@
1-
using System;
2-
using System.Collections.Generic;
3-
using System.Linq;
4-
using NUnit.Framework;
5-
6-
namespace SimpleBrowser.UnitTests.OfflineTests
7-
{
8-
[TestFixture]
9-
public class WindowsAndFrames
10-
{
11-
[Test]
12-
public void Clicking_Target_Blank()
13-
{
14-
Browser b = new Browser(Helper.GetMoviesRequestMocker());
15-
HttpRequestLog lastRequest = null;
16-
b.RequestLogged += (br, l) =>
17-
{
18-
lastRequest = l;
19-
};
20-
b.Navigate("http://localhost/movies/");
21-
Assert.That(b.Url == new Uri("http://localhost/movies/"));
22-
var link = b.Find(ElementType.Anchor, FindBy.Text, "About us");
23-
link.Click();
24-
Assert.That(b.Url == new Uri("http://localhost/movies/"));
25-
Assert.That(b.Windows.Count() == 2);
26-
link.Click();
27-
Assert.That(b.Windows.Count() == 3);
28-
var newBrowserWindow = b.Windows.First(br => br.WindowHandle != b.WindowHandle);
29-
Assert.That(newBrowserWindow.Url == new Uri("http://localhost/movies/About"));
30-
}
31-
32-
[Test]
33-
public void Holding_Ctrl_Shft_Opens_New_Window()
34-
{
35-
Browser b = new Browser(Helper.GetMoviesRequestMocker());
36-
HttpRequestLog lastRequest = null;
37-
b.RequestLogged += (br, l) =>
38-
{
39-
lastRequest = l;
40-
};
41-
b.Navigate("http://localhost/movies/");
42-
Assert.That(b.Url == new Uri("http://localhost/movies/"));
43-
var link = b.Find(ElementType.Anchor, FindBy.Text, "Home");
44-
link.Click();
45-
Assert.That(b.Windows.Count() == 1);
46-
link = b.Find(ElementType.Anchor, FindBy.Text, "Home");
47-
b.KeyState = KeyStateOption.Ctrl;
48-
link.Click();
49-
Assert.That(b.Windows.Count() == 2);
50-
link = b.Find(ElementType.Anchor, FindBy.Text, "Home");
51-
b.KeyState = KeyStateOption.Shift;
52-
link.Click();
53-
Assert.That(b.Windows.Count() == 3);
54-
link = b.Find(ElementType.Anchor, FindBy.Text, "Home");
55-
b.KeyState = KeyStateOption.Alt;
56-
link.Click();
57-
Assert.That(b.Windows.Count() == 3); // alt does not open new browser
58-
}
59-
60-
61-
[Test]
62-
public void Accessing_New_Windows_Using_Event()
63-
{
64-
Browser b = new Browser(Helper.GetMoviesRequestMocker());
65-
Browser newlyOpened = null;
66-
b.NewWindowOpened += (b1, b2) =>
67-
{
68-
newlyOpened = b2;
69-
};
70-
b.Navigate("http://localhost/movies/");
71-
Assert.That(b.Url == new Uri("http://localhost/movies/"));
72-
var link = b.Find(ElementType.Anchor, FindBy.Text, "Details");
73-
b.KeyState = KeyStateOption.Ctrl;
74-
link.Click();
75-
Assert.That(b.Windows.Count() == 2);
76-
Assert.NotNull(newlyOpened);
77-
Assert.That(b.Url.ToString() == "http://localhost/movies/");
78-
Assert.That(newlyOpened.Url.ToString() == "http://localhost/movies/Movies/Details/1");
79-
}
80-
81-
82-
[Test]
83-
public void ClosingBrowsers()
84-
{
85-
Browser b = new Browser(Helper.GetMoviesRequestMocker());
86-
HttpRequestLog lastRequest = null;
87-
b.RequestLogged += (br, l) =>
88-
{
89-
lastRequest = l;
90-
};
91-
b.Navigate("http://localhost/movies/");
92-
Assert.That(b.Url == new Uri("http://localhost/movies/"));
93-
var link = b.Find(ElementType.Anchor, FindBy.Text, "About us");
94-
link.Click();
95-
Assert.That(b.Url == new Uri("http://localhost/movies/"));
96-
Assert.That(b.Windows.Count() == 2);
97-
b.Close();
98-
Assert.That(b.Windows.Count() == 1);
99-
b.Windows.First().Close();
100-
Assert.That(b.Windows.Count() == 0);
101-
Assert.Throws(typeof(ObjectDisposedException), () => { Uri s = b.Url; });
102-
}
103-
[Test]
104-
public void Page_With_IFrames()
105-
{
106-
Browser b = new Browser(Helper.GetFramesMock());
107-
HttpRequestLog lastRequest = null;
108-
b.RequestLogged += (br, l) =>
109-
{
110-
lastRequest = l;
111-
};
112-
b.Navigate("http://localhost/");
113-
Assert.That(b.Frames.Count() == 2);
114-
115-
// now navigate away to a page without frames
116-
b.Navigate("http://localhost/bla");
117-
Assert.That(b.Frames.Count() == 0);
118-
Assert.That(b.Windows.Count() == 1);
119-
}
120-
[Test]
121-
public void GetAttribute_Backdoor_FrameHandle()
122-
{
123-
Browser b = new Browser(Helper.GetFramesMock());
124-
HttpRequestLog lastRequest = null;
125-
b.RequestLogged += (br, l) =>
126-
{
127-
lastRequest = l;
128-
};
129-
b.Navigate("http://localhost/");
130-
var elm = b.Select("iframe");
131-
string handle = elm.GetAttribute("SimpleBrowser.WebDriver:frameWindowHandle");
132-
Assert.AreEqual(handle, "frame1");
133-
}
134-
[Test]
135-
public void Navigating_IFrames_Using_Target()
136-
{
137-
Browser b = new Browser(Helper.GetFramesMock());
138-
HttpRequestLog lastRequest = null;
139-
b.RequestLogged += (br, l) =>
140-
{
141-
lastRequest = l;
142-
};
143-
b.Navigate("http://localhost/");
144-
Assert.That(b.Frames.Count() == 2);
145-
Assert.That(b.Frames.First().Url == new Uri("http://localhost/subdirectory/frame.htm"));
146-
147-
b.Find("framelink").Click();
148-
Assert.That(b.Frames.Count() == 2);
149-
Assert.That(b.Url == new Uri("http://localhost/"));
150-
Assert.That(b.Frames.First().Url == new Uri("http://localhost/bla.htm"));
151-
152-
}
153-
}
154-
}
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using NUnit.Framework;
5+
6+
namespace SimpleBrowser.UnitTests.OfflineTests
7+
{
8+
[TestFixture]
9+
public class WindowsAndFrames
10+
{
11+
[Test]
12+
public void Clicking_Target_Blank()
13+
{
14+
Browser b = new Browser(Helper.GetMoviesRequestMocker());
15+
HttpRequestLog lastRequest = null;
16+
b.RequestLogged += (br, l) =>
17+
{
18+
lastRequest = l;
19+
};
20+
b.Navigate("http://localhost/movies/");
21+
Assert.That(b.Url == new Uri("http://localhost/movies/"));
22+
var link = b.Find(ElementType.Anchor, FindBy.Text, "About us");
23+
link.Click();
24+
Assert.That(b.Url == new Uri("http://localhost/movies/"));
25+
Assert.That(b.Windows.Count() == 2);
26+
link.Click();
27+
Assert.That(b.Windows.Count() == 3);
28+
var newBrowserWindow = b.Windows.First(br => br.WindowHandle != b.WindowHandle);
29+
Assert.That(newBrowserWindow.Url == new Uri("http://localhost/movies/About"));
30+
}
31+
32+
[Test]
33+
public void Holding_Ctrl_Shft_Opens_New_Window()
34+
{
35+
Browser b = new Browser(Helper.GetMoviesRequestMocker());
36+
HttpRequestLog lastRequest = null;
37+
b.RequestLogged += (br, l) =>
38+
{
39+
lastRequest = l;
40+
};
41+
b.Navigate("http://localhost/movies/");
42+
Assert.That(b.Url == new Uri("http://localhost/movies/"));
43+
var link = b.Find(ElementType.Anchor, FindBy.Text, "Home");
44+
link.Click();
45+
Assert.That(b.Windows.Count() == 1);
46+
link = b.Find(ElementType.Anchor, FindBy.Text, "Home");
47+
b.KeyState = KeyStateOption.Ctrl;
48+
link.Click();
49+
Assert.That(b.Windows.Count() == 2);
50+
link = b.Find(ElementType.Anchor, FindBy.Text, "Home");
51+
b.KeyState = KeyStateOption.Shift;
52+
link.Click();
53+
Assert.That(b.Windows.Count() == 3);
54+
link = b.Find(ElementType.Anchor, FindBy.Text, "Home");
55+
b.KeyState = KeyStateOption.Alt;
56+
link.Click();
57+
Assert.That(b.Windows.Count() == 3); // alt does not open new browser
58+
}
59+
60+
61+
[Test]
62+
public void Accessing_New_Windows_Using_Event()
63+
{
64+
Browser b = new Browser(Helper.GetMoviesRequestMocker());
65+
Browser newlyOpened = null;
66+
b.NewWindowOpened += (b1, b2) =>
67+
{
68+
newlyOpened = b2;
69+
};
70+
b.Navigate("http://localhost/movies/");
71+
Assert.That(b.Url == new Uri("http://localhost/movies/"));
72+
var link = b.Find(ElementType.Anchor, FindBy.Text, "Details");
73+
b.KeyState = KeyStateOption.Ctrl;
74+
link.Click();
75+
Assert.That(b.Windows.Count() == 2);
76+
Assert.NotNull(newlyOpened);
77+
Assert.That(b.Url.ToString() == "http://localhost/movies/");
78+
Assert.That(newlyOpened.Url.ToString() == "http://localhost/movies/Movies/Details/1");
79+
}
80+
81+
82+
[Test]
83+
public void ClosingBrowsers()
84+
{
85+
Browser b = new Browser(Helper.GetMoviesRequestMocker());
86+
HttpRequestLog lastRequest = null;
87+
b.RequestLogged += (br, l) =>
88+
{
89+
lastRequest = l;
90+
};
91+
b.Navigate("http://localhost/movies/");
92+
Assert.That(b.Url == new Uri("http://localhost/movies/"));
93+
var link = b.Find(ElementType.Anchor, FindBy.Text, "About us");
94+
link.Click();
95+
Assert.That(b.Url == new Uri("http://localhost/movies/"));
96+
Assert.That(b.Windows.Count() == 2);
97+
b.Close();
98+
Assert.That(b.Windows.Count() == 1);
99+
b.Windows.First().Close();
100+
Assert.That(b.Windows.Count() == 0);
101+
Assert.Throws(typeof(ObjectDisposedException), () => { Uri s = b.Url; });
102+
}
103+
[Test]
104+
public void Page_With_IFrames()
105+
{
106+
Browser b = new Browser(Helper.GetFramesMock());
107+
HttpRequestLog lastRequest = null;
108+
b.RequestLogged += (br, l) =>
109+
{
110+
lastRequest = l;
111+
};
112+
b.Navigate("http://localhost/");
113+
Assert.That(b.Frames.Count() == 2);
114+
115+
// now navigate away to a page without frames
116+
b.Navigate("http://localhost/bla");
117+
Assert.That(b.Frames.Count() == 0);
118+
Assert.That(b.Windows.Count() == 1);
119+
}
120+
[Test]
121+
public void GetAttribute_Backdoor_FrameHandle()
122+
{
123+
Browser b = new Browser(Helper.GetFramesMock());
124+
HttpRequestLog lastRequest = null;
125+
b.RequestLogged += (br, l) =>
126+
{
127+
lastRequest = l;
128+
};
129+
b.Navigate("http://localhost/");
130+
var elm = b.Select("iframe");
131+
string handle = elm.GetAttribute("SimpleBrowser.WebDriver:frameWindowHandle");
132+
Assert.AreEqual(handle, "frame1");
133+
}
134+
[Test]
135+
public void Navigating_IFrames_Using_Target()
136+
{
137+
Browser b = new Browser(Helper.GetFramesMock());
138+
HttpRequestLog lastRequest = null;
139+
b.RequestLogged += (br, l) =>
140+
{
141+
lastRequest = l;
142+
};
143+
b.Navigate("http://localhost/");
144+
Assert.That(b.Frames.Count() == 2);
145+
Assert.That(b.Frames.First().Url == new Uri("http://localhost/subdirectory/frame.htm"));
146+
147+
b.Find("framelink").Click();
148+
Assert.That(b.Frames.Count() == 2);
149+
Assert.That(b.Url == new Uri("http://localhost/"));
150+
Assert.That(b.Frames.First().Url == new Uri("http://localhost/bla.htm"));
151+
152+
}
153+
[Test]
154+
public void Static_scoped_clear_works()
155+
{
156+
Browser b1 = new Browser(Helper.GetFramesMock());
157+
Browser b2 = new Browser(Helper.GetFramesMock());
158+
Browser.ClearWindows();
159+
Assert.Throws(typeof(ObjectDisposedException), () => b1.Navigate("http://localhost/"));
160+
}
161+
[Test]
162+
public void Instance_scoped_clear_works()
163+
{
164+
Browser b1 = new Browser(Helper.GetFramesMock());
165+
Browser b2 = new Browser(Helper.GetFramesMock());
166+
b2.ClearWindowsInContext();
167+
b1.Navigate("http://localhost/");
168+
Assert.That(b1.Url.ToString() == "http://localhost/");
169+
Assert.Throws(typeof(ObjectDisposedException), () => b2.Navigate("http://localhost/"));
170+
}
171+
}
172+
}

SimpleBrowser/Browser.cs

100644100755
Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -289,11 +289,19 @@ public void ClearException()
289289
LastWebException = null;
290290
}
291291

292-
public void ClearWindows()
292+
public void ClearWindowsInContext()
293293
{
294294
foreach (var window in _allWindows.ToArray()) window.Close();
295295
_allWindows.Clear();
296296
}
297+
public static void ClearWindows()
298+
{
299+
foreach (var list in _allContexts.ToArray())
300+
{
301+
foreach (var window in list.ToArray()) window.Close();
302+
}
303+
_allContexts.Clear();
304+
}
297305

298306
public void Close()
299307
{
@@ -1281,11 +1289,15 @@ private IHttpWebRequest PrepareRequestObject(Uri url, string method, string cont
12811289
private void Register(Browser browser)
12821290
{
12831291
_allWindows.Add(browser);
1292+
if(!_allContexts.Contains(_allWindows)){
1293+
_allContexts.Add(_allWindows);
1294+
}
12841295
if (browser.WindowHandle == null)
12851296
{
12861297
browser.WindowHandle = Guid.NewGuid().ToString().Substring(0, 8);
12871298
}
12881299
}
1300+
private static List<List<Browser>> _allContexts = new List<List<Browser>>();
12891301

12901302
#endregion private methods end
12911303
}

0 commit comments

Comments
 (0)