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