Merge automation from ECOMP's repository
[vid.git] / vid-automation / src / main / java / vid / automation / test / sections / DrawingBoardPage.java
1 package vid.automation.test.sections;
2
3 import com.google.common.collect.ImmutableList;
4 import com.google.common.collect.Sets;
5 import org.onap.sdc.ci.tests.utilities.GeneralUIUtils;
6 import org.openqa.selenium.By;
7 import org.openqa.selenium.JavascriptExecutor;
8 import org.openqa.selenium.NoSuchElementException;
9 import org.openqa.selenium.WebElement;
10 import org.testng.Assert;
11 import vid.automation.test.Constants;
12 import vid.automation.test.infra.Click;
13 import vid.automation.test.infra.Get;
14 import vid.automation.test.infra.Wait;
15
16 import java.util.*;
17 import java.util.function.Function;
18 import java.util.function.Predicate;
19 import java.util.stream.Collectors;
20
21 import static org.hamcrest.CoreMatchers.equalTo;
22 import static org.hamcrest.CoreMatchers.is;
23 import static org.hamcrest.MatcherAssert.assertThat;
24 import static org.hamcrest.collection.IsEmptyCollection.empty;
25 import static vid.automation.test.Constants.DrawingBoard.*;
26
27 public class DrawingBoardPage extends VidBasePage {
28
29     public DrawingBoardPage(){
30         super();
31     }
32
33     public void expandTreeByClickingNode(String nodeName, String... children) {
34         checkNodesVisible(children, false);
35         clickNode(nodeName);
36         checkNodesVisible(children, true);
37     }
38
39     public void clickNode(String nodeName) {
40         Click.byTestId(Constants.DrawingBoard.NODE_PREFIX + nodeName);
41     }
42
43     public void expandFirstItemInTreeByExpanderIcon(String treeDataTestId, String... children) {
44         checkNodesVisible(children, false);
45         Click.byXpath("//tree-root[@data-tests-id='" + treeDataTestId + "']//span[@class='" + Constants.DrawingBoard.TOGGLE_CHILDREN + "']");
46         checkNodesVisible(children, true);
47     }
48
49     public void checkLeafNodeHasNoExpander(String nodeName){
50         WebElement webElement = Get.byXpath("//div[contains(@class, '" + Constants.DrawingBoard.TREE_NODE_LEAF + "') and .//div[@data-tests-id='" + Constants.DrawingBoard.NODE_PREFIX + nodeName + "']]");
51         Assert.assertNotNull(webElement, "There is an expander to node " + nodeName + " without children");
52     }
53
54     public void verifyNonCollapsableTreeByClickingNode(String nodeName, String... children) {
55         checkNodesVisible(children, true);
56         clickNode(nodeName);
57         checkNodesVisible(children, true);
58     }
59
60     public void collapseFirstItemInTreeByCollapseIcon(String treeDataTestId, String... children) {
61         checkNodesVisible(children, true);
62         Click.byXpath("//tree-root[@data-tests-id='" + treeDataTestId + "']//span[@class='" + Constants.DrawingBoard.TOGGLE_CHILDREN + "']");
63         checkNodesVisible(children, false);
64     }
65
66     public void RefreshPage(){
67         GeneralUIUtils.getDriver().navigate().refresh();
68     }
69
70     public void assertInitalTextOfTree(String treeDataTestId, String[] initialElements) {
71         WebElement webElement = Get.byTestId(treeDataTestId);
72         String expected = String.join("\n", initialElements);
73         Wait.byText(expected);
74         Assert.assertEquals(webElement.getText(), expected);
75     }
76
77     public void checkAddButton(String[] rootElements){
78         String previousAddButton = null;
79         for (String root : rootElements) {
80             String currentButton = Constants.DrawingBoard.NODE_PREFIX + root + Constants.DrawingBoard.ADD_BUTTON;
81             checkThatButtonNotExist(currentButton);
82             GeneralUIUtils.hoverOnAreaByTestId(Constants.DrawingBoard.NODE_PREFIX + root);
83             checkThatButtonExist(currentButton);
84             if (previousAddButton != null) {
85                 checkThatButtonNotExist(previousAddButton);
86             }
87             Click.byTestId(currentButton);
88             previousAddButton = currentButton;
89         }
90     }
91
92     public void clickAddButtonByNodeName(String treeNodeId) {
93         String nodeElement = "node-"+ treeNodeId;
94         String addButtonTestId = Constants.DrawingBoard.NODE_PREFIX + treeNodeId + Constants.DrawingBoard.ADD_BUTTON;
95         GeneralUIUtils.hoverOnAreaByTestId(nodeElement);
96         GeneralUIUtils.hoverOnAreaByTestId(addButtonTestId);
97         Click.byTestId(addButtonTestId);
98     }
99
100     private void checkThatButtonNotExist(String dataTestId){
101 //        Assert.assertFalse(GeneralUIUtils.isElementVisibleByTestId(dataTestId),"button " + dataTestId + " should not exist");
102     }
103
104     private void checkThatButtonExist(String dataTestId){
105 //        Assert.assertTrue(GeneralUIUtils.isElementVisibleByTestId(dataTestId), "button " + dataTestId + " should exist");
106     }
107
108     private void checkThatPseudoElementNotExist(String dataTestId) {
109         assertPseudoElementDisplayProp(dataTestId, "none");
110     }
111
112     private void assertPseudoElementDisplayProp(String dataTestId, String expectedCssDisplayProp){
113         final JavascriptExecutor javascriptExecutor = (JavascriptExecutor) GeneralUIUtils.getDriver();
114         final Object cssDisplayProp = javascriptExecutor.executeScript("" +
115                 "return window.getComputedStyle(" +
116                 "   document.querySelector('[data-tests-id=\""+dataTestId+"\"]'),':before'" +
117                 ").getPropertyValue('display')"
118         );
119         assertThat("button " + dataTestId + " should exist", cssDisplayProp, is(expectedCssDisplayProp));
120     }
121
122     private void checkThatPseudoElementExist(String dataTestId) {
123         assertPseudoElementDisplayProp(dataTestId, "inline-block");
124     }
125
126     public void checkThatContextMenuExist(String contextMenu){
127         Assert.assertTrue(GeneralUIUtils.isWebElementExistByTestId(contextMenu), "context menu should appear");
128     }
129
130     public void checkThatContextMenuNotExist(String contextMenu){
131         Assert.assertFalse(GeneralUIUtils.isWebElementExistByTestId(contextMenu), "context menu should not appear");
132     }
133
134     public void checkNodesVisible(String[] children, boolean shouldExist) {
135         checkElements(ImmutableList.copyOf(children),
136                 childName -> GeneralUIUtils.isWebElementExistByTestId(Constants.DrawingBoard.NODE_PREFIX + childName) ? "exists" : "absent",
137                 shouldExist ? "exists" : "absent", "visibility");
138     }
139
140     public void checkNodesHighlighted(String[] children) {
141         checkElements(ImmutableList.copyOf(children),
142                 childName -> {
143                     final WebElement webElement = Get.byTestId(Constants.DrawingBoard.NODE_PREFIX + childName);
144                     final String color = webElement.getCssValue("color");
145                     return color;
146                 },
147                 HIGHLIGHTED_COLOR, "highlightning");
148     }
149
150     public void checkNodesVisibleAndMatchIsHighlighted(String searchString, String... children) {
151         checkElements(ImmutableList.copyOf(children),
152                 childName -> {
153                     final WebElement webElement = Get.byTestId(Constants.DrawingBoard.NODE_PREFIX + childName);
154                     String visible = webElement.isDisplayed() ? "visible" : "hidden";
155                     String highlightedText;
156                     String bgColor;
157                     try {
158                         final WebElement highlighted = webElement.findElement(By.cssSelector(".highlight"));
159                         highlightedText = highlighted.getText();
160                         bgColor = highlighted.getCssValue("background-color");
161                     } catch (NoSuchElementException e) {
162                         highlightedText = "";
163                         bgColor = "none";
164                     }
165                     return String.join("", visible, " and '", highlightedText, "' in ", bgColor);
166                 },
167                 "visible and '" + searchString + "' in rgb(157, 217, 239)", "match highlightning");
168     }
169
170     private void checkElements(Collection<String> elements, Function<String, String> predicate, String expected, final String description) {
171         final Map<String, String> expectedMap = elements.stream().collect(Collectors.toMap(
172                 childName -> childName,
173                 child -> expected
174         ));
175         final Map<String, String> actual = elements.stream().collect(Collectors.toMap(
176                 childName -> childName,
177                 predicate
178         ));
179
180         assertThat("There was an error in " + description + " of elements", actual, equalTo(expectedMap));
181     }
182
183     public void navigateToServicePlanningPage() {
184         navigateTo("/vid/app/ui/#/servicePlanning");
185     }
186
187     public void navigateToEmptyServicePlanningPage() {
188         navigateTo("/vid/app/ui/#/servicePlanningEmpty");
189     }
190
191     public void checkContextMenu(String node){
192         String contextMenuButton = Constants.DrawingBoard.NODE_PREFIX + node + Constants.DrawingBoard.CONTEXT_MENU_BUTTON;
193         final String contextMenu = Constants.DrawingBoard.CONTEXT_MENU_EDIT;
194
195         checkThatPseudoElementNotExist(contextMenuButton);
196         checkThatContextMenuNotExist(contextMenu);
197
198         GeneralUIUtils.hoverOnAreaByTestId(Constants.DrawingBoard.NODE_PREFIX + node);
199         checkThatPseudoElementExist(contextMenuButton);
200         Click.byTestId(contextMenuButton);
201
202         checkThatContextMenuExist(contextMenu);
203     }
204
205     public void checkSearch(){
206         String searchElement = Constants.DrawingBoard.SEARCH_LEFT_TREE;//  TODO - should add that it is on the left tree and should create the id of the search element???
207         Assert.assertTrue(GeneralUIUtils.isWebElementExistByTestId(searchElement), "search " + searchElement + " should exist");
208     }
209
210     public void showTooltipByHoverAboveAlertIcon(String element){
211         assertThat("tooltip should not appear before click",
212                 GeneralUIUtils.getDriver().findElements(By.xpath("//*[contains(@class, '" + "tooltip-inner" + "')]")),
213                 is(empty())
214         );
215
216         GeneralUIUtils.hoverOnAreaByTestId(Constants.DrawingBoard.NODE_PREFIX + element + Constants.DrawingBoard.ALERT_ICON);
217
218         final WebElement webElement = GeneralUIUtils.getWebElementByContainsClassName("tooltip-inner");
219         assertThat(webElement.getText(), is("Missing required information. Please open and fill in the details."));
220     }
221
222     public void clickDeployButton(){
223         GeneralUIUtils.ultimateWait();
224
225         try {
226             GeneralUIUtils.clickOnElementByTestId(DEPLOY_BUTTON);
227         } catch (org.openqa.selenium.WebDriverException e) {
228             // "deploy" replaces the iframe, so "TypeError: can't access dead object" exception is eventually thrown
229             if (!e.getMessage().startsWith("TypeError: can't access dead object")) {
230                 throw e;
231             }
232         }
233
234     }
235
236     public void checkDeployButtonDisabled(){
237         Assert.assertFalse(Get.byTestId(DEPLOY_BUTTON).isEnabled(),"Deploy button is enabled and should be disabled");
238     }
239
240     public void checkExistsAndEnabled(String dataTestId){
241         Assert.assertFalse(GeneralUIUtils.isElementDisabled(dataTestId),"Element " + dataTestId + " should exist and be enabled");
242     }
243
244     public void checkServiceInstanceName(String expectedServiceName){       
245         Assert.assertEquals(SERVICE_INSTANCE_VALUE, Get.byTestId(SERVICE_INSTANCE_TEST_ID).getText());
246         Assert.assertEquals(Get.byTestId(SERVICE_NAME).getText(),expectedServiceName);
247     }
248
249     public void checkServiceStatus() {
250         Assert.assertEquals(Get.byTestId(SERVICE_STATUS).getText(),STATUS_TEXT);
251     }
252
253     public void checkQuantityNumberIsCorrect(int expectedQuantity) {
254         Assert.assertEquals(Get.byTestId(QUANTITY_LABEL_TEST_ID).getText(), (String.valueOf(QUANTITY_LABEL_VALUE)));
255         Assert.assertEquals(Get.byTestId(SERVICE_QUANTITY).getText(), (String.valueOf(expectedQuantity)));
256     }
257
258     public static class  ServiceStatusChecker implements Predicate<Boolean> {
259         private String actualInstanceName;
260         private Set<String> expectedStatuses;
261         private Set<String> columnClassesSet;
262
263         public ServiceStatusChecker(String actualInstanceName, Set<String> expectedStatuses) {
264             this.actualInstanceName = actualInstanceName;
265             this.expectedStatuses = expectedStatuses;
266         }
267
268         @Override
269         public boolean test(Boolean noMeaning) {
270             InstantiationStatusPage.clickRefreshButton();
271             final WebElement row = InstantiationStatusPage.getInstantiationStatusRow(actualInstanceName);
272             if (row == null) {
273                 System.err.println("**********************" + actualInstanceName + "************************************************");
274                 columnClassesSet = Collections.singleton(actualInstanceName + " NOT FOUND");
275                 return false; // treat missing row as if test condition not fulfilled
276             } else {
277                 columnClassesSet = new HashSet<>(Arrays.asList(
278                         row.findElements(By.xpath(".//*[@id='" + "jobStatus" + "']")).get(0).getAttribute("class").split(" ")));
279                 return !(Sets.intersection(expectedStatuses, columnClassesSet).isEmpty());
280             }
281         }
282
283         public Set<String> getColumnClassesSet() {
284             return columnClassesSet;
285         }
286     }
287 }