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