How to handle conformation prompt dialog box using selenium webdriver ?

package SeleniumLearn.Com;
import org.testng.annotations.Test;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.BeforeTest;

public class ConformationAlertCanel {
    public WebDriver driver;

@Test
     public void ConformationAlertBox()throws Exception {
     driver.get("http://seleniumlearn.com/confirmation-dialog-box");
     driver.manage().timeouts().implicitlyWait(9, TimeUnit.SECONDS);
     driver.findElement(By.xpath("//*[@id='node-102']/div/div[1]/div/div/button")).click();
     Thread.sleep(5000);
     //driver.switchTo().alert().accept();//    accept means :click on "OK" option button
     driver.switchTo().alert().dismiss();  // dismiss means : click on "Cancel" option button
     }

@BeforeTest
     public void beforeTest() {
     driver= new FirefoxDriver();
     driver.manage().window().maximize();
     }
}

Tags: