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