23
23
import org .openqa .selenium .support .ui .WebDriverWait ;
24
24
25
25
import java .time .Duration ;
26
+
26
27
import static org .junit .jupiter .api .Assertions .assertEquals ;
27
28
28
29
public class AlertsTest {
29
30
30
31
@ Test
31
32
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 ();
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 ();
90
91
}
91
92
}
0 commit comments