Asta Vista
Asta Vista

Reputation: 41

How to move the mouse on canvas to draw, using selenium webdriver

I want to automate drawing on canvas element. I have written a test case & it passes.

But in my code, I have written function to select drawing tool & draw a simple line on canvas. By the end drawing tool is selected but line is not drawn.
Below is the code-

public void DrawLine() {    
wait.until(ExpectedConditions.elementToBeClickable(anotate_draw));    
    action.click(anotate_draw).perform();     
    action.clickAndHold(canvas_page1)
    .moveByOffset(420, 280)    
    .moveByOffset(550,300)  
    .release().build().perform();
}

Upvotes: 4

Views: 7226

Answers (1)

Moser
Moser

Reputation: 287

You probably have answered this yourself. But this may work for you also:

WebElement element = driver.findElement(By.xpath("Your xPath")); // where your canvas element is 

    Actions builder = new Actions(driver);
    Action drawAction = builder.moveToElement(element,50,50)  // start point
                 .click()
                 .moveByOffset(100, 60) // second point
                 .doubleClick()
                 .build();
       drawAction.perform();

Upvotes: 3

Related Questions