Highlight elements in WebDriver during runtime

后端 未结 10 1187
离开以前
离开以前 2020-12-02 15:54

Can someone please help!

How can I highlight all web elements in following class during test execution in WebDriver? With Selenium RC, it was quite straight forward

10条回答
  •  独厮守ぢ
    2020-12-02 16:43

    I am using ExtentReport as a report tool and I am adding images in the failed test steps as a base64 format, which allows me to display them right after the text in the output report. When I am taking screenshots and want to highlight element(s) in order to be more visible in the screenshot I am using the following method to highlight -> screenshot -> un-highlight.

    private void highlightElement(WebElement elemToHighlight) {
        if (_driver instanceof JavascriptExecutor) {
            ((JavascriptExecutor) _driver).executeScript("arguments[0].style.border='3px solid red'", elem);
        }
    }
    
    
    public String addScreenshotToHTML(WebElement... elmsToHighlight) {
        String result;
        List initStyle = new ArrayList<>();
        if (elmsToHighlight.length > 0) {
            for (WebElement elem : elmsToHighlight) {
                initStyle.add(elem.getCssValue("border"));
                highlightElement(elem);
            }
        }
        String screenshot = ((TakesScreenshot) _driver).getScreenshotAs(OutputType.BASE64);
        // NOTE: add the  tag content between the  tags with the Base64 image
        // 
        result = "";
        if (elmsToHighlight.length > 0) {
            for (WebElement elem : elmsToHighlight) {
                jsExec().executeScript("arguments[0].style.border='" + initStyle.get(0) + "'", elem);
                initStyle.remove(0);
            }
        }
        return result;
    }
    

    Hope that helps

提交回复
热议问题