Skip to content

Commit db69292

Browse files
add csharpwindowscommands (#1774)[deploy site]
* add csharpwindowscommands * Update WindowsTest.cs --------- Co-authored-by: Sri Harsha <[email protected]>
1 parent 77467ef commit db69292

File tree

5 files changed

+124
-157
lines changed

5 files changed

+124
-157
lines changed
Lines changed: 43 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,49 @@
1+
using System;
12
using Microsoft.VisualStudio.TestTools.UnitTesting;
2-
3+
using OpenQA.Selenium;
4+
using OpenQA.Selenium.Chrome;
5+
using System.Collections.Generic;
36
namespace SeleniumDocs.Interactions
47
{
58
[TestClass]
6-
public class WindowsTest : BaseTest
9+
public class WindowsTest
710
{
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+
}
848
}
9-
}
49+
}

website_and_docs/content/documentation/webdriver/interactions/windows.en.md

Lines changed: 21 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,9 @@ current window by using:
2323
{{< gh-codeblock path="examples/java/src/test/java/dev/selenium/interactions/WindowsTest.java#L16-L20" >}}
2424
{{< /tab >}}
2525
{{< tab header="Python" >}}driver.current_window_handle{{< /tab >}}
26-
{{< tab header="CSharp" >}}
27-
{{< gh-codeblock path="examples/dotnet/SeleniumDocs/Interactions/WindowsTest.cs#L14-L18" >}}
28-
{{< /tab >}}
26+
{{< tab header="CSharp" text=true >}}
27+
{{< gh-codeblock path="examples/dotnet/SeleniumDocs/Interactions/WindowsTest.cs#L17-L21" >}}
28+
{{< /tab >}}
2929
{{< tab header="Ruby" >}}driver.window_handle{{< /tab >}}
3030
{{< tab header="JavaScript" >}}await driver.getWindowHandle();{{< /tab >}}
3131
{{< tab header="Kotlin" >}}driver.windowHandle{{< /tab >}}
@@ -79,31 +79,11 @@ with webdriver.Firefox() as driver:
7979
# Wait for the new tab to finish loading content
8080
wait.until(EC.title_is("SeleniumHQ Browser Automation"))
8181
{{< /tab >}}
82-
{{< tab header="CSharp" >}}
83-
//Store the ID of the original window
84-
string originalWindow = driver.CurrentWindowHandle;
85-
86-
//Check we don't have other windows open already
87-
Assert.AreEqual(driver.WindowHandles.Count, 1);
88-
89-
//Click the link which opens in a new window
90-
driver.FindElement(By.LinkText("new window")).Click();
91-
92-
//Wait for the new window or tab
93-
wait.Until(wd => wd.WindowHandles.Count == 2);
94-
95-
//Loop through until we find a new window handle
96-
foreach(string window in driver.WindowHandles)
97-
{
98-
if(originalWindow != window)
99-
{
100-
driver.SwitchTo().Window(window);
101-
break;
102-
}
103-
}
104-
//Wait for the new tab to finish loading content
105-
wait.Until(wd => wd.Title == "Selenium documentation");
82+
83+
{{< tab header="CSharp" text=true >}}
84+
{{< gh-codeblock path="examples/dotnet/SeleniumDocs/Interactions/WindowsTest.cs#L23-L30" >}}
10685
{{< /tab >}}
86+
10787
{{< tab header="Ruby" >}}
10888

10989
# Store the ID of the original window
@@ -203,13 +183,11 @@ driver.close()
203183
#Switch back to the old tab or window
204184
driver.switch_to.window(original_window)
205185
{{< /tab >}}
206-
{{< tab header="CSharp" >}}
207-
//Close the tab or window
208-
driver.Close();
209-
210-
//Switch back to the old tab or window
211-
driver.SwitchTo().Window(originalWindow);
186+
187+
{{< tab header="CSharp" text=true >}}
188+
{{< gh-codeblock path="examples/dotnet/SeleniumDocs/Interactions/WindowsTest.cs#L32-L35" >}}
212189
{{< /tab >}}
190+
213191
{{< tab header="Ruby" >}}
214192
#Close the tab or window
215193
driver.close
@@ -259,13 +237,12 @@ driver.switch_to.new_window('tab')
259237
# Opens a new window and switches to new window
260238
driver.switch_to.new_window('window')
261239
{{< /tab >}}
262-
{{< tab header="CSharp" >}}
263-
// Opens a new tab and switches to new tab
264-
driver.SwitchTo().NewWindow(WindowType.Tab)
265-
266-
// Opens a new window and switches to new window
267-
driver.SwitchTo().NewWindow(WindowType.Window)
240+
241+
242+
{{< tab header="CSharp" text=true >}}
243+
{{< gh-codeblock path="examples/dotnet/SeleniumDocs/Interactions/WindowsTest.cs#L37-L43" >}}
268244
{{< /tab >}}
245+
269246
{{% tab header="Ruby" text=true %}}
270247
Opens a new tab and switches to new tab:
271248
{{< gh-codeblock path="/examples/ruby/spec/interactions/windows_spec.rb#L9" >}}
@@ -302,7 +279,11 @@ instead of close:
302279
{{< gh-codeblock path="examples/java/src/test/java/dev/selenium/interactions/WindowsTest.java#L44-L45" >}}
303280
{{< /tab >}}
304281
{{< tab header="Python" >}}driver.quit(){{< /tab >}}
305-
{{< tab header="CSharp" >}}driver.Quit();{{< /tab >}}
282+
283+
{{< tab header="CSharp" text=true >}}
284+
{{< gh-codeblock path="examples/dotnet/SeleniumDocs/Interactions/WindowsTest.cs#L45-L46" >}}
285+
{{< /tab >}}
286+
306287
{{< tab header="Ruby" >}}driver.quit{{< /tab >}}
307288
{{< tab header="JavaScript" >}}await driver.quit();{{< /tab >}}
308289
{{< tab header="Kotlin" >}}driver.quit(){{< /tab >}}

website_and_docs/content/documentation/webdriver/interactions/windows.ja.md

Lines changed: 23 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@ WebDriverは、ウィンドウとタブを区別しません。
2121
{{< gh-codeblock path="examples/java/src/test/java/dev/selenium/interactions/WindowsTest.java#L16-L20" >}}
2222
{{< /tab >}}
2323
{{< tab header="Python" >}}driver.current_window_handle{{< /tab >}}
24-
{{< tab header="CSharp" >}}
25-
{{< gh-codeblock path="examples/dotnet/SeleniumDocs/Interactions/WindowsTest.cs#L14-L18" >}}
26-
{{< /tab >}}
24+
{{< tab header="CSharp" text=true >}}
25+
{{< gh-codeblock path="examples/dotnet/SeleniumDocs/Interactions/WindowsTest.cs#L17-L21" >}}
26+
{{< /tab >}}
2727
{{< tab header="Ruby" >}}driver.window_handle{{< /tab >}}
2828
{{< tab header="JavaScript" >}}await driver.getWindowHandle();{{< /tab >}}
2929
{{< tab header="Kotlin" >}}driver.windowHandle{{< /tab >}}
@@ -75,31 +75,12 @@ with webdriver.Firefox() as driver:
7575
# Wait for the new tab to finish loading content
7676
wait.until(EC.title_is("SeleniumHQ Browser Automation"))
7777
{{< /tab >}}
78-
{{< tab header="CSharp" >}}
79-
//Store the ID of the original window
80-
string originalWindow = driver.CurrentWindowHandle;
81-
82-
//Check we don't have other windows open already
83-
Assert.AreEqual(driver.WindowHandles.Count, 1);
84-
85-
//Click the link which opens in a new window
86-
driver.FindElement(By.LinkText("new window")).Click();
87-
88-
//Wait for the new window or tab
89-
wait.Until(wd => wd.WindowHandles.Count == 2);
90-
91-
//Loop through until we find a new window handle
92-
foreach(string window in driver.WindowHandles)
93-
{
94-
if(originalWindow != window)
95-
{
96-
driver.SwitchTo().Window(window);
97-
break;
98-
}
99-
}
100-
//Wait for the new tab to finish loading content
101-
wait.Until(wd => wd.Title == "Selenium documentation");
78+
79+
{{< tab header="CSharp" text=true >}}
80+
{{< gh-codeblock path="examples/dotnet/SeleniumDocs/Interactions/WindowsTest.cs#L23-L30" >}}
10281
{{< /tab >}}
82+
83+
10384
{{< tab header="Ruby" >}}
10485
#Store the ID of the original window
10586
original_window = driver.window_handle
@@ -195,13 +176,12 @@ driver.close()
195176
#Switch back to the old tab or window
196177
driver.switch_to.window(original_window)
197178
{{< /tab >}}
198-
{{< tab header="CSharp" >}}
199-
//Close the tab or window
200-
driver.Close();
201179

202-
//Switch back to the old tab or window
203-
driver.SwitchTo().Window(originalWindow);
180+
{{< tab header="CSharp" text=true >}}
181+
{{< gh-codeblock path="examples/dotnet/SeleniumDocs/Interactions/WindowsTest.cs#L32-L35" >}}
204182
{{< /tab >}}
183+
184+
205185
{{< tab header="Ruby" >}}
206186
#Close the tab or window
207187
driver.close
@@ -247,34 +227,36 @@ driver.switch_to.new_window('tab')
247227
# Opens a new window and switches to new window
248228
driver.switch_to.new_window('window')
249229
{{< /tab >}}
250-
{{< tab header="CSharp" >}}
251-
// Opens a new tab and switches to new tab
252-
driver.SwitchTo().NewWindow(WindowType.Tab)
253-
254-
// Opens a new window and switches to new window
255-
driver.SwitchTo().NewWindow(WindowType.Window)
230+
231+
{{< tab header="CSharp" text=true >}}
232+
{{< gh-codeblock path="examples/dotnet/SeleniumDocs/Interactions/WindowsTest.cs#L37-L43" >}}
256233
{{< /tab >}}
234+
235+
257236
{{% tab header="Ruby" text=true %}}
258237
Opens a new tab and switches to new tab
259238
{{< gh-codeblock path="/examples/ruby/spec/interactions/windows_spec.rb#L9" >}}
260239

261240
Opens a new window and switches to new window
262241
{{< gh-codeblock path="/examples/ruby/spec/interactions/windows_spec.rb#L15" >}}
263242
{{% /tab %}}
243+
264244
{{< tab header="JavaScript" text=true >}}
265245
Opens a new tab and switches to new tab
266246
{{< gh-codeblock path="examples/javascript/test/interactions/windows.spec.js#L70" >}}
267247

268248
Opens a new window and switches to new window:
269249
{{< gh-codeblock path="examples/javascript/test/interactions/windows.spec.js#L75" >}}
270250
{{< /tab >}}
251+
271252
{{< tab header="Kotlin" >}}
272253
// Opens a new tab and switches to new tab
273254
driver.switchTo().newWindow(WindowType.TAB)
274255

275256
// Opens a new window and switches to new window
276257
driver.switchTo().newWindow(WindowType.WINDOW)
277258
{{< /tab >}}
259+
278260
{{< /tabpane >}}
279261

280262

@@ -287,7 +269,9 @@ driver.switchTo().newWindow(WindowType.WINDOW)
287269
{{< gh-codeblock path="examples/java/src/test/java/dev/selenium/interactions/WindowsTest.java#L44-L45" >}}
288270
{{< /tab >}}
289271
{{< tab header="Python" >}}driver.quit(){{< /tab >}}
290-
{{< tab header="CSharp" >}}driver.Quit();{{< /tab >}}
272+
{{< tab header="CSharp" text=true >}}
273+
{{< gh-codeblock path="examples/dotnet/SeleniumDocs/Interactions/WindowsTest.cs#L45-L46" >}}
274+
{{< /tab >}}
291275
{{< tab header="Ruby" >}}driver.quit{{< /tab >}}
292276
{{< tab header="JavaScript" >}}await driver.quit();{{< /tab >}}
293277
{{< tab header="Kotlin" >}}driver.quit(){{< /tab >}}

website_and_docs/content/documentation/webdriver/interactions/windows.pt-br.md

Lines changed: 18 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@ persistente em uma única sessão. Você pode pegar o identificador atual usando
2121
{{< gh-codeblock path="examples/java/src/test/java/dev/selenium/interactions/WindowsTest.java#L16-L20" >}}
2222
{{< /tab >}}
2323
{{< tab header="Python" >}}driver.current_window_handle{{< /tab >}}
24-
{{< tab header="CSharp" >}}
25-
{{< gh-codeblock path="examples/dotnet/SeleniumDocs/Interactions/WindowsTest.cs#L14-L18" >}}
26-
{{< /tab >}}
24+
{{< tab header="CSharp" text=true >}}
25+
{{< gh-codeblock path="examples/dotnet/SeleniumDocs/Interactions/WindowsTest.cs#L17-L21" >}}
26+
{{< /tab >}}
2727
{{< tab header="Ruby" >}}driver.window_handle{{< /tab >}}
2828
{{< tab header="JavaScript" >}}await driver.getWindowHandle();{{< /tab >}}
2929
{{< tab header="Kotlin" >}}driver.windowHandle{{< /tab >}}
@@ -81,31 +81,13 @@ with webdriver.Firefox() as driver:
8181
# Wait for the new tab to finish loading content
8282
wait.until(EC.title_is("SeleniumHQ Browser Automation"))
8383
{{< /tab >}}
84-
{{< tab header="CSharp" >}}
85-
//Store the ID of the original window
86-
string originalWindow = driver.CurrentWindowHandle;
8784

88-
//Check we don't have other windows open already
89-
Assert.AreEqual(driver.WindowHandles.Count, 1);
90-
91-
//Click the link which opens in a new window
92-
driver.FindElement(By.LinkText("new window")).Click();
85+
{{< tab header="CSharp" text=true >}}
86+
{{< gh-codeblock path="examples/dotnet/SeleniumDocs/Interactions/WindowsTest.cs#L23-L30" >}}
87+
{{< /tab >}}
88+
9389

94-
//Wait for the new window or tab
95-
wait.Until(wd => wd.WindowHandles.Count == 2);
9690

97-
//Loop through until we find a new window handle
98-
foreach(string window in driver.WindowHandles)
99-
{
100-
if(originalWindow != window)
101-
{
102-
driver.SwitchTo().Window(window);
103-
break;
104-
}
105-
}
106-
//Wait for the new tab to finish loading content
107-
wait.Until(wd => wd.Title == "Selenium documentation");
108-
{{< /tab >}}
10991
{{< tab header="Ruby" >}}
11092
#Store the ID of the original window
11193
original_window = driver.window_handle
@@ -203,13 +185,11 @@ driver.close()
203185
#Switch back to the old tab or window
204186
driver.switch_to.window(original_window)
205187
{{< /tab >}}
206-
{{< tab header="CSharp" >}}
207-
//Close the tab or window
208-
driver.Close();
209188

210-
//Switch back to the old tab or window
211-
driver.SwitchTo().Window(originalWindow);
189+
{{< tab header="CSharp" text=true >}}
190+
{{< gh-codeblock path="examples/dotnet/SeleniumDocs/Interactions/WindowsTest.cs#L32-L35" >}}
212191
{{< /tab >}}
192+
213193
{{< tab header="Ruby" >}}
214194
#Close the tab or window
215195
driver.close
@@ -257,13 +237,12 @@ driver.switch_to.new_window('tab')
257237
# Opens a new window and switches to new window
258238
driver.switch_to.new_window('window')
259239
{{< /tab >}}
260-
{{< tab header="CSharp" >}}
261-
// Opens a new tab and switches to new tab
262-
driver.SwitchTo().NewWindow(WindowType.Tab)
263-
264-
// Opens a new window and switches to new window
265-
driver.SwitchTo().NewWindow(WindowType.Window)
240+
241+
{{< tab header="CSharp" text=true >}}
242+
{{< gh-codeblock path="examples/dotnet/SeleniumDocs/Interactions/WindowsTest.cs#L37-L43" >}}
266243
{{< /tab >}}
244+
245+
267246
{{% tab header="Ruby" text=true %}}
268247
Opens a new tab and switches to new tab
269248
{{< gh-codeblock path="/examples/ruby/spec/interactions/windows_spec.rb#L9" >}}
@@ -301,7 +280,9 @@ em vez de fechar:
301280
{{< /tab >}}
302281

303282
{{< tab header="Python" >}}driver.quit(){{< /tab >}}
304-
{{< tab header="CSharp" >}}driver.Quit();{{< /tab >}}
283+
{{< tab header="CSharp" text=true >}}
284+
{{< gh-codeblock path="examples/dotnet/SeleniumDocs/Interactions/WindowsTest.cs#L45-L46" >}}
285+
{{< /tab >}}
305286
{{< tab header="Ruby" >}}driver.quit{{< /tab >}}
306287
{{< tab header="JavaScript" >}}await driver.quit();{{< /tab >}}
307288
{{< tab header="Kotlin" >}}driver.quit(){{< /tab >}}

0 commit comments

Comments
 (0)