Skip to content

Commit 020711f

Browse files
added csharp code for alert
1 parent 96a94db commit 020711f

File tree

5 files changed

+111
-148
lines changed

5 files changed

+111
-148
lines changed
Lines changed: 76 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,84 @@
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+
18+
using System;
119
using Microsoft.VisualStudio.TestTools.UnitTesting;
20+
using OpenQA.Selenium;
21+
using OpenQA.Selenium.Chrome;
22+
using System.Collections.Generic;
23+
using OpenQA.Selenium.Support.UI;
224

325
namespace SeleniumDocs.Interactions
426
{
527
[TestClass]
6-
public class AlertsTest : BaseTest
28+
public class AlertsTest
729
{
30+
[TestMethod]
31+
public void TestAlertCommands()
32+
{
33+
WebDriver driver = new ChromeDriver();
34+
driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromMilliseconds(500);
35+
36+
// Navigate to Url
37+
driver.Url= "https://www.selenium.dev/documentation/webdriver/interactions/alerts/";
38+
39+
// Simple Alert
40+
// Click the link to activate the alert
41+
IJavaScriptExecutor js = (IJavaScriptExecutor)driver;
42+
// Execute JS for alert
43+
js.ExecuteScript("alert('Sample Alert');");
44+
WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(30));
45+
// Wait for the alert to be displayed and store it in a variable
46+
wait.Until(SeleniumExtras.WaitHelpers.ExpectedConditions.AlertIsPresent());
47+
IAlert alert = driver.SwitchTo().Alert();
48+
// Store the alert text in a variable and verify it
49+
string text = alert.Text;
50+
Assert.AreEqual(text, "Sample Alert");
51+
alert.Accept();
52+
53+
54+
// Confirm Alert
55+
// Execute JS for confirm
56+
js.ExecuteScript("confirm('Are you sure?');");
57+
// Wait for the alert to be displayed
58+
wait.Until(SeleniumExtras.WaitHelpers.ExpectedConditions.AlertIsPresent());
59+
alert = driver.SwitchTo().Alert();
60+
// Store the alert text in a variable and verify it
61+
text = alert.Text;
62+
Assert.AreEqual(text, "Are you sure?");
63+
// Press the Cancel button
64+
alert.Dismiss();
65+
66+
// Prompt Alert
67+
// Execute JS for prompt
68+
js.ExecuteScript("prompt('What is your name?');");
69+
// Wait for the alert to be displayed
70+
wait.Until(SeleniumExtras.WaitHelpers.ExpectedConditions.AlertIsPresent());
71+
alert = driver.SwitchTo().Alert();
72+
// Store the alert text in a variable and verify it
73+
text = alert.Text;
74+
Assert.AreEqual(text, "What is your name?");
75+
// Type your message
76+
alert.SendKeys("Selenium");
77+
// Press the OK button
78+
alert.Accept();
79+
80+
//quitting driver
81+
driver.Quit(); //close all windows
82+
}
883
}
984
}

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

Lines changed: 9 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -34,19 +34,10 @@ 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;
37+
{{< tab header="CSharp" text=true >}}
38+
{{< gh-codeblock path="examples/dotnet/SeleniumDocs/Interactions/AlertsTest.cs#L39-L51" >}}
39+
{{< /tab >}}
4640

47-
//Press the OK button
48-
alert.Accept();
49-
{{< /tab >}}
5041
{{< tab header="Ruby" text=true >}}
5142
{{< gh-codeblock path="examples/ruby/spec/interactions/alerts_spec.rb#L15-L22" >}}
5243
{{< /tab >}}
@@ -86,22 +77,10 @@ This example also shows a different approach to storing an alert:
8677
{{< gh-codeblock path="examples/python/tests/interactions/test_alerts.py#L26-L32" >}}
8778
{{< /tab >}}
8879

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;
80+
{{< tab header="CSharp" text=true >}}
81+
{{< gh-codeblock path="examples/dotnet/SeleniumDocs/Interactions/AlertsTest.cs#L54-L64" >}}
82+
{{< /tab >}}
10183

102-
//Press the Cancel button
103-
alert.Dismiss();
104-
{{< /tab >}}
10584
{{< tab header="Ruby" text=true >}}
10685
{{< gh-codeblock path="examples/ruby/spec/interactions/alerts_spec.rb#L28-L35" >}}
10786
{{< /tab >}}
@@ -146,19 +125,11 @@ See a sample prompt</a>.
146125
{{< gh-codeblock path="examples/python/tests/interactions/test_alerts.py#L40-L47" >}}
147126
{{< /tab >}}
148127

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());
128+
{{< tab header="CSharp" text=true >}}
129+
{{< gh-codeblock path="examples/dotnet/SeleniumDocs/Interactions/AlertsTest.cs#L66-L78" >}}
130+
{{< /tab >}}
155131

156-
//Type your message
157-
alert.SendKeys("Selenium");
158132

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

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

Lines changed: 9 additions & 41 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#L39-L51" >}}
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,9 @@ 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;
94-
95-
//Press the Cancel button
96-
alert.Dismiss();
97-
{{< /tab >}}
72+
{{< tab header="CSharp" text=true >}}
73+
{{< gh-codeblock path="examples/dotnet/SeleniumDocs/Interactions/AlertsTest.cs#L54-L64" >}}
74+
{{< /tab >}}
9875
{{< tab header="Ruby" text=true >}}
9976
{{< gh-codeblock path="examples/ruby/spec/interactions/alerts_spec.rb#L28-L35" >}}
10077
{{< /tab >}}
@@ -136,19 +113,10 @@ alert.dismiss()
136113
{{< gh-codeblock path="examples/python/tests/interactions/test_alerts.py#L40-L47" >}}
137114
{{< /tab >}}
138115

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");
116+
{{< tab header="CSharp" text=true >}}
117+
{{< gh-codeblock path="examples/dotnet/SeleniumDocs/Interactions/AlertsTest.cs#L66-L78" >}}
118+
{{< /tab >}}
148119

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

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

Lines changed: 8 additions & 30 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#L39-L51" >}}
36+
{{< /tab >}}
3537
{{< tab header="Ruby" text=true >}}
3638
{{< gh-codeblock path="examples/ruby/spec/interactions/alerts_spec.rb#L15-L22" >}}
3739
{{< /tab >}}
@@ -70,24 +72,8 @@ 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;
85-
86-
//Press the Cancel button
87-
alert.Dismiss();
88-
{{< /tab >}}
89-
{{< tab header="Ruby" text=true >}}
90-
{{< gh-codeblock path="examples/ruby/spec/interactions/alerts_spec.rb#L28-L35" >}}
75+
{{< tab header="CSharp" text=true >}}
76+
{{< gh-codeblock path="examples/dotnet/SeleniumDocs/Interactions/AlertsTest.cs#L54-L64" >}}
9177
{{< /tab >}}
9278
{{< tab header="JavaScript" text=true >}}
9379
{{< gh-codeblock path="examples/javascript/test/interactions/alert.spec.js#L30-L32" >}}
@@ -129,19 +115,11 @@ Veja um exemplo de prompt </a>.
129115
{{< gh-codeblock path="examples/python/tests/interactions/test_alerts.py#L40-L47" >}}
130116
{{< /tab >}}
131117

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());
118+
{{< tab header="CSharp" text=true >}}
119+
{{< gh-codeblock path="examples/dotnet/SeleniumDocs/Interactions/AlertsTest.cs#L66-L78" >}}
120+
{{< /tab >}}
138121

139-
//Type your message
140-
alert.SendKeys("Selenium");
141122

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

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

Lines changed: 9 additions & 38 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#L39-L51" >}}
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#L54-L64" >}}
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,11 @@ 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());
113+
{{< tab header="CSharp" text=true >}}
114+
{{< gh-codeblock path="examples/dotnet/SeleniumDocs/Interactions/AlertsTest.cs#L66-L78" >}}
115+
{{< /tab >}}
140116

141-
//Type your message
142-
alert.SendKeys("Selenium");
143117

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

0 commit comments

Comments
 (0)