Alert Message with selenium

selenium

To check javascript alert message we can use the following sample code –

package com.webdriver_05;

import org.openqa.selenium.Alert;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

public class MyAlert {
	public WebDriver driver;
	private String baseUrl;
	
	public static void main(String[] args){
		MyAlert al = new MyAlert();
		al.launch_browser();
	}
	public void launch_browser(){
		try{
			baseUrl = "http://demo.tanmaysarkar.com/alert/";		
			driver = new FirefoxDriver();
			driver.get(baseUrl);		
			boolean isFlagePresent = false;
			Alert alert = driver.switchTo().alert();
			isFlagePresent = true;
			System.out.println(alert.getText());
			alert.accept();
			System.out.println(isFlagePresent); 
			driver.quit();
		}
		catch (Exception ex)
		{
			ex.getMessage();
		}
	}
}

Related posts:

Leave a Reply

Your email address will not be published. Required fields are marked *