aboutsummaryrefslogtreecommitdiffstats
path: root/vid-automation/src/main/java/vid/automation/test/sections/DrawingBoardPage.java
blob: 7cffd37b9beeb195039358ca3a725654e104c891 (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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
package vid.automation.test.sections;

import com.google.common.collect.ImmutableList;
import org.openecomp.sdc.ci.tests.utilities.GeneralUIUtils;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.NoSuchElementException;
import org.openqa.selenium.WebElement;
import org.testng.Assert;
import vid.automation.test.Constants;
import vid.automation.test.infra.Click;
import vid.automation.test.infra.Get;
import vid.automation.test.infra.Wait;

import java.util.Collection;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.collection.IsEmptyCollection.empty;
import static vid.automation.test.Constants.DrawingBoard.*;

public class DrawingBoardPage extends VidBasePage {

    public DrawingBoardPage(){
        super();
    }

    public void expandTreeByClickingNode(String nodeName, String... children) {
        checkNodesVisible(children, false);
        clickNode(nodeName);
        checkNodesVisible(children, true);
    }

    public void clickNode(String nodeName) {
        Click.byTestId(Constants.DrawingBoard.NODE_PREFIX + nodeName);
    }

    public void expandFirstItemInTreeByExpanderIcon(String treeDataTestId, String... children) {
        checkNodesVisible(children, false);
        Click.byXpath("//tree-root[@data-tests-id='" + treeDataTestId + "']//span[@class='" + Constants.DrawingBoard.TOGGLE_CHILDREN + "']");
        checkNodesVisible(children, true);
    }

    public void checkLeafNodeHasNoExpander(String nodeName){
        WebElement webElement = Get.byXpath("//div[contains(@class, '" + Constants.DrawingBoard.TREE_NODE_LEAF + "') and .//div[@data-tests-id='" + Constants.DrawingBoard.NODE_PREFIX + nodeName + "']]");
        Assert.assertNotNull(webElement, "There is an expander to node " + nodeName + " without children");
    }

    public void verifyNonCollapsableTreeByClickingNode(String nodeName, String... children) {
        checkNodesVisible(children, true);
        clickNode(nodeName);
        checkNodesVisible(children, true);
    }

    public void collapseFirstItemInTreeByCollapseIcon(String treeDataTestId, String... children) {
        checkNodesVisible(children, true);
        Click.byXpath("//tree-root[@data-tests-id='" + treeDataTestId + "']//span[@class='" + Constants.DrawingBoard.TOGGLE_CHILDREN + "']");
        checkNodesVisible(children, false);
    }

    public void RefreshPage(){
        GeneralUIUtils.getDriver().navigate().refresh();
    }

    public void assertInitalTextOfTree(String treeDataTestId, String[] initialElements) {
        WebElement webElement = Get.byTestId(treeDataTestId);
        String expected = String.join("\n", initialElements);
        Wait.byText(expected);
        Assert.assertEquals(webElement.getText(), expected);
    }

    public void checkAddButton(String[] rootElements){
        String previousAddButton = null;
        for (String root : rootElements) {
            String currentButton = Constants.DrawingBoard.NODE_PREFIX + root + Constants.DrawingBoard.ADD_BUTTON;
            checkThatButtonNotExist(currentButton);
            GeneralUIUtils.hoverOnAreaByTestId(Constants.DrawingBoard.NODE_PREFIX + root);
            checkThatButtonExist(currentButton);
            if (previousAddButton != null) {
                checkThatButtonNotExist(previousAddButton);
            }
            Click.byTestId(currentButton);
            previousAddButton = currentButton;
        }
    }

    public void clickAddButtonByNodeName(String treeNodeId) {
        String nodeElement = "node-"+ treeNodeId;
        String addButtonTestId = Constants.DrawingBoard.NODE_PREFIX + treeNodeId + Constants.DrawingBoard.ADD_BUTTON;
        GeneralUIUtils.hoverOnAreaByTestId(nodeElement);
        GeneralUIUtils.hoverOnAreaByTestId(addButtonTestId);
        Click.byTestId(addButtonTestId);
    }

    private void checkThatButtonNotExist(String dataTestId){
//        Assert.assertFalse(GeneralUIUtils.isElementVisibleByTestId(dataTestId),"button " + dataTestId + " should not exist");
    }

    private void checkThatButtonExist(String dataTestId){
//        Assert.assertTrue(GeneralUIUtils.isElementVisibleByTestId(dataTestId), "button " + dataTestId + " should exist");
    }

    private void checkThatPseudoElementNotExist(String dataTestId) {
        assertPseudoElementDisplayProp(dataTestId, "none");
    }

    private void assertPseudoElementDisplayProp(String dataTestId, String expectedCssDisplayProp){
        final JavascriptExecutor javascriptExecutor = (JavascriptExecutor) GeneralUIUtils.getDriver();
        final Object cssDisplayProp = javascriptExecutor.executeScript("" +
                "return window.getComputedStyle(" +
                "   document.querySelector('[data-tests-id=\""+dataTestId+"\"]'),':before'" +
                ").getPropertyValue('display')"
        );
        assertThat("button " + dataTestId + " should exist", cssDisplayProp, is(expectedCssDisplayProp));
    }

    private void checkThatPseudoElementExist(String dataTestId) {
        assertPseudoElementDisplayProp(dataTestId, "inline-block");
    }

    public void checkThatContextMenuExist(String contextMenu){
        Assert.assertTrue(GeneralUIUtils.isWebElementExistByTestId(contextMenu), "context menu should appear");
    }

    public void checkThatContextMenuNotExist(String contextMenu){
        Assert.assertFalse(GeneralUIUtils.isWebElementExistByTestId(contextMenu), "context menu should not appear");
    }

    public void checkNodesVisible(String[] children, boolean shouldExist) {
        checkElements(ImmutableList.copyOf(children),
                childName -> GeneralUIUtils.isWebElementExistByTestId(Constants.DrawingBoard.NODE_PREFIX + childName) ? "exists" : "absent",
                shouldExist ? "exists" : "absent", "visibility");
    }

    public void checkNodesHighlighted(String[] children) {
        checkElements(ImmutableList.copyOf(children),
                childName -> {
                    final WebElement webElement = Get.byTestId(Constants.DrawingBoard.NODE_PREFIX + childName);
                    final String color = webElement.getCssValue("color");
                    return color;
                },
                HIGHLIGHTED_COLOR, "highlightning");
    }

    public void checkNodesVisibleAndMatchIsHighlighted(String searchString, String... children) {
        checkElements(ImmutableList.copyOf(children),
                childName -> {
                    final WebElement webElement = Get.byTestId(Constants.DrawingBoard.NODE_PREFIX + childName);
                    String visible = webElement.isDisplayed() ? "visible" : "hidden";
                    String highlightedText;
                    String bgColor;
                    try {
                        final WebElement highlighted = webElement.findElement(By.cssSelector(".highlight"));
                        highlightedText = highlighted.getText();
                        bgColor = highlighted.getCssValue("background-color");
                    } catch (NoSuchElementException e) {
                        highlightedText = "";
                        bgColor = "none";
                    }
                    return String.join("", visible, " and '", highlightedText, "' in ", bgColor);
                },
                "visible and '" + searchString + "' in rgb(157, 217, 239)", "match highlightning");
    }

    private void checkElements(Collection<String> elements, Function<String, String> predicate, String expected, final String description) {
        final Map<String, String> expectedMap = elements.stream().collect(Collectors.toMap(
                childName -> childName,
                child -> expected
        ));
        final Map<String, String> actual = elements.stream().collect(Collectors.toMap(
                childName -> childName,
                predicate
        ));

        assertThat("There was an error in " + description + " of elements", actual, equalTo(expectedMap));
    }

    public void navigateToServicePlanningPage() {
        navigateTo("/vid/app/ui/#/servicePlanning");
    }

    public void navigateToEmptyServicePlanningPage() {
        navigateTo("/vid/app/ui/#/servicePlanningEmpty");
    }

    public void checkContextMenu(String node){
        String contextMenuButton = Constants.DrawingBoard.NODE_PREFIX + node + Constants.DrawingBoard.CONTEXT_MENU_BUTTON;
        final String contextMenu = Constants.DrawingBoard.CONTEXT_MENU_ITEM;

        checkThatPseudoElementNotExist(contextMenuButton);
        checkThatContextMenuNotExist(contextMenu);

        GeneralUIUtils.hoverOnAreaByTestId(Constants.DrawingBoard.NODE_PREFIX + node);
        checkThatPseudoElementExist(contextMenuButton);
        Click.byTestId(contextMenuButton);

        checkThatContextMenuExist(contextMenu);
    }

    public void checkSearch(){
        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???
        Assert.assertTrue(GeneralUIUtils.isWebElementExistByTestId(searchElement), "search " + searchElement + " should exist");
    }

    public void showTooltipByHoverAboveAlertIcon(String element){
        assertThat("tooltip should not appear before click",
                GeneralUIUtils.getDriver().findElements(By.xpath("//*[contains(@class, '" + "tooltip-inner" + "')]")),
                is(empty())
        );

        GeneralUIUtils.hoverOnAreaByTestId(Constants.DrawingBoard.NODE_PREFIX + element + Constants.DrawingBoard.ALERT_ICON);

        final WebElement webElement = GeneralUIUtils.getWebElementByContainsClassName("tooltip-inner");
        assertThat(webElement.getText(), is("Missing required information. Please open and fill in the details."));
    }

    public void clickDeployButton(){
        GeneralUIUtils.ultimateWait();

        try {
            GeneralUIUtils.clickOnElementByTestId(DEPLOY_BUTTON);
        } catch (org.openqa.selenium.WebDriverException e) {
            // "deploy" replaces the iframe, so "TypeError: can't access dead object" exception is eventually thrown
            if (!e.getMessage().startsWith("TypeError: can't access dead object")) {
                throw e;
            }
        }

    }

    public void checkDeployButtonDisabled(){
        Assert.assertFalse(Get.byTestId(DEPLOY_BUTTON).isEnabled(),"Deploy button is enabled and should be disabled");
    }

    public void checkExistsAndEnabled(String dataTestId){
        Assert.assertFalse(GeneralUIUtils.isElementDisabled(dataTestId),"Element " + dataTestId + " should exist and be enabled");
    }

    public void checkServiceInstanceName(String expectedServiceName){       
        Assert.assertEquals(SERVICE_INSTANCE_VALUE, Get.byTestId(SERVICE_INSTANCE_TEST_ID).getText());
        Assert.assertEquals(Get.byTestId(SERVICE_NAME).getText(),expectedServiceName);
    }

    public void checkServiceStatus() {
        Assert.assertEquals(Get.byTestId(SERVICE_STATUS).getText(),STATUS_TEXT);
    }

    public void checkQuantityNumberIsCorrect(int expectedQuantity) {
        Assert.assertEquals(Get.byTestId(QUANTITY_LABEL_TEST_ID).getText(), (String.valueOf(QUANTITY_LABEL_VALUE)));
        Assert.assertEquals(Get.byTestId(SERVICE_QUANTITY).getText(), (String.valueOf(expectedQuantity)));
    }

}