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

com.codacy.scoobydoo.web.WebElementWrapper Maven / Gradle / Ivy

There is a newer version: 3.28.3
Show newest version
package com.codacy.scoobydoo.web;

import com.codacy.scoobydoo.Constant;
import com.codacy.scoobydoo.LoggingHelper;
import com.codacy.scoobydoo.configuration.Configuration;
import com.codacy.scoobydoo.utils.WebUtils;
import org.openqa.selenium.By;
import org.openqa.selenium.InvalidArgumentException;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.matchesRegex;
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertFalse;
import static org.testng.Assert.assertTrue;

public class WebElementWrapper {

    private final boolean isHighlightEnabled;
    private final Configuration config;
    private final RemoteWebDriver driver;
    private final WebElement element;
    private final By locator;
    private final ScreenShotHelper screenShotHelper;
    private final WebUtils webUtils;

    public WebElementWrapper(RemoteWebDriver driver, Configuration configuration, By locator) {
        config = configuration;
        this.driver = driver;
        element = driver.findElement(locator);
        isHighlightEnabled = isHighlightEnabled();
        this.locator = locator;
        screenShotHelper = new ScreenShotHelper(driver, configuration);
        webUtils = new WebUtils(driver, configuration);

        turnOnHighlight();
    }

    public WebElementWrapper assertHasExactText(String expectedText) {
        assertIsDisplayed();
        assertEquals(element.getText(), expectedText);
        return this;
    }

    public WebElementWrapper assertIsDisplayed() {
        assertTrue(isDisplayed());
        return this;
    }

    public WebElementWrapper assertIsNotDisplayed() {
        assertFalse(isDisplayed());
        return this;
    }

    public WebElementWrapper assertTextContainsString(String targetString) {
        assertIsDisplayed();
        assertThat(element.getText(), containsString(targetString));
        return this;
    }

    public WebElementWrapper assertTextMatchesRegEx(String regex) {
        assertIsDisplayed();
        assertThat(element.getText(), matchesRegex(regex));
        return this;
    }

    public WebElementWrapper clear() {
        try {
            screenShotHelper.takesScreenshotOnAction("Clear element [" + locator.toString() + "].");
            element.clear();
        } catch (Exception e) {
            screenShotHelper.takesScreenshotOnError("Failed to clear element [" + locator.toString() + "].", e);
            throw e;
        } finally {
            turnOffHighlight();
        }
        return this;
    }

    public WebElementWrapper clearAndSendKeys(CharSequence... keys) {
        return this
                .clear()
                .sendKeys(keys);
    }

    public WebElementWrapper click() {
        try {
            screenShotHelper.takesScreenshotOnAction("Click on element [" + locator.toString() + "].");
            element.click();
        } catch (Exception ignored) {
            try {
                waitToBeClickable();
                element.click();
            } catch (Exception e) {
                screenShotHelper.takesScreenshotOnError("Failed to click on element [" + locator.toString() + "].", e);
                throw e;
            }
        } finally {
            turnOffHighlight();
        }

        return this;
    }

    @Deprecated
    public void deleteElement() {
        delete();
    }

    public WebElementWrapper delete() {
        try {
            turnOffHighlight();
            screenShotHelper.takesScreenshotOnAction("Delete element [" + locator.toString() + "] with javascript executor.");
            scriptExecutor(driver, element, "arguments[0].parentNode.removeChild(arguments[0])");
        } catch (Exception e) {
            screenShotHelper.takesScreenshotOnError("Failed to delete element [" + locator.toString() + "].", e);
            throw e;
        }
        return this;
    }

    // Use this only when default method clear() doesn't work.
    public WebElementWrapper forceClear() {
        element.sendKeys(Keys.chord(Keys.CONTROL, "a", Keys.DELETE));
        return this;
    }

    public String getAttribute(String attribute) {
        try {
            screenShotHelper.takesScreenshotOnAction("Get attribute [" + attribute + "] from element [" + locator.toString() + "].");
            return element.getAttribute(attribute);
        } catch (Exception e) {
            screenShotHelper.takesScreenshotOnError("Failed to get attribute [" + attribute + "] from element [" + locator.toString() + "].", e);
            throw e;
        } finally {
            turnOffHighlight();
        }
    }

    @Deprecated
    public WebElement getElement() {
        return getWebElement();
    }

    public WebElement getWebElement() {
        return element;
    }

    public String getText() {
        try {
            screenShotHelper.takesScreenshotOnAction("Get text from element [" + locator.toString() + "]." );
            return element.getText();
        } catch (Exception e) {
            screenShotHelper.takesScreenshotOnError("Failed to get text from element [" + locator.toString() + "].", e);
            throw e;
        } finally {
            turnOffHighlight();
        }
    }

    public boolean isDisplayed() {
        try {
            screenShotHelper.takesScreenshotOnAction("Check if element [" + locator.toString() + "] is displayed.");
            return element.isDisplayed();
        } catch (Exception e) {
            screenShotHelper.takesScreenshotOnError("Failed to check if element [" + locator.toString() + "] is displayed.", e);
            throw e;
        } finally {
            turnOffHighlight();
        }
    }

    public void jsClick() {
        try {
            screenShotHelper.takesScreenshotOnAction("Click on element [" + locator.toString() + "] with javascript executor.");
            scriptExecutor(driver, element, "arguments[0].click()");
        } catch (Exception e) {
            screenShotHelper.takesScreenshotOnError("Failed to click on element [" + locator.toString() + "].", e);
            throw e;
        } finally {
            turnOffHighlight();
        }
    }

    public WebElementWrapper sendKeys(CharSequence... key) {
        try {
            screenShotHelper.takesScreenshotOnAction("Send keys to element [" + locator.toString() + "].");
            element.sendKeys(key);
        } catch (Exception e) {
            screenShotHelper.takesScreenshotOnError("Failed to send keys to element [" + locator.toString() + "].", e);
            throw e;
        } finally {
            turnOffHighlight();
        }
        return this;
    }



    private boolean isHighlightEnabled() {

        final String valueName = "highlightElements";
        String highlightElements = config.getProperty(valueName);

        if(highlightElements == null || highlightElements.isBlank()) {
            return false;
        } else {
            switch (highlightElements) {
                case Constant.Status.ENABLED -> { return true; }
                case Constant.Status.DISABLED -> { return false; }
                default -> {
                    final String errorMessage = "Invalid value [" + highlightElements + "] for parameter [" + valueName
                            + "]. It should be [" + Constant.Status.ENABLED + "] or [" + Constant.Status.DISABLED + "].";
                    InvalidArgumentException exception = new InvalidArgumentException(errorMessage);
                    LoggingHelper.error(errorMessage, exception);
                    throw exception;
                }
            }
        }
    }

    private void scriptExecutor(WebDriver driver, WebElement element, String script) {
        ((JavascriptExecutor) driver).executeScript(script, element);
    }

    private void turnOffHighlight() {
        if(isHighlightEnabled) {
            try {
                scriptExecutor(driver, element, "arguments[0].style.border='none'");
            } catch (Exception ignored) {
            }
        }
    }

    private void turnOnHighlight() {
        if(isHighlightEnabled) {
            scriptExecutor(driver, element, "arguments[0].style.border='3px solid red'");
        }
    }

    private void waitToBeClickable() {
        webUtils.waitForCondition(
                config.getTimeout().FIND_ELEMENT_WAIT_TIMEOUT,
                ExpectedConditions.elementToBeClickable(this.getWebElement()));
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy