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

com.github.siwenyan.si.SiCommandExecutorClick Maven / Gradle / Ivy

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

import org.openqa.selenium.WebElement;

class SiCommandExecutorClick extends SiCommandExecutor implements ISiCommandExecutorFlagUi {

    @Override
    public void beforeExecute(SiCommand command, SiContext siContext) throws SiVerifyException, SiWebDriverException {
        super.beforeExecute(command, siContext);
        siContext.getExecutorManager().getBasicExecutor("waitForElementPresent").execute(command, siContext);
        siContext.getExecutorManager().getBasicExecutor("waitForElementVisible").execute(command, siContext);
    }

    @Override
    public void execute(SiCommand command, SiContext siContext) throws SiVerifyException, SiWebDriverException {
        WebElement element = command.getTargetElement();
        try {
            int retry = 3;
            while (retry-- > 0) {
                try {
                    element.click();
                    return;
                } catch (Exception e) {
                    Thread.sleep(1000);
                }
            }
            throw new RuntimeException("Click fail after three retries");
        } catch (Exception e) {
            siContext.getReporter().reportMessage(e);
            siContext.getReporter().reportMessage("Using javascript...");
            String id = element.getAttribute("id");
            String js = "document.getElementById('" + id + "').onclick();";
            siContext.asJavascriptExecutor().executeScript(js);
        }
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy