Friday, April 21, 2017

How to get text of an alert message in Selenium Webdriver: Mini-blogs

An alert message appears at times to notify the users of the alert as shown below. Using selenium webdriver, we can capture the text in the alert message using selenium web driver.


Using below two lines of code, we can get the alert message.


Alert alert = driver.switchTo().alert();
alert.getText();

Saturday, April 8, 2017

How to identify and close window alert using selenium

At times in browser,Alert box with a specified message and an OK button is displayed to ensure information comes through to the user. 

Alert box prevents the user from accessing other parts of the page until the box is closed. An alert is displayed is as shown below:

an example of alert window

While automating the web application, we can handle the pop up using alert Interface
The alert() method displays an alert box with a specified message and an OK button.

Below code shows how to close an window alert pop up using selenium:



public void Accept_Alert_Message() {
    try {
  //Using webdriver wait, we will wait for the alert
  //to appear before performing action on the alert
        WebDriverWait driverwait = new WebDriverWait(driver, 5);
        driverwait.until(ExpectedConditions.alertIsPresent());
  //switch to the alert element
        Alert alert = driver.switchTo().alert();
  //accept the alert displayed
        alert.accept();
  //switch to the default content
  driver.switchTo().defaultContent();
    } catch (Exception e) {
     // in case alert is not displayed, the code will go to catch loop
    }
}

Tuesday, March 14, 2017

How to select values in dropdown using select in Selenium


  •  How to define an element of type dropdown or tag as select: We can define a element of type dropdown using the statement as shown below:             



Select selectElement = new Select(driver.findElement(By.id("SelectApp")));

  • Example of Select tag in html page: Below example shows how the values are defined in a select dropdown.


<select id="SelectApp">
<option value="Facebook">FB</option>
<option value="TasteBook">TB</option>
<option value="ClassBook">CB</option>
<option value="HomeBook">HB</option>
<option value="TestBook">TTB</option>
</select>


  • SelectByValue would select the option from the dropdown for option: <option value="Facebook">FB</option> if provided. selectByValue: Selects value based on argument value for the tag.

 selectElement.selectByValue("Facebook");  

  • selectByVisibleText: Select value based on the text of the tag


selectElement.selectByVisibleText("FB");        

  • selectByIndex: select the fifth dropdown using selectByIndex



 selectByIndex.selectByIndex(4);