All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.openqa.selenium.htmlunit.remote.JsonToHtmlUnitWebElementConverter Maven / Gradle / Ivy

There is a newer version: 4.25.0
Show newest version
package org.openqa.selenium.htmlunit.remote;

import java.lang.reflect.Field;
import java.util.Map;

import org.openqa.selenium.htmlunit.HtmlUnitDriver;
import org.openqa.selenium.interactions.Interaction;
import org.openqa.selenium.remote.Dialect;
import org.openqa.selenium.remote.JsonToWebElementConverter;
import org.openqa.selenium.remote.RemoteWebDriver;

public class JsonToHtmlUnitWebElementConverter extends JsonToWebElementConverter {

    private HtmlUnitDriver driver;

    public JsonToHtmlUnitWebElementConverter(HtmlUnitDriver driver) {
        super((RemoteWebDriver) null);
        this.driver = driver;
    }

    @Override
    public Object apply(Object result) {
        if (result instanceof Map) {
            Map resultAsMap = (Map) result;
            String elementId = String.valueOf(resultAsMap.get(Dialect.W3C.getEncodedElementKey()));
            if (elementId != null) {
                return driver.toWebElement(elementId);
            }
        }
        
        if (result instanceof Interaction) {
            try {
                Field originField = result.getClass().getDeclaredField("origin");
                originField.setAccessible(true);
                Object originObj = originField.get(result);
                Field originObjectField = originObj.getClass().getDeclaredField("originObject");
                originObjectField.setAccessible(true);
                Object resolved = apply(originObjectField.get(originObj));
                originObjectField.set(originObj, resolved);
            } catch (Exception e) {
                // nothing to do here
            }
            return result;
        }
        
        return super.apply(result);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy