Drag and Drop Using Web Driver Action Class: Selenium Webdriver
Drag and Drop Using Web Driver Action Class: Selenium Webdriver
Drag and Drop Using Web Driver Action Class: Selenium Webdriver
SELENIUM WEBDRIVER
Page 1 of 7
package com.pack.dragndrop;
import org.openqa.selenium.By;
import org.openqa.selenium.NoSuchElementException;
import org.openqa.selenium.StaleElementReferenceException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.Assert;
import org.testng.annotations.Test;
Page 2 of 7
@Test
public void testDragAndDropExample() {
driver = new FirefoxDriver();
driver.manage().window().maximize();
driver.navigate().to("http://jqueryui.com/droppable/");
//Wait for the frame to be available and switch to it
WebDriverWait wait = new WebDriverWait(driver, 5);
wait.until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.cssSelector(".demoframe")));
WebElement Sourcelocator = driver.findElement(By.cssSelector(".ui-draggable"));
WebElement Destinationlocator = driver.findElement(By.cssSelector(".uidroppable"));
dragAndDrop(Sourcelocator,Destinationlocator);
String actualText=driver.findElement(By.cssSelector("#droppable>p")).getText();
Assert.assertEquals(actualText, "Dropped!");
}
}
We can make use of any of the two DragAndDrop methods. The first method handles exceptions
'StaleElementReferenceException ', 'NoSuchElementException ' and Exception if any unknown
exception occurs.
public void dragAndDrop(WebElement sourceElement, WebElement destinationElement) {
try {
if (sourceElement.isDisplayed() && destinationElement.isDisplayed()) {
Actions action = new Actions(driver);
action.dragAndDrop(sourceElement,
destinationElement).build().perform();
} else {
System.out.println("Element was not displayed to drag");
}
} catch (StaleElementReferenceException e) {
System.out.println("Element with " + sourceElement + "or" +
destinationElement + "is not attached to the page document "
+ e.getStackTrace());
} catch (NoSuchElementException e) {
System.out.println("Element " + sourceElement + "or" +
destinationElement + " was not found in DOM "+ e.getStackTrace());
} catch (Exception e) {
Page 3 of 7
The below is the simple method to perform drag and drop. But before performing we also need
to check if both the elements SourceElement and DestinationElements are available.
public void dragAndDrop(WebElement sourceElement, WebElement destinationElement)
{
(new Actions(driver)).dragAndDrop(sourceElement, destinationElement).perform();
}
}
get()
getClass()
getCurrentUrl()
Usage
The command launches a new browser and opens
the specified URL in the browser instance
The command takes a single string type parameter that is usually a URL
of application under test
To the Selenium IDE users, the command may look very much like open
command
driver.get("https://google.com");
The command is used to retrieve the Class object
that represents the runtime class of this object
driver.getClass();
The command is used to retrieve the URL of the webpage the user is
currently accessing
The command doesnt require any parameter and returns a string value
Page 4 of 7
WebDriver
command
getPageSource()
getTitle()
getText()
getAttribute()
Usage
driver.getCurrentUrl();
The command is used to retrieve the page source
of the webpage the user is currently accessing
The command doesnt require any parameter and returns a string value
The command can be used with various string operations like contains()
to ascertain the
presence of the specified string value
boolean result = driver.getPageSource().contains("String to find");
The command is used to retrieve the title of the webpage the user is
currently working on.
A null string is returned if the webpage has no title
The command doesnt require any parameter and returns a trimmed
string value
String title = driver.getTitle();
The command is used to retrieve the inner text
of the specified web element
The command doesnt require any parameter and returns a string value
It is also one of the extensively used commands for verification of
messages, labels, errors etc displayed
on the web pages.
String Text = driver.findElement(By.id("Text")).getText();
The command is used to retrieve the value of the specified attribute
The command requires a single string parameter that refers to an
attribute whose value we aspire to know and returns a string value as a
result.
driver.findElement(By.id("findID")).
getAttribute("value");
The command is used to tackle with the situation when we have more
than one window to deal with.
The command helps us switch to the newly opened window and
performs actions on the new window.
getWindowHandle() The user can also switch back to the previous window if he/she desires.
private String winHandleBefore;
winHandleBefore = driver.getWindowHandle();
driver.switchTo().window(winHandleBefore);
The command is similar to that of getWindowHandle() with the subtle
getWindowHandles() difference that it helps to deal with multiple windows i.e. when we have
to deal with more than 2 windows.
Page 5 of 7
WebDriver driver;
@Test
public void testExamples(){
FirefoxProfile profile = new FirefoxProfile();
profile.setPreference("browser.startup.homepage",
"http://www.google.com");
driver = new FirefoxDriver(profile);
Page 6 of 7
You can check if that has applied to the firefox or not. Please enter "about:config" in address bar,
and type "browser.startup.homepage".
It will display the website that you have added.
The below screen shot shows "www.google.com" which is added by using the code.
Page 7 of 7