Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Moved alert/prompts and confirmations examples of Java tab to example sections and updated the … #1900

Open
wants to merge 3 commits into
base: trunk
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
175 changes: 175 additions & 0 deletions examples/java/src/test/java/dev/selenium/interactions/AlertsTest.java
Original file line number Diff line number Diff line change
@@ -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();

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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" >}}
Expand Down Expand Up @@ -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" >}}
Expand Down Expand Up @@ -159,21 +135,11 @@ text. Pressing the cancel button will not submit any text.
See a sample prompt</a>.


{{< 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" >}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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" >}}
Expand Down Expand Up @@ -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" >}}
Expand Down Expand Up @@ -151,19 +129,10 @@ alert.dismiss()
<a onclick="window.prompt('What is your tool of choice?',navigator.appName)">サンプルプロンプトを参照してください</a>。

{{< 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" >}}
Expand Down
Loading
Loading