zebrunner / carina

Carina automation framework (TestNG): Web, Mobile, API, DB etc testing...
https://zebrunner.github.io/carina
Apache License 2.0
954 stars 218 forks source link

Stack overflow when printing missing element #2282

Open Pawel-12 opened 3 months ago

Pawel-12 commented 3 months ago

Describe the bug

If inside class that inherits from ExtendedWebElement (AbstractUIObject) there is method that uses isElementPresent() or isPresent() or isVisible() on ExtendedWebElement that isn`t present on page and there is overridden toString() that uses that method it causes infinite loop and stack overflow.

Example:

package com.solvd;
​
import com.zebrunner.carina.webdriver.decorator.ExtendedWebElement;
import com.zebrunner.carina.webdriver.gui.AbstractPage;
import com.zebrunner.carina.webdriver.gui.AbstractUIObject;
import org.openqa.selenium.SearchContext;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.FindBy;
​
import java.time.Duration;
​
public class Main {
    public static class Component extends AbstractUIObject {
        @FindBy(className = "imerror")
        private ExtendedWebElement notExistingElement;
​
        public Component(WebDriver driver, SearchContext searchContext) {
            super(driver, searchContext);
        }
​
        public String getNotExistingElement() {
            System.out.println(notExistingElement);
            if (notExistingElement.isElementPresent(Duration.ofMillis(100))) // causes infinite loop
            {/*code*/}
​
            return "missing";
        }
​
        @Override
        public String toString() {
            return getNotExistingElement();
        }
    }
​
    public static class Page extends AbstractPage {
        @FindBy(id = "error?")
        private Component component;
​
        public Page(WebDriver driver) {
            super(driver);
            setPageAbsoluteURL("https://www.google.com");
        }
​
        public String getComponent() {
            return component.toString();
        }
    }
​
    public static void main(String[] args) {
    WebDriver driver = new ChromeDriver();
​
    Page page = new Page(driver);
    page.open();
    System.out.println(page.getComponent());
    }
}

Logs/Links

Exception in thread "main" java.lang.StackOverflowError
    at java.base/java.lang.String.indexOf(String.java:2447)
    at java.base/java.net.URLStreamHandler.parseURL(URLStreamHandler.java:157)
    at java.base/sun.net.www.protocol.file.Handler.parseURL(Handler.java:56)
    at java.base/java.net.URL.<init>(URL.java:801)
    at java.base/java.net.URL.<init>(URL.java:654)
    at java.base/jdk.internal.loader.URLClassPath$FileLoader.getResource(URLClassPath.java:1053)
    at java.base/jdk.internal.loader.URLClassPath.getResource(URLClassPath.java:316)
    at java.base/jdk.internal.loader.BuiltinClassLoader.findClassOnClassPathOrNull(BuiltinClassLoader.java:757)
    at java.base/jdk.internal.loader.BuiltinClassLoader.loadClassOrNull(BuiltinClassLoader.java:681)
    at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:639)
    at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:188)
    at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:525)
    at org.openqa.selenium.support.ui.FluentWait.propagateIfNotIgnored(FluentWait.java:248)
    at org.openqa.selenium.support.ui.FluentWait.until(FluentWait.java:215)
    at com.zebrunner.carina.webdriver.helper.IWaitHelper.waitUntil(IWaitHelper.java:71)
    at com.zebrunner.carina.webdriver.helper.IWaitHelper.waitUntil(IWaitHelper.java:37)
    at com.zebrunner.carina.webdriver.decorator.ExtendedWebElement.isElementPresent(ExtendedWebElement.java:337)
    (...)

Web (please complete the following information):