问题
I have used Selenium Webdriver in the past for automating my Web UI tests but recently I have to write tests for Sharepoint. I got quite far using Selenium but got stuck at a point where I have to perform Drag and Drop. It seems that in my particular case I can't get drag and drop to work with that site. Now I want to look and other options and CodedUI seems to be the next best option to me. I do not want to record the tests, instead I use the Page object model therefore I want to use Coded UI Code first API library. However I am not sure if it supports drag and drop or not. Google search also came empty. Can anyone please confirm if Coded UI Code First API supports drag and drop and will it also work if the browser window is minimized? This is also important because I don't want the mouse to actually move to a location on screen and perform drag and drop because then it is a pain to maintain and run tests with open windows.
回答1:
After looking at their documentation, i can say that their API don't support drag and drop. But you can do drag and drop yourself by codedui native methods, and it works brilliantly.
Find the control which you want to drag by Coded UI Code first API
HtmlControl dragControl = browser.Find<HtmlControl>(new { Id = "DragableControl" });
Then drag the control using codedui native mathods.
Mouse.StartDragging(dragControl);
Mouse.StopDragging(new System.Drawing.Point(100, 100));
// or Mouse.StopDragging(overAnotherControl);
Note: It won't work if browser window is minimized. You cannot run codedui test and work on your machine at the same time. Browser/Application under test should be opened all the time. I suggest that you either run the test in a local VM or remote VM. In that way, you can fire the test in VM, then minimize it and continue with your work on your machine.
回答2:
I Create the Next Method because it Also Took me awhile findout how does it works.
so the mayor point its to know that the DropArea its not exactly the Container Div where all of the other elements exist or will be, instead in my case its where the other element is.
look, in my example i am dragging one cell to the position where the second cell its.
var rows = activeTab.GetElementsFromGrid();
var cellNameFirstRow= rows[0].CellsContent[2];
//name cell from second row.
var dropableArea = rows[1].CellsContent[2];
cellNameFirstRow.DragOn(dropableArea, dropableArea.BoundingRectangle.Location);
public static bool DragOn(this UITestControl argDragableElement, UITestControl argDropArea, Point argDestinationPoint)
{
try
{
// argDropArea.DrawHighlight();
argDropArea.EnsureClickable(argDestinationPoint);
Mouse.StartDragging(argDragableElement, argDragableElement.BoundingRectangle.Location);
Mouse.StopDragging(argDropArea, argDestinationPoint);
WriteLine($"Dragging, {argDragableElement.GetSummaryProperties()}");
return true;
}
catch (UITestControlNotFoundException ex)
{
WriteLine("Could not Drag element ,timeout exceeded ");
AssertClick(false, argDragableElement.Name.ToString(), ex.Message);
}
catch (CustomException ex)
{
WriteLine("Could not Drag element ,timeout exceeded ");
AssertClick(false, argDragableElement.Name.ToString(), ex.Message);
}
catch (Exception ex)
{
WriteLine("Could not Drag element ,timeout exceeded ");
AssertClick(false, argDragableElement.Name.ToString(), ex.Message);
}
return false;
}
来源:https://stackoverflow.com/questions/24816139/does-coded-ui-code-first-api-support-drag-and-drop