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