> For the complete documentation index, see [llms.txt](https://tvn.gitbook.io/selenium-java/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://tvn.gitbook.io/selenium-java/examples/javascript-alert.md).

# 5.6. JavaScript Alert

```java
public class JavaScriptAlertTest {
    WebDriver driver;

    @BeforeClass
    void setUp() {
        driver = new ChromeDriver();
        driver.get("https://the-internet.herokuapp.com/javascript_alerts");
    }

    @Test
    void clickForJSAlert(){
        driver.findElement(By.xpath("//button[.='Click for JS Alert']")).click();
        driver.switchTo().alert().accept(); // ~ click on OK button on alert popup

        String result = driver.findElement(By.id("result")).getText();
        Assert.assertEquals(result,"You successfully clicked an alert");
    }

    @Test
    void dismissForJsConfirm(){
        driver.findElement(By.xpath("//button[.='Click for JS Confirm']")).click();
        driver.switchTo().alert().dismiss(); // ~ click on Cancel button on alert popup

        String result = driver.findElement(By.id("result")).getText();
        Assert.assertEquals(result,"You clicked: Cancel");
    }

    @Test
    void acceptForJsConfirm(){
        driver.findElement(By.xpath("//button[.='Click for JS Confirm']")).click();
        driver.switchTo().alert().accept(); // ~ click on Cancel button on alert popup

        String result = driver.findElement(By.id("result")).getText();
        Assert.assertEquals(result,"You clicked: Ok");
    }

    @Test
    void clickForJsPrompt(){
        driver.findElement(By.xpath("//button[.='Click for JS Prompt']")).click();

        driver.switchTo().alert().sendKeys("hello");
        driver.switchTo().alert().accept();

        String result = driver.findElement(By.id("result")).getText();
        Assert.assertEquals(result,"You entered: hello");
    }

    @AfterClass
    void tearDown(){
        driver.quit();
    }
}
```

�
