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

selenium.wait.WebDriverWaitSteps.kt Maven / Gradle / Ivy

There is a newer version: 5.3.5
Show newest version
package selenium.wait

import com.testerum_api.testerum_steps_api.annotations.steps.Param
import com.testerum_api.testerum_steps_api.annotations.steps.When
import com.testerum_api.testerum_steps_api.services.TesterumServiceLocator
import org.openqa.selenium.WebElement
import selenium_steps_support.service.descriptions.SeleniumSharedDescriptions
import selenium_steps_support.service.elem_locators.ElementLocatorService
import selenium_steps_support.service.module_di.SeleniumModuleServiceLocator
import selenium_steps_support.service.text_match.TextMatcherService
import selenium_steps_support.service.webdriver_manager.WebDriverManager

class WebDriverWaitSteps {

    private val logger = TesterumServiceLocator.getTesterumLogger()
    private val webDriverManager: WebDriverManager = SeleniumModuleServiceLocator.bootstrapper.seleniumModuleFactory.webDriverManager

//----------------------------------------------------------------------------------------------------------------------
    @When(
            value = "I wait until the page title matches <>",
            description = "Wait until the title of the current page matches the given expression, or the timeout is exceeded."
    )
    fun waitForCurrentPageTitleIs(
            @Param(
                    description = SeleniumSharedDescriptions.TEXT_MATCH_EXPRESSION_DESCRIPTION
            )
            textMatchExpression: String
    ) {
        logger.info(
                "waiting for the title of the current page to be\n" +
                "-----------------------------------------------\n" +
                "textMatchExpression : $textMatchExpression\n" +
                "\n"
        )

        webDriverManager.waitUntil { driver ->
            TextMatcherService.matches(textMatchExpression, driver.title)
        }
    }

//----------------------------------------------------------------------------------------------------------------------
    @When(
            value = "I wait until the page title does not match <>",
            description = "Wait until the title of the current page does not match the given expression, or the timeout is exceeded."
    )
    fun waitForCurrentPageTitleIsNot(
            @Param(
                    description = SeleniumSharedDescriptions.TEXT_MATCH_EXPRESSION_DESCRIPTION
            )
            textMatchExpression: String
    ) {
        logger.info(
                "waiting for the title of the current page to NOT be\n" +
                "---------------------------------------------------\n" +
                "textMatchExpression : $textMatchExpression\n" +
                "\n"
        )

        webDriverManager.waitUntil { driver ->
            TextMatcherService.doesNotMatch(textMatchExpression, driver.title)
        }
    }

//----------------------------------------------------------------------------------------------------------------------
    @When(
            value = "I wait until the element <> is present",
            description = "Wait until the given element is present on the page, or the timeout is exceeded."
    )
    fun waitForElementPresent(
            @Param(
                    description = SeleniumSharedDescriptions.ELEMENT_LOCATOR_DESCRIPTION
            )
            elementLocator: String
    ) {
        logger.info(
                "waiting for an element to be present\n" +
                "------------------------------------\n" +
                "elementLocator : $elementLocator\n" +
                "\n"
        )

        webDriverManager.waitForElementPresent(elementLocator)
    }

//----------------------------------------------------------------------------------------------------------------------
    @When(
            value = "I wait until the element <> is not present",
            description = "Wait until the given element is not present on the page, or the timeout is exceeded."
    )
    fun waitForElementNotPresent(
            @Param(
                    description = SeleniumSharedDescriptions.ELEMENT_LOCATOR_DESCRIPTION
            )
            elementLocator: String
    ) {
        logger.info(
                "waiting for an element to NOT be present\n" +
                "----------------------------------------\n" +
                "elementLocator : $elementLocator\n" +
                "\n"
        )

        webDriverManager.waitUntil { driver ->
            ElementLocatorService.locateElement(driver, elementLocator) == null
        }
    }

//----------------------------------------------------------------------------------------------------------------------
    @When(
            value = "I wait until the element <> is visible",
            description = "Wait until the given element is visible on the page, or the timeout is exceeded."
    )
    fun waitForElementVisibile(
            @Param(
                    description = SeleniumSharedDescriptions.ELEMENT_LOCATOR_DESCRIPTION
            )
            elementLocator: String
    ) {
        logger.info(
                "waiting for an element to be visible\n" +
                "----------------------------------------\n" +
                "elementLocator : $elementLocator\n" +
                "\n"
        )

        webDriverManager.waitUntil { driver ->
            val element = ElementLocatorService.locateElement(driver, elementLocator)
            element != null && element.isDisplayed
        }
    }

//----------------------------------------------------------------------------------------------------------------------
    @When(
            value = "I wait until the element <> is hidden",
            description = "Wait until the given element is visible on the page, or the timeout is exceeded."
    )
    fun waitForElementHidden (
            @Param(
                    description = SeleniumSharedDescriptions.ELEMENT_LOCATOR_DESCRIPTION
            )
            elementLocator: String
    ) {
        logger.info(
                "waiting for an element to be hidden\n" +
                "----------------------------------------\n" +
                "elementLocator : $elementLocator\n" +
                "\n"
        )

        webDriverManager.waitUntil { driver ->
            val element = ElementLocatorService.locateElement(driver, elementLocator)
            element != null && !element.isDisplayed
        }
    }

//----------------------------------------------------------------------------------------------------------------------
    @When(
            value = "I wait until the text of element <> should be <>",
            description = "Wait until the given element text matches the provided expression, or the timeout is exceeded."
    )
    fun waitForTextMatch (
            @Param(
                    description = SeleniumSharedDescriptions.ELEMENT_LOCATOR_DESCRIPTION
            )
            elementLocator: String,

            @Param(
                    description = SeleniumSharedDescriptions.TEXT_MATCH_EXPRESSION_DESCRIPTION
            )
            textMatchExpression: String
    ) {
        logger.info(
                "waiting for element text to match expression\n" +
                "----------------------------------------\n" +
                "elementLocator      : $elementLocator\n" +
                "textMatchExpression : $textMatchExpression\n" +
                "\n"
        )

        webDriverManager.waitUntil { driver ->
            val element: WebElement = ElementLocatorService.locateElement(driver, elementLocator)
                    ?: throw AssertionError("the element [$elementLocator] should be present on the page, but is not")

            val actualText: String = element.text

            TextMatcherService.matches(textMatchExpression, actualText)
        }
    }

//----------------------------------------------------------------------------------------------------------------------
    @When(
            value = "I wait until the text of element <> should not be <>",
            description = "Wait until the given element text doesn't match the provided expression, or the timeout is exceeded."
    )
    fun waitForTextToNotMatch (
            @Param(
                    description = SeleniumSharedDescriptions.ELEMENT_LOCATOR_DESCRIPTION
            )
            elementLocator: String,

            @Param(
                    description = SeleniumSharedDescriptions.TEXT_MATCH_EXPRESSION_DESCRIPTION
            )
            textMatchExpression: String
    ) {
        logger.info(
                "waiting for element text to not match expression\n" +
                "----------------------------------------\n" +
                "elementLocator      : $elementLocator\n" +
                "textMatchExpression : $textMatchExpression\n" +
                "\n"
        )

        webDriverManager.waitUntil { driver ->
            val element: WebElement = ElementLocatorService.locateElement(driver, elementLocator)
                    ?: throw AssertionError("the element [$elementLocator] should be present on the page, but is not")

            val actualText: String = element.text

            !TextMatcherService.matches(textMatchExpression, actualText)
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy