2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
21 package org.openecomp.sdc.ci.tests.datatypes;
23 import java.util.HashMap;
24 import java.util.List;
26 import java.util.Random;
27 import java.util.UUID;
28 import java.util.stream.Collectors;
30 import org.apache.commons.lang3.tuple.ImmutablePair;
31 import org.openecomp.sdc.ci.tests.datatypes.DataTestIdEnum.LeftPanelCanvasItems;
32 import org.openecomp.sdc.ci.tests.execute.setup.ExtentTestActions;
33 import org.openecomp.sdc.ci.tests.execute.setup.SetupCDTest;
34 import org.openecomp.sdc.ci.tests.utilities.GeneralUIUtils;
35 import org.openqa.selenium.By;
36 import org.openqa.selenium.StaleElementReferenceException;
37 import org.openqa.selenium.WebElement;
38 import org.openqa.selenium.interactions.Actions;
39 import org.testng.Assert;
41 import com.aventstack.extentreports.Status;
43 public final class CanvasManager {
44 private Map<String, CanvasElement> canvasElements;
45 private Actions actions;
46 private WebElement canvas;
47 private int reduceCanvasWidthFactor;
48 private CanvasElement canvasElement;
49 // Offsets Are used to find upper right corner of canvas element in order to
51 private static final int CANVAS_ELEMENT_Y_OFFSET = 30;
52 private static final int CANVAS_ELEMENT_X_OFFSET = 18; // 14 - 27
54 private CanvasManager() {
55 canvasElements = new HashMap<>();
56 actions = new Actions(GeneralUIUtils.getDriver());
57 canvas = GeneralUIUtils.getWebElementByTestID(DataTestIdEnum.GeneralCanvasItems.CANVAS.getValue());
59 WebElement webElement = GeneralUIUtils
60 .getWebElementByTestID(DataTestIdEnum.GeneralCanvasItems.CANVAS_RIGHT_PANEL.getValue());
61 reduceCanvasWidthFactor = webElement.getSize().width;
62 } catch (Exception e) {
63 reduceCanvasWidthFactor = 0;
67 public static CanvasManager getCanvasManager() {
68 return new CanvasManager();
71 public List<CanvasElement> getCanvasElements() {
72 return canvasElements.values().stream().collect(Collectors.toList());
75 private void addCanvasElement(CanvasElement element) {
76 canvasElements.put(element.getUniqueId(), element);
79 private void moveElementOnCanvas(CanvasElement canvasElement, ImmutablePair<Integer, Integer> newLocation)
81 GeneralUIUtils.waitForLoader();
82 actions.moveToElement(canvas, canvasElement.getLocation().left, canvasElement.getLocation().right);
83 actions.clickAndHold();
84 actions.moveToElement(canvas, newLocation.left, newLocation.right);
87 canvasElement.setLocation(newLocation);
88 GeneralUIUtils.waitForLoader();
92 public void moveToFreeLocation(String containerName) {
96 boolean isKeepWaiting = false;
97 while (!isKeepWaiting) {
98 ImmutablePair<Integer, Integer> freePosition = getFreePosition();
99 actions.moveToElement(canvas, freePosition.left, freePosition.right);
100 actions.clickAndHold();
103 isKeepWaiting = GeneralUIUtils.getWebElementByTestID("selectedCompTitle").getText()
104 .equals(containerName);
105 sumOfWaiting += napPeriod;
106 if (sumOfWaiting > maxWait) {
107 Assert.fail("Can't click on VF");
112 public void clickOnCanvaElement(CanvasElement canvasElement) {
113 actions.moveToElement(canvas, canvasElement.getLocation().left, canvasElement.getLocation().right);
114 actions.clickAndHold();
117 actions.click().perform();
118 GeneralUIUtils.ultimateWait();
119 ExtentTestActions.log(Status.INFO, String.format("Canvas element %s selected", canvasElement.getElementType()));
122 public void moveElementOnCanvas(CanvasElement canvasElement) throws Exception {
123 moveElementOnCanvas(canvasElement, getFreePosition());
126 public void deleteElementFromCanvas(CanvasElement canvasElement) throws Exception {
127 GeneralUIUtils.waitForLoader();
128 actions.moveToElement(canvas, canvasElement.getLocation().left, canvasElement.getLocation().right);
131 GeneralUIUtils.getWebElementByTestID(DataTestIdEnum.GeneralCanvasItems.DELETE_INSTANCE_BUTTON.getValue())
133 GeneralUIUtils.getWebElementByTestID(DataTestIdEnum.ModalItems.OK.getValue()).click();
134 canvasElements.remove(canvasElement.getUniqueId());
135 GeneralUIUtils.ultimateWait();
136 if (canvasElement.getElementType().contains("-")){
137 ExtentTestActions.log(Status.INFO, String.format("Canvas element %s removed", canvasElement.getElementType().split("-")[4]));
140 ExtentTestActions.log(Status.INFO, String.format("Canvas element %s removed", canvasElement.getElementType()));
144 private WebElement findClickElement(String dataTestId) {
146 while (attempts < 2) {
148 return GeneralUIUtils.getWebElementByTestID(dataTestId);
149 } catch (StaleElementReferenceException e) {
156 public CanvasElement createElementOnCanvas(String elementName) throws Exception {
157 String actionDuration = GeneralUIUtils.getActionDuration(() -> {
159 canvasElement = createElementOnCanvasWithoutDuration(elementName);
160 } catch (Exception e) {
165 if (canvasElement != null){
166 ExtentTestActions.log(Status.INFO, String.format("The element %s should now be on the canvas", elementName), actionDuration);
168 return canvasElement;
171 private CanvasElement createElementOnCanvasWithoutDuration(String elementDataTestId) throws Exception {
173 WebElement element = findClickElement(elementDataTestId);
174 ImmutablePair<Integer, Integer> freePosition = getFreePosition();
175 actions.moveToElement(element, 20, 20);
176 actions.clickAndHold();
177 actions.moveToElement(canvas, freePosition.left, freePosition.right);
180 GeneralUIUtils.ultimateWait();
181 String uniqueId = elementDataTestId + "_" + UUID.randomUUID().toString();
182 CanvasElement canvasElement = new CanvasElement(uniqueId, freePosition, elementDataTestId);
183 addCanvasElement(canvasElement);
184 GeneralUIUtils.ultimateWait();
185 return canvasElement;
187 catch (Exception e) {
188 System.out.println("Can't create element on canvas");
194 public CanvasElement createElementOnCanvas(LeftPanelCanvasItems canvasItem) throws Exception {
195 return createElementOnCanvas(canvasItem.getValue());
198 private ImmutablePair<Integer, Integer> getFreePosition() {
199 ImmutablePair<Integer, Integer> randomPosition = null;
200 boolean freePosition = false;
202 while (!freePosition) {
203 ImmutablePair<Integer, Integer> tempRandomPosition = getRandomPosition();
204 freePosition = !canvasElements.values().stream().map(e -> e.getLocation())
205 .filter(e -> Math.abs(e.left - tempRandomPosition.left) < minSpace
206 && Math.abs(e.right - tempRandomPosition.right) < minSpace)
207 .findAny().isPresent();
208 randomPosition = tempRandomPosition;
210 return randomPosition;
213 private ImmutablePair<Integer, Integer> getRandomPosition() {
215 Random random = new Random();
216 int xElement = random.nextInt(canvas.getSize().width - 2 * edgeBuffer - reduceCanvasWidthFactor) + edgeBuffer;
217 int yElement = random.nextInt(canvas.getSize().height - 2 * edgeBuffer) + edgeBuffer;
218 return new ImmutablePair<Integer, Integer>(xElement, yElement);
221 public void linkElements(CanvasElement firstElement, CanvasElement secondElement) throws Exception {
222 ExtentTestActions.log(Status.INFO, String.format("Linking between the %s instance and the %s instance.", firstElement.getElementType(), secondElement.getElementType()));
223 drawSimpleLink(firstElement, secondElement);
224 selectReqAndCapAndConnect();
225 ExtentTestActions.log(Status.INFO, String.format("The instances %s and %s should now be connected.", firstElement.getElementType(), secondElement.getElementType()));
228 private void selectReqAndCapAndConnect() throws Exception {
230 GeneralUIUtils.getWebElementsListByTestID(DataTestIdEnum.LinkMenuItems.LINK_ITEM_CAP.getValue()).get(0).click();
232 GeneralUIUtils.getWebElementsListByTestID(DataTestIdEnum.LinkMenuItems.LINK_ITEM_REQ.getValue()).get(0).click();
234 GeneralUIUtils.getWebElementByTestID(DataTestIdEnum.LinkMenuItems.CONNECT_BUTTON.getValue()).click();
236 GeneralUIUtils.waitForLoader();
239 private void drawSimpleLink(CanvasElement firstElement, CanvasElement secondElement) throws Exception {
240 int yOffset = CANVAS_ELEMENT_Y_OFFSET;
241 int xOffset = CANVAS_ELEMENT_X_OFFSET;
243 actions.moveToElement(canvas, firstElement.getLocation().left + xOffset,
244 firstElement.getLocation().right - yOffset);
246 actions.clickAndHold();
247 actions.moveToElement(canvas, secondElement.getLocation().left + xOffset, secondElement.getLocation().right - yOffset);
250 GeneralUIUtils.ultimateWait();
253 public String updateElementNameInCanvas(CanvasElement canvasElement, String newInstanceName) throws Exception {
254 GeneralUIUtils.ultimateWait();;
255 clickOnCanvaElement(canvasElement);
256 WebElement updateInstanceName = GeneralUIUtils.getWebElementBy(By.id("editPencil"));
257 updateInstanceName.click();
258 WebElement instanceNameField = GeneralUIUtils.getWebElementByTestID(DataTestIdEnum.GeneralCanvasItems.INSTANCE_NAME_FIELD.getValue());
259 String oldInstanceName = instanceNameField.getAttribute("value");
260 instanceNameField.clear();
261 instanceNameField.sendKeys(newInstanceName);
262 GeneralUIUtils.getWebElementByTestID(DataTestIdEnum.ModalItems.OK.getValue()).click();
263 GeneralUIUtils.ultimateWait();
264 GeneralUIUtils.waitForElementInVisibilityByTestId(By.className("w-sdc-modal-resource-instance-name"));
265 SetupCDTest.getExtendTest().log(Status.INFO, String.format("Name of element instance changed from %s to %s", oldInstanceName, newInstanceName));
266 return oldInstanceName;