selenium-webdriverjmeter

How to identify that an image in canvas html element is loaded or not


I need to capture the time taken for displaying an image in Canvas HTML element using selenium after clicking a button/key press. Can you help with an example, Thanks


Solution

  • Your question cannot be comprehensively answered because essential data is missing, here is a generic piece of code which you can either use as is or adapt to your needs:

    import org.openqa.selenium.By
    import org.openqa.selenium.support.ui.ExpectedConditions
    import org.openqa.selenium.support.ui.WebDriverWait
    
    import java.time.Duration
    
    WDS.browser.get("http://example.com")
    
    WebDriverWait wait = new WebDriverWait(WDS.browser, Duration.ofSeconds(30))
    wait.until(ExpectedConditions.presenceOfElementLocated(By.id("yourCanvasElementId")))
    
    long before = System.currentTimeMillis()
    wait.until {
        WDS.browser.executeScript("""
                var canvas = document.getElementById('yourCanvasElementId');
                return canvas.getContext && canvas.getContext('2d').getImageData(0, 0, canvas.width, canvas.height).data.length > 0;
            """)
    }
    
    long after = System.currentTimeMillis()
    long delta = after - before
    
    WDS.log.info("Time taken for image to load in canvas: ${delta} ms")
    

    More information: