Skip to content

add csharpwindowscommands #1774

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Jul 5, 2024
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
46 changes: 43 additions & 3 deletions examples/dotnet/SeleniumDocs/Interactions/WindowsTest.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,49 @@
using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;

using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using System.Collections.Generic;
namespace SeleniumDocs.Interactions
{
[TestClass]
public class WindowsTest : BaseTest
public class WindowsTest
{
[TestMethod]
public void TestWindowCommands()
{
WebDriver driver = new ChromeDriver();
driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromMilliseconds(500);

// Navigate to Url
driver.Url="https://www.selenium.dev/selenium/web/window_switching_tests/page_with_frame.html";
//fetch handle of this
String currHandle = driver.CurrentWindowHandle;
Assert.IsNotNull(currHandle);

//click on link to open a new window
driver.FindElement(By.LinkText("Open new window")).Click();
//fetch handles of all windows, there will be two, [0]- default, [1] - new window
IList<string> windowHandles = new List<string>(driver.WindowHandles);
driver.SwitchTo().Window(windowHandles[1]);
//assert on title of new window
String title = driver.Title;
Assert.AreEqual("Simple Page", title);

//closing current window
driver.Close();
//Switch back to the old tab or window
driver.SwitchTo().Window(windowHandles[0]);

//Opens a new tab and switches to new tab
driver.SwitchTo().NewWindow(WindowType.Tab);
Assert.AreEqual("", driver.Title);

//Opens a new window and switches to new window
driver.SwitchTo().NewWindow(WindowType.Window);
Assert.AreEqual("", driver.Title);

//quitting driver
driver.Quit(); //close all windows
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ current window by using:
{{< gh-codeblock path="examples/java/src/test/java/dev/selenium/interactions/WindowsTest.java#L16-L20" >}}
{{< /tab >}}
{{< tab header="Python" >}}driver.current_window_handle{{< /tab >}}
{{< tab header="CSharp" >}}
{{< gh-codeblock path="examples/dotnet/SeleniumDocs/Interactions/WindowsTest.cs#L14-L18" >}}
{{< /tab >}}
{{< tab header="CSharp" text=true >}}
{{< gh-codeblock path="examples/dotnet/SeleniumDocs/Interactions/WindowsTest.cs#L17-L21" >}}
{{< /tab >}}
{{< tab header="Ruby" >}}driver.window_handle{{< /tab >}}
{{< tab header="JavaScript" >}}await driver.getWindowHandle();{{< /tab >}}
{{< tab header="Kotlin" >}}driver.windowHandle{{< /tab >}}
Expand Down Expand Up @@ -79,31 +79,11 @@ with webdriver.Firefox() as driver:
# Wait for the new tab to finish loading content
wait.until(EC.title_is("SeleniumHQ Browser Automation"))
{{< /tab >}}
{{< tab header="CSharp" >}}
//Store the ID of the original window
string originalWindow = driver.CurrentWindowHandle;

//Check we don't have other windows open already
Assert.AreEqual(driver.WindowHandles.Count, 1);

//Click the link which opens in a new window
driver.FindElement(By.LinkText("new window")).Click();

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

//Loop through until we find a new window handle
foreach(string window in driver.WindowHandles)
{
if(originalWindow != window)
{
driver.SwitchTo().Window(window);
break;
}
}
//Wait for the new tab to finish loading content
wait.Until(wd => wd.Title == "Selenium documentation");

{{< tab header="CSharp" text=true >}}
{{< gh-codeblock path="examples/dotnet/SeleniumDocs/Interactions/WindowsTest.cs#L23-L30" >}}
{{< /tab >}}

{{< tab header="Ruby" >}}

# Store the ID of the original window
Expand Down Expand Up @@ -203,13 +183,11 @@ driver.close()
#Switch back to the old tab or window
driver.switch_to.window(original_window)
{{< /tab >}}
{{< tab header="CSharp" >}}
//Close the tab or window
driver.Close();

//Switch back to the old tab or window
driver.SwitchTo().Window(originalWindow);

{{< tab header="CSharp" text=true >}}
{{< gh-codeblock path="examples/dotnet/SeleniumDocs/Interactions/WindowsTest.cs#L32-L35" >}}
{{< /tab >}}

{{< tab header="Ruby" >}}
#Close the tab or window
driver.close
Expand Down Expand Up @@ -259,13 +237,12 @@ driver.switch_to.new_window('tab')
# Opens a new window and switches to new window
driver.switch_to.new_window('window')
{{< /tab >}}
{{< tab header="CSharp" >}}
// Opens a new tab and switches to new tab
driver.SwitchTo().NewWindow(WindowType.Tab)

// Opens a new window and switches to new window
driver.SwitchTo().NewWindow(WindowType.Window)


{{< tab header="CSharp" text=true >}}
{{< gh-codeblock path="examples/dotnet/SeleniumDocs/Interactions/WindowsTest.cs#L37-L43" >}}
{{< /tab >}}

{{% tab header="Ruby" text=true %}}
Opens a new tab and switches to new tab:
{{< gh-codeblock path="/examples/ruby/spec/interactions/windows_spec.rb#L9" >}}
Expand Down Expand Up @@ -302,7 +279,11 @@ instead of close:
{{< gh-codeblock path="examples/java/src/test/java/dev/selenium/interactions/WindowsTest.java#L44-L45" >}}
{{< /tab >}}
{{< tab header="Python" >}}driver.quit(){{< /tab >}}
{{< tab header="CSharp" >}}driver.Quit();{{< /tab >}}

{{< tab header="CSharp" text=true >}}
{{< gh-codeblock path="examples/dotnet/SeleniumDocs/Interactions/WindowsTest.cs#L45-L46" >}}
{{< /tab >}}

{{< tab header="Ruby" >}}driver.quit{{< /tab >}}
{{< tab header="JavaScript" >}}await driver.quit();{{< /tab >}}
{{< tab header="Kotlin" >}}driver.quit(){{< /tab >}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ WebDriverは、ウィンドウとタブを区別しません。
{{< gh-codeblock path="examples/java/src/test/java/dev/selenium/interactions/WindowsTest.java#L16-L20" >}}
{{< /tab >}}
{{< tab header="Python" >}}driver.current_window_handle{{< /tab >}}
{{< tab header="CSharp" >}}
{{< gh-codeblock path="examples/dotnet/SeleniumDocs/Interactions/WindowsTest.cs#L14-L18" >}}
{{< /tab >}}
{{< tab header="CSharp" text=true >}}
{{< gh-codeblock path="examples/dotnet/SeleniumDocs/Interactions/WindowsTest.cs#L17-L21" >}}
{{< /tab >}}
{{< tab header="Ruby" >}}driver.window_handle{{< /tab >}}
{{< tab header="JavaScript" >}}await driver.getWindowHandle();{{< /tab >}}
{{< tab header="Kotlin" >}}driver.windowHandle{{< /tab >}}
Expand Down Expand Up @@ -75,31 +75,12 @@ with webdriver.Firefox() as driver:
# Wait for the new tab to finish loading content
wait.until(EC.title_is("SeleniumHQ Browser Automation"))
{{< /tab >}}
{{< tab header="CSharp" >}}
//Store the ID of the original window
string originalWindow = driver.CurrentWindowHandle;

//Check we don't have other windows open already
Assert.AreEqual(driver.WindowHandles.Count, 1);

//Click the link which opens in a new window
driver.FindElement(By.LinkText("new window")).Click();

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

//Loop through until we find a new window handle
foreach(string window in driver.WindowHandles)
{
if(originalWindow != window)
{
driver.SwitchTo().Window(window);
break;
}
}
//Wait for the new tab to finish loading content
wait.Until(wd => wd.Title == "Selenium documentation");

{{< tab header="CSharp" text=true >}}
{{< gh-codeblock path="examples/dotnet/SeleniumDocs/Interactions/WindowsTest.cs#L23-L30" >}}
{{< /tab >}}


{{< tab header="Ruby" >}}
#Store the ID of the original window
original_window = driver.window_handle
Expand Down Expand Up @@ -195,13 +176,12 @@ driver.close()
#Switch back to the old tab or window
driver.switch_to.window(original_window)
{{< /tab >}}
{{< tab header="CSharp" >}}
//Close the tab or window
driver.Close();

//Switch back to the old tab or window
driver.SwitchTo().Window(originalWindow);
{{< tab header="CSharp" text=true >}}
{{< gh-codeblock path="examples/dotnet/SeleniumDocs/Interactions/WindowsTest.cs#L32-L35" >}}
{{< /tab >}}


{{< tab header="Ruby" >}}
#Close the tab or window
driver.close
Expand Down Expand Up @@ -247,34 +227,36 @@ driver.switch_to.new_window('tab')
# Opens a new window and switches to new window
driver.switch_to.new_window('window')
{{< /tab >}}
{{< tab header="CSharp" >}}
// Opens a new tab and switches to new tab
driver.SwitchTo().NewWindow(WindowType.Tab)

// Opens a new window and switches to new window
driver.SwitchTo().NewWindow(WindowType.Window)

{{< tab header="CSharp" text=true >}}
{{< gh-codeblock path="examples/dotnet/SeleniumDocs/Interactions/WindowsTest.cs#L37-L43" >}}
{{< /tab >}}


{{% tab header="Ruby" text=true %}}
Opens a new tab and switches to new tab
{{< gh-codeblock path="/examples/ruby/spec/interactions/windows_spec.rb#L9" >}}

Opens a new window and switches to new window
{{< gh-codeblock path="/examples/ruby/spec/interactions/windows_spec.rb#L15" >}}
{{% /tab %}}

{{< tab header="JavaScript" text=true >}}
Opens a new tab and switches to new tab
{{< gh-codeblock path="examples/javascript/test/interactions/windows.spec.js#L70" >}}

Opens a new window and switches to new window:
{{< gh-codeblock path="examples/javascript/test/interactions/windows.spec.js#L75" >}}
{{< /tab >}}

{{< tab header="Kotlin" >}}
// Opens a new tab and switches to new tab
driver.switchTo().newWindow(WindowType.TAB)

// Opens a new window and switches to new window
driver.switchTo().newWindow(WindowType.WINDOW)
{{< /tab >}}

{{< /tabpane >}}


Expand All @@ -287,7 +269,9 @@ driver.switchTo().newWindow(WindowType.WINDOW)
{{< gh-codeblock path="examples/java/src/test/java/dev/selenium/interactions/WindowsTest.java#L44-L45" >}}
{{< /tab >}}
{{< tab header="Python" >}}driver.quit(){{< /tab >}}
{{< tab header="CSharp" >}}driver.Quit();{{< /tab >}}
{{< tab header="CSharp" text=true >}}
{{< gh-codeblock path="examples/dotnet/SeleniumDocs/Interactions/WindowsTest.cs#L45-L46" >}}
{{< /tab >}}
{{< tab header="Ruby" >}}driver.quit{{< /tab >}}
{{< tab header="JavaScript" >}}await driver.quit();{{< /tab >}}
{{< tab header="Kotlin" >}}driver.quit(){{< /tab >}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ persistente em uma única sessão. Você pode pegar o identificador atual usando
{{< gh-codeblock path="examples/java/src/test/java/dev/selenium/interactions/WindowsTest.java#L16-L20" >}}
{{< /tab >}}
{{< tab header="Python" >}}driver.current_window_handle{{< /tab >}}
{{< tab header="CSharp" >}}
{{< gh-codeblock path="examples/dotnet/SeleniumDocs/Interactions/WindowsTest.cs#L14-L18" >}}
{{< /tab >}}
{{< tab header="CSharp" text=true >}}
{{< gh-codeblock path="examples/dotnet/SeleniumDocs/Interactions/WindowsTest.cs#L17-L21" >}}
{{< /tab >}}
{{< tab header="Ruby" >}}driver.window_handle{{< /tab >}}
{{< tab header="JavaScript" >}}await driver.getWindowHandle();{{< /tab >}}
{{< tab header="Kotlin" >}}driver.windowHandle{{< /tab >}}
Expand Down Expand Up @@ -81,31 +81,13 @@ with webdriver.Firefox() as driver:
# Wait for the new tab to finish loading content
wait.until(EC.title_is("SeleniumHQ Browser Automation"))
{{< /tab >}}
{{< tab header="CSharp" >}}
//Store the ID of the original window
string originalWindow = driver.CurrentWindowHandle;

//Check we don't have other windows open already
Assert.AreEqual(driver.WindowHandles.Count, 1);

//Click the link which opens in a new window
driver.FindElement(By.LinkText("new window")).Click();
{{< tab header="CSharp" text=true >}}
{{< gh-codeblock path="examples/dotnet/SeleniumDocs/Interactions/WindowsTest.cs#L23-L30" >}}
{{< /tab >}}


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

//Loop through until we find a new window handle
foreach(string window in driver.WindowHandles)
{
if(originalWindow != window)
{
driver.SwitchTo().Window(window);
break;
}
}
//Wait for the new tab to finish loading content
wait.Until(wd => wd.Title == "Selenium documentation");
{{< /tab >}}
{{< tab header="Ruby" >}}
#Store the ID of the original window
original_window = driver.window_handle
Expand Down Expand Up @@ -203,13 +185,11 @@ driver.close()
#Switch back to the old tab or window
driver.switch_to.window(original_window)
{{< /tab >}}
{{< tab header="CSharp" >}}
//Close the tab or window
driver.Close();

//Switch back to the old tab or window
driver.SwitchTo().Window(originalWindow);
{{< tab header="CSharp" text=true >}}
{{< gh-codeblock path="examples/dotnet/SeleniumDocs/Interactions/WindowsTest.cs#L32-L35" >}}
{{< /tab >}}

{{< tab header="Ruby" >}}
#Close the tab or window
driver.close
Expand Down Expand Up @@ -257,13 +237,12 @@ driver.switch_to.new_window('tab')
# Opens a new window and switches to new window
driver.switch_to.new_window('window')
{{< /tab >}}
{{< tab header="CSharp" >}}
// Opens a new tab and switches to new tab
driver.SwitchTo().NewWindow(WindowType.Tab)

// Opens a new window and switches to new window
driver.SwitchTo().NewWindow(WindowType.Window)

{{< tab header="CSharp" text=true >}}
{{< gh-codeblock path="examples/dotnet/SeleniumDocs/Interactions/WindowsTest.cs#L37-L43" >}}
{{< /tab >}}


{{% tab header="Ruby" text=true %}}
Opens a new tab and switches to new tab
{{< gh-codeblock path="/examples/ruby/spec/interactions/windows_spec.rb#L9" >}}
Expand Down Expand Up @@ -301,7 +280,9 @@ em vez de fechar:
{{< /tab >}}

{{< tab header="Python" >}}driver.quit(){{< /tab >}}
{{< tab header="CSharp" >}}driver.Quit();{{< /tab >}}
{{< tab header="CSharp" text=true >}}
{{< gh-codeblock path="examples/dotnet/SeleniumDocs/Interactions/WindowsTest.cs#L45-L46" >}}
{{< /tab >}}
{{< tab header="Ruby" >}}driver.quit{{< /tab >}}
{{< tab header="JavaScript" >}}await driver.quit();{{< /tab >}}
{{< tab header="Kotlin" >}}driver.quit(){{< /tab >}}
Expand Down
Loading
Loading