Drag and drop using TestNG framework

package package_name;

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.interactions.Action;
import org.openqa.selenium.interactions.Actions;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
import org.testng.annotations.AfterTest;

public class class_name {
WebDriver driver;
@BeforeTest
public void setup() throws InterruptedException{
System.setProperty(“webdriver.chrome.driver”, path of driver”/chromedriver.exe”);
driver = new ChromeDriver();
driver.manage().window().maximize();
driver.get(“url”);
Thread.sleep(1000);
}
@Test
public void dragdrop() throws InterruptedException{
driver.findElement(By.id(“login_email”)).sendKeys(“Mail ID/Username”);
driver.findElement(By.id(“login_password”)).sendKeys(“Password”);
driver.findElement(By.xpath(“Submit button”)).click();
Thread.sleep(1000);
driver.findElement(By.xpath(“//img[@alt=’image alt tag’]”)).click();
WebElement dragElement=driver.findElement(By.xpath(“drag element”));
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
WebElement dropElement=driver.findElement(By.xpath(“drop element”));
Actions builder = new Actions(driver);
Action dragAndDrop = builder.clickAndHold(dragElement)
.moveToElement(dropElement)
.release(dropElement)
.build();
dragAndDrop.perform();
System.out.println(“Drag and drop successful”);
}
@Test
public void preview() throws InterruptedException {
driver.findElement(By.xpath(“opening preview”)).click();
Thread.sleep(6000);
System.out.println(“Preview sucessfull”);
driver.findElement(By.xpath(“Closing preview”)).click();
}
@AfterTest
public void teardown() throws InterruptedException{
System.out.println(“Closing the driver”);
Thread.sleep(2000);
driver.close();
}
}

Leave a comment