Skip to content

Instantly share code, notes, and snippets.

@SarahElson
Created March 20, 2024 11:10
Show Gist options
  • Select an option

  • Save SarahElson/bb979915cb18e1549736a95ae77ba247 to your computer and use it in GitHub Desktop.

Select an option

Save SarahElson/bb979915cb18e1549736a95ae77ba247 to your computer and use it in GitHub Desktop.
How to Handle Modal Dialog Box in Selenium WebDriver Java
package test;
import java.net.*;
import java.time.Duration;
import java.util.HashMap;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.openqa.selenium.chrome.ChromeOptions;
import org.testng.annotations.*;
public class BaseTest
{
public RemoteWebDriver driver = null;
public WebDriverWait wait;
String username = System.getenv("LT_USERNAME") == null ? "<lambdatest_username>" : System.getenv("LT_USERNAME");
String accessKey = System.getenv("LT_ACCESS_KEY") == null ? "<lambdatest_accesskey>" : System.getenv("LT_ACCESS");
@BeforeTest
public void setup() {
try {
ChromeOptions chromeOptions = new ChromeOptions();
chromeOptions.setPlatformName("Windows 10");
chromeOptions.setBrowserVersion("122.0");
HashMap<String, Object> ltOptions = new HashMap<String, Object>();
ltOptions.put("build", "Handling Dialog Box Selenium Java");
ltOptions.put("name", "Handling Dialog Box Selenium Java");
ltOptions.put("w3c", true);
chromeOptions.setCapability("LT:Options", ltOptions);
driver = new RemoteWebDriver(
new URL("https://" + username + ":" + accessKey + "@hub.lambdatest.com/wd/hub"), chromeOptions);
//to set default explicit wait duration as 20 seconds
wait = new WebDriverWait(driver, Duration.ofSeconds(20));
} catch (MalformedURLException e) {
e.printStackTrace();
}
}
@AfterTest
public void closeDriver() {
driver.quit();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment