diff --git a/examples/java/src/test/java/dev/selenium/interactions/AlertsTest.java b/examples/java/src/test/java/dev/selenium/interactions/AlertsTest.java index 34835005d867..e95f0cbb9e8c 100644 --- a/examples/java/src/test/java/dev/selenium/interactions/AlertsTest.java +++ b/examples/java/src/test/java/dev/selenium/interactions/AlertsTest.java @@ -1,7 +1,182 @@ package dev.selenium.interactions; import dev.selenium.BaseTest; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.openqa.selenium.*; +import org.openqa.selenium.chrome.ChromeDriver; +import org.openqa.selenium.support.ui.ExpectedConditions; +import org.openqa.selenium.support.ui.WebDriverWait; + +import java.time.Duration; public class AlertsTest extends BaseTest { + private AlertsTest() { + }; + + @BeforeEach + public void createSession() { + driver = new ChromeDriver(); + wait = new WebDriverWait(driver, Duration.ofSeconds(10)); + + } + + @AfterEach + public void endSession() { + driver.quit(); + } + + @Test + public void alertInformationTest() { + driver.get("https://www.selenium.dev/selenium/web/alerts.html#"); + + driver.findElement(By.id("alert")).click(); + //Wait for the alert to be displayed and store it in a variable + wait.until(ExpectedConditions.alertIsPresent()); + Alert alert = driver.switchTo().alert(); + Assertions.assertEquals("cheese", alert.getText()); + alert.accept(); + + } + + @Test + public void alertEmptyInformationTest() { + driver.get("https://www.selenium.dev/selenium/web/alerts.html#"); + driver.findElement(By.id("empty-alert")).click(); + + //Wait for the alert to be displayed and store it in a variable + wait.until(ExpectedConditions.alertIsPresent()); + + Alert alert = driver.switchTo().alert(); + Assertions.assertEquals("", alert.getText()); + alert.accept(); + + } + + @Test + public void promptDisplayAndInputTest() { + driver.get("https://www.selenium.dev/selenium/web/alerts.html#"); + driver.findElement(By.id("prompt")).click(); + + //Wait for the alert to be displayed and store it in a variable + wait.until(ExpectedConditions.alertIsPresent()); + + Alert alert = driver.switchTo().alert(); + Assertions.assertEquals("Enter something", alert.getText()); + + alert.sendKeys("Selenium"); + alert.accept(); + + } + + @Test + public void promptDefaultInputTest() { + driver.get("https://www.selenium.dev/selenium/web/alerts.html#"); + + driver.findElement(By.id("prompt-with-default")).click(); + //Wait for the alert to be displayed and store it in a variable + wait.until(ExpectedConditions.alertIsPresent()); + Alert alert = driver.switchTo().alert(); + Assertions.assertEquals("Enter something", alert.getText()); + alert.accept(); + // Implementation needed to check teh default value is accepted. + + } + + @Test + public void multiplePromptInputsTest() { + driver.get("https://www.selenium.dev/selenium/web/alerts.html#"); + driver.findElement(By.id("double-prompt")).click(); + + //Wait for the alert to be displayed and store it in a variable + wait.until(ExpectedConditions.alertIsPresent()); + + Alert alert1 = driver.switchTo().alert(); + Assertions.assertEquals("First", alert1.getText()); + + alert1.sendKeys("first"); + alert1.accept(); + + + Alert alert2 = driver.switchTo().alert(); + Assertions.assertEquals("Second", alert2.getText()); + alert2.sendKeys("second"); + alert2.accept(); + + } + + @Test + public void slowAlertTest() { + driver.get("https://www.selenium.dev/selenium/web/alerts.html#"); + driver.findElement(By.id("slow-alert")).click(); + + //Wait for the alert to be displayed and store it in a variable + wait.until(ExpectedConditions.alertIsPresent()); + + Alert alert = driver.switchTo().alert(); + Assertions.assertEquals("Slow", alert.getText()); + + alert.accept(); + + } + + + @Test + public void confirmationAlertTest() { + driver.get("https://www.selenium.dev/selenium/web/alerts.html#"); + + driver.findElement(By.id("confirm")).click(); + //Wait for the alert to be displayed and store it in a variable + wait.until(ExpectedConditions.alertIsPresent()); + Alert alert = driver.switchTo().alert(); + Assertions.assertEquals("Are you sure?", alert.getText()); + + alert.accept(); + Assertions.assertTrue(driver.getCurrentUrl().endsWith("simpleTest.html")); + + } + + + @Test + public void iframeAlertTest() { + driver.get("https://www.selenium.dev/selenium/web/alerts.html#"); + WebElement iframe = driver.findElement(By.name("iframeWithAlert")); + driver.switchTo().frame(iframe); + + driver.findElement(By.id("alertInFrame")).click(); + + //Wait for the alert to be displayed and store it in a variable + wait.until(ExpectedConditions.alertIsPresent()); + + Alert alert = driver.switchTo().alert(); + Assertions.assertEquals("framed cheese", alert.getText()); + + alert.accept(); + + } + + @Test + public void nestedIframeAlertTest() { + driver.get("https://www.selenium.dev/selenium/web/alerts.html#"); + WebElement iframe1 = driver.findElement(By.name("iframeWithIframe")); + driver.switchTo().frame(iframe1); + + WebElement iframe2 = driver.findElement(By.name("iframeWithAlert")); + driver.switchTo().frame(iframe2); + + driver.findElement(By.id("alertInFrame")).click(); + + //Wait for the alert to be displayed and store it in a variable + wait.until(ExpectedConditions.alertIsPresent()); + + Alert alert = driver.switchTo().alert(); + Assertions.assertEquals("framed cheese", alert.getText()); + + alert.accept(); + + } + } diff --git a/website_and_docs/content/documentation/webdriver/interactions/alerts.en.md b/website_and_docs/content/documentation/webdriver/interactions/alerts.en.md index e25e08f9e721..c3e9cee12639 100644 --- a/website_and_docs/content/documentation/webdriver/interactions/alerts.en.md +++ b/website_and_docs/content/documentation/webdriver/interactions/alerts.en.md @@ -25,20 +25,9 @@ WebDriver can get the text from the popup and accept or dismiss these alerts. {{< tabpane langEqualsHeader=true >}} -{{< badge-examples >}} - {{< tab header="Java" >}} -//Click the link to activate the alert -driver.findElement(By.linkText("See an example alert")).click(); - -//Wait for the alert to be displayed and store it in a variable -Alert alert = wait.until(ExpectedConditions.alertIsPresent()); - -//Store the alert text in a variable -String text = alert.getText(); - -//Press the OK button -alert.accept(); - {{< /tab >}} +{{< tab header="Java" text=true >}} +{{< gh-codeblock path="examples/java/src/test/java/dev/selenium/interactions/AlertsTest.java#L36-L41" >}} +{{< /tab >}} {{< tab header="Python" text=true >}} {{< gh-codeblock path="examples/python/tests/interactions/test_alerts.py#L12-L18" >}} @@ -88,22 +77,9 @@ This example also shows a different approach to storing an alert: {{< tabpane langEqualsHeader=true >}} {{< badge-examples >}} - {{< tab header="Java" >}} -//Click the link to activate the alert -driver.findElement(By.linkText("See a sample confirm")).click(); - -//Wait for the alert to be displayed -wait.until(ExpectedConditions.alertIsPresent()); - -//Store the alert in a variable -Alert alert = driver.switchTo().alert(); - -//Store the alert in a variable for reuse -String text = alert.getText(); - -//Press the Cancel button -alert.dismiss(); - {{< /tab >}} +{{< tab header="Java" text=true >}} +{{< gh-codeblock path="examples/java/src/test/java/dev/selenium/interactions/AlertsTest.java#L131-L138" >}} +{{< /tab >}} {{< tab header="Python" text=true >}} {{< gh-codeblock path="examples/python/tests/interactions/test_alerts.py#L26-L32" >}} @@ -159,21 +135,11 @@ text. Pressing the cancel button will not submit any text. See a sample prompt. -{{< tabpane langEqualsHeader=true >}} +{{< tabpane langEqualsHeader=true text=true >}} {{< badge-examples >}} - {{< tab header="Java" >}} -//Click the link to activate the alert -driver.findElement(By.linkText("See a sample prompt")).click(); - -//Wait for the alert to be displayed and store it in a variable -Alert alert = wait.until(ExpectedConditions.alertIsPresent()); - -//Type your message -alert.sendKeys("Selenium"); - -//Press the OK button -alert.accept(); - {{< /tab >}} +{{< tab header="Java" text=true >}} +{{< gh-codeblock path="examples/java/src/test/java/dev/selenium/interactions/AlertsTest.java#L79-L84" >}} +{{< /tab >}} {{< tab header="Python" text=true >}} {{< gh-codeblock path="examples/python/tests/interactions/test_alerts.py#L40-L47" >}} diff --git a/website_and_docs/content/documentation/webdriver/interactions/alerts.ja.md b/website_and_docs/content/documentation/webdriver/interactions/alerts.ja.md index b1401ca2ffb3..26a38ce7f364 100644 --- a/website_and_docs/content/documentation/webdriver/interactions/alerts.ja.md +++ b/website_and_docs/content/documentation/webdriver/interactions/alerts.ja.md @@ -21,19 +21,9 @@ WebDriverは、JavaScriptが提供する3種類のネイティブポップアッ WebDriverはポップアップからテキストを取得し、これらのアラートを受け入れるか、または閉じることができます。 {{< tabpane langEqualsHeader=true >}} - {{< tab header="Java" >}} -//Click the link to activate the alert -driver.findElement(By.linkText("See an example alert")).click(); - -//Wait for the alert to be displayed and store it in a variable -Alert alert = wait.until(ExpectedConditions.alertIsPresent()); - -//Store the alert text in a variable -String text = alert.getText(); - -//Press the OK button -alert.accept(); - {{< /tab >}} +{{< tab header="Java" text=true >}} +{{< gh-codeblock path="examples/java/src/test/java/dev/selenium/interactions/AlertsTest.java#L36-L41" >}} +{{< /tab >}} {{< tab header="Python" text=true >}} {{< gh-codeblock path="examples/python/tests/interactions/test_alerts.py#L12-L18" >}} @@ -81,22 +71,10 @@ alert.accept() この例は、アラートを保存する別の方法も示しています。 {{< tabpane langEqualsHeader=true >}} - {{< tab header="Java" >}} -//Click the link to activate the alert -driver.findElement(By.linkText("See a sample confirm")).click(); - -//Wait for the alert to be displayed -wait.until(ExpectedConditions.alertIsPresent()); - -//Store the alert in a variable -Alert alert = driver.switchTo().alert(); - -//Store the alert in a variable for reuse -String text = alert.getText(); - -//Press the Cancel button -alert.dismiss(); - {{< /tab >}} +{{< badge-examples >}} +{{< tab header="Java" text=true >}} +{{< gh-codeblock path="examples/java/src/test/java/dev/selenium/interactions/AlertsTest.java#L131-L138" >}} +{{< /tab >}} {{< tab header="Python" text=true >}} {{< gh-codeblock path="examples/python/tests/interactions/test_alerts.py#L26-L32" >}} @@ -151,19 +129,10 @@ alert.dismiss() サンプルプロンプトを参照してください。 {{< tabpane langEqualsHeader=true >}} - {{< tab header="Java" >}} -//Click the link to activate the alert -driver.findElement(By.linkText("See a sample prompt")).click(); - -//Wait for the alert to be displayed and store it in a variable -Alert alert = wait.until(ExpectedConditions.alertIsPresent()); - -//Type your message -alert.sendKeys("Selenium"); - -//Press the OK button -alert.accept(); - {{< /tab >}} +{{< badge-examples >}} +{{< tab header="Java" text=true >}} +{{< gh-codeblock path="examples/java/src/test/java/dev/selenium/interactions/AlertsTest.java#L79-L84" >}} +{{< /tab >}} {{< tab header="Python" text=true >}} {{< gh-codeblock path="examples/python/tests/interactions/test_alerts.py#L40-L47" >}} diff --git a/website_and_docs/content/documentation/webdriver/interactions/alerts.pt-br.md b/website_and_docs/content/documentation/webdriver/interactions/alerts.pt-br.md index 9c30bda412d0..203c15017805 100644 --- a/website_and_docs/content/documentation/webdriver/interactions/alerts.pt-br.md +++ b/website_and_docs/content/documentation/webdriver/interactions/alerts.pt-br.md @@ -25,19 +25,9 @@ O WebDriver pode obter o texto do pop-up e aceitar ou dispensar esses alertas. {{< tabpane langEqualsHeader=true >}} - {{< tab header="Java" >}} -//Click the link to activate the alert -driver.findElement(By.linkText("See an example alert")).click(); - -//Wait for the alert to be displayed and store it in a variable -Alert alert = wait.until(ExpectedConditions.alertIsPresent()); - -//Store the alert text in a variable -String text = alert.getText(); - -//Press the OK button -alert.accept(); - {{< /tab >}} +{{< tab header="Java" text=true >}} +{{< gh-codeblock path="examples/java/src/test/java/dev/selenium/interactions/AlertsTest.java#L36-L41" >}} +{{< /tab >}} {{< tab header="Python" text=true >}} {{< gh-codeblock path="examples/python/tests/interactions/test_alerts.py#L12-L18" >}} @@ -73,22 +63,10 @@ uma amostra de confirmação . Este exemplo também mostra uma abordagem diferente para armazenar um alerta: {{< tabpane langEqualsHeader=true >}} - {{< tab header="Java" >}} -//Click the link to activate the alert -driver.findElement(By.linkText("See a sample confirm")).click(); - -//Wait for the alert to be displayed -wait.until(ExpectedConditions.alertIsPresent()); - -//Store the alert in a variable -Alert alert = driver.switchTo().alert(); - -//Store the alert in a variable for reuse -String text = alert.getText(); - -//Press the Cancel button -alert.dismiss(); - {{< /tab >}} +{{< badge-examples >}} +{{< tab header="Java" text=true >}} +{{< gh-codeblock path="examples/java/src/test/java/dev/selenium/interactions/AlertsTest.java#L131-L138" >}} +{{< /tab >}} {{< tab header="Python" text=true >}} {{< gh-codeblock path="examples/python/tests/interactions/test_alerts.py#L26-L32" >}} @@ -145,19 +123,10 @@ Veja um exemplo de prompt . {{< tabpane langEqualsHeader=true >}} - {{< tab header="Java" >}} -//Click the link to activate the alert -driver.findElement(By.linkText("See a sample prompt")).click(); - -//Wait for the alert to be displayed and store it in a variable -Alert alert = wait.until(ExpectedConditions.alertIsPresent()); - -//Type your message -alert.sendKeys("Selenium"); - -//Press the OK button -alert.accept(); - {{< /tab >}} +{{< badge-examples >}} +{{< tab header="Java" text=true >}} +{{< gh-codeblock path="examples/java/src/test/java/dev/selenium/interactions/AlertsTest.java#L79-L84" >}} +{{< /tab >}} {{< tab header="Python" text=true >}} {{< gh-codeblock path="examples/python/tests/interactions/test_alerts.py#L40-L47" >}} diff --git a/website_and_docs/content/documentation/webdriver/interactions/alerts.zh-cn.md b/website_and_docs/content/documentation/webdriver/interactions/alerts.zh-cn.md index 3aeafede61d0..022387380159 100644 --- a/website_and_docs/content/documentation/webdriver/interactions/alerts.zh-cn.md +++ b/website_and_docs/content/documentation/webdriver/interactions/alerts.zh-cn.md @@ -18,19 +18,9 @@ WebDriver提供了一个API, 用于处理JavaScript提供的三种类型的原 WebDriver可以从弹窗获取文本并接受或关闭这些警告. {{< tabpane langEqualsHeader=true >}} - {{< tab header="Java" >}} -//Click the link to activate the alert -driver.findElement(By.linkText("See an example alert")).click(); - -//Wait for the alert to be displayed and store it in a variable -Alert alert = wait.until(ExpectedConditions.alertIsPresent()); - -//Store the alert text in a variable -String text = alert.getText(); - -//Press the OK button -alert.accept(); - {{< /tab >}} +{{< tab header="Java" text=true >}} +{{< gh-codeblock path="examples/java/src/test/java/dev/selenium/interactions/AlertsTest.java#L36-L41" >}} +{{< /tab >}} {{< tab header="Python" text=true >}} {{< gh-codeblock path="examples/python/tests/interactions/test_alerts.py#L12-L18" >}} @@ -77,22 +67,10 @@ alert.accept() 此示例还呈现了警告的另一种实现: {{< tabpane langEqualsHeader=true >}} - {{< tab header="Java" >}} -//Click the link to activate the alert -driver.findElement(By.linkText("See a sample confirm")).click(); - -//Wait for the alert to be displayed -wait.until(ExpectedConditions.alertIsPresent()); - -//Store the alert in a variable -Alert alert = driver.switchTo().alert(); - -//Store the alert in a variable for reuse -String text = alert.getText(); - -//Press the Cancel button -alert.dismiss(); - {{< /tab >}} +{{< badge-examples >}} +{{< tab header="Java" text=true >}} +{{< gh-codeblock path="examples/java/src/test/java/dev/selenium/interactions/AlertsTest.java#L131-L138" >}} +{{< /tab >}} {{< tab header="Python" text=true >}} {{< gh-codeblock path="examples/python/tests/interactions/test_alerts.py#L26-L32" >}} @@ -146,19 +124,10 @@ alert.dismiss() {{< tabpane langEqualsHeader=true >}} - {{< tab header="Java" >}} -//Click the link to activate the alert -driver.findElement(By.linkText("See a sample prompt")).click(); - -//Wait for the alert to be displayed and store it in a variable -Alert alert = wait.until(ExpectedConditions.alertIsPresent()); - -//Type your message -alert.sendKeys("Selenium"); - -//Press the OK button -alert.accept(); - {{< /tab >}} +{{< badge-examples >}} +{{< tab header="Java" text=true >}} +{{< gh-codeblock path="examples/java/src/test/java/dev/selenium/interactions/AlertsTest.java#L79-L84" >}} +{{< /tab >}} {{< tab header="Python" text=true >}} {{< gh-codeblock path="examples/python/tests/interactions/test_alerts.py#L40-L47" >}}