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

br.com.jarch.test.TestUtils Maven / Gradle / Ivy

There is a newer version: 24.9.0
Show newest version
package br.com.jarch.test;

import br.com.jarch.util.FileUtils;
import br.com.jarch.util.LogUtils;
import br.com.jarch.util.SleepUtils;
import org.openqa.selenium.*;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.FluentWait;

import java.io.File;
import java.io.IOException;
import java.time.Duration;
import java.util.List;

public final class TestUtils {

    private TestUtils() {
    }

    public static void login(WebDriver driver, String url, String login, String senha) {
        driver.get(url);

        closeLoginExpired(driver, url);

        fillLogin(driver, login);
        fillPassword(driver, senha);
        clickButtonEntrar(driver);

        waitForMessageProcess(driver, 30);
    }

    private static void clickButtonEntrar(WebDriver driver) {
        By byEntrar = By.cssSelector("a[data-test = 'entrar']");
        driver.findElement(byEntrar).click();
    }

    private static void fillPassword(WebDriver driver, String senha) {
        By byPassword = By.cssSelector("input[data-test = 'password']");
        driver.findElement(byPassword).clear();
        driver.findElement(byPassword).sendKeys(senha);
    }

    private static void fillLogin(WebDriver driver, String login) {
        By byLogin = By.cssSelector("input[data-test = 'login']");
        driver.findElement(byLogin).clear();
        driver.findElement(byLogin).sendKeys(login);
    }

    private static void closeLoginExpired(WebDriver driver, String url) {
        try {
            List elements = driver.findElements(By.cssSelector(".swal-button--confirm"));

            if (elements.isEmpty())
                return;

            var element = elements.get(0);

            new FluentWait<>(driver)
                    .withTimeout(Duration.ofSeconds(1))
                    .pollingEvery(Duration.ofSeconds(1))
                    .ignoring(Exception.class)
                    .until(d -> ExpectedConditions.visibilityOf(element));

            new FluentWait<>(driver)
                    .withTimeout(Duration.ofSeconds(1))
                    .pollingEvery(Duration.ofSeconds(1))
                    .ignoring(Exception.class)
                    .until(d -> element.isDisplayed());

            element.click();

            waitForAjaxProcess(driver, 3);

            driver.get(url);
        } catch (Exception ex) {
            LogUtils.generate(ex);
        }
    }

    public static void logout(WebDriver driver) {
        SleepUtils.pauseSeconds(1);

        new FluentWait<>(driver)
                .withTimeout(Duration.ofSeconds(30))
                .pollingEvery(Duration.ofSeconds(1))
                .until(d -> d.findElement(By.cssSelector("#navbartop > ul > li > .botaoSairMenu")) != null
                        && d.findElement(By.cssSelector("#navbartop > ul > li > .botaoSairMenu")).isEnabled());

        driver.findElement(By.cssSelector("#navbartop > ul > li > .botaoSairMenu")).click();
        SleepUtils.pauseSeconds(1);
        driver.findElement(By.cssSelector(".swal-button--confirm")).click();
    }

    public static void scrollTo(WebDriver driver, WebElement webElement) {
        ((JavascriptExecutor) driver).executeScript("arguments[0].scrollIntoView(); var pos = window.scrollY; if (pos) { window.scrollTo(0, pos - 90); }", webElement);
    }

    public static void scrollToButtonsDivDataMaster(WebDriver driver, WebElement webElement) {
        ((JavascriptExecutor) driver).executeScript("arguments[0].scrollIntoView(); var pos = window.scrollY; if (pos) { window.scrollTo(0, pos); }", webElement);
    }

    public static void waitForMessageProcess(WebDriver driver) {
        waitForMessageProcess(driver, 30);
    }

    public static void waitForMessageProcess(WebDriver driver, int timeSecondsWaitMessage) {
        By by = By.cssSelector("div.swal-overlay--show-modal");

        new FluentWait<>(driver)
                .withTimeout(Duration.ofSeconds(timeSecondsWaitMessage))
                .pollingEvery(Duration.ofSeconds(1))
                .ignoring(Exception.class)
                .until(d -> d.findElements(by).isEmpty());
    }

    public static void waitForAjaxProcess(WebDriver driver) {
        waitForAjaxProcess(driver, 30);
    }

    public static void waitForAjaxProcess(WebDriver driver, int timeSecondsWaitMessage) {
        By by = By.cssSelector(".jarch__ajaxstatus > div");

        new FluentWait<>(driver)
                .withTimeout(Duration.ofSeconds(timeSecondsWaitMessage))
                .pollingEvery(Duration.ofSeconds(1))
                .ignoring(Exception.class)
                .until(d -> d.findElements(by).isEmpty() || !d.findElements(by).get(0).isDisplayed());
    }

    public static void screenshot(WebDriver driver, File filePng) {
        try {
            File scrFile = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
            scrFile.deleteOnExit();
            FileUtils.copy(scrFile, filePng);
            LogUtils.warning("Screenshot save " + filePng.getAbsolutePath());
        } catch (IOException ex) {
            LogUtils.generate(ex);
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy