How to handle a JavaScript pop-up with Java-TestNG?

How to handle a JavaScript pop-up with Java-TestNG?

Hi Ian,

To handle a JavaScript pop-up with Java-TestNG, it depends on which action you want to do with the alert. You can refer to the syntax below it:

//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();