Skip to content

Commit c66b31d

Browse files
added csharp alert code
1 parent 3a3c6b9 commit c66b31d

File tree

5 files changed

+137
-148
lines changed

5 files changed

+137
-148
lines changed
Lines changed: 101 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,109 @@
1+
// Licensed to the Software Freedom Conservancy (SFC) under one
2+
// or more contributor license agreements. See the NOTICE file
3+
// distributed with this work for additional information
4+
// regarding copyright ownership. The SFC licenses this file
5+
// to you under the Apache License, Version 2.0 (the
6+
// "License"); you may not use this file except in compliance
7+
// with the License. You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing,
12+
// software distributed under the License is distributed on an
13+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
// KIND, either express or implied. See the License for the
15+
// specific language governing permissions and limitations
16+
// under the License.
17+
using System;
118
using Microsoft.VisualStudio.TestTools.UnitTesting;
19+
using OpenQA.Selenium;
20+
using OpenQA.Selenium.Chrome;
21+
using OpenQA.Selenium.Support.UI;
222

323
namespace SeleniumDocs.Interactions
424
{
525
[TestClass]
6-
public class AlertsTest : BaseTest
26+
public class AlertsTest
727
{
28+
[TestMethod]
29+
public void TestAlertCommands(){
30+
WebDriver driver = new ChromeDriver();
31+
driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromMilliseconds(500);
32+
// Navigate to Url
33+
driver.Url= "https://www.selenium.dev/documentation/webdriver/interactions/alerts/";
34+
// Simple Alert
35+
IJavaScriptExecutor js = (IJavaScriptExecutor)driver;
36+
// Execute JS for alert
37+
js.ExecuteScript("alert('Sample Alert');");
38+
WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(30));
39+
// Wait for the alert to be displayed and store it in a variable
40+
wait.Until((driver) => {
41+
try
42+
{
43+
return driver.SwitchTo().Alert();
44+
}
45+
catch (Exception ex)
46+
{
47+
return null;
48+
}
49+
});
50+
51+
IAlert alert = driver.SwitchTo().Alert();
52+
// Store the alert text in a variable and verify it
53+
string text = alert.Text;
54+
Assert.AreEqual(text, "Sample Alert");
55+
alert.Accept();
56+
57+
// Confirm Alert
58+
// Execute JS for confirm
59+
js.ExecuteScript("confirm('Are you sure?');");
60+
// Wait for the alert to be displayed
61+
// Wait for the alert to be displayed and store it in a variable
62+
wait.Until((driver) => {
63+
try
64+
{
65+
return driver.SwitchTo().Alert();
66+
}
67+
catch (Exception ex)
68+
{
69+
return null;
70+
}
71+
});
72+
73+
alert = driver.SwitchTo().Alert();
74+
// Store the alert text in a variable and verify it
75+
text = alert.Text;
76+
Assert.AreEqual(text, "Are you sure?");
77+
// Press the Cancel button
78+
alert.Dismiss();
79+
80+
// Prompt Alert
81+
// Execute JS for prompt
82+
js.ExecuteScript("prompt('What is your name?');");
83+
// Wait for the alert to be displayed
84+
// Wait for the alert to be displayed and store it in a variable
85+
wait.Until((driver) => {
86+
try
87+
{
88+
return driver.SwitchTo().Alert();
89+
}
90+
catch (Exception ex)
91+
{
92+
return null;
93+
}
94+
});
95+
96+
alert = driver.SwitchTo().Alert();
97+
// Store the alert text in a variable and verify it
98+
text = alert.Text;
99+
Assert.AreEqual(text, "What is your name?");
100+
// Type your message
101+
alert.SendKeys("Selenium");
102+
// Press the OK button
103+
alert.Accept();
104+
105+
//quitting driver
106+
driver.Quit(); //close all windows
107+
}
8108
}
9109
}

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

Lines changed: 9 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -34,19 +34,9 @@ alerts.
3434
{{< gh-codeblock path="examples/python/tests/interactions/test_alerts.py#L12-L18" >}}
3535
{{< /tab >}}
3636

37-
{{< tab header="CSharp" >}}
38-
//Click the link to activate the alert
39-
driver.FindElement(By.LinkText("See an example alert")).Click();
40-
41-
//Wait for the alert to be displayed and store it in a variable
42-
IAlert alert = wait.Until(ExpectedConditions.AlertIsPresent());
43-
44-
//Store the alert text in a variable
45-
string text = alert.Text;
46-
47-
//Press the OK button
48-
alert.Accept();
49-
{{< /tab >}}
37+
{{< tab header="CSharp" text=true >}}
38+
{{< gh-codeblock path="examples/dotnet/SeleniumDocs/Interactions/AlertsTest.cs#L51-L55" >}}
39+
{{< /tab >}}
5040
{{< tab header="Ruby" text=true >}}
5141
{{< gh-codeblock path="examples/ruby/spec/interactions/alerts_spec.rb#L15-L22" >}}
5242
{{< /tab >}}
@@ -86,22 +76,10 @@ This example also shows a different approach to storing an alert:
8676
{{< gh-codeblock path="examples/python/tests/interactions/test_alerts.py#L26-L32" >}}
8777
{{< /tab >}}
8878

89-
{{< tab header="CSharp" >}}
90-
//Click the link to activate the alert
91-
driver.FindElement(By.LinkText("See a sample confirm")).Click();
92-
93-
//Wait for the alert to be displayed
94-
wait.Until(ExpectedConditions.AlertIsPresent());
95-
96-
//Store the alert in a variable
97-
IAlert alert = driver.SwitchTo().Alert();
98-
99-
//Store the alert in a variable for reuse
100-
string text = alert.Text;
79+
{{< tab header="CSharp" text=true >}}
80+
{{< gh-codeblock path="examples/dotnet/SeleniumDocs/Interactions/AlertsTest.cs#L73-L78" >}}
81+
{{< /tab >}}
10182

102-
//Press the Cancel button
103-
alert.Dismiss();
104-
{{< /tab >}}
10583
{{< tab header="Ruby" text=true >}}
10684
{{< gh-codeblock path="examples/ruby/spec/interactions/alerts_spec.rb#L28-L35" >}}
10785
{{< /tab >}}
@@ -146,19 +124,10 @@ See a sample prompt</a>.
146124
{{< gh-codeblock path="examples/python/tests/interactions/test_alerts.py#L40-L47" >}}
147125
{{< /tab >}}
148126

149-
{{< tab header="CSharp" >}}
150-
//Click the link to activate the alert
151-
driver.FindElement(By.LinkText("See a sample prompt")).Click();
152-
153-
//Wait for the alert to be displayed and store it in a variable
154-
IAlert alert = wait.Until(ExpectedConditions.AlertIsPresent());
155-
156-
//Type your message
157-
alert.SendKeys("Selenium");
127+
{{< tab header="CSharp" text=true >}}
128+
{{< gh-codeblock path="examples/dotnet/SeleniumDocs/Interactions/AlertsTest.cs#L96-L103" >}}
129+
{{< /tab >}}
158130

159-
//Press the OK button
160-
alert.Accept();
161-
{{< /tab >}}
162131
{{< tab header="Ruby" text=true >}}
163132
{{< gh-codeblock path="examples/ruby/spec/interactions/alerts_spec.rb#L41-L48" >}}
164133
{{< /tab >}}

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

Lines changed: 9 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -29,19 +29,9 @@ WebDriverはポップアップからテキストを取得し、これらのア
2929
{{< gh-codeblock path="examples/python/tests/interactions/test_alerts.py#L12-L18" >}}
3030
{{< /tab >}}
3131

32-
{{< tab header="CSharp" >}}
33-
//Click the link to activate the alert
34-
driver.FindElement(By.LinkText("See an example alert")).Click();
35-
36-
//Wait for the alert to be displayed and store it in a variable
37-
IAlert alert = wait.Until(ExpectedConditions.AlertIsPresent());
38-
39-
//Store the alert text in a variable
40-
string text = alert.Text;
41-
42-
//Press the OK button
43-
alert.Accept();
44-
{{< /tab >}}
32+
{{< tab header="CSharp" text=true >}}
33+
{{< gh-codeblock path="examples/dotnet/SeleniumDocs/Interactions/AlertsTest.cs#L51-L55" >}}
34+
{{< /tab >}}
4535
{{< tab header="Ruby" text=true >}}
4636
{{< gh-codeblock path="examples/ruby/spec/interactions/alerts_spec.rb#L15-L22" >}}
4737
{{< /tab >}}
@@ -79,22 +69,10 @@ alert.accept()
7969
{{< gh-codeblock path="examples/python/tests/interactions/test_alerts.py#L26-L32" >}}
8070
{{< /tab >}}
8171

82-
{{< tab header="CSharp" >}}
83-
//Click the link to activate the alert
84-
driver.FindElement(By.LinkText("See a sample confirm")).Click();
85-
86-
//Wait for the alert to be displayed
87-
wait.Until(ExpectedConditions.AlertIsPresent());
88-
89-
//Store the alert in a variable
90-
IAlert alert = driver.SwitchTo().Alert();
91-
92-
//Store the alert in a variable for reuse
93-
string text = alert.Text;
72+
{{< tab header="CSharp" text=true >}}
73+
{{< gh-codeblock path="examples/dotnet/SeleniumDocs/Interactions/AlertsTest.cs#L73-L78" >}}
74+
{{< /tab >}}
9475

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

139-
{{< tab header="CSharp" >}}
140-
//Click the link to activate the alert
141-
driver.FindElement(By.LinkText("See a sample prompt")).Click();
142-
143-
//Wait for the alert to be displayed and store it in a variable
144-
IAlert alert = wait.Until(ExpectedConditions.AlertIsPresent());
145-
146-
//Type your message
147-
alert.SendKeys("Selenium");
117+
{{< tab header="CSharp" text=true >}}
118+
{{< gh-codeblock path="examples/dotnet/SeleniumDocs/Interactions/AlertsTest.cs#L96-L103" >}}
119+
{{< /tab >}}
148120

149-
//Press the OK button
150-
alert.Accept();
151-
{{< /tab >}}
152121
{{< tab header="Ruby" text=true >}}
153122
{{< gh-codeblock path="examples/ruby/spec/interactions/alerts_spec.rb#L41-L48" >}}
154123
{{< /tab >}}

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

Lines changed: 9 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,9 @@ alertas.
3131
{{< tab header="Python" text=true >}}
3232
{{< gh-codeblock path="examples/python/tests/interactions/test_alerts.py#L12-L18" >}}
3333
{{< /tab >}}
34-
34+
{{< tab header="CSharp" text=true >}}
35+
{{< gh-codeblock path="examples/dotnet/SeleniumDocs/Interactions/AlertsTest.cs#L51-L55" >}}
36+
{{< /tab >}}
3537
{{< tab header="Ruby" text=true >}}
3638
{{< gh-codeblock path="examples/ruby/spec/interactions/alerts_spec.rb#L15-L22" >}}
3739
{{< /tab >}}
@@ -70,22 +72,10 @@ Este exemplo também mostra uma abordagem diferente para armazenar um alerta:
7072
{{< gh-codeblock path="examples/python/tests/interactions/test_alerts.py#L26-L32" >}}
7173
{{< /tab >}}
7274

73-
{{< tab header="CSharp" >}}
74-
//Click the link to activate the alert
75-
driver.FindElement(By.LinkText("See a sample confirm")).Click();
76-
77-
//Wait for the alert to be displayed
78-
wait.Until(ExpectedConditions.AlertIsPresent());
79-
80-
//Store the alert in a variable
81-
IAlert alert = driver.SwitchTo().Alert();
82-
83-
//Store the alert in a variable for reuse
84-
string text = alert.Text;
75+
{{< tab header="CSharp" text=true >}}
76+
{{< gh-codeblock path="examples/dotnet/SeleniumDocs/Interactions/AlertsTest.cs#L73-L78" >}}
77+
{{< /tab >}}
8578

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

132-
{{< tab header="CSharp" >}}
133-
//Click the link to activate the alert
134-
driver.FindElement(By.LinkText("See a sample prompt")).Click();
135-
136-
//Wait for the alert to be displayed and store it in a variable
137-
IAlert alert = wait.Until(ExpectedConditions.AlertIsPresent());
138-
139-
//Type your message
140-
alert.SendKeys("Selenium");
122+
{{< tab header="CSharp" text=true >}}
123+
{{< gh-codeblock path="examples/dotnet/SeleniumDocs/Interactions/AlertsTest.cs#L96-L103" >}}
124+
{{< /tab >}}
141125

142-
//Press the OK button
143-
alert.Accept();
144-
{{< /tab >}}
145126
{{< tab header="Ruby" text=true >}}
146127
{{< gh-codeblock path="examples/ruby/spec/interactions/alerts_spec.rb#L41-L48" >}}
147128
{{< /tab >}}

website_and_docs/content/documentation/webdriver/interactions/alerts.zh-cn.md

Lines changed: 9 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -26,19 +26,10 @@ WebDriver可以从弹窗获取文本并接受或关闭这些警告.
2626
{{< gh-codeblock path="examples/python/tests/interactions/test_alerts.py#L12-L18" >}}
2727
{{< /tab >}}
2828

29-
{{< tab header="CSharp" >}}
30-
//Click the link to activate the alert
31-
driver.FindElement(By.LinkText("See an example alert")).Click();
32-
33-
//Wait for the alert to be displayed and store it in a variable
34-
IAlert alert = wait.Until(ExpectedConditions.AlertIsPresent());
35-
36-
//Store the alert text in a variable
37-
string text = alert.Text;
29+
{{< tab header="CSharp" text=true >}}
30+
{{< gh-codeblock path="examples/dotnet/SeleniumDocs/Interactions/AlertsTest.cs#L51-L55" >}}
31+
{{< /tab >}}
3832

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

78-
{{< tab header="CSharp" >}}
79-
//Click the link to activate the alert
80-
driver.FindElement(By.LinkText("See a sample confirm")).Click();
81-
82-
//Wait for the alert to be displayed
83-
wait.Until(ExpectedConditions.AlertIsPresent());
84-
85-
//Store the alert in a variable
86-
IAlert alert = driver.SwitchTo().Alert();
87-
88-
//Store the alert in a variable for reuse
89-
string text = alert.Text;
69+
{{< tab header="CSharp" text=true >}}
70+
{{< gh-codeblock path="examples/dotnet/SeleniumDocs/Interactions/AlertsTest.cs#L73-L78" >}}
71+
{{< /tab >}}
9072

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

134-
{{< tab header="CSharp" >}}
135-
//Click the link to activate the alert
136-
driver.FindElement(By.LinkText("See a sample prompt")).Click();
137-
138-
//Wait for the alert to be displayed and store it in a variable
139-
IAlert alert = wait.Until(ExpectedConditions.AlertIsPresent());
140-
141-
//Type your message
142-
alert.SendKeys("Selenium");
113+
{{< tab header="CSharp" text=true >}}
114+
{{< gh-codeblock path="examples/dotnet/SeleniumDocs/Interactions/AlertsTest.cs#L96-L103" >}}
115+
{{< /tab >}}
143116

144-
//Press the OK button
145-
alert.Accept();
146-
{{< /tab >}}
147117
{{< tab header="Ruby" text=true >}}
148118
{{< gh-codeblock path="examples/ruby/spec/interactions/alerts_spec.rb#L41-L48" >}}
149119
{{< /tab >}}

0 commit comments

Comments
 (0)