re base code
[sdc.git] / ui-ci / src / main / java / org / openecomp / sdc / ci / tests / datatypes / CanvasManager.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
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
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
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=========================================================
19  */
20
21 package org.openecomp.sdc.ci.tests.datatypes;
22
23 import com.aventstack.extentreports.Status;
24 import com.clearspring.analytics.util.Pair;
25 import com.google.gson.JsonElement;
26 import com.google.gson.JsonParser;
27 import org.apache.commons.lang3.tuple.ImmutablePair;
28 import org.openecomp.sdc.ci.tests.datatypes.DataTestIdEnum.LeftPanelCanvasItems;
29 import org.openecomp.sdc.ci.tests.datatypes.enums.CircleSize;
30 import org.openecomp.sdc.ci.tests.execute.setup.ExtentTestActions;
31 import org.openecomp.sdc.ci.tests.execute.setup.SetupCDTest;
32 import org.openecomp.sdc.ci.tests.pages.CompositionPage;
33 import org.openecomp.sdc.ci.tests.pages.PropertiesAssignmentPage;
34 import org.openecomp.sdc.ci.tests.pages.PropertyNameBuilder;
35 import org.openecomp.sdc.ci.tests.utilities.GeneralUIUtils;
36 import org.openqa.selenium.By;
37 import org.openqa.selenium.StaleElementReferenceException;
38 import org.openqa.selenium.WebElement;
39 import org.openqa.selenium.interactions.Actions;
40 import org.testng.Assert;
41 import org.testng.SkipException;
42
43 import java.util.*;
44 import java.util.concurrent.TimeUnit;
45 import java.util.stream.Collectors;
46
47 public final class CanvasManager {
48         private Map<String, CanvasElement> canvasElements;
49         private Actions actions;
50         private WebElement canvas;
51         private int reduceCanvasWidthFactor;
52         private CanvasElement canvasElement;
53         // Offsets Are used to find upper right corner of canvas element in order to
54         // connect links
55         private static final int CANVAS_VF_Y_OFFSET = 30;
56         private static final int CANVAS_VF_X_OFFSET = 18; // 14 - 27
57
58         private static final int CANVAS_NORMATIVE_ELEMENT_Y_OFFSET = 12;
59         private static final int CANVAS_NORMATIVE_ELEMENT_X_OFFSET = 7;
60
61     private static final int CANVAS_SERVICE_Y_OFFSET = 27;
62         private static final int CANVAS_SERVICE_X_OFFSET = 16;
63
64         private CanvasManager() {
65                 canvasElements = new HashMap<>();
66                 actions = new Actions(GeneralUIUtils.getDriver());
67                 canvas = GeneralUIUtils.getWebElementByTestID(DataTestIdEnum.GeneralCanvasItems.CANVAS.getValue());
68                 try {
69                         WebElement webElement = GeneralUIUtils
70                                         .getWebElementByTestID(DataTestIdEnum.GeneralCanvasItems.CANVAS_RIGHT_PANEL.getValue());
71                         reduceCanvasWidthFactor = webElement.getSize().width;
72                 } catch (Exception e) {
73                         reduceCanvasWidthFactor = 0;
74                 }
75         }
76
77         public static CanvasManager getCanvasManager() {
78                 return new CanvasManager();
79         }
80
81         public List<CanvasElement> getCanvasElements() {
82                 return canvasElements.values().stream().collect(Collectors.toList());
83         }
84
85         private void addCanvasElement(CanvasElement element) {
86                 String prefix = element.getElementType();
87                 List<CanvasElement> canvasElementsFromSameTemplate = new ArrayList<>();
88                 
89                 // collect all elements from from same template
90                 for(CanvasElement currElement:canvasElements.values()){
91                         if(currElement.getElementNameOnCanvas().toLowerCase().startsWith(prefix.toLowerCase())){
92                                 canvasElementsFromSameTemplate.add(currElement);
93                         }
94                 }
95                 
96                 // match element name to actual name on canvas
97                 if( canvasElementsFromSameTemplate.size() > 0){
98                         String newName = prefix + " " + canvasElementsFromSameTemplate.size();
99                         element.setElementNameOnCanvas(newName);
100                 }
101                 
102                 canvasElements.put(element.getUniqueId(), element);
103         }
104
105         private void moveElementOnCanvas(CanvasElement canvasElement, ImmutablePair<Integer, Integer> newLocation)
106                         throws Exception {
107                 GeneralUIUtils.waitForLoader();
108                 actions.moveToElement(canvas, canvasElement.getLocation().left, canvasElement.getLocation().right);
109                 actions.clickAndHold();
110                 actions.moveToElement(canvas, newLocation.left, newLocation.right);
111                 actions.release();
112                 actions.perform();
113                 canvasElement.setLocation(newLocation);
114                 GeneralUIUtils.waitForLoader();
115
116         }
117
118         public void moveToFreeLocation(String containerName) {
119                 int maxWait = 5000;
120                 int sumOfWaiting = 0;
121                 int napPeriod = 200;
122                 boolean isKeepWaiting = false;
123                 while (!isKeepWaiting) {
124                         ImmutablePair<Integer, Integer> freePosition = getFreePosition();
125                         actions.moveToElement(canvas, freePosition.left, freePosition.right);
126                         actions.clickAndHold();
127                         actions.release();
128                         actions.perform();
129                         isKeepWaiting = GeneralUIUtils.getWebElementByTestID(DataTestIdEnum.CompositionRightPanel.COMPONENT_TITLE.getValue()).getText()
130                                         .equals(containerName);
131                         sumOfWaiting += napPeriod;
132                         if (sumOfWaiting > maxWait) {
133                                 Assert.fail("Can't click on VF");
134                         }
135                 }
136         }
137
138         public void clickOnCanvaElement(CanvasElement canvasElement) {
139 //              actions.moveToElement(canvas, canvasElement.getLocation().left, canvasElement.getLocation().right);
140                 ImmutablePair<Integer, Integer> coordinates = getElementCoordinates(canvasElement.getElementNameOnCanvas());
141                 actions.moveToElement(canvas, coordinates.left, coordinates.right);
142                 actions.clickAndHold();
143                 actions.release();
144                 actions.perform();
145                 GeneralUIUtils.ultimateWait();
146                 actions.click().perform();
147                 GeneralUIUtils.ultimateWait();
148
149             validateInstanceSelected(canvasElement);
150                 ExtentTestActions.log(Status.INFO, String.format("Canvas element %s selected", canvasElement.getElementType()));
151         }
152
153         public void openLinkPopupReqsCapsConnection(CanvasElement canvasElement)
154         {
155                 ExtentTestActions.log(Status.INFO, "Open Link popup");
156                 clickOnCanvasLink(canvasElement);
157                 int x = canvasElement.getLocation().getLeft() + 30; // view button x delta
158                 int y = canvasElement.getLocation().getRight() + 11; // view button y delta
159                 clickOnCanvasPosition(x,y);
160                 GeneralUIUtils.ultimateWait();
161         }
162         
163         public void openLinkPopupReqsCapsConnection(CanvasElement sourceElement, CanvasElement destElement)
164         {
165                 ExtentTestActions.log(Status.INFO, "Open Link popup");
166                 ImmutablePair<Integer, Integer> sourceCoordinates = getElementCoordinates(sourceElement.getElementNameOnCanvas());
167                 ImmutablePair<Integer, Integer> destCoordinates = getElementCoordinates(destElement.getElementNameOnCanvas());
168                 ImmutablePair<Integer, Integer> linkPosition = calcMidOfLink(sourceCoordinates, destCoordinates);
169                 
170                 clickOnCanvasPosition(linkPosition.left, linkPosition.right); // click on link
171                 int x = linkPosition.left + 30;
172                 int y = linkPosition.right + 11;
173                 clickOnCanvasPosition(x,y); // click on view popup
174                 GeneralUIUtils.ultimateWait();
175         }
176         
177         public void closeLinkPopupReqsCapsConnection()
178         {
179                 GeneralUIUtils.clickOnElementByTestId("Cancel");
180 //              GeneralUIUtils.ultimateWait();
181         }
182
183         public void clickSaveOnLinkPopup()
184         {
185                 ExtentTestActions.log(Status.INFO, "Click save on link popup");
186                 GeneralUIUtils.clickOnElementByTestId("Save");
187 //              GeneralUIUtils.ultimateWait();
188         }
189
190         public void deleteLinkPopupReqsCapsConnection(CanvasElement canvasElement)
191         {
192                 ExtentTestActions.log(Status.INFO, "Delete Link ");
193                 clickOnCanvasLink(canvasElement);
194                 int x = canvasElement.getLocation().getLeft() + 30; // delete button x delta
195                 int y = canvasElement.getLocation().getRight() + 30; // delete button x delta
196                 clickOnCanvasPosition(x,y);
197         }
198         
199         public void deleteLinkPopupReqsCapsConnection(CanvasElement sourceElement, CanvasElement destElement)
200         {
201                 ExtentTestActions.log(Status.INFO, "Delete Link ");
202                 ImmutablePair<Integer, Integer> sourceCoordinates = getElementCoordinates(sourceElement.getElementNameOnCanvas());
203                 ImmutablePair<Integer, Integer> destCoordinates = getElementCoordinates(destElement.getElementNameOnCanvas());
204                 ImmutablePair<Integer, Integer> linkPosition = calcMidOfLink(sourceCoordinates, destCoordinates);
205                 clickOnCanvasPosition(linkPosition.left, linkPosition.right); // click on link
206                 int x = linkPosition.left + 30; // delete button x delta
207                 int y = linkPosition.right + 30; // delete button y delta
208                 clickOnCanvasPosition(x,y);
209         }
210
211         public void clickOnCanvasLink(CanvasElement canvasElement) {
212                 actions.moveToElement(canvas, canvasElement.getLocation().left, canvasElement.getLocation().right);
213                 actions.click().perform();
214                 GeneralUIUtils.ultimateWait();
215         }
216
217         public void clickOnCanvasPosition(int x, int y) {
218
219                 try {
220                         actions.moveToElement(canvas, x, y);
221                         actions.click();
222                         actions.perform();
223                         GeneralUIUtils.ultimateWait();
224                 }
225                 catch (Exception e)
226                 {
227                         System.out.println(e);
228                 }
229         }
230
231         public void moveElementOnCanvas(CanvasElement canvasElement) throws Exception {
232                 moveElementOnCanvas(canvasElement, getFreePosition());
233         }
234
235         public void deleteElementFromCanvas(CanvasElement canvasElement) throws Exception {
236                 GeneralUIUtils.waitForLoader();
237                 actions.moveToElement(canvas, canvasElement.getLocation().left, canvasElement.getLocation().right);
238                 actions.click();
239                 actions.perform();
240                 ExtentTestActions.log(Status.INFO, String.format("Removing canvas element %s ", canvasElement.getElementType()));
241                 GeneralUIUtils.getWebElementByTestID(DataTestIdEnum.GeneralCanvasItems.DELETE_INSTANCE_BUTTON.getValue())
242                                 .click();
243                 GeneralUIUtils.getWebElementByTestID(DataTestIdEnum.ModalItems.DELETE_INSTANCE_CANCEL.getValue()).click();
244                 GeneralUIUtils.getWebElementByTestID(DataTestIdEnum.GeneralCanvasItems.DELETE_INSTANCE_BUTTON.getValue())
245                                 .click();
246                 GeneralUIUtils.getWebElementByTestID(DataTestIdEnum.ModalItems.DELETE_INSTANCE_OK.getValue()).click();
247                 canvasElements.remove(canvasElement.getUniqueId());
248                 GeneralUIUtils.ultimateWait();
249                 if (canvasElement.getElementType().contains("-")){
250                         ExtentTestActions.log(Status.INFO, String.format("Canvas element %s is removed", canvasElement.getElementType().split("-")[4]));
251                 }
252                 else{
253                         ExtentTestActions.log(Status.INFO, String.format("Canvas element %s is removed", canvasElement.getElementType()));
254                 }
255         }
256
257         private WebElement findClickElement(String dataTestId) {
258                 int attempts = 0;
259                 while (attempts < 2) {
260                         try {
261                                 return GeneralUIUtils.getWebElementByTestID(dataTestId);
262                         } catch (StaleElementReferenceException e) {
263                         }
264                         attempts++;
265                 }
266                 return null;
267         }
268
269         public CanvasElement createElementOnCanvas(String elementName) throws Exception {
270                 String actionDuration = GeneralUIUtils.getActionDuration(() -> {
271                         try {
272                                 canvasElement = createElementOnCanvasWithoutDuration(elementName);
273                         } catch (Exception e) {
274                                 e.printStackTrace();
275                         }
276                 });
277
278                 if (canvasElement != null){
279                         ExtentTestActions.log(Status.INFO, String.format("The element %s should now be on the canvas", elementName), actionDuration);
280                 }
281                 return canvasElement;
282         }
283
284         private CanvasElement createElementOnCanvasWithoutDuration(String elementDataTestId) throws Exception {
285                 try {
286                         CompositionPage.searchForElement(elementDataTestId);
287                         WebElement element = findClickElement(elementDataTestId);
288                         ImmutablePair<Integer, Integer> freePosition = getFreePosition();
289                         actions.moveToElement(element, 20, 20);
290                         actions.clickAndHold();
291                         actions.moveToElement(canvas, freePosition.left, freePosition.right);
292                         actions.release();
293                         actions.perform();
294                         GeneralUIUtils.ultimateWait();
295                         String uniqueId = elementDataTestId + "_" + UUID.randomUUID().toString();
296                         CanvasElement canvasElement = new CanvasElement(uniqueId, freePosition, elementDataTestId);
297                         addCanvasElement(canvasElement);
298                         GeneralUIUtils.ultimateWait();
299                         return canvasElement;
300                 }
301                 catch (Exception e) {
302                         System.out.println("Can't create element on canvas");
303                         e.printStackTrace();
304                 }
305                 return null;
306         }
307
308         public CanvasElement createElementOnCanvas(LeftPanelCanvasItems canvasItem) throws Exception {
309                 return createElementOnCanvas(canvasItem.getValue());
310         }
311
312         private ImmutablePair<Integer, Integer> getFreePosition() {
313                 ImmutablePair<Integer, Integer> randomPosition = null;
314                 boolean freePosition = false;
315                 int minSpace = 150;
316                 while (!freePosition) {
317                         ImmutablePair<Integer, Integer> tempRandomPosition = getRandomPosition();
318                         freePosition = !canvasElements.values().stream().map(e -> e.getLocation())
319                                         .filter(e -> Math.abs(e.left - tempRandomPosition.left) < minSpace
320                                                         && Math.abs(e.right - tempRandomPosition.right) < minSpace)
321                                         .findAny().isPresent();
322                         randomPosition = tempRandomPosition;
323                 }
324                 return randomPosition;
325         }
326
327         private ImmutablePair<Integer, Integer> getRandomPosition() {
328                 int edgeBuffer = 50;
329                 Random random = new Random();
330                 int xElement = random.nextInt(canvas.getSize().width - 2 * edgeBuffer - reduceCanvasWidthFactor) + edgeBuffer;
331                 int yElement = random.nextInt(canvas.getSize().height - 2 * edgeBuffer) + edgeBuffer;
332                 return new ImmutablePair<Integer, Integer>(xElement, yElement);
333         }
334
335         // Will work only if 2 elements are big sized (VF size), if one of the elements is Small use the function linkElements
336         public void linkElements(CanvasElement firstElement, CanvasElement secondElement) throws Exception {
337                 ExtentTestActions.log(Status.INFO, String.format("Linking between the %s instance and the %s instance.", firstElement.getElementType(), secondElement.getElementType()));
338                 drawSimpleLink(firstElement.getElementNameOnCanvas(), secondElement.getElementNameOnCanvas());
339                 selectReqAndCapAndConnect();
340                 ExtentTestActions.log(Status.INFO, String.format("The instances %s and %s should now be connected.", firstElement.getElementType(), secondElement.getElementType()));
341         }
342     
343         // old version, depricated
344         public void linkElements(CanvasElement firstElement, CircleSize firstElementSize, CanvasElement secondElement, CircleSize secondElementSize) throws Exception {
345                 drawSimpleLink(firstElement,firstElementSize, secondElement,secondElementSize);
346                 selectReqAndCapAndConnect();
347                 ExtentTestActions.log(Status.INFO, String.format("The instances %s and %s should now be connected.", firstElement.getElementType(), secondElement.getElementType()));
348         }
349         
350         public void linkElements(String firstElement, String secondElement) throws Exception {
351                 drawSimpleLink(firstElement, secondElement);
352                 selectReqAndCapAndConnect();
353                 ExtentTestActions.log(Status.INFO, String.format("The instances %s and %s should now be connected.", firstElement, secondElement));
354         }
355     
356         // use JS to get coordinates of elements
357         private void drawSimpleLink(String firstElement, String secondElement) {
358                 ImmutablePair<Integer, Integer> firstElementCoordinates = getGreenDotCoordinatesOfElement(firstElement);
359                 ImmutablePair<Integer, Integer> secondElementCoordinates = getElementCoordinates(secondElement);
360                 
361                 actions.moveToElement(canvas, firstElementCoordinates.left, firstElementCoordinates.right);
362                 actions.perform();
363                 actions.moveToElement(canvas, firstElementCoordinates.left, firstElementCoordinates.right);
364                 actions.clickAndHold();
365                 actions.moveToElement(canvas, secondElementCoordinates.left, secondElementCoordinates.right);
366                 actions.release();
367                 actions.perform();
368                 GeneralUIUtils.ultimateWait();
369         }
370
371         private void selectReqAndCapAndConnect() throws Exception {
372                 addFirstReqOrCapAndPressNext();
373                 addFirstReqOrCapAndPressNext();
374                 linkMenuClickOnFinishButton();
375         }
376
377         private void addFirstReqOrCapAndPressNext() throws Exception {
378                 addFirstReqOrCap();
379                 linkMenuClickOnNextButton();
380         }
381
382         private void addFirstReqOrCap() {
383                 GeneralUIUtils.getWebElementsListByClassName(DataTestIdEnum.LinkMenuItems.LINK_ITEM_CAP_Or_REQ.getValue()).get(0).click();
384         }
385
386         private void linkMenuClickOnNextButton() throws Exception {
387                 GeneralUIUtils.clickOnElementByText("Next");
388                 GeneralUIUtils.ultimateWait();
389         }
390
391         private void linkMenuClickOnFinishButton() throws Exception {
392                 GeneralUIUtils.clickOnElementByText("Finish");
393                 GeneralUIUtils.ultimateWait();
394         }
395
396
397         private void drawSimpleLink(CanvasElement firstElement, CanvasElement secondElement) throws Exception {
398                 int yOffset = CANVAS_VF_Y_OFFSET;
399                 int xOffset = CANVAS_VF_X_OFFSET;
400
401                 actions.moveToElement(canvas, firstElement.getLocation().left + xOffset,
402                                 firstElement.getLocation().right - yOffset);
403
404                 actions.clickAndHold();
405                 actions.moveToElement(canvas, secondElement.getLocation().left + xOffset, secondElement.getLocation().right - yOffset);
406                 actions.release();
407                 actions.perform();
408                 GeneralUIUtils.ultimateWait();
409         }
410
411         private void drawSimpleLink(CanvasElement firstElement, CircleSize firstElementSize, CanvasElement secondElement, CircleSize secondElementSize) throws Exception {
412                 ExtentTestActions.log(Status.INFO, String.format("Linking between the %s instance and the %s instance.", firstElement.getElementType(), secondElement.getElementType()));
413                 Integer yOffset = getCircleOffset(firstElementSize).right;
414                 Integer xOffset = getCircleOffset(firstElementSize).left;
415                 firstElement.getElementType();
416
417
418                                 actions.moveToElement(canvas, firstElement.getLocation().left + xOffset,
419                                 firstElement.getLocation().right - yOffset);
420
421                 actions.clickAndHold();
422
423                 yOffset = getCircleOffset(secondElementSize).right;
424                 xOffset = getCircleOffset(secondElementSize).left;
425
426                 actions.moveToElement(canvas, secondElement.getLocation().left + xOffset, secondElement.getLocation().right - yOffset);
427                 actions.release();
428                 actions.build();
429                 actions.perform();
430                 GeneralUIUtils.ultimateWait();
431         }
432
433         private Pair<Integer,Integer> getCircleOffset(CircleSize circleSize)
434         {
435                 Pair<Integer,Integer> circleSizes;
436                 if(circleSize.equals(CircleSize.VF))
437                 {
438                         circleSizes = new Pair <Integer,Integer> (CANVAS_VF_X_OFFSET,CANVAS_VF_Y_OFFSET);
439                 }
440                 else if (circleSize.equals(CircleSize.NORMATIVE))
441                 {
442                         circleSizes = new Pair <Integer,Integer> (CANVAS_NORMATIVE_ELEMENT_X_OFFSET,CANVAS_NORMATIVE_ELEMENT_Y_OFFSET);
443                 }
444                 else
445                 {
446                         circleSizes = new Pair <Integer,Integer> (CANVAS_SERVICE_X_OFFSET,CANVAS_SERVICE_Y_OFFSET);
447                 }
448                 return circleSizes;
449         }
450
451         public String updateElementNameInCanvas(CanvasElement canvasElement, String newInstanceName) throws Exception {
452                 GeneralUIUtils.ultimateWait();;
453                 clickOnCanvaElement(canvasElement);
454                 GeneralUIUtils.getWebElementBy(By.id("editPencil")).click();
455                 GeneralUIUtils.getWebElementByTestID(DataTestIdEnum.ModalItems.RENAME_INSTANCE_CANCEL.getValue()).click();
456                 GeneralUIUtils.getWebElementBy(By.id("editPencil")).click();
457                 WebElement instanceNameField = GeneralUIUtils.getWebElementByTestID(DataTestIdEnum.GeneralCanvasItems.INSTANCE_NAME_FIELD.getValue());
458                 String oldInstanceName = instanceNameField.getAttribute("value");
459                 instanceNameField.clear();
460                 instanceNameField.sendKeys(newInstanceName);
461                 GeneralUIUtils.getWebElementByTestID(DataTestIdEnum.ModalItems.RENAME_INSTANCE_OK.getValue()).click();
462                 GeneralUIUtils.ultimateWait();
463                 GeneralUIUtils.waitForElementInVisibilityByTestId(By.className("w-sdc-modal-resource-instance-name"));
464                 SetupCDTest.getExtendTest().log(Status.INFO, String.format("Name of element instance changed from %s to %s", oldInstanceName, newInstanceName));
465                 return oldInstanceName;
466         }
467
468         /**
469          * @param canvasElement
470          * Validate that instance was selected on right sidebar
471          */
472         public void validateInstanceSelected(CanvasElement canvasElement) {
473                 long maxWait = 5000;
474                 long sumOfWaiting = 0;
475                 long napPeriod = 200;
476                 boolean isInstanceSelected;
477                 do {
478                         isInstanceSelected = CompositionPage.getSelectedInstanceName().toLowerCase().contains(canvasElement.getElementType().toLowerCase());
479
480                         if (!isInstanceSelected) {
481                                 try {
482                                         TimeUnit.MILLISECONDS.sleep(napPeriod);
483                                 } catch (InterruptedException e) {
484                                         e.printStackTrace();
485                                 }
486                         }
487
488                         sumOfWaiting += napPeriod;
489                         if (sumOfWaiting > maxWait) {
490                                 throw new SkipException(String.format("Bug 342260, can't select instance properly, waited for %s seconds after click on instance", (int) (maxWait/1000)));
491                         }
492                 } while (!isInstanceSelected);
493         }
494
495         public void validateLinkIsSelected() {
496                 long maxWait = 5000;
497                 long sumOfWaiting = 0;
498                 long napPeriod = 200;
499                 boolean isInstanceSelected;
500                 do {
501                         isInstanceSelected = GeneralUIUtils.isWebElementExistByClass("w-sdc-menu-item w-sdc-canvas-menu-item-view");
502
503                         if (!isInstanceSelected) {
504                                 try {
505                                         TimeUnit.MILLISECONDS.sleep(napPeriod);
506                                 } catch (InterruptedException e) {
507                                         e.printStackTrace();
508                                 }
509                         }
510
511                         sumOfWaiting += napPeriod;
512                         if (sumOfWaiting > maxWait) {
513                                 Assert.fail(String.format("Can't select link properly, waited for %s seconds", (int) (maxWait/1000)));          
514                         }
515                 } while (!isInstanceSelected);
516         }
517
518         private void selectReqCapByName(String reqCapName)
519         {
520             GeneralUIUtils.clickOnElementByText(reqCapName);
521         GeneralUIUtils.ultimateWait();
522     }
523
524         private void selectTypeOfReqCap(String dataTestId, String reqCapType)
525         {
526         GeneralUIUtils.selectByValueTextContained(dataTestId, reqCapType);
527         }
528
529         public void linkElementsAndSelectCapReqTypeAndCapReqName(CanvasElement firstElement, CanvasElement secondElement, ConnectionWizardPopUpObject connectionWizardPopUpObject) throws Exception {
530         SetupCDTest.getExtendTest().log(Status.INFO, String.format("Creating link between %s and %s", firstElement.getElementType(), secondElement.getElementType()));
531 //              drawSimpleLink(firstElement, firstElementSize, secondElement, secondElementSize);
532         drawSimpleLink(firstElement.getElementNameOnCanvas(), secondElement.getElementNameOnCanvas());
533         selectTypeOfReqCap(DataTestIdEnum.LinkMenuItems.REQ_CAP_SELECT_DATA_TESTS_ID.getValue(),connectionWizardPopUpObject.getCapabilityTypeSecondItem());
534         addFirstReqOrCapAndPressNext();
535         selectReqCapByName(connectionWizardPopUpObject.getCapabilityNameSecondItem());
536         linkMenuClickOnNextButton();
537         linkMenuClickOnFinishButton();
538     }
539
540     public Map<String, String> linkElementsWithCapPropAssignment(CanvasElement firstElement, CanvasElement secondElement, ConnectionWizardPopUpObject connectionWizardPopUpObject) throws Exception {
541         SetupCDTest.getExtendTest().log(Status.INFO, String.format("Creating link between %s and %s", firstElement.getElementType(), secondElement.getElementType()));
542         drawSimpleLink(firstElement.getElementNameOnCanvas(), secondElement.getElementNameOnCanvas());
543         selectTypeOfReqCap(DataTestIdEnum.LinkMenuItems.REQ_CAP_SELECT_DATA_TESTS_ID.getValue(),connectionWizardPopUpObject.getCapabilityTypeSecondItem());
544         addFirstReqOrCapAndPressNext();
545         selectReqCapByName(connectionWizardPopUpObject.getCapabilityNameSecondItem());
546         linkMenuClickOnNextButton();
547         Map<String, String> mapOfValues = connectionWizardAssignCapPropValues();
548         linkMenuClickOnFinishButton();
549         Thread.sleep(5000);
550                 return mapOfValues;
551     }
552
553
554
555     public Map<String, String> connectionWizardAssignCapPropValues() throws Exception{
556                 //get list of capability property value fields data-tests-ids in connection wizard
557                 List<String> valueField = getListOfValueFieldIDs();
558         //get map of field ids and their values, fill in values if empty
559                 Map<String, String> propValues = getMapOfCapPropValues(valueField, true);
560         return propValues;
561     }
562
563     public Map<String, String> connectionWizardCollectCapPropValues() throws Exception{
564         //get list of capability property value fields data-tests-ids in connection wizard
565         List<String> valueField = getListOfValueFieldIDs();
566         //get map of field ids and their values, collect existing values
567         Map<String, String> propValues = getMapOfCapPropValues(valueField, false);
568         return propValues;
569     }
570
571         private List<String> getListOfValueFieldIDs() {
572                 String propName = GeneralUIUtils.getWebElementsListByContainsClassName("multiline-ellipsis-content").get(0).getText();
573                 List<WebElement> valueNameElement = GeneralUIUtils.findElementsByXpath("//div[@class='dynamic-property-row nested-level-1']/div[1]");
574                 List<String> valueName = new ArrayList<>();
575                 for(int i=0; i < valueNameElement.size(); i++){
576                         valueName.add(valueNameElement.get(i).getText());
577                 }
578                 //get list of value field names as appear in data-tests-id
579                 List<String> valueField = new ArrayList<>();
580                 for(int i=0; i < valueName.size(); i++){
581                         valueField.add(PropertyNameBuilder.buildIComplexField(propName, valueName.get(i)));
582                 }
583                 return valueField;
584         }
585
586         private Map<String, String> getMapOfCapPropValues(List<String> valueField, boolean isValueAssign) throws Exception {
587         SetupCDTest.getExtendTest().log(Status.INFO, String.format("Assigning values to properties of capabilities and/or collecting existing ones"));
588                 Map<String, String> propValues = new HashMap<>();
589                 for(int i=0; i < valueField.size(); i++){
590             String fieldId = valueField.get(i);
591             if(GeneralUIUtils.getWebElementByTestID(fieldId).getAttribute("value").isEmpty() && isValueAssign) {
592                 //add value and put into map
593                 propValues.put(fieldId, "value" + i);
594                 PropertiesAssignmentPage.editPropertyValue(fieldId, "value" + i);
595             } else {
596                 //put existing value into map
597                 propValues.put(fieldId, GeneralUIUtils.getWebElementByTestID(valueField.get(i)).getAttribute("value"));
598             }
599         }
600                 return propValues;
601         }
602
603
604         public ImmutablePair<Integer, Integer> calcMidOfLink(ImmutablePair<Integer, Integer> location1, ImmutablePair<Integer, Integer> location2)
605         {
606                 int x = (location1.getLeft()+location2.getLeft())/2;
607                 int y = (location1.getRight()+location2.getRight())/2;
608
609                 ImmutablePair<Integer, Integer> location = new ImmutablePair<>(x,y);
610                 return location;
611         }
612         
613         public ImmutablePair<Integer, Integer> getElementCoordinates(String elementName){
614                 Object position = GeneralUIUtils.getElementPositionOnCanvas(elementName);
615                 return converJSJsonToCoordinates(position);
616         }
617         
618         public ImmutablePair<Integer, Integer> getGreenDotCoordinatesOfElement(String elementName){
619                 Object position = GeneralUIUtils.getElementGreenDotPositionOnCanvas(elementName);
620                 return converJSJsonToCoordinates(position);
621         }
622
623         public ImmutablePair<Integer, Integer> converJSJsonToCoordinates(Object position) {
624                 JsonElement root  = new JsonParser().parse(position.toString());
625                 int xElement = root.getAsJsonObject().get("x").getAsInt();
626                 int yElement = root.getAsJsonObject().get("y").getAsInt();
627                 return new ImmutablePair<Integer, Integer>(xElement, yElement);
628         }
629 }