aboutsummaryrefslogtreecommitdiffstats
path: root/vid-automation/src/main/java/vid/automation/test/infra/Click.java
blob: 0b5f40b849b0eb7002df37efdb609328e378ad61 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
package vid.automation.test.infra;

import org.junit.Assert;
import org.onap.sdc.ci.tests.utilities.GeneralUIUtils;
import org.openqa.selenium.Alert;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.Select;
import org.openqa.selenium.support.ui.WebDriverWait;

import java.util.List;

import static org.onap.sdc.ci.tests.utilities.GeneralUIUtils.getDriver;

public class Click {
    public static void byText(String text) {
        WebElement element = GeneralUIUtils.findByText(text);
        Assert.assertTrue(element != null);

        clickWhenClickable(element);
    }

    public static void byId(String id) {
        WebElement element = Get.byId(id);
        Assert.assertTrue(element != null);

        clickWhenClickable(element);
    }

    public static void byTestId(String testId) {
        WebElement element = Get.byTestId(testId);
        Assert.assertTrue(element != null);
        clickWhenClickable(element);
    }

    public static void byClass(String className) {
        List<WebElement> elements = Get.byClass(className);
        Assert.assertTrue(elements != null && elements.size() > 0);

        clickWhenClickable(elements.get(0));
    }

    public static void byXpath(String xpath) {
        WebElement element = Get.byXpath(xpath);
        Assert.assertNotNull(element);
        clickWhenClickable(element);
    }


    public static void onFirstSelectOptionById(String id) {
        Select selectlist = new Select(Get.byId(id));
        if(selectlist.getOptions().size() > 1) {
            selectlist.selectByIndex(1);
        }
    }

    public static void onFirstSelectOptionByTestId(String dataTestId) {
        Select selectList = new Select(Get.byTestId(dataTestId));
        if(selectList.getOptions().size() > 1) {
            selectList.selectByIndex(1);
        }
    }

    public static void onFirstSelectOptionByClass(String className) {
        final List<WebElement> webElements = Get.byClass(className);
        webElements.forEach(webElement -> {
            Select selectlist = new Select(webElement);
            if (selectlist.getOptions().size() > 1) {
                selectlist.selectByIndex(1);
            }
        });
    }

    public static void byClassAndVisibleText(String className, String text ) {
        WebElement element = Get.byClassAndText(className, text);
        clickWhenClickable(element);
    }



    public static void acceptAlert() {
        Alert alert = GeneralUIUtils.getDriver().switchTo().alert();
        Assert.assertTrue(alert != null);
        alert.accept();
    }

    private static void clickWhenClickable(WebElement element) {
        new WebDriverWait(getDriver(), 1)
                .until(ExpectedConditions.elementToBeClickable(element))
                .click();
    }

}