healenium / healenium-web

Self-healing library for Selenium Web-based tests
Apache License 2.0
169 stars 40 forks source link

Error while taking screenshot with WebDriver - java.lang.ClassCastException: class com.sun.proxy.$Proxy25 cannot be cast to class org.openqa.selenium.TakesScreenshot (com.sun.proxy.$Proxy25 and org.openqa.selenium.TakesScreenshot are in unnamed module of loader 'app') #136

Closed khajanizamuddink closed 3 years ago

khajanizamuddink commented 3 years ago

Hi,

Selenium - 3.141.59 Healenium-web - 3.1.4 Healenium-backend - latest

I have integrated with Healenium with very simple testng-java framework, while taking screenshot I am getting below exception

WARNING: Augmenter should be applied to the instances of @Augmentable classes or previously augmented instances only (instance class was: class com.sun.proxy.$Proxy25) java.lang.ClassCastException: class com.sun.proxy.$Proxy25 cannot be cast to class org.openqa.selenium.TakesScreenshot (com.sun.proxy.$Proxy25 and org.openqa.selenium.TakesScreenshot are in unnamed module of loader 'app') at com.framework.tests.Testcases.testcase_1(Testcases.java:25) at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.base/java.lang.reflect.Method.invoke(Method.java:566) at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:134) at org.testng.internal.TestInvoker.invokeMethod(TestInvoker.java:597) at org.testng.internal.TestInvoker.invokeTestMethod(TestInvoker.java:173) at org.testng.internal.MethodRunner.runInSequence(MethodRunner.java:46) at org.testng.internal.TestInvoker$MethodInvocationAgent.invoke(TestInvoker.java:816) at org.testng.internal.TestInvoker.invokeTestMethods(TestInvoker.java:146) at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:146) at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:128) at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128) at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628) at java.base/java.lang.Thread.run(Thread.java:834)

Here is code I used to take screenshot ,

        WebDriver augmentedDriver = new Augmenter().augment(driver);
        byte[] bytes = ((TakesScreenshot) augmentedDriver).getScreenshotAs(OutputType.BYTES);

Please advice if I am doing anything wrong, I see this issue occurs if I integrate it with Healenium.

Alex-Reif commented 3 years ago

Hi @khajanizamuddink ,

Please use the following code to take screenshot:

((TakesScreenshot) driver.getDelegate()).getScreenshotAs(OutputType.BYTES);

dineshmcac commented 2 years ago

@Aliaksei-Ashukha driver.getDelegate() - getDelegate() method is not showing up for driver image Please suggest

wakatuts commented 2 years ago

not sure if you still need help but you need to cast the driver as SelfHealingDriver first before you could use getDelegate(). WebDriver delegatedDriver = ((SelfHealingDriver) augmentedDriver).getDelegate(); ((TakesScreenshot) delegatedDriver).getScreenshotAs(OutputType.BYTES);

amolsawantdev commented 1 year ago

File sourceFile = this.context.getBean(TakesScreenshot.class).getScreenshotAs(OutputType.FILE); for this springboot framework getting error as org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'org.openqa.selenium.TakesScreenshot' available please help on this issue.

ijobs567 commented 10 months ago

Hello , I am facing similar issue WARN | Trying to heal... When User enters reference number "ABCDE12345" # framework.bdd.testcases.UC01Login.user_enters_reference_number(java.lang.String) java.lang.NoSuchMethodError: 'org.jsoup.nodes.Element org.jsoup.nodes.Document.root()' at com.epam.healenium.treecomparing.JsoupHTMLParser.parse(JsoupHTMLParser.java:25) at com.epam.healenium.SelfHealingEngine.parseTree(SelfHealingEngine.java:144) at com.epam.healenium.processor.HealingProcessor.execute(HealingProcessor.java:35) at com.epam.healenium.processor.BaseProcessor.process(BaseProcessor.java:42) at com.epam.healenium.processor.BaseProcessor.process(BaseProcessor.java:50) at com.epam.healenium.processor.BaseProcessor.process(BaseProcessor.java:50) at com.epam.healenium.handlers.proxy.BaseHandler.findElement(BaseHandler.java:63) at com.epam.healenium.handlers.proxy.SelfHealingProxyInvocationHandler.invoke(SelfHealingProxyInvocationHandler.java:39) at jdk.proxy2/jdk.proxy2.$Proxy44.findElement(Unknown Source) at org.openqa.selenium.support.pagefactory.DefaultElementLocator.findElement(DefaultElementLocator.java:68) at org.openqa.selenium.support.pagefactory.internal.LocatingElementHandler.invoke(LocatingElementHandler.java:38) at jdk.proxy2/jdk.proxy2.$Proxy46.click(Unknown Source)

automation.util.SeleniumUtil - This web driver implementation jdk.proxy2.$Proxy44 cannot create screenshots

code I used for screenshot:
public static void takeFullScreenShotForBDD(final SelfHealingDriver driver, final ExtentTest test, String screenshotLog) {
    WebDriver delegatedDriver = driver.getDelegate();

    if (Objects.isNull(driver)) {
        LOG.info("This is NON UI Test Script. Screenshot is not allowed.");
    } else {
        Date d = new Date();
        String fileName = d.toString().replace(" ", "_").replace(":", "_");
        String filePath = fileName + ".png";

        try {

            // Using AShot
            Screenshot screenshot = new AShot().shootingStrategy(ShootingStrategies.viewportPasting(INTEGER_1000))
                    .takeScreenshot(delegatedDriver);

            new File(SCREENSHOTS).mkdir();
            ImageIO.write(screenshot.getImage(), "PNG", new File(SCREENSHOTS + filePath));
            test.info(screenshotLog,
                    MediaEntityBuilder.createScreenCaptureFromPath(SCREENCAPTUREPATH + filePath).build());

        } catch (Exception e) {
            LOG.error(TEST_FAILURE_IOE);
            LOG.error(IO_EXCEPTION + "{}", e);
        }

        try {
            // Using WebDriver's getScreenshotAs
            File scrFile = ((TakesScreenshot) delegatedDriver).getScreenshotAs(OutputType.FILE);
            new File(SCREENSHOTS).mkdir();
            FileUtils.copyFile(scrFile, new File(SCREENSHOTS + filePath));
            test.info(screenshotLog,
                    MediaEntityBuilder.createScreenCaptureFromPath(SCREENCAPTUREPATH + filePath).build());

        } catch (Exception e) {
            LOG.error(TEST_FAILURE_IOE);
            LOG.error(IO_EXCEPTION + "{}", e);
        }
    }
}
ijobs567 commented 9 months ago

Can anyone help me