Sunday, September 20, 2015

Drag and Drop using Selenium Webdriver


A tester need to drag an object and drop it somewhere on the UI and through automation it is also achievable with the help of Action class. Let’s try it.

Step 1: Create a new project and add a new item “Basic Unit Test”.

Step2:  Add the following code into it.

using OpenQA.Selenium.Interactions;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using OpenQA.Selenium;
using OpenQA.Selenium.Firefox;

namespace WordPressAutomation.RegressionTests
{
    [TestClass]
    public class DragAndDrops
    {
        [TestMethod]
        public void Drag_and_drop()
        {
            FirefoxDriver driver = new FirefoxDriver();
            driver.Navigate().GoToUrl("http://jqueryui.com/droppable/#default&rdquo");
            driver.Manage().Window.Maximize();

            Actions act = new Actions(driver);

            // Script for dragging an element and dropping it in another place
            IWebElement iFrame = driver.FindElement(By.TagName("iframe"));
            driver.SwitchTo().Frame(iFrame);
            IWebElement From = driver.FindElement(By.Id("draggable"));
            IWebElement To = driver.FindElement(By.Id("droppable"));
            act.DragAndDrop(From, To).Build().Perform();
           
            driver.Close();
        }
    }
}






Step 3: Execute it and it is done.

No comments:

Post a Comment