summaryrefslogtreecommitdiffstats
path: root/ui-ci-dev/src/main/java/org/openecomp/sdc/uici/tests/utilities/GeneralUIUtils.java
blob: 4414499f69759d835fdbe4b1bb0c7a4645cad639 (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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
package org.openecomp.sdc.uici.tests.utilities;

import static org.openecomp.sdc.common.datastructure.FunctionalInterfaces.retryMethodOnException;
import static org.openecomp.sdc.common.datastructure.FunctionalInterfaces.retryMethodOnResult;
import static org.openecomp.sdc.common.datastructure.FunctionalInterfaces.swallowException;

import java.net.MalformedURLException;
import java.net.URL;
import java.util.List;
import java.util.function.Function;
import java.util.function.Supplier;

import org.openecomp.sdc.uici.tests.datatypes.CreateAndUpdateStepsEnum;
import org.openecomp.sdc.uici.tests.datatypes.DataTestIdEnum;
import org.openecomp.sdc.uici.tests.datatypes.DataTestIdEnum.Dashboard;
import org.openecomp.sdc.uici.tests.execute.base.SetupCDTest;
import org.openqa.selenium.By;
import org.openqa.selenium.Platform;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.openqa.selenium.support.ui.ExpectedCondition;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.Select;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.Assert;

import org.openecomp.sdc.common.datastructure.FunctionalInterfaces;

public final class GeneralUIUtils {

	private static final int DEFAULT_WAIT_TIME_IN_SECONDS = 10;
	/**************** DRIVERS ****************/
	private static WebDriver driver;

	private GeneralUIUtils() {
		throw new UnsupportedOperationException();
	}
	
	/**
	 * Finding a component in the home screen by name and clicks on it
	 * Uses the search 
	 * 
	 * @param componentName
	 * @throws Exception
	 */
	public static void findComponentAndClick(String componentName) throws Exception {
		getWebElementWaitForVisible("main-menu-input-search").sendKeys(componentName);
		try {
			getWebElementWaitForClickable(componentName).click();
			GeneralUIUtils.waitForLoader();
			getWebElementWaitForVisible("formlifecyclestate");
		} catch (Exception e) {
			String msg = String.format("DID NOT FIND A COMPONENT NAMED %s", componentName);
			System.out.println(msg);
			Assert.fail(msg);
		}
	}

	public static WebElement getWebElementWaitForVisible(String dataTestId) {
		return getWebElementWaitForVisible(dataTestId, DEFAULT_WAIT_TIME_IN_SECONDS);
	}

	public static WebElement getWebElementWaitForVisible(String dataTestId, int time) {
		WebDriverWait wait = new WebDriverWait(getDriver(), time);
		ExpectedCondition<WebElement> visibilityOfElementLocated = ExpectedConditions
				.visibilityOfElementLocated(builDataTestIdLocator(dataTestId));
		WebElement webElement = wait.until(visibilityOfElementLocated);
		return webElement;
	}

	public static WebElement getWebElementWaitForClickable(String dataTestId) {
		WebDriverWait wait = new WebDriverWait(getDriver(), DEFAULT_WAIT_TIME_IN_SECONDS);
		ExpectedCondition<WebElement> condition = ExpectedConditions
				.elementToBeClickable(builDataTestIdLocator(dataTestId));
		WebElement webElement = wait.until(condition);
		return webElement;
	}

	private static By builDataTestIdLocator(String dataTestId) {
		return By.xpath("//*[@data-tests-id='" + dataTestId + "']");

	}

	/**
	 * Returns A list of Web Elements When they are all visible
	 * 
	 * @param dataTestId
	 * @return
	 */
	public static List<WebElement> getWebElementsListWaitForVisible(String dataTestId) {
		WebDriverWait wait = new WebDriverWait(getDriver(), DEFAULT_WAIT_TIME_IN_SECONDS);
		ExpectedCondition<List<WebElement>> visibilityOfAllElementsLocatedBy = ExpectedConditions
				.visibilityOfAllElementsLocatedBy(builDataTestIdLocator(dataTestId));
		return wait.until(visibilityOfAllElementsLocatedBy);
	}

	/**
	 * @deprecated Do not use. use {@link #getWebElementWaitForVisible(String)}
	 * @param dataTestId
	 * @return
	 */
	public static WebElement getWebElementByDataTestId(String dataTestId) {
		return driver.findElement(builDataTestIdLocator(dataTestId));
	}

	/**
	 * Checks if element is present with given dataTestsId
	 * 
	 * @param dataTestId
	 * @return
	 */
	public static boolean isElementPresent(String dataTestId) {
		final boolean isPresent = !driver.findElements(builDataTestIdLocator(dataTestId)).isEmpty();
		return isPresent;
	}

	public static void clickOnCreateEntityFromDashboard(String buttonId) {
		Supplier<WebElement> addVfButtonSipplier = () -> {
			// TODO ui-ci replace with data-test-id
			GeneralUIUtils.moveToHTMLElementByClassName("w-sdc-dashboard-card-new");
			return GeneralUIUtils.getWebElementByDataTestId(buttonId);
		};
		WebElement addVfButton = FunctionalInterfaces.retryMethodOnException(addVfButtonSipplier);
		addVfButton.click();
	}

	// this function located select list by the data-test-id value and the item
	// to be selected..
	public static Select getSelectList(String item, String dataTestId) {
		Select selectlist = new Select(driver.findElement(builDataTestIdLocator(dataTestId)));
		if (item != null) {
			selectlist.selectByVisibleText(item);
		}
		return selectlist;
	}

	// Define description area .
	public static String defineDescription(String descriptionText) {

		WebElement resourceDescriptionTextbox = GeneralUIUtils.getWebElementWaitForVisible("description");
		resourceDescriptionTextbox.clear();
		resourceDescriptionTextbox.sendKeys(descriptionText);

		return descriptionText;
	}

	/**
	 * Clicks on the create button waits for the create to finish and the check
	 * in button to appear
	 */
	public static void clickCreateButton() {
		GeneralUIUtils.waitForLoader();
		getWebElementWaitForClickable(DataTestIdEnum.LifeCyleChangeButtons.CREATE.getValue()).click();
		GeneralUIUtils.waitForLoader();
		WebElement successNotification = driver.findElement(By.className("ui-notification"));
		if (successNotification != null) {
			successNotification.click();
		}
		getWebElementWaitForVisible(DataTestIdEnum.LifeCyleChangeButtons.CHECK_IN.getValue());
	}

	public static void clickSaveButton() {
		WebElement createButton = getWebElementWaitForClickable(DataTestIdEnum.LifeCyleChangeButtons.CREATE.getValue());
		createButton.click();
	}

	public static void closeNotificatin() {
		WebElement notification = driver.findElement(By.className("ui-notification"));
		if (notification != null) {
			notification.click();
		}
	}
	public static void checkIn() {
		waitForLoader();
		getWebElementWaitForVisible(DataTestIdEnum.LifeCyleChangeButtons.CHECK_IN.getValue()).click();
		getWebElementWaitForVisible(DataTestIdEnum.ModalItems.ACCEP_TESTING_MESSAGE.getValue()).sendKeys("Check in !");
		getWebElementWaitForVisible(DataTestIdEnum.ModalItems.OK.getValue()).click();
		waitForLoader();
	}

	public static void moveToStep(CreateAndUpdateStepsEnum Stepname) {
		waitForLoader();
		getWebElementWaitForClickable(Stepname.getValue()).click();
		waitForLoader();
	}

	public static void sleep(int duration) {
		swallowException(() -> Thread.sleep(duration));
	}

	public static WebDriver getDriver() {
		return driver;
	}

	public static void initDriver() {
		try {
			System.out.println("opening browser");
			WebDriver webDriver;
			boolean remoteTesting = SetupCDTest.config.isRemoteTesting();
			if (!remoteTesting) {
				webDriver = new FirefoxDriver();
			} else {
				String remoteEnvIP = SetupCDTest.config.getRemoteTestingMachineIP();
				String remoteEnvPort = SetupCDTest.config.getRemoteTestingMachinePort();
				DesiredCapabilities cap = new DesiredCapabilities();
				cap = DesiredCapabilities.firefox();
				cap.setPlatform(Platform.WINDOWS);
				cap.setBrowserName("firefox");

				String remoteNodeUrl = String.format(SetupCDTest.SELENIUM_NODE_URL, remoteEnvIP, remoteEnvPort);
				webDriver = new RemoteWebDriver(new URL(remoteNodeUrl), cap);

			}
			driver = webDriver;

		} catch (MalformedURLException e) {
			throw new RuntimeException(e);
		}

	}

	/**
	 * waits until either loader finishes or 10 seconds has passed.<br>
	 * If 10 seconds has passed and loader didn't finish throws
	 * LoaderStuckException.<br>
	 */
	public static void waitForLoader() {
		waitForLoader(10);
	}

	/**
	 * waits until either loader finishes or maxWaitTimeInSeconds has
	 * passed.<br>
	 * If maxWaitTimeInSeconds has passed and loader didn't finish throws
	 * LoaderStuckException.<br>
	 * 
	 * @param maxWaitTimeInSeconds
	 */
	public static void waitForLoader(int maxWaitTimeInSeconds) {
		long maxWaitTimeMS = maxWaitTimeInSeconds * 1000L;
		Boolean loaderIsRunning = retryMethodOnResult(
				() -> isElementPresent(DataTestIdEnum.GeneralSection.LOADER.getValue()),
				isLoaderPresent -> !isLoaderPresent, maxWaitTimeMS, 50);
		if (loaderIsRunning) {
			throw new LoaderStuckException(
					"UI Loader is stuck, max wait time of " + maxWaitTimeInSeconds + " seconds has passed.");
		}

	}

	private static class LoaderStuckException extends RuntimeException {
		private static final long serialVersionUID = 1L;

		private LoaderStuckException(String message) {
			super(message);
		}
	}

	/**
	 * Move to HTML element by class name. When moving to the HTML element, it
	 * will raise hover event.
	 * 
	 * @param className
	 */
	public static void moveToHTMLElementByClassName(String className) {
		Actions actions = new Actions(getDriver());
		final WebElement createButtonsArea = getDriver().findElement(By.className(className));
		actions.moveToElement(createButtonsArea).perform();
	}

	/**
	 * Move to HTML element by element id. When moving to the HTML element, it
	 * will raise hover event.
	 * 
	 * @param className
	 */
	public static void moveToHTMLElementByDataTestId(String dataTestId) {
		Actions actions = new Actions(getDriver());
		final WebElement createButtonsArea = getWebElementByDataTestId(dataTestId);
		actions.moveToElement(createButtonsArea).perform();
	}

	public static void defineVendorName(String resourceVendorName) {
		// TODO ui-ci replace with Enum
		WebElement resourceVendorNameTextbox = getWebElementWaitForVisible("vendorName");
		resourceVendorNameTextbox.clear();
		resourceVendorNameTextbox.sendKeys(resourceVendorName);
	}

	public static String defineUserId(String userId) {
		// TODO ui-ci replace with Enum
		WebElement resourceTagsTextbox = getWebElementWaitForVisible("userId");
		resourceTagsTextbox.clear();
		resourceTagsTextbox.sendKeys(userId);
		return userId;
	}

	public static void clickAddComponent(Dashboard componentType) {
		Runnable clickAddTask = () -> {
			// TODO ui-ci replace with data-test-id
			moveToHTMLElementByClassName("w-sdc-dashboard-card-new");
			WebElement addVfButton = getWebElementByDataTestId(componentType.getValue());
			addVfButton.click();
		};
		retryMethodOnException(clickAddTask);
	}

	/**
	 * This method perform submit for testing process for existing service or
	 * resource.<br>
	 * It assumes it is activated when in the resource screen and the Submit For
	 * Testing button is available.
	 * 
	 * @param componentNameForMessage
	 *            TODO
	 */
	public static void submitForTestingElement(String componentNameForMessage) {
		waitForLoader();
		getWebElementWaitForVisible(DataTestIdEnum.LifeCyleChangeButtons.SUBMIT_FOR_TESTING.getValue()).click();
		waitForLoader();
		getWebElementWaitForVisible(DataTestIdEnum.ModalItems.SUMBIT_FOR_TESTING_MESSAGE.getValue())
				.sendKeys("Submit for testing for " + componentNameForMessage);
		waitForLoader();
		getWebElementWaitForClickable(DataTestIdEnum.ModalItems.OK.getValue()).click();
		waitForLoader();
		waitForElementToDisappear(DataTestIdEnum.ModalItems.OK.getValue());

	}

	/**
	 * Waits Until elements disappears or until 10 seconds pass
	 * 
	 * @param dataTestId
	 */
	public static void waitForElementToDisappear(String dataTestId) {
		Supplier<Boolean> elementPresenseChecker = () -> GeneralUIUtils.isElementPresent(dataTestId);
		Function<Boolean, Boolean> verifier = isElementPresent -> !isElementPresent;
		FunctionalInterfaces.retryMethodOnResult(elementPresenseChecker, verifier);

	}

}