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

com.seleniumtests.uipage.htmlelements.SeleniumElement Maven / Gradle / Ivy

There is a newer version: 5.1.13
Show newest version
package com.seleniumtests.uipage.htmlelements;

import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.Select;

import java.lang.reflect.Field;

/**
 * Class that holds a Selenium element (select or WebElement) to ease reporting
 */
public class SeleniumElement extends Element {

    private WebElement webElement;
    private Select select;

    public SeleniumElement(WebElement webElement) {
        super(null);
        this.webElement = webElement;
    }

    public SeleniumElement(Select select) {
        super(null);
        this.select = select;
    }

    @Override
    public String getName() {
        String name = super.getName();
        if (name != null) {
            return name;
        } else if (select != null) {
            try {
                Field elementField = Select.class.getDeclaredField("element");
                elementField.setAccessible(true);
                WebElement element = (WebElement) elementField.get(select);
                return "Select " + getElementLocator(element);
            } catch (NoSuchFieldException | IllegalAccessException e) {
                throw new RuntimeException(e);
            }
        } else if (webElement != null) {
            return getElementLocator(webElement);
        }
        return null;
    }

    private String getElementLocator(WebElement el) {

        if (el.toString().contains("->")) {
            try {
                return el.toString().split("->")[1].replace("]", "").replace("}", "").trim();
            } catch (IndexOutOfBoundsException e) {
                // we should never go here
            }
        }
        return  el.toString();
    }

    @Override
    public String toString() {
        return webElement.toString();
    }

    @Override
    protected void findElement(boolean waitForVisibility) {

    }

    @Override
    public void click() {

    }

    @Override
    public void sendKeys(CharSequence... text) {

    }

    @Override
    public boolean isElementPresent() {
        return false;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy