Handling a prompt box alert
A prompt box is often used to input a value by the user before entering a page. When a prompt box pops up, the user will have to click either on the OK or the Cancel button to proceed after entering an input value, as shown in the following screenshot:
If the user clicks on the OK button, the box returns the input value. If the user clicks on the Cancel button, the box returns null
.
In this recipe, we will handle a prompt box using the Selenium WebDriver's Alert
class.
How to do it...
We will create a test that handles a prompt box. We will enter text into the prompt box's input field and later verify if the same value is displayed on the page as follows:
@Test public void testPrompt() { //Clicking button will show a Prompt Alert asking user to enter //value/text with OK and Cancel Button WebElement button = driver.findElement(By.id("prompt")); button.click(); try { //Get the Alert Alert alert = driver.switchTo().alert...