"Fossies" - the Fresh Open Source Software Archive 
Member "selenium-selenium-4.8.1/dotnet/test/common/WindowSwitchingTest.cs" (17 Feb 2023, 17405 Bytes) of package /linux/www/selenium-selenium-4.8.1.tar.gz:
As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C# source code syntax highlighting (style:
standard) with prefixed line numbers and
code folding option.
Alternatively you can here
view or
download the uninterpreted source code file.
See also the last
Fossies "Diffs" side-by-side code changes report for "WindowSwitchingTest.cs":
4.6.0_vs_4.7.0.
1 using System;
2 using System.Collections.Generic;
3 using NUnit.Framework;
4 using System.Collections.ObjectModel;
5 using OpenQA.Selenium.Environment;
6
7 namespace OpenQA.Selenium
8 {
9 [TestFixture]
10 public class WindowSwitchingTest : DriverTestFixture
11 {
12 [Test]
13 [IgnoreBrowser(Browser.IE, "Edge in IE Mode does not properly handle multiple windows")]
14 public void ShouldSwitchFocusToANewWindowWhenItIsOpenedAndNotStopFutureOperations()
15 {
16 driver.Url = xhtmlTestPage;
17 String current = driver.CurrentWindowHandle;
18
19 driver.FindElement(By.LinkText("Open new window")).Click();
20 Assert.AreEqual("XHTML Test Page", driver.Title);
21
22 WaitFor(WindowCountToBe(2), "Window count was not 2");
23 WaitFor(WindowWithName("result"), "Could not find window with name 'result'");
24 WaitFor(() => { return driver.Title == "We Arrive Here"; }, "Browser title was not 'We Arrive Here'");
25 Assert.AreEqual("We Arrive Here", driver.Title);
26
27 driver.Url = iframesPage;
28 string handle = driver.CurrentWindowHandle;
29 driver.FindElement(By.Id("iframe_page_heading"));
30 driver.SwitchTo().Frame("iframe1");
31 Assert.AreEqual(driver.CurrentWindowHandle, handle);
32 driver.SwitchTo().DefaultContent();
33 driver.Close();
34
35 driver.SwitchTo().Window(current);
36 //Assert.AreEqual("XHTML Test Page", driver.Title);
37 }
38
39 [Test]
40 public void ShouldThrowNoSuchWindowException() {
41 driver.Url = xhtmlTestPage;
42 String current = driver.CurrentWindowHandle;
43 try
44 {
45 driver.SwitchTo().Window("invalid name");
46 }
47 catch (NoSuchWindowException)
48 {
49 // This is expected.
50 }
51
52 driver.SwitchTo().Window(current);
53 }
54
55 [Test]
56 [IgnoreBrowser(Browser.IE, "Edge in IE Mode does not properly handle multiple windows")]
57 public void ShouldThrowNoSuchWindowExceptionOnAnAttemptToGetItsHandle()
58 {
59 driver.Url = (xhtmlTestPage);
60 String current = driver.CurrentWindowHandle;
61 int currentWindowHandles = driver.WindowHandles.Count;
62
63 driver.FindElement(By.LinkText("Open new window")).Click();
64
65 WaitFor(WindowCountToBe(2), "Window count was not 2");
66 Assert.AreEqual(2, driver.WindowHandles.Count);
67
68 WaitFor(WindowWithName("result"), "Could not find window with name 'result'");
69 driver.SwitchTo().Window("result");
70 driver.Close();
71
72 try
73 {
74 string currentHandle = driver.CurrentWindowHandle;
75 Assert.Fail("NoSuchWindowException expected");
76 }
77 catch (NoSuchWindowException)
78 {
79 // Expected.
80 }
81 finally
82 {
83 driver.SwitchTo().Window(current);
84 }
85 }
86
87 [Test]
88 [IgnoreBrowser(Browser.IE, "Edge in IE Mode does not properly handle multiple windows")]
89 public void ShouldThrowNoSuchWindowExceptionOnAnyOperationIfAWindowIsClosed()
90 {
91 driver.Url = (xhtmlTestPage);
92 String current = driver.CurrentWindowHandle;
93 int currentWindowHandles = driver.WindowHandles.Count;
94
95 driver.FindElement(By.LinkText("Open new window")).Click();
96
97 WaitFor(WindowCountToBe(2), "Window count was not 2");
98 Assert.AreEqual(2, driver.WindowHandles.Count);
99
100 WaitFor(WindowWithName("result"), "Could not find window with name 'result'");
101 driver.SwitchTo().Window("result");
102 driver.Close();
103
104 try
105 {
106 try
107 {
108 string title = driver.Title;
109 Assert.Fail("NoSuchWindowException expected");
110 }
111 catch (NoSuchWindowException)
112 {
113 // Expected.
114 }
115
116 try
117 {
118 driver.FindElement(By.TagName("body"));
119 Assert.Fail("NoSuchWindowException expected");
120 }
121 catch (NoSuchWindowException)
122 {
123 // Expected.
124 }
125 }
126 finally
127 {
128 driver.SwitchTo().Window(current);
129 }
130 }
131
132 [Test]
133 [IgnoreBrowser(Browser.IE, "Edge in IE Mode does not properly handle multiple windows")]
134 public void ShouldThrowNoSuchWindowExceptionOnAnyElementOperationIfAWindowIsClosed()
135 {
136 driver.Url = (xhtmlTestPage);
137 String current = driver.CurrentWindowHandle;
138 int currentWindowHandles = driver.WindowHandles.Count;
139
140 driver.FindElement(By.LinkText("Open new window")).Click();
141
142 WaitFor(WindowCountToBe(2), "Window count was not 2");
143 Assert.AreEqual(2, driver.WindowHandles.Count);
144
145 WaitFor(WindowWithName("result"), "Could not find window with name 'result'");
146 driver.SwitchTo().Window("result");
147 IWebElement body = driver.FindElement(By.TagName("body"));
148 driver.Close();
149
150 try
151 {
152 string bodyText = body.Text;
153 Assert.Fail("NoSuchWindowException expected");
154 }
155 catch (NoSuchWindowException)
156 {
157 // Expected.
158 }
159 finally
160 {
161 driver.SwitchTo().Window(current);
162 }
163 }
164
165 [Test]
166 [IgnoreBrowser(Browser.IE, "Edge in IE Mode does not properly handle multiple windows")]
167 [NeedsFreshDriver(IsCreatedBeforeTest = true, IsCreatedAfterTest = true)]
168 public void ShouldBeAbleToIterateOverAllOpenWindows()
169 {
170 driver.Url = xhtmlTestPage;
171 driver.FindElement(By.Name("windowOne")).Click();
172 WaitFor(WindowCountToBe(2), "Window count was not 2");
173 driver.FindElement(By.Name("windowTwo")).Click();
174 WaitFor(WindowCountToBe(3), "Window count was not 3");
175
176 ReadOnlyCollection<string> allWindowHandles = driver.WindowHandles;
177
178 // There should be three windows. We should also see each of the window titles at least once.
179 List<string> seenHandles = new List<string>();
180 foreach (string handle in allWindowHandles)
181 {
182 Assert.That(seenHandles, Has.No.Member(handle));
183 driver.SwitchTo().Window(handle);
184 seenHandles.Add(handle);
185 }
186
187 Assert.AreEqual(3, allWindowHandles.Count);
188 }
189
190 [Test]
191 [IgnoreBrowser(Browser.IE, "Edge in IE Mode does not properly handle multiple windows")]
192 public void ClickingOnAButtonThatClosesAnOpenWindowDoesNotCauseTheBrowserToHang()
193 {
194 driver.Url = xhtmlTestPage;
195
196 String currentHandle = driver.CurrentWindowHandle;
197
198 driver.FindElement(By.Name("windowThree")).Click();
199
200 driver.SwitchTo().Window("result");
201
202 try
203 {
204 IWebElement closeElement = WaitFor(() => { return driver.FindElement(By.Id("close")); }, "Could not find element with id 'close'");
205 closeElement.Click();
206 }
207 finally
208 {
209 driver.SwitchTo().Window(currentHandle);
210 driver.FindElement(By.Id("linkId"));
211 }
212 }
213
214 [Test]
215 [IgnoreBrowser(Browser.IE, "Edge in IE Mode does not properly handle multiple windows")]
216 public void CanCallGetWindowHandlesAfterClosingAWindow()
217 {
218 bool isIEDriver = TestUtilities.IsInternetExplorer(driver);
219 bool isIE6 = TestUtilities.IsIE6(driver);
220
221 driver.Url = xhtmlTestPage;
222
223 String currentHandle = driver.CurrentWindowHandle;
224
225 driver.FindElement(By.Name("windowThree")).Click();
226
227 driver.SwitchTo().Window("result");
228
229 try
230 {
231 IWebElement closeElement = WaitFor(() => { return driver.FindElement(By.Id("close")); }, "Could not find element with id 'close'");
232 closeElement.Click();
233 if (isIEDriver && !isIE6)
234 {
235 IAlert alert = WaitFor<IAlert>(AlertToBePresent(), "No alert found");
236 alert.Accept();
237 }
238 ReadOnlyCollection<string> handles = driver.WindowHandles;
239 // If we make it this far, we're all good.
240 }
241 finally
242 {
243 driver.SwitchTo().Window(currentHandle);
244 }
245 }
246
247 [Test]
248 public void CanObtainAWindowHandle()
249 {
250 driver.Url = xhtmlTestPage;
251
252 String currentHandle = driver.CurrentWindowHandle;
253
254 Assert.That(currentHandle, Is.Not.Null);
255 }
256
257 [Test]
258 public void FailingToSwitchToAWindowLeavesTheCurrentWindowAsIs()
259 {
260 driver.Url = xhtmlTestPage;
261 String current = driver.CurrentWindowHandle;
262
263 try
264 {
265 driver.SwitchTo().Window("i will never exist");
266 Assert.Fail("Should not be ablt to change to a non-existant window");
267 }
268 catch (NoSuchWindowException)
269 {
270 // expected
271 }
272
273 String newHandle = driver.CurrentWindowHandle;
274
275 Assert.AreEqual(current, newHandle);
276 }
277
278 [Test]
279 [IgnoreBrowser(Browser.IE, "Edge in IE Mode does not properly handle multiple windows")]
280 [NeedsFreshDriver(IsCreatedBeforeTest = true, IsCreatedAfterTest = true)]
281 public void CanCloseWindowWhenMultipleWindowsAreOpen()
282 {
283 driver.Url = xhtmlTestPage;
284 driver.FindElement(By.Name("windowOne")).Click();
285
286 WaitFor(WindowCountToBe(2), "Window count was not 2");
287
288 ReadOnlyCollection<string> allWindowHandles = driver.WindowHandles;
289
290 // There should be two windows. We should also see each of the window titles at least once.
291 Assert.AreEqual(2, allWindowHandles.Count);
292 string handle1 = allWindowHandles[1];
293 driver.SwitchTo().Window(handle1);
294 driver.Close();
295
296 WaitFor(WindowCountToBe(1), "Window count was not 1");
297
298 allWindowHandles = driver.WindowHandles;
299 Assert.AreEqual(1, allWindowHandles.Count);
300 }
301
302 [Test]
303 [IgnoreBrowser(Browser.IE, "Edge in IE Mode does not properly handle multiple windows")]
304 [NeedsFreshDriver(IsCreatedBeforeTest = true, IsCreatedAfterTest = true)]
305 public void CanCloseWindowAndSwitchBackToMainWindow()
306 {
307 driver.Url = xhtmlTestPage;
308
309 ReadOnlyCollection<string> currentWindowHandles = driver.WindowHandles;
310 string mainHandle = driver.CurrentWindowHandle;
311
312 driver.FindElement(By.Name("windowOne")).Click();
313
314 WaitFor(WindowCountToBe(2), "Window count was not 2");
315
316 ReadOnlyCollection<string> allWindowHandles = driver.WindowHandles;
317
318 // There should be two windows. We should also see each of the window titles at least once.
319 Assert.AreEqual(2, allWindowHandles.Count);
320
321 foreach(string handle in allWindowHandles)
322 {
323 if (handle != mainHandle)
324 {
325 driver.SwitchTo().Window(handle);
326 driver.Close();
327 }
328 }
329
330 driver.SwitchTo().Window(mainHandle);
331
332 string newHandle = driver.CurrentWindowHandle;
333 Assert.AreEqual(mainHandle, newHandle);
334
335 Assert.AreEqual(1, driver.WindowHandles.Count);
336 }
337
338 [Test]
339 [NeedsFreshDriver(IsCreatedBeforeTest = true, IsCreatedAfterTest = true)]
340 public void ClosingOnlyWindowShouldNotCauseTheBrowserToHang()
341 {
342 driver.Url = xhtmlTestPage;
343 driver.Close();
344 }
345
346 [Test]
347 [IgnoreBrowser(Browser.IE, "Edge in IE Mode does not properly handle multiple windows")]
348 [NeedsFreshDriver(IsCreatedBeforeTest = true, IsCreatedAfterTest = true)]
349 [IgnoreBrowser(Browser.Firefox, "https://github.com/mozilla/geckodriver/issues/610")]
350 public void ShouldFocusOnTheTopMostFrameAfterSwitchingToAWindow()
351 {
352 driver.Url = EnvironmentManager.Instance.UrlBuilder.WhereIs("window_switching_tests/page_with_frame.html");
353
354 ReadOnlyCollection<string> currentWindowHandles = driver.WindowHandles;
355 string mainWindow = driver.CurrentWindowHandle;
356
357 driver.FindElement(By.Id("a-link-that-opens-a-new-window")).Click();
358 WaitFor(WindowCountToBe(2), "Window count was not 2");
359
360 driver.SwitchTo().Frame("myframe");
361
362 driver.SwitchTo().Window("newWindow");
363 driver.Close();
364 driver.SwitchTo().Window(mainWindow);
365
366 driver.FindElement(By.Name("myframe"));
367 }
368
369 //------------------------------------------------------------------
370 // Tests below here are not included in the Java test suite
371 //------------------------------------------------------------------
372 [Test]
373 [IgnoreBrowser(Browser.IE, "Edge in IE Mode does not properly handle multiple windows")]
374 public void ShouldGetBrowserHandles()
375 {
376 driver.Url = xhtmlTestPage;
377 driver.FindElement(By.LinkText("Open new window")).Click();
378
379 WaitFor(WindowCountToBe(2), "Window count was not 2");
380
381 string handle1, handle2;
382 handle1 = driver.CurrentWindowHandle;
383
384 System.Threading.Thread.Sleep(1000);
385 driver.SwitchTo().Window("result");
386 handle2 = driver.CurrentWindowHandle;
387
388 ReadOnlyCollection<string> handles = driver.WindowHandles;
389
390 // At least the two handles we want should be there.
391 Assert.Contains(handle1, handles, "Should have contained current handle");
392 Assert.Contains(handle2, handles, "Should have contained result handle");
393
394 // Some (semi-)clean up..
395 driver.SwitchTo().Window(handle2);
396 driver.Close();
397 driver.SwitchTo().Window(handle1);
398 driver.Url = macbethPage;
399 }
400
401 [Test]
402 [IgnoreBrowser(Browser.IE, "Edge in IE Mode does not properly handle multiple windows")]
403 [NeedsFreshDriver(IsCreatedAfterTest = true)]
404 public void CloseShouldCloseCurrentHandleOnly()
405 {
406 driver.Url = xhtmlTestPage;
407 driver.FindElement(By.LinkText("Open new window")).Click();
408
409 WaitFor(WindowCountToBe(2), "Window count was not 2");
410
411 string handle1, handle2;
412 handle1 = driver.CurrentWindowHandle;
413
414 driver.SwitchTo().Window("result");
415 handle2 = driver.CurrentWindowHandle;
416
417 driver.Close();
418
419 SleepBecauseWindowsTakeTimeToOpen();
420
421 ReadOnlyCollection<string> handles = driver.WindowHandles;
422
423 Assert.That(handles, Has.No.Member(handle2), "Invalid handle still in handle list");
424 Assert.That(handles, Contains.Item(handle1), "Valid handle not in handle list");
425 }
426
427 [Test]
428 public void ShouldBeAbleToCreateANewWindow()
429 {
430 driver.Url = xhtmlTestPage;
431 string originalHandle = driver.CurrentWindowHandle;
432 driver.SwitchTo().NewWindow(WindowType.Tab);
433 WaitFor(WindowCountToBe(2), "Window count was not 2");
434 string newWindowHandle = driver.CurrentWindowHandle;
435 driver.Close();
436 driver.SwitchTo().Window(originalHandle);
437 Assert.That(newWindowHandle, Is.Not.EqualTo(originalHandle));
438 }
439
440 private void SleepBecauseWindowsTakeTimeToOpen()
441 {
442 try
443 {
444 System.Threading.Thread.Sleep(1000);
445 }
446 catch (Exception)
447 {
448 Assert.Fail("Interrupted");
449 }
450 }
451
452 private Func<bool> WindowCountToBe(int desiredCount)
453 {
454 return () =>
455 {
456 return driver.WindowHandles.Count == desiredCount;
457 };
458 }
459
460 private Func<bool> WindowWithName(string name)
461 {
462 return () =>
463 {
464 try
465 {
466 driver.SwitchTo().Window(name);
467 return true;
468 }
469 catch (NoSuchWindowException)
470 {
471 }
472
473 return false;
474 };
475 }
476
477 private Func<IAlert> AlertToBePresent()
478 {
479 return () =>
480 {
481 IAlert alert = null;
482 try
483 {
484 alert = driver.SwitchTo().Alert();
485 }
486 catch (NoAlertPresentException)
487 {
488 }
489
490 return alert;
491 };
492 }
493 }
494 }