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