1 package org.openecomp.sdc.uici.tests.utilities;
3 import static org.openecomp.sdc.common.datastructure.FunctionalInterfaces.retryMethodOnException;
4 import static org.openecomp.sdc.common.datastructure.FunctionalInterfaces.retryMethodOnResult;
5 import static org.openecomp.sdc.common.datastructure.FunctionalInterfaces.swallowException;
7 import java.net.MalformedURLException;
10 import java.util.function.Function;
11 import java.util.function.Supplier;
13 import org.openecomp.sdc.uici.tests.datatypes.CreateAndUpdateStepsEnum;
14 import org.openecomp.sdc.uici.tests.datatypes.DataTestIdEnum;
15 import org.openecomp.sdc.uici.tests.datatypes.DataTestIdEnum.Dashboard;
16 import org.openecomp.sdc.uici.tests.execute.base.SetupCDTest;
17 import org.openqa.selenium.By;
18 import org.openqa.selenium.Platform;
19 import org.openqa.selenium.WebDriver;
20 import org.openqa.selenium.WebElement;
21 import org.openqa.selenium.firefox.FirefoxDriver;
22 import org.openqa.selenium.interactions.Actions;
23 import org.openqa.selenium.remote.DesiredCapabilities;
24 import org.openqa.selenium.remote.RemoteWebDriver;
25 import org.openqa.selenium.support.ui.ExpectedCondition;
26 import org.openqa.selenium.support.ui.ExpectedConditions;
27 import org.openqa.selenium.support.ui.Select;
28 import org.openqa.selenium.support.ui.WebDriverWait;
29 import org.testng.Assert;
31 import org.openecomp.sdc.common.datastructure.FunctionalInterfaces;
33 public final class GeneralUIUtils {
35 private static final int DEFAULT_WAIT_TIME_IN_SECONDS = 10;
36 /**************** DRIVERS ****************/
37 private static WebDriver driver;
39 private GeneralUIUtils() {
40 throw new UnsupportedOperationException();
44 * Finding a component in the home screen by name and clicks on it
47 * @param componentName
50 public static void findComponentAndClick(String componentName) throws Exception {
51 getWebElementWaitForVisible("main-menu-input-search").sendKeys(componentName);
53 getWebElementWaitForClickable(componentName).click();
54 GeneralUIUtils.waitForLoader();
55 getWebElementWaitForVisible("formlifecyclestate");
56 } catch (Exception e) {
57 String msg = String.format("DID NOT FIND A COMPONENT NAMED %s", componentName);
58 System.out.println(msg);
63 public static WebElement getWebElementWaitForVisible(String dataTestId) {
64 return getWebElementWaitForVisible(dataTestId, DEFAULT_WAIT_TIME_IN_SECONDS);
67 public static WebElement getWebElementWaitForVisible(String dataTestId, int time) {
68 WebDriverWait wait = new WebDriverWait(getDriver(), time);
69 ExpectedCondition<WebElement> visibilityOfElementLocated = ExpectedConditions
70 .visibilityOfElementLocated(builDataTestIdLocator(dataTestId));
71 WebElement webElement = wait.until(visibilityOfElementLocated);
75 public static WebElement getWebElementWaitForClickable(String dataTestId) {
76 WebDriverWait wait = new WebDriverWait(getDriver(), DEFAULT_WAIT_TIME_IN_SECONDS);
77 ExpectedCondition<WebElement> condition = ExpectedConditions
78 .elementToBeClickable(builDataTestIdLocator(dataTestId));
79 WebElement webElement = wait.until(condition);
83 private static By builDataTestIdLocator(String dataTestId) {
84 return By.xpath("//*[@data-tests-id='" + dataTestId + "']");
89 * Returns A list of Web Elements When they are all visible
94 public static List<WebElement> getWebElementsListWaitForVisible(String dataTestId) {
95 WebDriverWait wait = new WebDriverWait(getDriver(), DEFAULT_WAIT_TIME_IN_SECONDS);
96 ExpectedCondition<List<WebElement>> visibilityOfAllElementsLocatedBy = ExpectedConditions
97 .visibilityOfAllElementsLocatedBy(builDataTestIdLocator(dataTestId));
98 return wait.until(visibilityOfAllElementsLocatedBy);
102 * @deprecated Do not use. use {@link #getWebElementWaitForVisible(String)}
106 public static WebElement getWebElementByDataTestId(String dataTestId) {
107 return driver.findElement(builDataTestIdLocator(dataTestId));
111 * Checks if element is present with given dataTestsId
116 public static boolean isElementPresent(String dataTestId) {
117 final boolean isPresent = !driver.findElements(builDataTestIdLocator(dataTestId)).isEmpty();
121 public static void clickOnCreateEntityFromDashboard(String buttonId) {
122 Supplier<WebElement> addVfButtonSipplier = () -> {
123 // TODO ui-ci replace with data-test-id
124 GeneralUIUtils.moveToHTMLElementByClassName("w-sdc-dashboard-card-new");
125 return GeneralUIUtils.getWebElementByDataTestId(buttonId);
127 WebElement addVfButton = FunctionalInterfaces.retryMethodOnException(addVfButtonSipplier);
131 // this function located select list by the data-test-id value and the item
133 public static Select getSelectList(String item, String dataTestId) {
134 Select selectlist = new Select(driver.findElement(builDataTestIdLocator(dataTestId)));
136 selectlist.selectByVisibleText(item);
141 // Define description area .
142 public static String defineDescription(String descriptionText) {
144 WebElement resourceDescriptionTextbox = GeneralUIUtils.getWebElementWaitForVisible("description");
145 resourceDescriptionTextbox.clear();
146 resourceDescriptionTextbox.sendKeys(descriptionText);
148 return descriptionText;
152 * Clicks on the create button waits for the create to finish and the check
153 * in button to appear
155 public static void clickCreateButton() {
156 GeneralUIUtils.waitForLoader();
157 getWebElementWaitForClickable(DataTestIdEnum.LifeCyleChangeButtons.CREATE.getValue()).click();
158 GeneralUIUtils.waitForLoader();
160 getWebElementWaitForVisible(DataTestIdEnum.LifeCyleChangeButtons.CHECK_IN.getValue());
163 public static void closeNotificatin() {
164 WebElement notification = driver.findElement(By.className("ui-notification"));
165 if (notification != null) {
166 notification.click();
170 public static void clickSaveButton() {
171 WebElement createButton = getWebElementWaitForClickable(DataTestIdEnum.LifeCyleChangeButtons.CREATE.getValue());
172 createButton.click();
175 public static void checkIn() {
177 getWebElementWaitForVisible(DataTestIdEnum.LifeCyleChangeButtons.CHECK_IN.getValue()).click();
178 getWebElementWaitForVisible(DataTestIdEnum.ModalItems.ACCEP_TESTING_MESSAGE.getValue()).sendKeys("Check in !");
179 getWebElementWaitForVisible(DataTestIdEnum.ModalItems.OK.getValue()).click();
183 public static void moveToStep(CreateAndUpdateStepsEnum Stepname) {
185 getWebElementWaitForClickable(Stepname.getValue()).click();
189 public static void sleep(int duration) {
190 swallowException(() -> Thread.sleep(duration));
193 public static WebDriver getDriver() {
197 public static void initDriver() {
199 System.out.println("opening browser");
201 boolean remoteTesting = SetupCDTest.config.isRemoteTesting();
202 if (!remoteTesting) {
203 webDriver = new FirefoxDriver();
205 String remoteEnvIP = SetupCDTest.config.getRemoteTestingMachineIP();
206 String remoteEnvPort = SetupCDTest.config.getRemoteTestingMachinePort();
207 DesiredCapabilities cap = new DesiredCapabilities();
208 cap = DesiredCapabilities.firefox();
209 cap.setPlatform(Platform.WINDOWS);
210 cap.setBrowserName("firefox");
212 String remoteNodeUrl = String.format(SetupCDTest.SELENIUM_NODE_URL, remoteEnvIP, remoteEnvPort);
213 webDriver = new RemoteWebDriver(new URL(remoteNodeUrl), cap);
218 } catch (MalformedURLException e) {
219 throw new RuntimeException(e);
225 * waits until either loader finishes or 10 seconds has passed.<br>
226 * If 10 seconds has passed and loader didn't finish throws
227 * LoaderStuckException.<br>
229 public static void waitForLoader() {
234 * waits until either loader finishes or maxWaitTimeInSeconds has
236 * If maxWaitTimeInSeconds has passed and loader didn't finish throws
237 * LoaderStuckException.<br>
239 * @param maxWaitTimeInSeconds
241 public static void waitForLoader(int maxWaitTimeInSeconds) {
242 long maxWaitTimeMS = maxWaitTimeInSeconds * 1000L;
243 Boolean loaderIsRunning = retryMethodOnResult(
244 () -> isElementPresent(DataTestIdEnum.GeneralSection.LOADER.getValue()),
245 isLoaderPresent -> !isLoaderPresent, maxWaitTimeMS, 50);
246 if (loaderIsRunning) {
247 throw new LoaderStuckException(
248 "UI Loader is stuck, max wait time of " + maxWaitTimeInSeconds + " seconds has passed.");
253 private static class LoaderStuckException extends RuntimeException {
254 private static final long serialVersionUID = 1L;
256 private LoaderStuckException(String message) {
262 * Move to HTML element by class name. When moving to the HTML element, it
263 * will raise hover event.
267 public static void moveToHTMLElementByClassName(String className) {
268 Actions actions = new Actions(getDriver());
269 final WebElement createButtonsArea = getDriver().findElement(By.className(className));
270 actions.moveToElement(createButtonsArea).perform();
274 * Move to HTML element by element id. When moving to the HTML element, it
275 * will raise hover event.
279 public static void moveToHTMLElementByDataTestId(String dataTestId) {
280 Actions actions = new Actions(getDriver());
281 final WebElement createButtonsArea = getWebElementByDataTestId(dataTestId);
282 actions.moveToElement(createButtonsArea).perform();
285 public static void defineVendorName(String resourceVendorName) {
286 // TODO ui-ci replace with Enum
287 WebElement resourceVendorNameTextbox = getWebElementWaitForVisible("vendorName");
288 resourceVendorNameTextbox.clear();
289 resourceVendorNameTextbox.sendKeys(resourceVendorName);
292 public static String defineUserId(String UserId) {
293 // TODO ui-ci replace with Enum
294 WebElement resourceTagsTextbox = getWebElementWaitForVisible("contactId");
295 resourceTagsTextbox.clear();
296 resourceTagsTextbox.sendKeys(UserId);
300 public static void clickAddComponent(Dashboard componentType) {
301 Runnable clickAddTask = () -> {
302 // TODO ui-ci replace with data-test-id
303 moveToHTMLElementByClassName("w-sdc-dashboard-card-new");
304 WebElement addVfButton = getWebElementByDataTestId(componentType.getValue());
307 retryMethodOnException(clickAddTask);
311 * This method perform submit for testing process for existing service or
313 * It assumes it is activated when in the resource screen and the Submit For
314 * Testing button is available.
316 * @param componentNameForMessage
319 public static void submitForTestingElement(String componentNameForMessage) {
321 getWebElementWaitForVisible(DataTestIdEnum.LifeCyleChangeButtons.SUBMIT_FOR_TESTING.getValue()).click();
323 getWebElementWaitForVisible(DataTestIdEnum.ModalItems.SUMBIT_FOR_TESTING_MESSAGE.getValue())
324 .sendKeys("Submit for testing for " + componentNameForMessage);
326 getWebElementWaitForClickable(DataTestIdEnum.ModalItems.OK.getValue()).click();
328 waitForElementToDisappear(DataTestIdEnum.ModalItems.OK.getValue());
333 * Waits Until elements disappears or until 10 seconds pass
337 public static void waitForElementToDisappear(String dataTestId) {
338 Supplier<Boolean> elementPresenseChecker = () -> GeneralUIUtils.isElementPresent(dataTestId);
339 Function<Boolean, Boolean> verifier = isElementPresent -> !isElementPresent;
340 FunctionalInterfaces.retryMethodOnResult(elementPresenseChecker, verifier);