Anda di halaman 1dari 7

SUDHEER REDDY ANKIREDDYGARI

SELENIUM WEBDRIVER

Drag and Drop using Web driver Action Class


We have taken example program to perform drag and drop. In the below example, as the
DragAndDrop divs are in a Frame, First we need to switch to the frame before performing drag
and drop. And then we also need to check for the availability
of SourceElement and DestinationElements.

Syntax for drag and drop


Actions action = new Actions(driver);
action.dragAndDrop(Sourcelocator, Destinationlocator).build().perform();
We can also make it as below:
(new Actions(driver)).dragAndDrop(element, target).perform();
We have also used Webdriver Wait Expected conditions to wait for a frame to be available and
then switch to the frame.
The below is the source image on which we will perform operation :

Page 1 of 7

SUDHEER REDDY ANKIREDDYGARI


SELENIUM WEBDRIVER

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;

public class DragNDropExample {

Page 2 of 7

SUDHEER REDDY ANKIREDDYGARI


SELENIUM WEBDRIVER
WebDriver driver;

@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

SUDHEER REDDY ANKIREDDYGARI


SELENIUM WEBDRIVER
System.out.println("Error occurred while performing drag and drop
operation "+ e.getStackTrace());
}
}

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

#1) get() Methods


WebDriver
command

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

SUDHEER REDDY ANKIREDDYGARI


SELENIUM WEBDRIVER

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

SUDHEER REDDY ANKIREDDYGARI


SELENIUM WEBDRIVER

Defining Firefox Profile preferences using Selenium Webdriver


Some times, you may need to update the preferences within Firefox. We can do this by
instantiating a Firefox Profile object and then update the settings.
We will then need to pass this object into FirefoxDriver which will load the profile with your
defined settings.
You can check them using command "about:config" in the borwser. It will display a warning
message and then asks you to proceed if required. Becarefull when doing any changes.
Let us assume that you wanted to have google as the startup page for Firefox. To do this we will
need to update the browser.startup.homepage preference.
Follow below steps to add a website as your homepage:
1. Let's first start by creating the FirefoxProfile object:
FirefoxProfile ffprofile= new FirefoxProfile();
2. Now we will set the preference by using the below method:
profile.setPreference("browser.startup.homepage","http://www.google.com");
3. To get the profile to be used, we need to pass it in to the driver. To do this, we need
to do the following:
driver = new FirefoxDriver(ffprofile);
4. Now run your test. The final code should look like the following:
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxProfile;
import org.testng.annotations.Test;

public class FireFoxProfileExample {

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

SUDHEER REDDY ANKIREDDYGARI


SELENIUM WEBDRIVER

WebElement element = driver.findElement(By.name("q"));


element.sendKeys("100");

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.

Creating fire fox profile with mobile user agent,


FirefoxProfile profile=new FirefoxProfile(path);
profile.setPreference("general.useragent.override", "Mozilla/5.0 (iPhone; U;
CPU iPhone OS 3_0 like Mac OS X; en-us) AppleWebKit/528.18 (KHTML, like Gecko)
Version/4.0 Mobile/7A341 Safari/528.16");
WebDriver driver=new FirefoxDriver(profile);
driver.get("https://www.google.com/");
Handling window dimension,
Dimension d=new Dimension(420,600);
Thread.sleep(3000);
driver.manage().window().setSize(d);
Thread.sleep(3000);

Page 7 of 7

Anda mungkin juga menyukai