|
18 | 18 | package dev.selenium.interactions;
|
19 | 19 |
|
20 | 20 | import dev.selenium.BaseTest;
|
| 21 | +import org.junit.jupiter.api.AfterEach; |
| 22 | +import org.junit.jupiter.api.Assertions; |
| 23 | +import org.junit.jupiter.api.BeforeEach; |
21 | 24 | import org.junit.jupiter.api.Test;
|
22 | 25 | import org.openqa.selenium.Alert;
|
| 26 | +import org.openqa.selenium.By; |
23 | 27 | import org.openqa.selenium.JavascriptExecutor;
|
24 | 28 | import org.openqa.selenium.WebDriver;
|
| 29 | +import org.openqa.selenium.WebElement; |
25 | 30 | import org.openqa.selenium.chrome.ChromeDriver;
|
26 | 31 | import org.openqa.selenium.chrome.ChromeOptions;
|
27 | 32 | import org.openqa.selenium.support.ui.ExpectedConditions;
|
|
33 | 38 |
|
34 | 39 | public class AlertsTest extends BaseTest {
|
35 | 40 |
|
| 41 | + @BeforeEach |
| 42 | + public void createSession() { |
| 43 | + driver = new ChromeDriver(); |
| 44 | + wait = new WebDriverWait(driver, Duration.ofSeconds(10)); |
| 45 | + } |
| 46 | + |
| 47 | + @AfterEach |
| 48 | + public void endSession() { |
| 49 | + driver.quit(); |
| 50 | + } |
| 51 | + |
| 52 | + @Test |
| 53 | + public void alertInformationTest() { |
| 54 | + driver.get("https://www.selenium.dev/selenium/web/alerts.html#"); |
| 55 | + |
| 56 | + driver.findElement(By.id("alert")).click(); |
| 57 | + |
| 58 | + wait.until(ExpectedConditions.alertIsPresent()); |
| 59 | + Alert alert = driver.switchTo().alert(); |
| 60 | + Assertions.assertEquals("cheese", alert.getText()); |
| 61 | + alert.accept(); |
| 62 | + |
| 63 | + } |
| 64 | + |
| 65 | + @Test |
| 66 | + public void alertEmptyInformationTest() { |
| 67 | + driver.get("https://www.selenium.dev/selenium/web/alerts.html#"); |
| 68 | + driver.findElement(By.id("empty-alert")).click(); |
| 69 | + |
| 70 | + |
| 71 | + wait.until(ExpectedConditions.alertIsPresent()); |
| 72 | + |
| 73 | + Alert alert = driver.switchTo().alert(); |
| 74 | + Assertions.assertEquals("", alert.getText()); |
| 75 | + alert.accept(); |
| 76 | + |
| 77 | + } |
| 78 | + |
| 79 | + @Test |
| 80 | + public void promptDisplayAndInputTest() { |
| 81 | + driver.get("https://www.selenium.dev/selenium/web/alerts.html#"); |
| 82 | + driver.findElement(By.id("prompt")).click(); |
| 83 | + |
| 84 | + //Wait for the alert to be displayed and store it in a variable |
| 85 | + wait.until(ExpectedConditions.alertIsPresent()); |
| 86 | + |
| 87 | + Alert alert = driver.switchTo().alert(); |
| 88 | + Assertions.assertEquals("Enter something", alert.getText()); |
| 89 | + |
| 90 | + alert.sendKeys("Selenium"); |
| 91 | + alert.accept(); |
| 92 | + |
| 93 | + } |
| 94 | + |
| 95 | + @Test |
| 96 | + public void promptDefaultInputTest() { |
| 97 | + driver.get("https://www.selenium.dev/selenium/web/alerts.html#"); |
| 98 | + |
| 99 | + driver.findElement(By.id("prompt-with-default")).click(); |
| 100 | + |
| 101 | + wait.until(ExpectedConditions.alertIsPresent()); |
| 102 | + Alert alert = driver.switchTo().alert(); |
| 103 | + Assertions.assertEquals("Enter something", alert.getText()); |
| 104 | + alert.accept(); |
| 105 | + } |
| 106 | + |
| 107 | + @Test |
| 108 | + public void multiplePromptInputsTest() { |
| 109 | + driver.get("https://www.selenium.dev/selenium/web/alerts.html#"); |
| 110 | + driver.findElement(By.id("double-prompt")).click(); |
| 111 | + |
| 112 | + wait.until(ExpectedConditions.alertIsPresent()); |
| 113 | + |
| 114 | + Alert alert1 = driver.switchTo().alert(); |
| 115 | + Assertions.assertEquals("First", alert1.getText()); |
| 116 | + |
| 117 | + alert1.sendKeys("first"); |
| 118 | + alert1.accept(); |
| 119 | + |
| 120 | + |
| 121 | + Alert alert2 = driver.switchTo().alert(); |
| 122 | + Assertions.assertEquals("Second", alert2.getText()); |
| 123 | + alert2.sendKeys("second"); |
| 124 | + alert2.accept(); |
| 125 | + |
| 126 | + } |
| 127 | + |
| 128 | + @Test |
| 129 | + public void slowAlertTest() { |
| 130 | + driver.get("https://www.selenium.dev/selenium/web/alerts.html#"); |
| 131 | + driver.findElement(By.id("slow-alert")).click(); |
| 132 | + |
| 133 | + wait.until(ExpectedConditions.alertIsPresent()); |
| 134 | + |
| 135 | + Alert alert = driver.switchTo().alert(); |
| 136 | + Assertions.assertEquals("Slow", alert.getText()); |
| 137 | + |
| 138 | + alert.accept(); |
| 139 | + |
| 140 | + } |
| 141 | + |
| 142 | + |
| 143 | + @Test |
| 144 | + public void confirmationAlertTest() { |
| 145 | + driver.get("https://www.selenium.dev/selenium/web/alerts.html#"); |
| 146 | + |
| 147 | + driver.findElement(By.id("confirm")).click(); |
| 148 | + |
| 149 | + wait.until(ExpectedConditions.alertIsPresent()); |
| 150 | + Alert alert = driver.switchTo().alert(); |
| 151 | + Assertions.assertEquals("Are you sure?", alert.getText()); |
| 152 | + |
| 153 | + alert.accept(); |
| 154 | + Assertions.assertTrue(driver.getCurrentUrl().endsWith("simpleTest.html")); |
| 155 | + |
| 156 | + } |
| 157 | + |
| 158 | + |
| 159 | + @Test |
| 160 | + public void iframeAlertTest() { |
| 161 | + driver.get("https://www.selenium.dev/selenium/web/alerts.html#"); |
| 162 | + WebElement iframe = driver.findElement(By.name("iframeWithAlert")); |
| 163 | + driver.switchTo().frame(iframe); |
| 164 | + |
| 165 | + driver.findElement(By.id("alertInFrame")).click(); |
| 166 | + |
| 167 | + |
| 168 | + wait.until(ExpectedConditions.alertIsPresent()); |
| 169 | + |
| 170 | + Alert alert = driver.switchTo().alert(); |
| 171 | + Assertions.assertEquals("framed cheese", alert.getText()); |
| 172 | + |
| 173 | + alert.accept(); |
| 174 | + |
| 175 | + } |
| 176 | + |
| 177 | + @Test |
| 178 | + public void nestedIframeAlertTest() { |
| 179 | + driver.get("https://www.selenium.dev/selenium/web/alerts.html#"); |
| 180 | + WebElement iframe1 = driver.findElement(By.name("iframeWithIframe")); |
| 181 | + driver.switchTo().frame(iframe1); |
| 182 | + |
| 183 | + WebElement iframe2 = driver.findElement(By.name("iframeWithAlert")); |
| 184 | + driver.switchTo().frame(iframe2); |
| 185 | + |
| 186 | + driver.findElement(By.id("alertInFrame")).click(); |
| 187 | + |
| 188 | + |
| 189 | + wait.until(ExpectedConditions.alertIsPresent()); |
| 190 | + |
| 191 | + Alert alert = driver.switchTo().alert(); |
| 192 | + Assertions.assertEquals("framed cheese", alert.getText()); |
| 193 | + |
| 194 | + alert.accept(); |
| 195 | + |
| 196 | + } |
| 197 | + |
36 | 198 | @Test
|
37 | 199 | public void testForAlerts() {
|
38 | 200 |
|
|
0 commit comments