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 com.aventstack.extentreports.Status;
24 import com.clearspring.analytics.util.Pair;
25 import org.apache.commons.lang3.tuple.ImmutablePair;
26 import org.openecomp.sdc.ci.tests.datatypes.DataTestIdEnum.LeftPanelCanvasItems;
27 import org.openecomp.sdc.ci.tests.datatypes.enums.CircleSize;
28 import org.openecomp.sdc.ci.tests.execute.setup.ExtentTestActions;
29 import org.openecomp.sdc.ci.tests.execute.setup.SetupCDTest;
30 import org.openecomp.sdc.ci.tests.pages.CompositionPage;
31 import org.openecomp.sdc.ci.tests.utilities.GeneralUIUtils;
32 import org.openqa.selenium.By;
33 import org.openqa.selenium.StaleElementReferenceException;
34 import org.openqa.selenium.WebElement;
35 import org.openqa.selenium.interactions.Actions;
36 import org.testng.Assert;
37 import org.testng.SkipException;
40 import java.util.concurrent.TimeUnit;
41 import java.util.stream.Collectors;
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_VF_Y_OFFSET = 30;
52 private static final int CANVAS_VF_X_OFFSET = 18; // 14 - 27
54 private static final int CANVAS_NORMATIVE_ELEMENT_Y_OFFSET = 12;
55 private static final int CANVAS_NORMATIVE_ELEMENT_X_OFFSET = 7;
57 private static final int CANVAS_SERVICE_Y_OFFSET = 27;
58 private static final int CANVAS_SERVICE_X_OFFSET = 16;
60 private CanvasManager() {
61 canvasElements = new HashMap<>();
62 actions = new Actions(GeneralUIUtils.getDriver());
63 canvas = GeneralUIUtils.getWebElementByTestID(DataTestIdEnum.GeneralCanvasItems.CANVAS.getValue());
65 WebElement webElement = GeneralUIUtils
66 .getWebElementByTestID(DataTestIdEnum.GeneralCanvasItems.CANVAS_RIGHT_PANEL.getValue());
67 reduceCanvasWidthFactor = webElement.getSize().width;
68 } catch (Exception e) {
69 reduceCanvasWidthFactor = 0;
73 public static CanvasManager getCanvasManager() {
74 return new CanvasManager();
77 public List<CanvasElement> getCanvasElements() {
78 return canvasElements.values().stream().collect(Collectors.toList());
81 private void addCanvasElement(CanvasElement element) {
82 canvasElements.put(element.getUniqueId(), element);
85 private void moveElementOnCanvas(CanvasElement canvasElement, ImmutablePair<Integer, Integer> newLocation)
87 GeneralUIUtils.waitForLoader();
88 actions.moveToElement(canvas, canvasElement.getLocation().left, canvasElement.getLocation().right);
89 actions.clickAndHold();
90 actions.moveToElement(canvas, newLocation.left, newLocation.right);
93 canvasElement.setLocation(newLocation);
94 GeneralUIUtils.waitForLoader();
98 public void moveToFreeLocation(String containerName) {
100 int sumOfWaiting = 0;
102 boolean isKeepWaiting = false;
103 while (!isKeepWaiting) {
104 ImmutablePair<Integer, Integer> freePosition = getFreePosition();
105 actions.moveToElement(canvas, freePosition.left, freePosition.right);
106 actions.clickAndHold();
109 isKeepWaiting = GeneralUIUtils.getWebElementByTestID(DataTestIdEnum.CompositionRightPanel.COMPONENT_TITLE.getValue()).getText()
110 .equals(containerName);
111 sumOfWaiting += napPeriod;
112 if (sumOfWaiting > maxWait) {
113 Assert.fail("Can't click on VF");
118 public void clickOnCanvaElement(CanvasElement canvasElement) {
119 actions.moveToElement(canvas, canvasElement.getLocation().left, canvasElement.getLocation().right);
120 actions.clickAndHold();
123 actions.click().perform();
124 GeneralUIUtils.ultimateWait();
126 validateInstanceSelected(canvasElement);
127 ExtentTestActions.log(Status.INFO, String.format("Canvas element %s selected", canvasElement.getElementType()));
130 public void openLinkPopupReqsCapsConnection(CanvasElement canvasElement)
132 ExtentTestActions.log(Status.INFO, "Open Link popup");
133 clickOnCanvasLink(canvasElement);
134 int x = canvasElement.getLocation().getLeft() + 30; // view button x delta
135 int y = canvasElement.getLocation().getRight() + 11; // view button y delta
136 clickOnCanvasPosition(x,y);
137 GeneralUIUtils.ultimateWait();
139 public void closeLinkPopupReqsCapsConnection()
141 GeneralUIUtils.clickOnElementByTestId("Cancel");
142 GeneralUIUtils.ultimateWait();
145 public void clickSaveOnLinkPopup()
147 ExtentTestActions.log(Status.INFO, "Click save on link popup");
148 GeneralUIUtils.clickOnElementByTestId("Save");
149 GeneralUIUtils.ultimateWait();
152 public void deleteLinkPopupReqsCapsConnection(CanvasElement canvasElement)
154 clickOnCanvasLink(canvasElement);
155 int x = canvasElement.getLocation().getLeft() + 30; // delete button x delta
156 int y = canvasElement.getLocation().getRight() + 30; // delete button x delta
157 clickOnCanvasPosition(x,y);
160 public void clickOnCanvasLink(CanvasElement canvasElement) {
161 actions.moveToElement(canvas, canvasElement.getLocation().left, canvasElement.getLocation().right);
162 actions.click().perform();
163 GeneralUIUtils.ultimateWait();
166 public void clickOnCanvasPosition(int x, int y) {
169 actions.moveToElement(canvas, x, y);
172 GeneralUIUtils.ultimateWait();
176 System.out.println(e);
180 public void moveElementOnCanvas(CanvasElement canvasElement) throws Exception {
181 moveElementOnCanvas(canvasElement, getFreePosition());
184 public void deleteElementFromCanvas(CanvasElement canvasElement) throws Exception {
185 GeneralUIUtils.waitForLoader();
186 actions.moveToElement(canvas, canvasElement.getLocation().left, canvasElement.getLocation().right);
189 GeneralUIUtils.getWebElementByTestID(DataTestIdEnum.GeneralCanvasItems.DELETE_INSTANCE_BUTTON.getValue())
191 GeneralUIUtils.getWebElementByTestID(DataTestIdEnum.ModalItems.OK.getValue()).click();
192 canvasElements.remove(canvasElement.getUniqueId());
193 GeneralUIUtils.ultimateWait();
194 if (canvasElement.getElementType().contains("-")){
195 ExtentTestActions.log(Status.INFO, String.format("Canvas element %s removed", canvasElement.getElementType().split("-")[4]));
198 ExtentTestActions.log(Status.INFO, String.format("Canvas element %s removed", canvasElement.getElementType()));
202 private WebElement findClickElement(String dataTestId) {
204 while (attempts < 2) {
206 return GeneralUIUtils.getWebElementByTestID(dataTestId);
207 } catch (StaleElementReferenceException e) {
214 public CanvasElement createElementOnCanvas(String elementName) throws Exception {
215 String actionDuration = GeneralUIUtils.getActionDuration(() -> {
217 canvasElement = createElementOnCanvasWithoutDuration(elementName);
218 } catch (Exception e) {
223 if (canvasElement != null){
224 ExtentTestActions.log(Status.INFO, String.format("The element %s should now be on the canvas", elementName), actionDuration);
226 return canvasElement;
229 private CanvasElement createElementOnCanvasWithoutDuration(String elementDataTestId) throws Exception {
231 CompositionPage.searchForElement(elementDataTestId);
232 WebElement element = findClickElement(elementDataTestId);
233 ImmutablePair<Integer, Integer> freePosition = getFreePosition();
234 actions.moveToElement(element, 20, 20);
235 actions.clickAndHold();
236 actions.moveToElement(canvas, freePosition.left, freePosition.right);
239 GeneralUIUtils.ultimateWait();
240 String uniqueId = elementDataTestId + "_" + UUID.randomUUID().toString();
241 CanvasElement canvasElement = new CanvasElement(uniqueId, freePosition, elementDataTestId);
242 addCanvasElement(canvasElement);
243 GeneralUIUtils.ultimateWait();
244 return canvasElement;
246 catch (Exception e) {
247 System.out.println("Can't create element on canvas");
253 public CanvasElement createElementOnCanvas(LeftPanelCanvasItems canvasItem) throws Exception {
254 return createElementOnCanvas(canvasItem.getValue());
257 private ImmutablePair<Integer, Integer> getFreePosition() {
258 ImmutablePair<Integer, Integer> randomPosition = null;
259 boolean freePosition = false;
261 while (!freePosition) {
262 ImmutablePair<Integer, Integer> tempRandomPosition = getRandomPosition();
263 freePosition = !canvasElements.values().stream().map(e -> e.getLocation())
264 .filter(e -> Math.abs(e.left - tempRandomPosition.left) < minSpace
265 && Math.abs(e.right - tempRandomPosition.right) < minSpace)
266 .findAny().isPresent();
267 randomPosition = tempRandomPosition;
269 return randomPosition;
272 private ImmutablePair<Integer, Integer> getRandomPosition() {
274 Random random = new Random();
275 int xElement = random.nextInt(canvas.getSize().width - 2 * edgeBuffer - reduceCanvasWidthFactor) + edgeBuffer;
276 int yElement = random.nextInt(canvas.getSize().height - 2 * edgeBuffer) + edgeBuffer;
277 return new ImmutablePair<Integer, Integer>(xElement, yElement);
280 // Will work only if 2 elements are big sized (VF size), if one of the elements is Small use the function linkElements
281 public void linkElements(CanvasElement firstElement, CanvasElement secondElement) throws Exception {
282 ExtentTestActions.log(Status.INFO, String.format("Linking between the %s instance and the %s instance.", firstElement.getElementType(), secondElement.getElementType()));
283 drawSimpleLink(firstElement, secondElement);
284 selectReqAndCapAndConnect();
285 ExtentTestActions.log(Status.INFO, String.format("The instances %s and %s should now be connected.", firstElement.getElementType(), secondElement.getElementType()));
288 public void linkElements(CanvasElement firstElement, CircleSize firstElementSize, CanvasElement secondElement, CircleSize secondElementSize) throws Exception {
289 drawSimpleLink(firstElement,firstElementSize, secondElement,secondElementSize);
290 selectReqAndCapAndConnect();
291 ExtentTestActions.log(Status.INFO, String.format("The instances %s and %s should now be connected.", firstElement.getElementType(), secondElement.getElementType()));
294 private void selectReqAndCapAndConnect() throws Exception {
295 addFirstReqOrCapAndPressNext();
296 addFirstReqOrCapAndPressNext();
297 linkMenuClickOnFinishButton();
300 private void addFirstReqOrCapAndPressNext() throws Exception {
302 linkMenuClickOnNextButton();
305 private void addFirstReqOrCap() {
306 GeneralUIUtils.getWebElementsListByClassName(DataTestIdEnum.LinkMenuItems.LINK_ITEM_CAP_Or_REQ.getValue()).get(0).click();
309 private void linkMenuClickOnNextButton() throws Exception {
310 GeneralUIUtils.clickOnElementByText("Next");
311 GeneralUIUtils.ultimateWait();
314 private void linkMenuClickOnFinishButton() throws Exception {
315 GeneralUIUtils.clickOnElementByText("Finish");
316 GeneralUIUtils.ultimateWait();
320 private void drawSimpleLink(CanvasElement firstElement, CanvasElement secondElement) throws Exception {
321 int yOffset = CANVAS_VF_Y_OFFSET;
322 int xOffset = CANVAS_VF_X_OFFSET;
324 actions.moveToElement(canvas, firstElement.getLocation().left + xOffset,
325 firstElement.getLocation().right - yOffset);
327 actions.clickAndHold();
328 actions.moveToElement(canvas, secondElement.getLocation().left + xOffset, secondElement.getLocation().right - yOffset);
331 GeneralUIUtils.ultimateWait();
334 private void drawSimpleLink(CanvasElement firstElement, CircleSize firstElementSize, CanvasElement secondElement, CircleSize secondElementSize) throws Exception {
335 ExtentTestActions.log(Status.INFO, String.format("Linking between the %s instance and the %s instance.", firstElement.getElementType(), secondElement.getElementType()));
336 Integer yOffset = getCircleOffset(firstElementSize).right;
337 Integer xOffset = getCircleOffset(firstElementSize).left;
338 firstElement.getElementType();
341 actions.moveToElement(canvas, firstElement.getLocation().left + xOffset,
342 firstElement.getLocation().right - yOffset);
344 actions.clickAndHold();
346 yOffset = getCircleOffset(secondElementSize).right;
347 xOffset = getCircleOffset(secondElementSize).left;
349 actions.moveToElement(canvas, secondElement.getLocation().left + xOffset, secondElement.getLocation().right - yOffset);
352 GeneralUIUtils.ultimateWait();
355 private Pair<Integer,Integer> getCircleOffset(CircleSize circleSize)
357 Pair<Integer,Integer> circleSizes;
358 if(circleSize.equals(CircleSize.VF))
360 circleSizes = new Pair <Integer,Integer> (CANVAS_VF_X_OFFSET,CANVAS_VF_Y_OFFSET);
362 else if (circleSize.equals(CircleSize.NORMATIVE))
364 circleSizes = new Pair <Integer,Integer> (CANVAS_NORMATIVE_ELEMENT_X_OFFSET,CANVAS_NORMATIVE_ELEMENT_Y_OFFSET);
368 circleSizes = new Pair <Integer,Integer> (CANVAS_SERVICE_X_OFFSET,CANVAS_SERVICE_Y_OFFSET);
373 public String updateElementNameInCanvas(CanvasElement canvasElement, String newInstanceName) throws Exception {
374 GeneralUIUtils.ultimateWait();;
375 clickOnCanvaElement(canvasElement);
376 WebElement updateInstanceName = GeneralUIUtils.getWebElementBy(By.id("editPencil"));
377 updateInstanceName.click();
378 WebElement instanceNameField = GeneralUIUtils.getWebElementByTestID(DataTestIdEnum.GeneralCanvasItems.INSTANCE_NAME_FIELD.getValue());
379 String oldInstanceName = instanceNameField.getAttribute("value");
380 instanceNameField.clear();
381 instanceNameField.sendKeys(newInstanceName);
382 GeneralUIUtils.getWebElementByTestID(DataTestIdEnum.ModalItems.OK.getValue()).click();
383 GeneralUIUtils.ultimateWait();
384 GeneralUIUtils.waitForElementInVisibilityByTestId(By.className("w-sdc-modal-resource-instance-name"));
385 SetupCDTest.getExtendTest().log(Status.INFO, String.format("Name of element instance changed from %s to %s", oldInstanceName, newInstanceName));
386 return oldInstanceName;
390 * @param canvasElement
391 * Validate that instance was selected on right sidebar
393 public void validateInstanceSelected(CanvasElement canvasElement) {
395 long sumOfWaiting = 0;
396 long napPeriod = 200;
397 boolean isInstanceSelected;
399 isInstanceSelected = CompositionPage.getSelectedInstanceName().toLowerCase().contains(canvasElement.getElementType().toLowerCase());
401 if (!isInstanceSelected) {
403 TimeUnit.MILLISECONDS.sleep(napPeriod);
404 } catch (InterruptedException e) {
409 sumOfWaiting += napPeriod;
410 if (sumOfWaiting > maxWait) {
411 throw new SkipException(String.format("Open bug 342260, can't select instance properly, waited for %s seconds", (int) (maxWait/1000)));
413 } while (!isInstanceSelected);
416 public void validateLinkIsSelected() {
418 long sumOfWaiting = 0;
419 long napPeriod = 200;
420 boolean isInstanceSelected;
422 isInstanceSelected = GeneralUIUtils.isWebElementExistByClass("w-sdc-menu-item w-sdc-canvas-menu-item-view");
424 if (!isInstanceSelected) {
426 TimeUnit.MILLISECONDS.sleep(napPeriod);
427 } catch (InterruptedException e) {
432 sumOfWaiting += napPeriod;
433 if (sumOfWaiting > maxWait) {
434 Assert.fail(String.format("Can't select link properly, waited for %s seconds", (int) (maxWait/1000)));
436 } while (!isInstanceSelected);
439 private void selectReqCapByName(String reqCapName)
441 GeneralUIUtils.clickOnElementByText(reqCapName);
442 GeneralUIUtils.ultimateWait();
445 private void selectTypeOfReqCap(String dataTestId, String reqCapType)
447 GeneralUIUtils.selectByValueTextContained(dataTestId, reqCapType);
450 public void linkElementsAndSelectCapReqTypeAndCapReqName(CanvasElement firstElement, CircleSize firstElementSize, CanvasElement secondElement, CircleSize secondElementSize, ConnectionWizardPopUpObject connectionWizardPopUpObject) throws Exception {
451 SetupCDTest.getExtendTest().log(Status.INFO, String.format("Creating link between %s and %s", firstElement.getElementType(), secondElement.getElementType()));
452 drawSimpleLink(firstElement, firstElementSize, secondElement, secondElementSize);
453 selectTypeOfReqCap(DataTestIdEnum.LinkMenuItems.REQ_CAP_SELECT_DATA_TESTS_ID.getValue(),connectionWizardPopUpObject.getCapabilityTypeSecondItem());
454 addFirstReqOrCapAndPressNext();
455 selectReqCapByName(connectionWizardPopUpObject.getCapabilityNameSecondItem());
456 linkMenuClickOnNextButton();
457 linkMenuClickOnFinishButton();
460 public ImmutablePair<Integer, Integer> calcMidOfLink(ImmutablePair<Integer, Integer> location1, ImmutablePair<Integer, Integer> location2)
462 int x = (location1.getLeft()+location2.getLeft())/2;
463 int y = (location1.getRight()+location2.getRight())/2;
465 ImmutablePair<Integer, Integer> location = new ImmutablePair<>(x,y);