我需要使用C#和Selenium WebDriver自动化HTML Canvas测试。手动完成此测试时,通过按住鼠标并将光标移动到图像上来完成。下面是我到目前为止尝试过的代码。这不会在图像上绘制。但是,在运行此测试并停止在图像后,如果我手动将鼠标移动到图像上而不单击,则会在HTML Canvas上绘制。
IWebElement canvasContainer = WebDriver.FindElement(By.ClassName("canvas-container"));
System.Drawing.Point Location = canvasContainer.Location;
System.Drawing.Size Space = canvasContainer.Size;
Actions builder = new Actions(WebDriver);
builder.ClickAndHold();
IAction drawAction = builder.MoveToElement(canvasContainer,
Location.X, Location.Y) //start points x axis and y axis.
.ClickAndHold()
.MoveByOffset(Location.X, Location.Y)
.ClickAndHold()
.MoveByOffset(-Location.Y, -Location.Y)
.ClickAndHold()
.MoveByOffset(-Location.Y, -Location.Y)
.ClickAndHold()
.Build();
drawAction.Perform();
int move1 = Space.Width / 2;
int move2 = Space.Height / 2;
builder.MoveToElement(canvasContainer, move1, move2);
答案 0 :(得分:0)
由于您要将画布的位置添加为偏移,因此坐标可能位于画布之外。尝试使用相对于元素的偏移量:
var canvas = WebDriver.FindElement(By.ClassName("canvas-container"));
var size = canvas.Size;
new Actions(WebDriver)
.MoveToElement(canvas, 2, 2)
.ClickAndHold()
.MoveByOffset(size.Width / 2, size.Height / 2)
.Release()
.Perform();