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