Ashot - element screenshot is not taken correcly

373 Views Asked by At

I'm trying to take a screenshot of specific Element using Ashot. unfortunately, I'm not getting the element screenshot. When I sent my code to someone else who ran it the element screenshot was taken correctly. I don't understand why it is not working on my computer.

The code is:

public class ScreenShot
{
    WebDriver driver;

    @BeforeClass
    public void StartSession()
    {
        WebDriverManager.chromedriver().setup();
        driver = new ChromeDriver();
        driver.manage().window().maximize();
        driver.get("https://www.google.com/");
    }

    @Test
    public void VerifyScreenShot()
    {
        TakeScreenShot();
        //CompareScreenShot();
    }

    @Step("take element screen shot")
    public void TakeScreenShot()
    {
        try
        {
            //find the element you want to picture
            WebElement imageElement = driver.findElement(By.id("hplogo"));
            //take the element screenshot
            Screenshot imageScreenShot = new AShot().coordsProvider(new WebDriverCoordsProvider()).takeScreenshot(driver,imageElement);
            //save the element picture in the project folder under img folder
            ImageIO.write(imageScreenShot.getImage(),"png",new File("./img/glogo.png"));
        }
        catch (Exception e)
        {
            System.out.println("Error writing image file: "+ e);
        }
    }

    @AfterClass
    public void EndSession()
    {
        driver.quit();
    }

}
1

There are 1 best solutions below

1
lior On

i found the solution. i was needed to change my computer resolution to 100% and than take the element screenshot. than the element image was as i expected.