Merge automation from ECOMP's repository
[vid.git] / vid-automation / src / main / java / vid / automation / test / infra / SelectOption.java
1 package vid.automation.test.infra;
2
3 import com.google.common.collect.ImmutableList;
4 import org.apache.commons.lang3.ObjectUtils;
5 import org.onap.sdc.ci.tests.utilities.GeneralUIUtils;
6 import org.openqa.selenium.WebElement;
7 import org.openqa.selenium.support.ui.Select;
8 import vid.automation.test.Constants;
9
10 import java.util.List;
11 import java.util.concurrent.TimeUnit;
12
13 /**
14  * Created by itzikliderman on 18/07/2017.
15  */
16 public class SelectOption {
17     public static Select byValue(String value, String dataTestsId) {
18         Select select = new Select(GeneralUIUtils.getWebElementByTestID(dataTestsId));
19         if(value != null) {
20             select.selectByValue(value);
21         }
22
23         return select;
24     }
25
26     public static Select byIdAndVisibleText(String id, String text) {
27         Select selectlist = new Select(Get.byId(id));
28         if(text != null) {
29             selectlist.selectByVisibleText(text);
30         }
31
32         return selectlist;
33     }
34
35     public static void byClassAndVisibleText(String className, String text) {
36         final List<WebElement> webElements = Get.byClass(className);
37         webElements.forEach(webElement -> {
38             final String id = webElement.getAttribute("id");
39             byIdAndVisibleText(id, text);
40         });
41     }
42     public static List<WebElement> getList(String selectDataTestId) {
43         Select selectList = GeneralUIUtils.getSelectList(null, selectDataTestId);
44         return selectList.getOptions();
45     }
46     public static String getSelectedOption(String selectDataTestId) {
47         Select selectList = GeneralUIUtils.getSelectList(null, selectDataTestId);
48         return selectList.getFirstSelectedOption().getText();
49     }
50     public static void byTestIdAndVisibleText(String displayName, String selectDataTestId) {
51         GeneralUIUtils.getSelectList(displayName, selectDataTestId);
52     }
53
54     public static void selectFirstTwoOptionsFromMultiselectById(String multiSelectId) throws InterruptedException {
55         Click.byId(multiSelectId);
56         Thread.sleep(1000);
57         Click.byClass(Constants.MULTI_SELECT_UNSELECTED_CLASS);
58         Click.byClass(Constants.MULTI_SELECT_UNSELECTED_CLASS);
59
60     }
61
62     public static void waitForOptionInSelect(String option, String selectTestId) {
63         Wait.waitFor(foo ->
64                         ObjectUtils.defaultIfNull(SelectOption.getList(selectTestId), ImmutableList.<WebElement>of())
65                                 .stream().map(o -> o.getText()).filter(o -> option.equals(o)).findAny().isPresent(),
66                 "", 10, 200, TimeUnit.MILLISECONDS);
67     }
68
69     public static void selectOptionsFromMultiselectById(String multiSelectId, List<String> options) {
70         Click.byId(multiSelectId);
71         try {
72             Thread.sleep(1000);
73         } catch (InterruptedException e) {
74             throw new RuntimeException(e);
75         }
76         for(String option:options) {
77             Click.byClassAndVisibleText(Constants.MULTI_SELECT_UNSELECTED_CLASS, option);
78         }
79     }
80 }