Skip to content

Commit 8389e17

Browse files
added java example code for alert (#1977)
Co-authored-by: Sri Harsha <[email protected]>
1 parent 88059e6 commit 8389e17

File tree

5 files changed

+124
-173
lines changed

5 files changed

+124
-173
lines changed
Lines changed: 88 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,91 @@
1-
package dev.selenium.interactions;
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.
217

3-
import dev.selenium.BaseTest;
18+
import org.junit.jupiter.api.Test;
19+
import org.openqa.selenium.*;
20+
import org.openqa.selenium.chrome.ChromeDriver;
21+
import org.openqa.selenium.chrome.ChromeOptions;
22+
import org.openqa.selenium.support.ui.ExpectedConditions;
23+
import org.openqa.selenium.support.ui.WebDriverWait;
424

5-
public class AlertsTest extends BaseTest {
25+
import java.time.Duration;
26+
import static org.junit.jupiter.api.Assertions.assertEquals;
627

7-
}
28+
public class AlertsTest {
29+
30+
@Test
31+
public void testForAlerts() throws Exception {
32+
33+
ChromeOptions chromeOptions = new ChromeOptions();
34+
chromeOptions.addArguments("disable-search-engine-choice-screen");
35+
WebDriver driver = new ChromeDriver(chromeOptions);
36+
37+
driver.manage().timeouts().implicitlyWait(Duration.ofMillis(500));
38+
driver.manage().window().maximize();
39+
//Navigate to Url
40+
driver.get("https://www.selenium.dev/documentation/webdriver/interactions/alerts/");
41+
42+
//Simple Alert
43+
//Click the link to activate the alert
44+
JavascriptExecutor js = (JavascriptExecutor) driver;
45+
//execute js for alert
46+
js.executeScript("alert('Sample Alert');");
47+
WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(30));
48+
//Wait for the alert to be displayed and store it in a variable
49+
wait.until(ExpectedConditions.alertIsPresent());
50+
51+
Alert alert=driver.switchTo().alert();
52+
//Store the alert text in a variable and verify it
53+
String text = alert.getText();
54+
assertEquals(text,"Sample Alert");
55+
//Press the OK button
56+
alert.accept();
57+
58+
//Confirm
59+
//execute js for confirm
60+
js.executeScript("confirm('Are you sure?');");
61+
//Wait for the alert to be displayed
62+
wait = new WebDriverWait(driver, Duration.ofSeconds(30));
63+
wait.until(ExpectedConditions.alertIsPresent());
64+
65+
66+
alert = driver.switchTo().alert();
67+
//Store the alert text in a variable and verify it
68+
text = alert.getText();
69+
assertEquals(text,"Are you sure?");
70+
//Press the Cancel button
71+
alert.dismiss();
72+
73+
//Prompt
74+
//execute js for prompt
75+
js.executeScript("prompt('What is your name?');");
76+
//Wait for the alert to be displayed and store it in a variable
77+
wait = new WebDriverWait(driver, Duration.ofSeconds(30));
78+
wait.until(ExpectedConditions.alertIsPresent());
79+
80+
alert = driver.switchTo().alert();
81+
//Store the alert text in a variable and verify it
82+
text = alert.getText();
83+
assertEquals(text,"What is your name?");
84+
//Type your message
85+
alert.sendKeys("Selenium");
86+
//Press the OK button
87+
alert.accept();
88+
//quit the browser
89+
driver.quit();
90+
}
91+
}

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

Lines changed: 9 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -26,19 +26,9 @@ alerts.
2626

2727
{{< tabpane langEqualsHeader=true >}}
2828
{{< badge-examples >}}
29-
{{< tab header="Java" >}}
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-
Alert alert = wait.until(ExpectedConditions.alertIsPresent());
35-
36-
//Store the alert text in a variable
37-
String text = alert.getText();
38-
39-
//Press the OK button
40-
alert.accept();
41-
{{< /tab >}}
29+
{{< tab header="Java" text=true >}}
30+
{{< gh-codeblock path="examples/java/src/test/java/dev/selenium/interactions/AlertsTest.java#L51-L56" >}}
31+
{{< /tab >}}
4232

4333
{{< tab header="Python" text=true >}}
4434
{{< gh-codeblock path="examples/python/tests/interactions/test_alerts.py#L12-L18" >}}
@@ -88,22 +78,9 @@ This example also shows a different approach to storing an alert:
8878

8979
{{< tabpane langEqualsHeader=true >}}
9080
{{< badge-examples >}}
91-
{{< tab header="Java" >}}
92-
//Click the link to activate the alert
93-
driver.findElement(By.linkText("See a sample confirm")).click();
94-
95-
//Wait for the alert to be displayed
96-
wait.until(ExpectedConditions.alertIsPresent());
97-
98-
//Store the alert in a variable
99-
Alert alert = driver.switchTo().alert();
100-
101-
//Store the alert in a variable for reuse
102-
String text = alert.getText();
103-
104-
//Press the Cancel button
105-
alert.dismiss();
106-
{{< /tab >}}
81+
{{< tab header="Java" text=true >}}
82+
{{< gh-codeblock path="examples/java/src/test/java/dev/selenium/interactions/AlertsTest.java#L66-L71" >}}
83+
{{< /tab >}}
10784

10885
{{< tab header="Python" text=true >}}
10986
{{< gh-codeblock path="examples/python/tests/interactions/test_alerts.py#L26-L32" >}}
@@ -161,19 +138,9 @@ See a sample prompt</a>.
161138

162139
{{< tabpane langEqualsHeader=true >}}
163140
{{< badge-examples >}}
164-
{{< tab header="Java" >}}
165-
//Click the link to activate the alert
166-
driver.findElement(By.linkText("See a sample prompt")).click();
167-
168-
//Wait for the alert to be displayed and store it in a variable
169-
Alert alert = wait.until(ExpectedConditions.alertIsPresent());
170-
171-
//Type your message
172-
alert.sendKeys("Selenium");
173-
174-
//Press the OK button
175-
alert.accept();
176-
{{< /tab >}}
141+
{{< tab header="Java" text=true >}}
142+
{{< gh-codeblock path="examples/java/src/test/java/dev/selenium/interactions/AlertsTest.java#L80-L87" >}}
143+
{{< /tab >}}
177144

178145
{{< tab header="Python" text=true >}}
179146
{{< gh-codeblock path="examples/python/tests/interactions/test_alerts.py#L40-L47" >}}

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

Lines changed: 9 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -21,19 +21,9 @@ WebDriverは、JavaScriptが提供する3種類のネイティブポップアッ
2121
WebDriverはポップアップからテキストを取得し、これらのアラートを受け入れるか、または閉じることができます。
2222

2323
{{< tabpane langEqualsHeader=true >}}
24-
{{< tab header="Java" >}}
25-
//Click the link to activate the alert
26-
driver.findElement(By.linkText("See an example alert")).click();
27-
28-
//Wait for the alert to be displayed and store it in a variable
29-
Alert alert = wait.until(ExpectedConditions.alertIsPresent());
30-
31-
//Store the alert text in a variable
32-
String text = alert.getText();
33-
34-
//Press the OK button
35-
alert.accept();
36-
{{< /tab >}}
24+
{{< tab header="Java" text=true >}}
25+
{{< gh-codeblock path="examples/java/src/test/java/dev/selenium/interactions/AlertsTest.java#L51-L56" >}}
26+
{{< /tab >}}
3727

3828
{{< tab header="Python" text=true >}}
3929
{{< gh-codeblock path="examples/python/tests/interactions/test_alerts.py#L12-L18" >}}
@@ -81,22 +71,9 @@ alert.accept()
8171
この例は、アラートを保存する別の方法も示しています。
8272

8373
{{< tabpane langEqualsHeader=true >}}
84-
{{< tab header="Java" >}}
85-
//Click the link to activate the alert
86-
driver.findElement(By.linkText("See a sample confirm")).click();
87-
88-
//Wait for the alert to be displayed
89-
wait.until(ExpectedConditions.alertIsPresent());
90-
91-
//Store the alert in a variable
92-
Alert alert = driver.switchTo().alert();
93-
94-
//Store the alert in a variable for reuse
95-
String text = alert.getText();
96-
97-
//Press the Cancel button
98-
alert.dismiss();
99-
{{< /tab >}}
74+
{{< tab header="Java" text=true >}}
75+
{{< gh-codeblock path="examples/java/src/test/java/dev/selenium/interactions/AlertsTest.java#L66-L71" >}}
76+
{{< /tab >}}
10077

10178
{{< tab header="Python" text=true >}}
10279
{{< gh-codeblock path="examples/python/tests/interactions/test_alerts.py#L26-L32" >}}
@@ -151,19 +128,9 @@ alert.dismiss()
151128
<a onclick="window.prompt('What is your tool of choice?',navigator.appName)">サンプルプロンプトを参照してください</a>。
152129

153130
{{< tabpane langEqualsHeader=true >}}
154-
{{< tab header="Java" >}}
155-
//Click the link to activate the alert
156-
driver.findElement(By.linkText("See a sample prompt")).click();
157-
158-
//Wait for the alert to be displayed and store it in a variable
159-
Alert alert = wait.until(ExpectedConditions.alertIsPresent());
160-
161-
//Type your message
162-
alert.sendKeys("Selenium");
163-
164-
//Press the OK button
165-
alert.accept();
166-
{{< /tab >}}
131+
{{< tab header="Java" text=true >}}
132+
{{< gh-codeblock path="examples/java/src/test/java/dev/selenium/interactions/AlertsTest.java#L80-L87" >}}
133+
{{< /tab >}}
167134

168135
{{< tab header="Python" text=true >}}
169136
{{< gh-codeblock path="examples/python/tests/interactions/test_alerts.py#L40-L47" >}}

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

Lines changed: 9 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -25,20 +25,9 @@ O WebDriver pode obter o texto do pop-up e aceitar ou dispensar esses
2525
alertas.
2626

2727
{{< tabpane langEqualsHeader=true >}}
28-
{{< tab header="Java" >}}
29-
//Click the link to activate the alert
30-
driver.findElement(By.linkText("See an example alert")).click();
31-
32-
//Wait for the alert to be displayed and store it in a variable
33-
Alert alert = wait.until(ExpectedConditions.alertIsPresent());
34-
35-
//Store the alert text in a variable
36-
String text = alert.getText();
37-
38-
//Press the OK button
39-
alert.accept();
40-
{{< /tab >}}
41-
28+
{{< tab header="Java" text=true >}}
29+
{{< gh-codeblock path="examples/java/src/test/java/dev/selenium/interactions/AlertsTest.java#L51-L56" >}}
30+
{{< /tab >}}
4231
{{< tab header="Python" text=true >}}
4332
{{< gh-codeblock path="examples/python/tests/interactions/test_alerts.py#L12-L18" >}}
4433
{{< /tab >}}
@@ -73,22 +62,9 @@ uma amostra de confirmação </a>.
7362
Este exemplo também mostra uma abordagem diferente para armazenar um alerta:
7463

7564
{{< tabpane langEqualsHeader=true >}}
76-
{{< tab header="Java" >}}
77-
//Click the link to activate the alert
78-
driver.findElement(By.linkText("See a sample confirm")).click();
79-
80-
//Wait for the alert to be displayed
81-
wait.until(ExpectedConditions.alertIsPresent());
82-
83-
//Store the alert in a variable
84-
Alert alert = driver.switchTo().alert();
85-
86-
//Store the alert in a variable for reuse
87-
String text = alert.getText();
88-
89-
//Press the Cancel button
90-
alert.dismiss();
91-
{{< /tab >}}
65+
{{< tab header="Java" text=true >}}
66+
{{< gh-codeblock path="examples/java/src/test/java/dev/selenium/interactions/AlertsTest.java#L66-L71" >}}
67+
{{< /tab >}}
9268

9369
{{< tab header="Python" text=true >}}
9470
{{< gh-codeblock path="examples/python/tests/interactions/test_alerts.py#L26-L32" >}}
@@ -145,19 +121,9 @@ Veja um exemplo de prompt </a>.
145121

146122

147123
{{< tabpane langEqualsHeader=true >}}
148-
{{< tab header="Java" >}}
149-
//Click the link to activate the alert
150-
driver.findElement(By.linkText("See a sample prompt")).click();
151-
152-
//Wait for the alert to be displayed and store it in a variable
153-
Alert alert = wait.until(ExpectedConditions.alertIsPresent());
154-
155-
//Type your message
156-
alert.sendKeys("Selenium");
157-
158-
//Press the OK button
159-
alert.accept();
160-
{{< /tab >}}
124+
{{< tab header="Java" text=true >}}
125+
{{< gh-codeblock path="examples/java/src/test/java/dev/selenium/interactions/AlertsTest.java#L80-L87" >}}
126+
{{< /tab >}}
161127

162128
{{< tab header="Python" text=true >}}
163129
{{< gh-codeblock path="examples/python/tests/interactions/test_alerts.py#L40-L47" >}}

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

Lines changed: 9 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -18,19 +18,9 @@ WebDriver提供了一个API, 用于处理JavaScript提供的三种类型的原
1818
WebDriver可以从弹窗获取文本并接受或关闭这些警告.
1919

2020
{{< tabpane langEqualsHeader=true >}}
21-
{{< tab header="Java" >}}
22-
//Click the link to activate the alert
23-
driver.findElement(By.linkText("See an example alert")).click();
24-
25-
//Wait for the alert to be displayed and store it in a variable
26-
Alert alert = wait.until(ExpectedConditions.alertIsPresent());
27-
28-
//Store the alert text in a variable
29-
String text = alert.getText();
30-
31-
//Press the OK button
32-
alert.accept();
33-
{{< /tab >}}
21+
{{< tab header="Java" text=true >}}
22+
{{< gh-codeblock path="examples/java/src/test/java/dev/selenium/interactions/AlertsTest.java#L51-L56" >}}
23+
{{< /tab >}}
3424

3525
{{< tab header="Python" text=true >}}
3626
{{< gh-codeblock path="examples/python/tests/interactions/test_alerts.py#L12-L18" >}}
@@ -77,22 +67,9 @@ alert.accept()
7767
此示例还呈现了警告的另一种实现:
7868

7969
{{< tabpane langEqualsHeader=true >}}
80-
{{< tab header="Java" >}}
81-
//Click the link to activate the alert
82-
driver.findElement(By.linkText("See a sample confirm")).click();
83-
84-
//Wait for the alert to be displayed
85-
wait.until(ExpectedConditions.alertIsPresent());
86-
87-
//Store the alert in a variable
88-
Alert alert = driver.switchTo().alert();
89-
90-
//Store the alert in a variable for reuse
91-
String text = alert.getText();
92-
93-
//Press the Cancel button
94-
alert.dismiss();
95-
{{< /tab >}}
70+
{{< tab header="Java" text=true >}}
71+
{{< gh-codeblock path="examples/java/src/test/java/dev/selenium/interactions/AlertsTest.java#L66-L71" >}}
72+
{{< /tab >}}
9673

9774
{{< tab header="Python" text=true >}}
9875
{{< gh-codeblock path="examples/python/tests/interactions/test_alerts.py#L26-L32" >}}
@@ -146,19 +123,9 @@ alert.dismiss()
146123

147124

148125
{{< tabpane langEqualsHeader=true >}}
149-
{{< tab header="Java" >}}
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-
Alert alert = wait.until(ExpectedConditions.alertIsPresent());
155-
156-
//Type your message
157-
alert.sendKeys("Selenium");
158-
159-
//Press the OK button
160-
alert.accept();
161-
{{< /tab >}}
126+
{{< tab header="Java" text=true >}}
127+
{{< gh-codeblock path="examples/java/src/test/java/dev/selenium/interactions/AlertsTest.java#L80-L87" >}}
128+
{{< /tab >}}
162129

163130
{{< tab header="Python" text=true >}}
164131
{{< gh-codeblock path="examples/python/tests/interactions/test_alerts.py#L40-L47" >}}

0 commit comments

Comments
 (0)