Wednesday, 25 April 2018

How to Perform Drag and Drop using Selenium Web Driver

We have already seen Actions class in Selenium and learnt basics about it. If you want Read Here for more details.Basically action class is used to perform various mouse and keyboard driven actions such as click and hold, mouse over etc. In this blog post we are going to use Actions class in Selenium to perform Drag and Drop actions. 

Many websites offers Drag and Drop and hence we will see how we can automate it.

Video Tutorial -


For Demo purpose I am using sample Drag and Drop -

https://jqueryui.com/droppable/

Lets see the simple Test case which performs drag and drop -


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.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.interactions.Actions;

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

       driver.manage().window().maximize();
  
        // start the application
  
       driver.get("https://jqueryui.com/droppable/");
  
 }
 
 @Test
 public void testBootStrap() throws Exception
 {
 
  // Add 10 seconds wait
  Thread.sleep(10000);
   
  // Create object of actions class
  Actions act=new Actions(driver);
   
  driver.switchTo().frame(driver.findElement(By.xpath("//iframe[@class='demo-frame']")));
  // find element which we need to drag
  WebElement drag=driver.findElement(By.xpath("//*[@id='draggable']"));
   
  // find element which we need to drop
  WebElement drop=driver.findElement(By.xpath("//*[@id='droppable']"));
   
  // this will drag element to destination
  act.dragAndDrop(drag, drop).build().perform();
  
  // Add 2 seconds wait
  Thread.sleep(2000);
    }
 
@After
public void tearDown()
{ 
 driver.quit();
}


}

How to Handle Bootstrap Dropdown in Selenium WebDriver

In our previous post we have already seen How to Handle Dropdowns in Selenium WebDriver . In this post we will see how to handle Bootsrap Dropdown.
Bootstrap dropdowns and interactive dropdowns which are dynamically positioned and formed using list of <ul> and <li> html tags.

Below is the simple example of Bootstrap Dropdown-

https://www.w3schools.com/bootstrap/bootstrap_dropdowns.asp

Video Tutorial - 
 

Let us see sample selenium code -

package test;

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.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;

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

       driver.manage().window().maximize();
  
        // start the application
  
       driver.get("https://www.w3schools.com/bootstrap/bootstrap_dropdowns.asp");
  
 }
 
 @Test
 public void testBootStrap() throws Exception
 {
        // First we have to click on menu item then only dropdown items will display
  
       driver.findElement(By.xpath(".//*[@id='menu1']")).click();
  
  
        // adding 2 seconds wait to avoid Sync issue
  
        Thread.sleep(2000);
  
  
  
        // Dropdown items are coming in <a> tag so below xpath will get all
  
        // elements and findElements will return list of WebElements
  
        List<WebElement> list = driver.findElementsByXPath("//ul[@class='dropdown-menu test']//li/a");
  
  
  
        // We are using enhanced for loop to get the elements
  
        for (WebElement ele : list)
  
        {
  
           // for every elements it will print the name using innerHTML
  
           System.out.println("Values " + ele.getAttribute("innerHTML"));
  
  
  
           // Here we will verify if link (item) is equal to Java Script
  
           if (ele.getAttribute("innerHTML").contains("JavaScript")) {
  
              // if yes then click on link (iteam)
  
              ele.click();
  
  
  
              // break the loop or come out of loop
  
              break;
  
           }
  
        }
  
        // here you can write rest piece of code
  
    }
 
@After
public void tearDown()
{ 
 driver.quit();
}

}

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();
 }
 
}