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

com.github.siwenyan.si.spy.SpyWebElement Maven / Gradle / Ivy

There is a newer version: 1.25.1.0
Show newest version
package com.github.siwenyan.si.spy;

import com.github.siwenyan.si.model.SiModelCommand;
import org.openqa.selenium.*;
import org.openqa.selenium.interactions.internal.Coordinates;
import org.openqa.selenium.interactions.internal.Locatable;
import org.openqa.selenium.remote.FileDetector;
import org.openqa.selenium.remote.RemoteWebElement;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class SpyWebElement extends RemoteWebElement {

    private static Pattern p = Pattern.compile("By\\.xpath: \\.\\/\\/option\\[normalize-space\\(\\.\\) = \"(.+)\"\\]");

    public final SpyWebDriver driver;
    public final WebElement el;
    public final By by;
    public final SpyWebElement parentEl;

    public SpyWebElement(WebDriver driver, WebElement el, By by, SpyWebElement parentEl) {
        this.driver = (SpyWebDriver) driver;
        this.el = el;
        this.by = by;
        this.parentEl = parentEl;
    }

    @Override
    public  X getScreenshotAs(OutputType target) throws WebDriverException {
        return this.el.getScreenshotAs(target);
    }

    @Override
    public void click() {
        this.el.click();
        SiModelCommand c = new SiModelCommand();
        c.setCommand("click");
        c.setTarget(this.getSiBy());
        driver.getSiSpy().appendCommand(c);
    }

    @Override
    public void submit() {
        this.el.submit();
    }

    @Override
    public void sendKeys(CharSequence... keysToSend) {
        this.el.sendKeys(keysToSend);

        for (CharSequence cs : keysToSend) {
            SiModelCommand c = new SiModelCommand();
            c.setCommand("type");
            c.setValue(cs.toString());
            c.setTarget(getSiBy());
            driver.getSiSpy().appendCommand(c);
        }
    }

    @Override
    public void clear() {
        this.el.clear();
    }

    @Override
    public String getTagName() {
        return this.el.getTagName();
    }

    @Override
    public String getAttribute(String name) {
        return this.el.getAttribute(name);
    }

    @Override
    public boolean isSelected() {
        return this.el.isSelected();
    }

    @Override
    public boolean isEnabled() {
        return this.el.isEnabled();
    }

    @Override
    public String getText() {
        return this.el.getText();
    }

    @Override
    public List findElements(By by) {
        List subEls = this.el.findElements(by);
        List spies = new ArrayList(subEls.size());
        for (WebElement subEl : subEls) {
            spies.add(new SpyWebElement(this.driver, subEl, by, this));
        }
        return spies;
    }

    @Override
    public WebElement findElement(By by) {
        return new SpyWebElement(this.driver, el.findElement(by), by, this);
    }

    @Override
    public boolean isDisplayed() {
        boolean isDisplayed = this.el.isDisplayed();

        if (isDisplayed) {
            SiModelCommand c = new SiModelCommand();
            c.setCommand("waitForElementVisible");
            c.setTarget(getSiBy());
            c.setValue("10000");
            driver.getSiSpy().appendCommand(c);
        }

        return isDisplayed;
    }

    @Override
    public Point getLocation() {
        return this.el.getLocation();
    }

    @Override
    public Dimension getSize() {
        return this.el.getSize();
    }

    @Override
    public Rectangle getRect() {
        return this.el.getRect();
    }

    @Override
    public String getCssValue(String propertyName) {
        return this.el.getCssValue(propertyName);
    }

    @Override
    public Coordinates getCoordinates() {
        return ((Locatable) this.el).getCoordinates();
    }

    @Override
    public void setFileDetector(FileDetector detector) {
        ((RemoteWebElement) this.el).setFileDetector(detector);
    }

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

    private String asOptionLabel(By by) {
        Matcher m = p.matcher(by.toString());
        if (m.matches()) {
            return "label=" + m.group(1);
        }
        return "Unsupported select option: " + by.toString();
    }

    private String getSiBy() {
        String[] siBy = toSiBy(by);
        if (null != parentEl
                && parentEl.by instanceof By.ByXPath
                && by instanceof By.ByXPath
                && siBy[1].startsWith(".")) {
            String appendXpath = siBy[1].substring(1);
            String parentSiBy = parentEl.getSiBy();
            return parentSiBy + appendXpath;
        } else {
            return siBy[0] + "=" + siBy[1];
        }
    }

    public static String[] toSiBy(By by) {
        String s = by.toString();
        String[] siBy = s.split(":", 2);
        switch (siBy[0].trim()) {
            case "By.xpath":
                siBy[0] = "xpath";
                break;
            case "By.id":
                siBy[0] = "id";
                break;
            case "By.cssSelector":
                siBy[0] = "css";
                break;
            case "By.name":
                siBy[0] = "name";
                break;
            default:
                throw new IllegalArgumentException("Unsupported type: " + s);
        }
        siBy[1] = siBy[1].trim();
        return siBy;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy