Selenium RC drag and drop:
作者: Max.Bai
时间: 2014/01
(1) dragAndDrop - Locates the target element and drags the element by x pixels horizontally and y pixels vertically.
In the IDE this should look like;
Command - dragAndDrop
Target - [ locator of the target element ]
Value - [ (x-pixels),(y-pixels) ]
- where x-pixels/y-pixels can be negative (left/up respectively) or positive (right/down respectively)
string dragLocator = @"//a[@id=abc]";
string dropLocator = @"//td[text()='drop to here']";
decimal dropX = selenium.GetElementPositionLeft(dropLocator);
decimal dropY = selenium.GetElementPositionTop(dropLocator);
decimal endX = selenium.GetElementWidth(dropLocator);
decimal endY = selenium.GetElementHeight(dropLocator);
endX = Math.Round(dropX + (endX / 2));
endY = Math.Round(dropY + (endY / 2));
String movementsString = "" + endX + "," + endY;
selenium.DragAndDrop(dragLocator, movementsString);
(2) dragAndDropToObject - Locates the target element and drags the element to the centre pixel location of the destination element
In the IDE this should look like;
Command - dragAndDropToObject Target - [ locator of the target element ] Value - [ locator of the destination element you want to drop it on top of ]
string dragLocator = @"//a[@id=abc]";
string dropLocator = @"//td[text()='drop to here']";
Selenium.DragAndDropToObject(dragLocator, dropLocator);
(3) Use MouseDownAt, MouseMoveAt, MouseUpAt to resolve drag and drop.
string dragLocator = @"//a[@id=abc]";
string dropLocator = @"//td[text()='drop to here']";
selenium.MouseMove(dragLocator);
Thread.Sleep(3000);
selenium.MouseDownAt(dragLocator, "0,0");
Thread.Sleep(3000);
selenium.MouseMoveAt(dropLocator, "0,0");
Thread.Sleep(3000);
selenium.MouseUpAt(dropLocator, "0,0");
Thread.Sleep(3000);