Fix for Penetration test _ Session and cookie management
[vid.git] / vid-automation / src / main / java / vid / automation / test / sections / SideMenu.java
1 package vid.automation.test.sections;
2
3 import org.junit.Assert;
4 import org.onap.sdc.ci.tests.utilities.GeneralUIUtils;
5 import org.openqa.selenium.ElementClickInterceptedException;
6 import org.openqa.selenium.UnhandledAlertException;
7 import org.slf4j.Logger;
8 import org.slf4j.LoggerFactory;
9 import vid.automation.test.Constants;
10
11 public class SideMenu {
12     static final Logger logger = LoggerFactory.getLogger(SideMenu.class);
13
14     public static void navigateToBrowseASDCPage() {
15         navigateToPage(Constants.SideMenu.BROWSE_SDC_SERVICE_MODELS);
16     }
17
18     public static void navigateToSearchExistingPage() {
19         navigateToPage(Constants.SideMenu.SEARCH_EXISTING_SERVICE);
20     }
21
22     public static void navigateToCreateNewServicePage() {
23         navigateToPage(Constants.SideMenu.CREATE_NEW_SERVICE);
24     }
25
26     public static void navigateToTestEnvironmentsPage() {
27         navigateToPage(Constants.SideMenu.TEST_ENVIRONMENTS);
28     }
29
30     public static void navigateToMacroInstantiationStatus() {
31         navigateToPage("Instantiation Status");
32         new VidBasePage().goToIframe();
33     }
34
35     private static void navigateToPage(String PageName) {
36
37         boolean findAndWaitByText = GeneralUIUtils.findAndWaitByText(PageName, 30);
38
39         if (!findAndWaitByText) {
40             doEvenIfAlertIsShown(SideMenu::navigateToWelcomePage);
41             findAndWaitByText = GeneralUIUtils.findAndWaitByText(PageName, 10);
42         }
43
44         Assert.assertTrue(findAndWaitByText);
45         doEvenIfAlertIsShown(() -> {
46             try {
47                 GeneralUIUtils.clickOnElementByText(PageName, 50);
48             } catch (ElementClickInterceptedException e) {
49                 navigateToWelcomePage();
50                 GeneralUIUtils.clickOnElementByText(PageName, 100);
51             }
52         });
53         GeneralUIUtils.ultimateWait();
54         logger.info("navigated to {}", PageName);
55     }
56
57     public static void navigateToWelcomePage() {
58         doEvenIfAlertIsShown(() -> {
59             VidBasePage base = new VidBasePage();
60             base.navigateTo("welcome.htm");
61         });
62     }
63
64     private static void doEvenIfAlertIsShown(Runnable runnable) {
65         try {
66             runnable.run();
67         } catch (UnhandledAlertException e) {
68             // an alert popup was shown; dismiss it if it's still there
69             try {
70                 GeneralUIUtils.getDriver().switchTo().alert().dismiss();
71             } catch (org.openqa.selenium.NoAlertPresentException e2) {
72                 // YOLO
73             }
74             runnable.run();
75         }
76     }
77 }