Selenium WebDriver : Drag And Drop Element By Pixel(X, Y) Offset

We tin toilet use Advanced User Interactions API of selenium webdriver software testing tool to drag the targeted chemical ingredient too driblet It on goal element. I get got already described usage of dragAndDrop method alongside instance In THIS POST. Now supposing, You get got an chemical ingredient which you lot wants to drag too driblet yesteryear 100 pixel get-go
In horizontal management or 100 pixel get-go In Vertical management or both the directions at the same fourth dimension too thence you lot tin toilet use dragAndDropBy method of webdriver's Actions class.

Drag And Drop Element In Horizontal Direction By 100 Pixel
If you lot wants to drag too driblet chemical ingredient yesteryear 100 pixel get-go In horizontal management too thence you lot tin toilet purpose syntax similar bellow.
new Actions(driver).dragAndDropBy(dragElementFrom, 100, 0).build() .perform();

Drag And Drop Element In Vertical Direction By 100 Pixel
To drag too driblet chemical ingredient yesteryear 100 pixel get-go In vertical direction, You tin toilet purpose bellow given syntax In your software seek out case.
new Actions(driver).dragAndDropBy(dragElementFrom, 0, 100).build() .perform();

Drag And Drop Element In Horizontal And Vertical Direction By -100 Pixel
To drag too driblet chemical ingredient yesteryear -100 pixel get-go In horizontal too vertical direction, You tin toilet write your software seek out instance code equally bellow. Here -X get-go represents correct to left management too -Y get-go represents bottom to give management of spider web page.
new Actions(driver).dragAndDropBy(dragElementFrom, -100, -100).build() .perform();

Full practical instance to perform all higher upward 3 operations Is equally bellow.

package Testing_Pack;  import java.util.concurrent.TimeUnit; import org.openqa.selenium.By; import org.openqa.selenium.JavascriptExecutor; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.firefox.FirefoxDriver; import org.openqa.selenium.interactions.Actions; import org.testng.annotations.BeforeTest; import org.testng.annotations.Test;  world shape DragAndDrop {    WebDriver driver = null;    @BeforeTest     world void setup() throws Exception {     driver = novel FirefoxDriver();          driver.manage().window().maximize();          driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);          driver.get(" ");     }     @Test  world void dragAndDrop() throws InterruptedException {   //Locate chemical ingredient which you lot wants to drag.   WebElement dragElementFrom = driver.findElement(By.xpath("//div[@id='dragdiv']"));        //To drag too driblet chemical ingredient yesteryear 100 pixel get-go In horizontal management X.   novel Actions(driver).dragAndDropBy(dragElementFrom, 100, 0).build() .perform();      //To generate warning later horizontal management drag too drop. VIEW EXAMPLE   JavascriptExecutor javascript = (JavascriptExecutor) driver;   javascript.executeScript("alert('Element Is drag too driblet yesteryear 100 pixel get-go In horizontal direction.');");   Thread.sleep(5000);   driver.switchTo().alert().accept();        //To drag too driblet chemical ingredient yesteryear 100 pixel get-go In Vertical management Y.   novel Actions(driver).dragAndDropBy(dragElementFrom, 0, 100).build() .perform();       //To generate warning later Vertical management drag too drop.   javascript.executeScript("alert('Element Is drag too driblet yesteryear 100 pixel get-go In Vertical direction.');");   Thread.sleep(5000);   driver.switchTo().alert().accept();      //To drag too driblet chemical ingredient yesteryear -100 pixel get-go In horizontal too -100 pixel get-go In Vertical direction.   novel Actions(driver).dragAndDropBy(dragElementFrom, -100, -100).build() .perform();     //To generate warning later horizontal too vertical management drag too drop.   javascript.executeScript("alert('Element Is drag too driblet yesteryear -100 pixel get-go In horizontal too -100 pixel get-go In Vertical direction.');");   Thread.sleep(5000);   driver.switchTo().alert().accept();  } }

You tin toilet run It In your eclipse too bring out the X Y management movement of  software spider web element. So this Is approximately other instance of selenium webdriver's advanced user Interactions using Actions class.

Alternatively you lot tin toilet purpose moveByOffset method of selenium WebDriver's Advanced User Interactions API to displace software spider web chemical ingredient yesteryear given pixel offset. Syntax Is equally bellow.
new Actions(driver).clickAndHold(dragElementFrom).moveByOffset(100,100).release().perform();



More interesting articles here :Generation Enggelmundus Internet Marketing Tool here :Zeageat IM http://www.software-testing-tutorials-automation.com/
Post a Comment (0)
Previous Post Next Post