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

com.epam.jdi.uitests.web.selenium.elements.common.CheckBox Maven / Gradle / Ivy

There is a newer version: 1.1.34
Show newest version
package com.epam.jdi.uitests.web.selenium.elements.common;
/*
 * Copyright 2004-2016 EPAM Systems
 *
 * This file is part of JDI project.
 *
 * JDI is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * JDI is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; 
 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with JDI. If not, see .
 */


import com.epam.jdi.uitests.core.interfaces.common.ICheckBox;
import com.epam.jdi.uitests.web.selenium.elements.base.Clickable;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;

import java.util.function.Function;

import static com.epam.jdi.uitests.core.settings.JDISettings.exception;

/**
 * Checkbox control implementation
 *
 * @author Alexeenko Yan
 * @author Belousov Andrey
 */
public class CheckBox extends Clickable implements ICheckBox {
    private Function isSelected = WebElement::isSelected;
    private Function isChecked = el -> el.getAttribute("checked") != null;
    private Function isCheckedFunc = el -> {
        if (isSelected.apply(el)) {
            isCheckedFunc = isSelected;
            return true;
        }
        if (isChecked.apply(el)) {
            isCheckedFunc = isChecked;
            return true;
        }
        return false;
    };

    public CheckBox() {
    }

    public CheckBox(By byLocator) {
        super(byLocator);
    }

    public CheckBox(WebElement webElement) {
        super(webElement);
    }

    protected String getValueAction() {
        return isChecked() + "";
    }

    protected void setValueAction(String value) {
        switch (value.toLowerCase()) {
            case "true":
            case "1":
            case "check":
                check();
                break;
            case "false":
            case "0":
            case "uncheck":
                uncheck();
                break;
        }
    }

    protected void checkAction() {
        if (!isCheckedAction())
            clickAction();
        if (!isCheckedAction())
            throw exception("Can't check element. Verify locator for click or isCheckedAction");
    }

    protected void uncheckAction() {
        if (isCheckedAction())
            clickAction();
        if (isCheckedAction())
            throw exception("Can't uncheck element. Verify locator for click or isCheckedAction");
    }

    protected boolean isCheckedAction() {
        return isCheckedFunc.apply(getWebElement());
    }

    /**
     * Set checkbox checked
     */
    public final void check() {
        actions.check(this::checkAction);
    }

    /**
     * Set checkbox unchecked
     */
    public final void uncheck() {
        actions.uncheck(this::uncheckAction);
    }

    /**
     * @return Verify is checkbox checked
     */
    public final boolean isChecked() {
        return actions.isChecked(this::isCheckedAction);
    }

    /**
     * @return Get value of Element
     */
    public final String getValue() {
        return actions.getValue(this::getValueAction);
    }

    /**
     * @param value Specify element value
     *              Set value to Element
     */
    public final void setValue(String value) {
        actions.setValue(value, this::setValueAction);
    }

    protected String getTextAction() {
        return isCheckedAction() + "";
    }
    /**
     * @return Get Element’s text
     */
    public final String getText() {
        return actions.getText(this::getTextAction);
    }

    /**
     * @param text Specify expected text
     * @return Wait while Element’s text contains expected text. Returns Element’s text
     */
    public final String waitText(String text) {
        return actions.waitText(text, this::getTextAction);
    }

    /**
     * @param regEx Specify expected regular expression Text
     * @return Wait while Element’s text matches regEx. Returns Element’s text
     */
    public final String waitMatchText(String regEx) {
        return actions.waitMatchText(regEx, this::getTextAction);
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy