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

com.shaft.gui.element.AlertActions Maven / Gradle / Ivy

Go to download

SHAFT is a unified test automation engine. Powered by best-in-class frameworks, SHAFT provides a wizard-like syntax to drive your automation efficiently, maximize your ROI, and minimize your learning curve. Stop reinventing the wheel. Upgrade now!

There is a newer version: 8.2.20240402
Show newest version
package com.shaft.gui.element;

import com.shaft.driver.SHAFT;
import com.shaft.driver.internal.DriverFactoryHelper;
import com.shaft.gui.browser.internal.FluentBrowserActions;
import com.shaft.gui.element.internal.ElementActionsHelper;
import com.shaft.gui.element.internal.FluentElementActions;
import com.shaft.tools.io.ReportManager;
import org.openqa.selenium.NoAlertPresentException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

import java.time.Duration;

@SuppressWarnings("unused")
public class AlertActions {

    public AlertActions(WebDriver driver) {
        new AlertActions();
    }

    public AlertActions() {
    }

    public static AlertActions getInstance() {
        return new AlertActions();
    }

    public TouchActions touch() {
        return new TouchActions();
    }

    public FluentBrowserActions browser() {
        return FluentBrowserActions.getInstance();
    }

    public AlertActions and() {
        return this;
    }

    private static void waitForAlertToBePresent() {
        try {
            (new WebDriverWait(DriverFactoryHelper.getDriver().get(), Duration.ofSeconds(SHAFT.Properties.timeouts.defaultElementIdentificationTimeout()))).until(ExpectedConditions.alertIsPresent());
            DriverFactoryHelper.getDriver().get().switchTo().alert();
            ReportManager.logDiscrete("Alert is present");
        } catch (Exception rootCauseException) {
            ReportManager.logDiscrete("Alert is not present");
            ElementActionsHelper.failAction(DriverFactoryHelper.getDriver().get(), null, rootCauseException);
        }
    }

    public FluentElementActions performElementAction() {
        return FluentElementActions.getInstance();
    }

    public boolean isAlertPresent() {
        try {
            DriverFactoryHelper.getDriver().get().switchTo().alert();
            ElementActionsHelper.passAction(DriverFactoryHelper.getDriver().get(), null, Thread.currentThread().getStackTrace()[1].getMethodName(), null, null, null);
            ReportManager.logDiscrete("Alert is present");
            return true;
        } catch (NoAlertPresentException exception) {
            ElementActionsHelper.failAction(DriverFactoryHelper.getDriver().get(), null, exception);
            ReportManager.logDiscrete("Alert is not present");
            return false;
        } catch (Exception rootCauseException) {
            ElementActionsHelper.failAction(DriverFactoryHelper.getDriver().get(), null, rootCauseException);
            return false;
        }
    }

    @SuppressWarnings("UnusedReturnValue")
    public AlertActions acceptAlert() {
        try {
            waitForAlertToBePresent();
            DriverFactoryHelper.getDriver().get().switchTo().alert().accept();
            ElementActionsHelper.passAction(DriverFactoryHelper.getDriver().get(), null, Thread.currentThread().getStackTrace()[1].getMethodName(), null, null, null);
        } catch (Exception rootCauseException) {
            ElementActionsHelper.failAction(DriverFactoryHelper.getDriver().get(), null, rootCauseException);
        }
        return this;
    }

    @SuppressWarnings("UnusedReturnValue")
    public AlertActions dismissAlert() {
        try {
            waitForAlertToBePresent();
            DriverFactoryHelper.getDriver().get().switchTo().alert().dismiss();
            ElementActionsHelper.passAction(DriverFactoryHelper.getDriver().get(), null, Thread.currentThread().getStackTrace()[1].getMethodName(), null, null, null);
        } catch (Exception rootCauseException) {
            ElementActionsHelper.failAction(DriverFactoryHelper.getDriver().get(), null, rootCauseException);
        }
        return this;
    }

    public String getAlertText() {
        try {
            waitForAlertToBePresent();
            var alertText = DriverFactoryHelper.getDriver().get().switchTo().alert().getText();
            ReportManager.logDiscrete("Alert Text is: [" + alertText + "]");
            ElementActionsHelper.passAction(DriverFactoryHelper.getDriver().get(), null, Thread.currentThread().getStackTrace()[1].getMethodName(), null, null, null);
            return alertText;
        } catch (Exception rootCauseException) {
            ElementActionsHelper.failAction(DriverFactoryHelper.getDriver().get(), null, rootCauseException);
            return null;
        }
    }

    @SuppressWarnings("UnusedReturnValue")
    public AlertActions typeIntoPromptAlert(String text) {
        try {
            waitForAlertToBePresent();
            DriverFactoryHelper.getDriver().get().switchTo().alert().sendKeys(text);
            ReportManager.logDiscrete("Text typed into Alert is: [" + text + "]");
            ElementActionsHelper.passAction(DriverFactoryHelper.getDriver().get(), null, Thread.currentThread().getStackTrace()[1].getMethodName(), null, null, null);
        } catch (Exception rootCauseException) {
            ElementActionsHelper.failAction(DriverFactoryHelper.getDriver().get(), null, rootCauseException);
        }
        return this;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy