Skip to content
102 changes: 101 additions & 1 deletion examples/dotnet/SeleniumDocs/Interactions/AlertsTest.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,109 @@
// Licensed to the Software Freedom Conservancy (SFC) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The SFC licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using OpenQA.Selenium.Support.UI;

namespace SeleniumDocs.Interactions
{
[TestClass]
public class AlertsTest : BaseTest
public class AlertsTest
{
[TestMethod]
public void TestAlertCommands(){
WebDriver driver = new ChromeDriver();
driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromMilliseconds(500);
// Navigate to Url
driver.Url= "https://www.selenium.dev/documentation/webdriver/interactions/alerts/";
// Simple Alert
IJavaScriptExecutor js = (IJavaScriptExecutor)driver;
// Execute JS for alert
js.ExecuteScript("alert('Sample Alert');");
WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(30));
// Wait for the alert to be displayed and store it in a variable
wait.Until((driver) => {
try
{
return driver.SwitchTo().Alert();
}
catch (Exception ex)

Check warning on line 45 in examples/dotnet/SeleniumDocs/Interactions/AlertsTest.cs

View workflow job for this annotation

GitHub Actions / tests (macos, nightly)

The variable 'ex' is declared but never used

Check warning on line 45 in examples/dotnet/SeleniumDocs/Interactions/AlertsTest.cs

View workflow job for this annotation

GitHub Actions / tests (macos, nightly)

The variable 'ex' is declared but never used

Check warning on line 45 in examples/dotnet/SeleniumDocs/Interactions/AlertsTest.cs

View workflow job for this annotation

GitHub Actions / tests (macos, nightly)

The variable 'ex' is declared but never used

Check warning on line 45 in examples/dotnet/SeleniumDocs/Interactions/AlertsTest.cs

View workflow job for this annotation

GitHub Actions / tests (ubuntu, nightly)

The variable 'ex' is declared but never used

Check warning on line 45 in examples/dotnet/SeleniumDocs/Interactions/AlertsTest.cs

View workflow job for this annotation

GitHub Actions / tests (ubuntu, nightly)

The variable 'ex' is declared but never used

Check warning on line 45 in examples/dotnet/SeleniumDocs/Interactions/AlertsTest.cs

View workflow job for this annotation

GitHub Actions / tests (ubuntu, nightly)

The variable 'ex' is declared but never used

Check warning on line 45 in examples/dotnet/SeleniumDocs/Interactions/AlertsTest.cs

View workflow job for this annotation

GitHub Actions / tests (windows, nightly)

The variable 'ex' is declared but never used

Check warning on line 45 in examples/dotnet/SeleniumDocs/Interactions/AlertsTest.cs

View workflow job for this annotation

GitHub Actions / tests (windows, nightly)

The variable 'ex' is declared but never used

Check warning on line 45 in examples/dotnet/SeleniumDocs/Interactions/AlertsTest.cs

View workflow job for this annotation

GitHub Actions / tests (windows, nightly)

The variable 'ex' is declared but never used

Check warning on line 45 in examples/dotnet/SeleniumDocs/Interactions/AlertsTest.cs

View workflow job for this annotation

GitHub Actions / tests (ubuntu, stable)

The variable 'ex' is declared but never used

Check warning on line 45 in examples/dotnet/SeleniumDocs/Interactions/AlertsTest.cs

View workflow job for this annotation

GitHub Actions / tests (macos, stable)

The variable 'ex' is declared but never used

Check warning on line 45 in examples/dotnet/SeleniumDocs/Interactions/AlertsTest.cs

View workflow job for this annotation

GitHub Actions / tests (windows, stable)

The variable 'ex' is declared but never used
{
return null;
}
});

IAlert alert = driver.SwitchTo().Alert();
// Store the alert text in a variable and verify it
string text = alert.Text;
Assert.AreEqual(text, "Sample Alert");
alert.Accept();

// Confirm Alert
// Execute JS for confirm
js.ExecuteScript("confirm('Are you sure?');");
// Wait for the alert to be displayed
// Wait for the alert to be displayed and store it in a variable
wait.Until((driver) => {
try
{
return driver.SwitchTo().Alert();
}
catch (Exception ex)

Check warning on line 67 in examples/dotnet/SeleniumDocs/Interactions/AlertsTest.cs

View workflow job for this annotation

GitHub Actions / tests (macos, nightly)

The variable 'ex' is declared but never used

Check warning on line 67 in examples/dotnet/SeleniumDocs/Interactions/AlertsTest.cs

View workflow job for this annotation

GitHub Actions / tests (macos, nightly)

The variable 'ex' is declared but never used

Check warning on line 67 in examples/dotnet/SeleniumDocs/Interactions/AlertsTest.cs

View workflow job for this annotation

GitHub Actions / tests (macos, nightly)

The variable 'ex' is declared but never used

Check warning on line 67 in examples/dotnet/SeleniumDocs/Interactions/AlertsTest.cs

View workflow job for this annotation

GitHub Actions / tests (ubuntu, nightly)

The variable 'ex' is declared but never used

Check warning on line 67 in examples/dotnet/SeleniumDocs/Interactions/AlertsTest.cs

View workflow job for this annotation

GitHub Actions / tests (ubuntu, nightly)

The variable 'ex' is declared but never used

Check warning on line 67 in examples/dotnet/SeleniumDocs/Interactions/AlertsTest.cs

View workflow job for this annotation

GitHub Actions / tests (ubuntu, nightly)

The variable 'ex' is declared but never used

Check warning on line 67 in examples/dotnet/SeleniumDocs/Interactions/AlertsTest.cs

View workflow job for this annotation

GitHub Actions / tests (windows, nightly)

The variable 'ex' is declared but never used

Check warning on line 67 in examples/dotnet/SeleniumDocs/Interactions/AlertsTest.cs

View workflow job for this annotation

GitHub Actions / tests (windows, nightly)

The variable 'ex' is declared but never used

Check warning on line 67 in examples/dotnet/SeleniumDocs/Interactions/AlertsTest.cs

View workflow job for this annotation

GitHub Actions / tests (windows, nightly)

The variable 'ex' is declared but never used

Check warning on line 67 in examples/dotnet/SeleniumDocs/Interactions/AlertsTest.cs

View workflow job for this annotation

GitHub Actions / tests (ubuntu, stable)

The variable 'ex' is declared but never used

Check warning on line 67 in examples/dotnet/SeleniumDocs/Interactions/AlertsTest.cs

View workflow job for this annotation

GitHub Actions / tests (macos, stable)

The variable 'ex' is declared but never used

Check warning on line 67 in examples/dotnet/SeleniumDocs/Interactions/AlertsTest.cs

View workflow job for this annotation

GitHub Actions / tests (windows, stable)

The variable 'ex' is declared but never used
{
return null;
}
});

alert = driver.SwitchTo().Alert();
// Store the alert text in a variable and verify it
text = alert.Text;
Assert.AreEqual(text, "Are you sure?");
// Press the Cancel button
alert.Dismiss();

// Prompt Alert
// Execute JS for prompt
js.ExecuteScript("prompt('What is your name?');");
// Wait for the alert to be displayed
// Wait for the alert to be displayed and store it in a variable
wait.Until((driver) => {
try
{
return driver.SwitchTo().Alert();
}
catch (Exception ex)

Check warning on line 90 in examples/dotnet/SeleniumDocs/Interactions/AlertsTest.cs

View workflow job for this annotation

GitHub Actions / tests (macos, nightly)

The variable 'ex' is declared but never used

Check warning on line 90 in examples/dotnet/SeleniumDocs/Interactions/AlertsTest.cs

View workflow job for this annotation

GitHub Actions / tests (macos, nightly)

The variable 'ex' is declared but never used

Check warning on line 90 in examples/dotnet/SeleniumDocs/Interactions/AlertsTest.cs

View workflow job for this annotation

GitHub Actions / tests (ubuntu, nightly)

The variable 'ex' is declared but never used

Check warning on line 90 in examples/dotnet/SeleniumDocs/Interactions/AlertsTest.cs

View workflow job for this annotation

GitHub Actions / tests (ubuntu, nightly)

The variable 'ex' is declared but never used

Check warning on line 90 in examples/dotnet/SeleniumDocs/Interactions/AlertsTest.cs

View workflow job for this annotation

GitHub Actions / tests (windows, nightly)

The variable 'ex' is declared but never used

Check warning on line 90 in examples/dotnet/SeleniumDocs/Interactions/AlertsTest.cs

View workflow job for this annotation

GitHub Actions / tests (windows, nightly)

The variable 'ex' is declared but never used

Check warning on line 90 in examples/dotnet/SeleniumDocs/Interactions/AlertsTest.cs

View workflow job for this annotation

GitHub Actions / tests (ubuntu, stable)

The variable 'ex' is declared but never used

Check warning on line 90 in examples/dotnet/SeleniumDocs/Interactions/AlertsTest.cs

View workflow job for this annotation

GitHub Actions / tests (macos, stable)

The variable 'ex' is declared but never used

Check warning on line 90 in examples/dotnet/SeleniumDocs/Interactions/AlertsTest.cs

View workflow job for this annotation

GitHub Actions / tests (windows, stable)

The variable 'ex' is declared but never used
{
return null;
}
});

alert = driver.SwitchTo().Alert();
// Store the alert text in a variable and verify it
text = alert.Text;
Assert.AreEqual(text, "What is your name?");
// Type your message
alert.SendKeys("Selenium");
// Press the OK button
alert.Accept();

//quitting driver
driver.Quit(); //close all windows
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,19 +33,9 @@ alerts.
{{< gh-codeblock path="examples/python/tests/interactions/test_alerts.py#L12-L18" >}}
{{< /tab >}}

{{< tab header="CSharp" >}}
//Click the link to activate the alert
driver.FindElement(By.LinkText("See an example alert")).Click();

//Wait for the alert to be displayed and store it in a variable
IAlert alert = wait.Until(ExpectedConditions.AlertIsPresent());

//Store the alert text in a variable
string text = alert.Text;

//Press the OK button
alert.Accept();
{{< /tab >}}
{{< tab header="CSharp" text=true >}}
{{< gh-codeblock path="examples/dotnet/SeleniumDocs/Interactions/AlertsTest.cs#L51-L55" >}}
{{< /tab >}}
{{< tab header="Ruby" text=true >}}
{{< gh-codeblock path="examples/ruby/spec/interactions/alerts_spec.rb#L15-L22" >}}
{{< /tab >}}
Expand Down Expand Up @@ -84,22 +74,10 @@ This example also shows a different approach to storing an alert:
{{< gh-codeblock path="examples/python/tests/interactions/test_alerts.py#L26-L32" >}}
{{< /tab >}}

{{< tab header="CSharp" >}}
//Click the link to activate the alert
driver.FindElement(By.LinkText("See a sample confirm")).Click();

//Wait for the alert to be displayed
wait.Until(ExpectedConditions.AlertIsPresent());

//Store the alert in a variable
IAlert alert = driver.SwitchTo().Alert();

//Store the alert in a variable for reuse
string text = alert.Text;
{{< tab header="CSharp" text=true >}}
{{< gh-codeblock path="examples/dotnet/SeleniumDocs/Interactions/AlertsTest.cs#L73-L78" >}}
{{< /tab >}}

//Press the Cancel button
alert.Dismiss();
{{< /tab >}}
{{< tab header="Ruby" text=true >}}
{{< gh-codeblock path="examples/ruby/spec/interactions/alerts_spec.rb#L28-L35" >}}
{{< /tab >}}
Expand Down Expand Up @@ -143,19 +121,10 @@ See a sample prompt</a>.
{{< gh-codeblock path="examples/python/tests/interactions/test_alerts.py#L40-L47" >}}
{{< /tab >}}

{{< tab header="CSharp" >}}
//Click the link to activate the alert
driver.FindElement(By.LinkText("See a sample prompt")).Click();

//Wait for the alert to be displayed and store it in a variable
IAlert alert = wait.Until(ExpectedConditions.AlertIsPresent());

//Type your message
alert.SendKeys("Selenium");
{{< tab header="CSharp" text=true >}}
{{< gh-codeblock path="examples/dotnet/SeleniumDocs/Interactions/AlertsTest.cs#L96-L103" >}}
{{< /tab >}}

//Press the OK button
alert.Accept();
{{< /tab >}}
{{< tab header="Ruby" text=true >}}
{{< gh-codeblock path="examples/ruby/spec/interactions/alerts_spec.rb#L41-L48" >}}
{{< /tab >}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,19 +29,9 @@ WebDriverはポップアップからテキストを取得し、これらのア
{{< gh-codeblock path="examples/python/tests/interactions/test_alerts.py#L12-L18" >}}
{{< /tab >}}

{{< tab header="CSharp" >}}
//Click the link to activate the alert
driver.FindElement(By.LinkText("See an example alert")).Click();

//Wait for the alert to be displayed and store it in a variable
IAlert alert = wait.Until(ExpectedConditions.AlertIsPresent());

//Store the alert text in a variable
string text = alert.Text;

//Press the OK button
alert.Accept();
{{< /tab >}}
{{< tab header="CSharp" text=true >}}
{{< gh-codeblock path="examples/dotnet/SeleniumDocs/Interactions/AlertsTest.cs#L51-L55" >}}
{{< /tab >}}
{{< tab header="Ruby" text=true >}}
{{< gh-codeblock path="examples/ruby/spec/interactions/alerts_spec.rb#L15-L22" >}}
{{< /tab >}}
Expand Down Expand Up @@ -79,22 +69,10 @@ alert.accept()
{{< gh-codeblock path="examples/python/tests/interactions/test_alerts.py#L26-L32" >}}
{{< /tab >}}

{{< tab header="CSharp" >}}
//Click the link to activate the alert
driver.FindElement(By.LinkText("See a sample confirm")).Click();

//Wait for the alert to be displayed
wait.Until(ExpectedConditions.AlertIsPresent());

//Store the alert in a variable
IAlert alert = driver.SwitchTo().Alert();

//Store the alert in a variable for reuse
string text = alert.Text;
{{< tab header="CSharp" text=true >}}
{{< gh-codeblock path="examples/dotnet/SeleniumDocs/Interactions/AlertsTest.cs#L73-L78" >}}
{{< /tab >}}

//Press the Cancel button
alert.Dismiss();
{{< /tab >}}
{{< tab header="Ruby" text=true >}}
{{< gh-codeblock path="examples/ruby/spec/interactions/alerts_spec.rb#L28-L35" >}}
{{< /tab >}}
Expand Down Expand Up @@ -136,19 +114,10 @@ alert.dismiss()
{{< gh-codeblock path="examples/python/tests/interactions/test_alerts.py#L40-L47" >}}
{{< /tab >}}

{{< tab header="CSharp" >}}
//Click the link to activate the alert
driver.FindElement(By.LinkText("See a sample prompt")).Click();

//Wait for the alert to be displayed and store it in a variable
IAlert alert = wait.Until(ExpectedConditions.AlertIsPresent());

//Type your message
alert.SendKeys("Selenium");
{{< tab header="CSharp" text=true >}}
{{< gh-codeblock path="examples/dotnet/SeleniumDocs/Interactions/AlertsTest.cs#L96-L103" >}}
{{< /tab >}}

//Press the OK button
alert.Accept();
{{< /tab >}}
{{< tab header="Ruby" text=true >}}
{{< gh-codeblock path="examples/ruby/spec/interactions/alerts_spec.rb#L41-L48" >}}
{{< /tab >}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,9 @@ alertas.
{{< tab header="Python" text=true >}}
{{< gh-codeblock path="examples/python/tests/interactions/test_alerts.py#L12-L18" >}}
{{< /tab >}}

{{< tab header="CSharp" text=true >}}
{{< gh-codeblock path="examples/dotnet/SeleniumDocs/Interactions/AlertsTest.cs#L51-L55" >}}
{{< /tab >}}
{{< tab header="Ruby" text=true >}}
{{< gh-codeblock path="examples/ruby/spec/interactions/alerts_spec.rb#L15-L22" >}}
{{< /tab >}}
Expand Down Expand Up @@ -70,22 +72,10 @@ Este exemplo também mostra uma abordagem diferente para armazenar um alerta:
{{< gh-codeblock path="examples/python/tests/interactions/test_alerts.py#L26-L32" >}}
{{< /tab >}}

{{< tab header="CSharp" >}}
//Click the link to activate the alert
driver.FindElement(By.LinkText("See a sample confirm")).Click();

//Wait for the alert to be displayed
wait.Until(ExpectedConditions.AlertIsPresent());

//Store the alert in a variable
IAlert alert = driver.SwitchTo().Alert();

//Store the alert in a variable for reuse
string text = alert.Text;
{{< tab header="CSharp" text=true >}}
{{< gh-codeblock path="examples/dotnet/SeleniumDocs/Interactions/AlertsTest.cs#L73-L78" >}}
{{< /tab >}}

//Press the Cancel button
alert.Dismiss();
{{< /tab >}}
{{< tab header="Ruby" text=true >}}
{{< gh-codeblock path="examples/ruby/spec/interactions/alerts_spec.rb#L28-L35" >}}
{{< /tab >}}
Expand Down Expand Up @@ -129,19 +119,10 @@ Veja um exemplo de prompt </a>.
{{< gh-codeblock path="examples/python/tests/interactions/test_alerts.py#L40-L47" >}}
{{< /tab >}}

{{< tab header="CSharp" >}}
//Click the link to activate the alert
driver.FindElement(By.LinkText("See a sample prompt")).Click();

//Wait for the alert to be displayed and store it in a variable
IAlert alert = wait.Until(ExpectedConditions.AlertIsPresent());

//Type your message
alert.SendKeys("Selenium");
{{< tab header="CSharp" text=true >}}
{{< gh-codeblock path="examples/dotnet/SeleniumDocs/Interactions/AlertsTest.cs#L96-L103" >}}
{{< /tab >}}

//Press the OK button
alert.Accept();
{{< /tab >}}
{{< tab header="Ruby" text=true >}}
{{< gh-codeblock path="examples/ruby/spec/interactions/alerts_spec.rb#L41-L48" >}}
{{< /tab >}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,19 +26,10 @@ WebDriver可以从弹窗获取文本并接受或关闭这些警告.
{{< gh-codeblock path="examples/python/tests/interactions/test_alerts.py#L12-L18" >}}
{{< /tab >}}

{{< tab header="CSharp" >}}
//Click the link to activate the alert
driver.FindElement(By.LinkText("See an example alert")).Click();

//Wait for the alert to be displayed and store it in a variable
IAlert alert = wait.Until(ExpectedConditions.AlertIsPresent());

//Store the alert text in a variable
string text = alert.Text;
{{< tab header="CSharp" text=true >}}
{{< gh-codeblock path="examples/dotnet/SeleniumDocs/Interactions/AlertsTest.cs#L51-L55" >}}
{{< /tab >}}

//Press the OK button
alert.Accept();
{{< /tab >}}
{{< tab header="Ruby" text=true >}}
{{< gh-codeblock path="examples/ruby/spec/interactions/alerts_spec.rb#L15-L22" >}}
{{< /tab >}}
Expand Down Expand Up @@ -75,22 +66,10 @@ alert.accept()
{{< gh-codeblock path="examples/python/tests/interactions/test_alerts.py#L26-L32" >}}
{{< /tab >}}

{{< tab header="CSharp" >}}
//Click the link to activate the alert
driver.FindElement(By.LinkText("See a sample confirm")).Click();

//Wait for the alert to be displayed
wait.Until(ExpectedConditions.AlertIsPresent());

//Store the alert in a variable
IAlert alert = driver.SwitchTo().Alert();

//Store the alert in a variable for reuse
string text = alert.Text;
{{< tab header="CSharp" text=true >}}
{{< gh-codeblock path="examples/dotnet/SeleniumDocs/Interactions/AlertsTest.cs#L73-L78" >}}
{{< /tab >}}

//Press the Cancel button
alert.Dismiss();
{{< /tab >}}
{{< tab header="Ruby" text=true >}}
{{< gh-codeblock path="examples/ruby/spec/interactions/alerts_spec.rb#L28-L35" >}}
{{< /tab >}}
Expand Down Expand Up @@ -131,19 +110,10 @@ alert.dismiss()
{{< gh-codeblock path="examples/python/tests/interactions/test_alerts.py#L40-L47" >}}
{{< /tab >}}

{{< tab header="CSharp" >}}
//Click the link to activate the alert
driver.FindElement(By.LinkText("See a sample prompt")).Click();

//Wait for the alert to be displayed and store it in a variable
IAlert alert = wait.Until(ExpectedConditions.AlertIsPresent());

//Type your message
alert.SendKeys("Selenium");
{{< tab header="CSharp" text=true >}}
{{< gh-codeblock path="examples/dotnet/SeleniumDocs/Interactions/AlertsTest.cs#L96-L103" >}}
{{< /tab >}}

//Press the OK button
alert.Accept();
{{< /tab >}}
{{< tab header="Ruby" text=true >}}
{{< gh-codeblock path="examples/ruby/spec/interactions/alerts_spec.rb#L41-L48" >}}
{{< /tab >}}
Expand Down
Loading