adding orchestration type to service models view
[vid.git] / vid-automation / src / main / java / vid / automation / test / sections / VidBasePage.java
1 package vid.automation.test.sections;
2
3 import com.aventstack.extentreports.Status;
4 import org.junit.Assert;
5 import org.onap.sdc.ci.tests.execute.setup.ExtentTestActions;
6 import org.onap.sdc.ci.tests.utilities.GeneralUIUtils;
7 import org.openqa.selenium.*;
8 import org.openqa.selenium.support.ui.ExpectedConditions;
9 import org.openqa.selenium.support.ui.WebDriverWait;
10 import vid.automation.test.Constants;
11 import vid.automation.test.Constants.ViewEdit;
12 import vid.automation.test.infra.*;
13
14 import java.io.IOException;
15 import java.net.URI;
16 import java.net.URISyntaxException;
17 import java.text.SimpleDateFormat;
18 import java.util.Calendar;
19 import java.util.Date;
20 import java.util.List;
21 import java.util.function.Function;
22
23 import static java.util.stream.Collectors.toMap;
24 import static org.hamcrest.CoreMatchers.containsString;
25 import static org.hamcrest.MatcherAssert.assertThat;
26 import static org.onap.sdc.ci.tests.utilities.GeneralUIUtils.getDriver;
27 import static org.testng.Assert.assertEquals;
28
29 public class VidBasePage {
30
31
32     public VidBasePage setInstanceName(String name) {
33         setInputText(Constants.INSTANCE_NAME_SELECT_TESTS_ID, name);
34         return this;
35     }
36
37
38     public VidBasePage setLegacyRegion(String name) {
39         setInputText(Constants.ViewEdit.LEGACY_REGION_INPUT_TESTS_ID, name);
40         return this;
41     }
42
43     public String generateAndSetInstanceName(String prefix) {
44         String instanceName = generateInstanceName(prefix);
45         setInstanceName(instanceName);
46         return instanceName;
47     }
48
49     public VidBasePage setInputText(String inputTestsId, String text) {
50         WebElement instanceNameInput = GeneralUIUtils.getInputElement(inputTestsId);
51         instanceNameInput.sendKeys(text);
52         return this;
53     }
54
55     public String getInputValue(String inputTestsId) {
56         WebElement instanceNameInput = GeneralUIUtils.getInputElement(inputTestsId);
57         return instanceNameInput.getAttribute("value");
58     }
59
60     public String generateInstanceName(String prefix) {
61         SimpleDateFormat sdf = new SimpleDateFormat(Constants.BrowseASDC.DATE_FORMAT);
62         Date now = Calendar.getInstance().getTime();
63         return prefix + sdf.format(now);
64     }
65
66     public VidBasePage selectServiceTypeByName(String serviceType) {
67         SelectOption.byTestIdAndVisibleText(serviceType, Constants.SERVICE_TYPE_SELECT_TESTS_ID);
68         return this;
69     }
70     public VidBasePage selectFromDropdownByTestId(String itemTestId, String dropdownButtonTestId) {
71         GeneralUIUtils.clickOnElementByTestId(dropdownButtonTestId, 60);
72         Assert.assertTrue(String.format(Constants.ViewEdit.OPTION_IN_DROPDOWN_NOT_EXISTS,itemTestId, dropdownButtonTestId),GeneralUIUtils.getWebElementByTestID(itemTestId) != null );
73         GeneralUIUtils.clickOnElementByTestId(itemTestId, 60);
74         return this;
75     }
76     public VidBasePage noOptionDropdownByTestId( String dropdownButtonTestId) {
77         List<WebElement> selectList= SelectOption.getList(dropdownButtonTestId);
78         Assert.assertTrue("The Select Input "+ dropdownButtonTestId+" should be empty",selectList.size()==1);
79         return this;
80     }
81
82     public static void selectSubscriberById(String subscriberId) {
83         SelectOption.byValue(subscriberId, Constants.SUBSCRIBER_NAME_SELECT_TESTS_ID);
84     }
85
86     public VidBasePage selectSubscriberByName(String subscriberName) {
87         SelectOption.byTestIdAndVisibleText(subscriberName, Constants.SUBSCRIBER_NAME_SELECT_TESTS_ID);
88         return this;
89     }
90
91     public VidBasePage selectProductFamily(String productFamily) {
92         SelectOption.byValue(productFamily, Constants.ViewEdit.PRODUCT_FAMILY_SELECT_TESTS_ID);
93         return this;
94     }
95
96     public VidBasePage selectSuppressRollback(String shouldSuppress) {
97         SelectOption.byTestIdAndVisibleText(shouldSuppress, Constants.SUPPRESS_ROLLBACK_SELECT_TESTS_ID);
98         return this;
99     }
100
101     public VidBasePage clickDeployServiceButtonByServiceUUID(String serviceUUID) {
102         Input.replaceText(serviceUUID, Constants.BROWSE_SEARCH);
103         GeneralUIUtils.ultimateWait();
104         String elementTestId = Constants.DEPLOY_BUTTON_TESTS_ID_PREFIX + serviceUUID;
105         GeneralUIUtils.clickOnElementByTestId(elementTestId, 30);
106         GeneralUIUtils.ultimateWait();
107
108         screenshotDeployDialog(serviceUUID);
109
110         return this;
111     }
112
113     public boolean isModelWithGivenServiceUUIDVisible(String serviceUUID) {
114         String elementTestId = Constants.DEPLOY_BUTTON_TESTS_ID_PREFIX + serviceUUID;
115         try {
116             GeneralUIUtils.getWebElementByTestID(elementTestId, 10);
117             GeneralUIUtils.ultimateWait();
118         } catch (TimeoutException te) {
119             return false;
120         }
121         return true;
122     }
123
124     public void screenshotDeployDialog(String serviceUUID) {
125         try {
126             GeneralUIUtils.ultimateWait();
127             GeneralUIUtils.ultimateWait(); // better screenshot
128             String screenshotName = "deployService-" + serviceUUID;
129             ExtentTestActions.addScreenshot(Status.INFO, screenshotName, screenshotName);
130         } catch (IOException e) {
131             throw new RuntimeException(e);
132         }
133     }
134
135
136     public VidBasePage clickEditViewByInstanceId(String instanceId) {
137         String elementTestId = Constants.VIEW_EDIT_TEST_ID_PREFIX + instanceId;
138         GeneralUIUtils.clickOnElementByTestId(elementTestId, 100);
139
140         return this;
141     }
142
143     public Boolean checkEditOrViewExistsByInstanceId(String instanceId) {
144         String elementTestId = Constants.VIEW_EDIT_TEST_ID_PREFIX + instanceId;
145         return Exists.byTestId(elementTestId);
146     }
147
148
149
150     public VidBasePage clickSubmitButton() {
151         GeneralUIUtils.clickOnElementByText(Constants.SUBMIT_BUTTON_TEXT, 30);
152         return this;
153     }
154
155     public VidBasePage clickCancelButton() {
156         Click.byId(Constants.generalCancelButtonId);
157         return this;
158     }
159
160     public VidBasePage clickCancelButtonByTestID() {
161         GeneralUIUtils.clickOnElementByTestId(Constants.CANCEL_BUTTON_TEST_ID, 30);
162         return this;
163     }
164
165
166     public VidBasePage clickConfirmButton() {
167         GeneralUIUtils.clickOnElementByTestId(Constants.CONFIRM_BUTTON_TESTS_ID, 30);
168         return this;
169     }
170
171     public VidBasePage clickConfirmButtonInResumeDelete() {
172         GeneralUIUtils.clickOnElementByTestId(Constants.CONFIRM_RESUME_DELETE_TESTS_ID);
173         return this;
174     }
175
176     public VidBasePage clickButtonByTestId(String testId) {
177         GeneralUIUtils.clickOnElementByTestId(testId);
178         return this;
179     }
180
181     public VidBasePage clickCommitCloseButton() {
182         GeneralUIUtils.clickOnElementByTestId(Constants.COMMIT_CLOSE_BUTTON_ID, 30);
183         return this;
184     }
185
186     public VidBasePage clickCloseButton() {
187         return clickCloseButton(30);
188     }
189
190     public VidBasePage clickCloseButton(int customTimeout) {
191         GeneralUIUtils.clickOnElementByText(Constants.CLOSE_BUTTON_TEXT, customTimeout);
192         return this;
193     }
194
195     public VidBasePage selectLcpRegion(String lcpRegion) {
196         return selectLcpRegion(lcpRegion, "AIC");
197     }
198
199     public VidBasePage selectLcpRegion(String lcpRegion, String cloudOwner) {
200         GeneralUIUtils.ultimateWait();
201         String visibleText = (Features.FLAG_1810_CR_ADD_CLOUD_OWNER_TO_MSO_REQUEST.isActive()) ?
202             String.format("%s (%s)", lcpRegion, cloudOwner) : lcpRegion;
203         SelectOption.byTestIdAndVisibleText(visibleText, Constants.ViewEdit.LCP_REGION_SELECT_TESTS_ID);
204         return this;
205     }
206
207     public VidBasePage selectLineOfBusiness(String lineOfBusines) {
208         GeneralUIUtils.ultimateWait();
209         SelectOption.byValue(lineOfBusines, Constants.ViewEdit.LINE_OF_BUSINESS_SELECT_TESTS_ID);
210         return this;
211     }
212
213     public VidBasePage selectTenant(String tenant) {
214         GeneralUIUtils.ultimateWait();
215         SelectOption.byValue(tenant, Constants.ViewEdit.TENANT_SELECT_TESTS_ID);
216         return this;
217     }
218
219     public VidBasePage selectAicZone(String aicZone) {
220         SelectOption.byValue(aicZone, Constants.ViewEdit.AIC_ZONE_TEST_ID);
221         return this;
222     }
223
224     public VidBasePage selectRollbackOption(boolean rollback) {
225         SelectOption.byValue(String.valueOf(rollback) , Constants.ViewEdit.ROLLBACK_TEST_ID);
226         return this;
227     }
228
229     public VidBasePage selectPlatform(String platform) {
230         SelectOption.byValue(platform, Constants.OwningEntity.PLATFORM_SELECT_TEST_ID);
231         return this;
232     }
233
234
235     public void assertButtonState(String dataTestId, boolean shouldBeEnabled) {
236         assertButtonStateInternal(dataTestId, shouldBeEnabled,
237                 (dataTestIdInner) -> GeneralUIUtils.getWebElementByTestID(dataTestIdInner, 60));
238     }
239
240     public void assertButtonStateEvenIfButtonNotVisible(String dataTestId, boolean shouldBeEnabled) {
241         // getInputElement is quite similar to getWebElementByTestID, but doesn't use
242         // the visibility predicate, so button is reachable bhind the grayed-out panel
243         assertButtonStateInternal(dataTestId, shouldBeEnabled,
244                 (dataTestIdInner) -> GeneralUIUtils.getInputElement(dataTestIdInner));
245     }
246
247     protected void assertButtonStateInternal(String dataTestId, boolean shouldBeEnabled, Function<String,WebElement> strategy) {
248         GeneralUIUtils.ultimateWait();
249         boolean enabledElement= strategy.apply(dataTestId).getAttribute("disabled") == null;
250         if(shouldBeEnabled) {
251             Assert.assertTrue(String.format(Constants.ViewEdit.DISABLE_ERROR_MESSAGE,dataTestId), enabledElement);
252         }else{
253             Assert.assertFalse(String.format(Constants.ViewEdit.ENABLE_ERROR_MESSAGE,dataTestId),enabledElement);
254         }
255
256     }
257     public VidBasePage assertMsoRequestModal(String statusMsg) {
258         boolean waitForTextResult = Wait.waitByClassAndText("status", statusMsg, 20);
259         Assert.assertTrue(statusMsg + " message didn't appear on time", waitForTextResult);
260
261         return this;
262     }
263
264     public VidBasePage refreshPage() {
265         getDriver().navigate().refresh();
266         return this;
267     }
268
269     public String navigateTo(String path) {
270         String envUrl = System.getProperty("ENV_URL");
271         URI uri;
272         try {
273             uri = new URI(envUrl);
274         } catch (URISyntaxException e) {
275             throw new RuntimeException(e);
276         }
277         String target = uri.resolve(path).toString();
278
279         getDriver().navigate().to(target);
280         GeneralUIUtils.ultimateWait();
281
282         return target;
283     }
284
285     public String getTextByTestID(String testId){
286         WebElement webElement= GeneralUIUtils.getWebElementByTestID(testId);
287         return webElement.getText();
288     }
289
290     public void checkAndCloseAlert(String expectedText) {
291        String alertText= Get.alertText();
292        Assert.assertEquals(expectedText, alertText);
293        Click.acceptAlert();
294     }
295     public static void goToIframe() {
296         final long start = System.currentTimeMillis();
297         goOutFromIframe();
298         GeneralUIUtils.ultimateWait();
299         System.out.println("ultimateWait waited " + (System.currentTimeMillis() - start));
300         final WebDriver iframeReady = new WebDriverWait(getDriver(), 20).until(
301                 ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.tagName("iframe"))
302         );
303         Assert.assertNotNull("failed going into iframe", iframeReady);
304
305         final long start2 = System.currentTimeMillis();
306         GeneralUIUtils.ultimateWait();
307         System.out.println("ultimateWait waited " + (System.currentTimeMillis() - start2));
308     }
309
310     public static void goOutFromIframe(){
311         getDriver().switchTo().defaultContent();
312     }
313
314
315     public void verifyOpenOldViewEdit(String serviceInstanceName, String serviceInstanceId, boolean openShouldBeEnabled, boolean checkPortMirroring, boolean checkAddVnf) {
316         InstantiationStatusPage.checkMenuItem(serviceInstanceName, Constants.InstantiationStatus.CONTEXT_MENU_HEADER_OPEN_ITEM, openShouldBeEnabled, contextMenuOpen -> {
317             Click.byTestId(contextMenuOpen);
318             VidBasePage.goOutFromIframe();
319             GeneralUIUtils.ultimateWait();
320
321             Wait.byText("View/Edit Service Instance");
322             if (serviceInstanceId != null) {
323                 Wait.byText(serviceInstanceId);
324             }
325             Wait.byText(serviceInstanceName);
326
327             if (checkPortMirroring) {
328                 Wait.byText("Add node instance");
329                 Wait.byText(ViewEdit.COMMON_PORT_MIRRORING_PORT_NAME);
330             }
331
332             if (checkAddVnf) {
333                 // Validate bug fix - we open old popup in view/edit
334                 Click.byTestId("addVNFButton");
335                 Click.byTestId("addVNFOption-2017-488_PASQUALE-vPE 0");
336                 assertThat(Get.byTestId("create-modal-title").getText(), containsString("a la carte"));
337                 Click.byTestId("cancelButton");
338                 //end of bug fix validation
339             }
340
341             screenshotDeployDialog("view-edit-" + serviceInstanceName);
342             SideMenu.navigateToMacroInstantiationStatus();
343         });
344     }
345
346
347     public static WebDriverWait waitUntilDriverIsReady(int time) {
348         return new WebDriverWait(getDriver(), (long)time);
349     }
350
351     public String getReduxState() {
352         final JavascriptExecutor javascriptExecutor = (JavascriptExecutor) GeneralUIUtils.getDriver();
353         String reduxState = (String)javascriptExecutor.executeScript("return window.sessionStorage.getItem('reduxState');");
354         System.out.println(reduxState);
355         return reduxState;
356     }
357
358     public void setReduxState(String state) {
359         final JavascriptExecutor javascriptExecutor = (JavascriptExecutor) GeneralUIUtils.getDriver();
360         String script = String.format("window.sessionStorage.setItem('reduxState', '%s');", state);
361         System.out.println("executing script:");
362         System.out.println(script);
363         javascriptExecutor.executeScript(script);
364     }
365
366 }