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..f47955e674bb 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,91 @@ -package dev.selenium.interactions; +// Licensed to the Software Freedom Conservancy (SFC) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The SFC licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. -import dev.selenium.BaseTest; +import org.junit.jupiter.api.Test; +import org.openqa.selenium.*; +import org.openqa.selenium.chrome.ChromeDriver; +import org.openqa.selenium.chrome.ChromeOptions; +import org.openqa.selenium.support.ui.ExpectedConditions; +import org.openqa.selenium.support.ui.WebDriverWait; -public class AlertsTest extends BaseTest { +import java.time.Duration; +import static org.junit.jupiter.api.Assertions.assertEquals; -} +public class AlertsTest { + + @Test + public void testForAlerts() throws Exception { + + ChromeOptions chromeOptions = new ChromeOptions(); + chromeOptions.addArguments("disable-search-engine-choice-screen"); + WebDriver driver = new ChromeDriver(chromeOptions); + + driver.manage().timeouts().implicitlyWait(Duration.ofMillis(500)); + driver.manage().window().maximize(); + //Navigate to Url + driver.get("https://www.selenium.dev/documentation/webdriver/interactions/alerts/"); + + //Simple Alert + //Click the link to activate the alert + JavascriptExecutor js = (JavascriptExecutor) driver; + //execute js for alert + js.executeScript("alert('Sample Alert');"); + WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(30)); + //Wait for the alert to be displayed and store it in a variable + wait.until(ExpectedConditions.alertIsPresent()); + + Alert alert=driver.switchTo().alert(); + //Store the alert text in a variable and verify it + String text = alert.getText(); + assertEquals(text,"Sample Alert"); + //Press the OK button + alert.accept(); + + //Confirm + //execute js for confirm + js.executeScript("confirm('Are you sure?');"); + //Wait for the alert to be displayed + wait = new WebDriverWait(driver, Duration.ofSeconds(30)); + wait.until(ExpectedConditions.alertIsPresent()); + + + alert = driver.switchTo().alert(); + //Store the alert text in a variable and verify it + text = alert.getText(); + assertEquals(text,"Are you sure?"); + //Press the Cancel button + alert.dismiss(); + + //Prompt + //execute js for prompt + js.executeScript("prompt('What is your name?');"); + //Wait for the alert to be displayed and store it in a variable + wait = new WebDriverWait(driver, Duration.ofSeconds(30)); + wait.until(ExpectedConditions.alertIsPresent()); + + alert = driver.switchTo().alert(); + //Store the alert text in a variable and verify it + text = alert.getText(); + assertEquals(text,"What is your name?"); + //Type your message + alert.sendKeys("Selenium"); + //Press the OK button + alert.accept(); + //quit the browser + driver.quit(); + } +} \ No newline at end of file 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..961f2143639a 100644 --- a/website_and_docs/content/documentation/webdriver/interactions/alerts.en.md +++ b/website_and_docs/content/documentation/webdriver/interactions/alerts.en.md @@ -26,19 +26,9 @@ 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#L51-L56" >}} +{{< /tab >}} {{< tab header="Python" text=true >}} {{< 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: {{< 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#L66-L71" >}} +{{< /tab >}} {{< tab header="Python" text=true >}} {{< gh-codeblock path="examples/python/tests/interactions/test_alerts.py#L26-L32" >}} @@ -161,19 +138,9 @@ See a sample prompt. {{< tabpane langEqualsHeader=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#L80-L87" >}} +{{< /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..519f9142dc5d 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#L51-L56" >}} +{{< /tab >}} {{< tab header="Python" text=true >}} {{< gh-codeblock path="examples/python/tests/interactions/test_alerts.py#L12-L18" >}} @@ -81,22 +71,9 @@ 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 >}} + {{< tab header="Java" text=true >}} +{{< gh-codeblock path="examples/java/src/test/java/dev/selenium/interactions/AlertsTest.java#L66-L71" >}} +{{< /tab >}} {{< tab header="Python" text=true >}} {{< gh-codeblock path="examples/python/tests/interactions/test_alerts.py#L26-L32" >}} @@ -151,19 +128,9 @@ 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 >}} + {{< tab header="Java" text=true >}} +{{< gh-codeblock path="examples/java/src/test/java/dev/selenium/interactions/AlertsTest.java#L80-L87" >}} +{{< /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..9b17d798786a 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,20 +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#L51-L56" >}} +{{< /tab >}} {{< tab header="Python" text=true >}} {{< gh-codeblock path="examples/python/tests/interactions/test_alerts.py#L12-L18" >}} {{< /tab >}} @@ -73,22 +62,9 @@ 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 >}} + {{< tab header="Java" text=true >}} +{{< gh-codeblock path="examples/java/src/test/java/dev/selenium/interactions/AlertsTest.java#L66-L71" >}} +{{< /tab >}} {{< tab header="Python" text=true >}} {{< gh-codeblock path="examples/python/tests/interactions/test_alerts.py#L26-L32" >}} @@ -145,19 +121,9 @@ 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 >}} + {{< tab header="Java" text=true >}} +{{< gh-codeblock path="examples/java/src/test/java/dev/selenium/interactions/AlertsTest.java#L80-L87" >}} +{{< /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..2ebcf0ced87e 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#L51-L56" >}} +{{< /tab >}} {{< tab header="Python" text=true >}} {{< gh-codeblock path="examples/python/tests/interactions/test_alerts.py#L12-L18" >}} @@ -77,22 +67,9 @@ 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 >}} + {{< tab header="Java" text=true >}} +{{< gh-codeblock path="examples/java/src/test/java/dev/selenium/interactions/AlertsTest.java#L66-L71" >}} +{{< /tab >}} {{< tab header="Python" text=true >}} {{< gh-codeblock path="examples/python/tests/interactions/test_alerts.py#L26-L32" >}} @@ -146,19 +123,9 @@ 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 >}} + {{< tab header="Java" text=true >}} +{{< gh-codeblock path="examples/java/src/test/java/dev/selenium/interactions/AlertsTest.java#L80-L87" >}} +{{< /tab >}} {{< tab header="Python" text=true >}} {{< gh-codeblock path="examples/python/tests/interactions/test_alerts.py#L40-L47" >}}