Monday, 26 March 2018

How to Handle Dropdown in Selenium webdriver

Hey guys in this post we are going to learn about how to handle simple dropdowns in Selenium Webdriver.For handling dropdowns Selenium already provides Select class that has some predefined method which help is a lot while working with Dropdown.

In the example given below we are using select by index , select by value and select by visible dropdown text. These are three different ways by which you can select dropdown values. plus we can so some more verification on dropdowns by verifiyting selected dropdown fields and we can also fetch all the dropdown data using selenium webdriver.

Try out below code and learn how we can automate dropdown testing for anyweb application using selenium webdriver.

Video Tutorial -



import java.util.List;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.Select;

public class DropDownTest {

 WebDriver driver;
 WebElement month_dropdown;
 
 @Before
 public void setup()
 {
  System.setProperty("webdriver.chrome.driver", "C:\\Softwares\\chromedriver_win32\\chromedriver.exe");
  
      driver=new ChromeDriver();

       driver.manage().window().maximize();

       driver.get("http://www.facebook.com");
       
     month_dropdown=driver.findElement(By.id("month"));
 }
 
 @Test
 public void testSelectByIndex()
 {
  
  Select month=new Select(month_dropdown);
   
   month.selectByIndex(4);
  
 }
 @Test
 public void testSelectByValues()
 {
  
   
   Select month=new Select(month_dropdown);
   
   month.selectByValue("5");
 }
 @Test
 public void testSelectByVisisbleField()
 {
  
   
   Select month=new Select(month_dropdown);
   
   month.selectByVisibleText("Aug");
 }
 @Test
 public void testSelectedOption()
 {
  
   
   Select month=new Select(month_dropdown);
   
   WebElement first_value=month.getFirstSelectedOption();
   
   String value=first_value.getText();
   
   System.out.println("select dropdown value is- "+value);
 }
 @Test
 public void testAllDropDownOptions()
 {
   
  Select month=new Select(month_dropdown);
   
  List<WebElement> dropdown=month.getOptions();
   
   for(int i=0;i<dropdown.size();i++){
   
   String drop_down_values=dropdown.get(i).getText();
   
   System.out.println("dropdown values are "+drop_down_values);
   
   }
   
   
 }
 
 @After
 public void teardown()
 {
  driver.quit();
 }
 
}

Wednesday, 21 March 2018

How to Disable Chrome notifications popup in Selenium WebDriver

Many Times we have faced issues that chrome browser is showing notification pop while executing selenium test cases which causes your test cases to fail. In this post we are going to look at simple code which you can use to hide or disable notification pop-up displayed on chrome browser. 



Video Tutorial -



import java.util.HashMap;
import java.util.Map;

import org.junit.Test;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
 
public class HandlePopupTest {
 
 @Test
 public  void test() throws Exception {
  
                // Create object of HashMap Class
  Map<String, Object> prefs = new HashMap<String, Object>();
              
                // Set the notification setting it will override the default setting
  prefs.put("profile.default_content_setting_values.notifications", 2);
 
                // Create object of ChromeOption class
  ChromeOptions options = new ChromeOptions();
 
                // Set the experimental option
  options.setExperimentalOption("prefs", prefs);
 
                // pass the options object in Chrome driver
  System.setProperty("webdriver.chrome.driver", "C:\\Softwares\\chromedriver_win32\\chromedriver.exe");
  WebDriver driver = new ChromeDriver(options);
  driver.get("https://www.facebook.com/");
  driver.manage().window().maximize();
  
  
  
 }
 
}

Tuesday, 20 March 2018

How to Automate Radio button and Checkbox in Selenium webdriver

In this post we are going to learn about how to test checkboxes and radio buttons using selenium.Before jumping into that
let us see how the html code for checkbox and radio button looks like.
   
HTML For Checkbox
<input type=”checkbox”>
   
HTML For Radio Button
<input type=”radio”>


The main difference between radio button and checkbox is checkbox you can select multiple but for radio button, only one selection is possible.
we can perform click action using selenium click() method but before that it is very important to verify / test some conditions listed below-

  •     isEnabled()-You need to verify whether radio button or checkbox is enabled.
  •     isDisplayed()-You need to verify whether radio button or checkbox is Displayed on UI or not.
  •     isSelected()-You need to verify whether checkbox and radio button is default selected or not.
Video Tutorial - 
 

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;

import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;

 
public class RadioButtonTest {

WebDriver driver;


@Test
 
     public  void test() throws Exception {
 
 System.setProperty("webdriver.chrome.driver", "C:\\Softwares\\chromedriver_win32\\chromedriver.exe");
 
    WebDriver driver=new ChromeDriver();

      driver.manage().window().maximize();

      driver.get("http://www.facebook.com");
        
         WebElement female_radio_button=driver.findElement(By.id("u_0_b"));
 
         boolean status=female_radio_button.isDisplayed();
 
         System.out.println("Female radio button is Displayed >>"+status);
 
          boolean enabled_status=female_radio_button.isEnabled();
 
          System.out.println("Female radio button is Enabled >>"+enabled_status);
 
        boolean selected_status=female_radio_button.isSelected();
 
          System.out.println("Female radio button is Selected >>"+selected_status);
 
          female_radio_button.click();
 
        boolean selected_status_new=female_radio_button.isSelected();
 
          System.out.println("Female radio button is Selected >>"+selected_status_new);
          driver.quit();
 
     }
 

}

You can practice with same code for chekboxes. Do try and share your feedback, suggestions and questions in the comment section below.