1
+ using System ;
1
2
using Microsoft . VisualStudio . TestTools . UnitTesting ;
2
-
3
+ using OpenQA . Selenium ;
4
+ using OpenQA . Selenium . Chrome ;
5
+ using System . Collections . Generic ;
3
6
namespace SeleniumDocs . Interactions
4
7
{
5
8
[ TestClass ]
6
- public class WindowsTest : BaseTest
9
+ public class WindowsTest
7
10
{
11
+ [ TestMethod ]
12
+ public void TestWindowCommands ( )
13
+ {
14
+ WebDriver driver = new ChromeDriver ( ) ;
15
+ driver . Manage ( ) . Timeouts ( ) . ImplicitWait = TimeSpan . FromMilliseconds ( 500 ) ;
16
+
17
+ // Navigate to Url
18
+ driver . Url = "https://www.selenium.dev/selenium/web/window_switching_tests/page_with_frame.html" ;
19
+ //fetch handle of this
20
+ String currHandle = driver . CurrentWindowHandle ;
21
+ Assert . IsNotNull ( currHandle ) ;
22
+
23
+ //click on link to open a new window
24
+ driver . FindElement ( By . LinkText ( "Open new window" ) ) . Click ( ) ;
25
+ //fetch handles of all windows, there will be two, [0]- default, [1] - new window
26
+ IList < string > windowHandles = new List < string > ( driver . WindowHandles ) ;
27
+ driver . SwitchTo ( ) . Window ( windowHandles [ 1 ] ) ;
28
+ //assert on title of new window
29
+ String title = driver . Title ;
30
+ Assert . AreEqual ( "Simple Page" , title ) ;
31
+
32
+ //closing current window
33
+ driver . Close ( ) ;
34
+ //Switch back to the old tab or window
35
+ driver . SwitchTo ( ) . Window ( windowHandles [ 0 ] ) ;
36
+
37
+ //Opens a new tab and switches to new tab
38
+ driver . SwitchTo ( ) . NewWindow ( WindowType . Tab ) ;
39
+ Assert . AreEqual ( "" , driver . Title ) ;
40
+
41
+ //Opens a new window and switches to new window
42
+ driver . SwitchTo ( ) . NewWindow ( WindowType . Window ) ;
43
+ Assert . AreEqual ( "" , driver . Title ) ;
44
+
45
+ //quitting driver
46
+ driver . Quit ( ) ; //close all windows
47
+ }
8
48
}
9
- }
49
+ }
0 commit comments