vid-automation selenium tests
[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.openecomp.sdc.ci.tests.execute.setup.ExtentTestActions;
6 import org.openecomp.sdc.ci.tests.utilities.GeneralUIUtils;
7 import org.openqa.selenium.By;
8 import org.openqa.selenium.WebDriver;
9 import org.openqa.selenium.WebElement;
10 import org.openqa.selenium.support.ui.ExpectedConditions;
11 import org.openqa.selenium.support.ui.WebDriverWait;
12 import vid.automation.test.Constants;
13 import vid.automation.test.infra.*;
14
15 import java.io.IOException;
16 import java.net.URI;
17 import java.net.URISyntaxException;
18 import java.text.SimpleDateFormat;
19 import java.util.Calendar;
20 import java.util.Date;
21 import java.util.List;
22 import java.util.function.Function;
23
24 import static org.openecomp.sdc.ci.tests.utilities.GeneralUIUtils.getDriver;
25
26 public class VidBasePage {
27
28     public VidBasePage setInstanceName(String name) {
29         setInputText(Constants.INSTANCE_NAME_SELECT_TESTS_ID, name);
30         return this;
31     }
32
33
34     public VidBasePage setLegacyRegion(String name) {
35         setInputText(Constants.ViewEdit.LEGACY_REGION_INPUT_TESTS_ID, name);
36         return this;
37     }
38
39     public String generateAndSetInstanceName(String prefix) {
40         String instanceName = generateInstanceName(prefix);
41         setInstanceName(instanceName);
42         return instanceName;
43     }
44
45     public VidBasePage setInputText(String inputTestsId, String text) {
46         WebElement instanceNameInput = GeneralUIUtils.getInputElement(inputTestsId);
47         instanceNameInput.sendKeys(text);
48         return this;
49     }
50
51     public String getInputValue(String inputTestsId) {
52         WebElement instanceNameInput = GeneralUIUtils.getInputElement(inputTestsId);
53         String value =instanceNameInput.getAttribute("value");
54         return value;
55     }
56
57     public String generateInstanceName(String prefix) {
58         SimpleDateFormat sdf = new SimpleDateFormat(Constants.BrowseASDC.DATE_FORMAT);
59         Date now = Calendar.getInstance().getTime();
60         return prefix + sdf.format(now);
61     }
62
63     public VidBasePage selectServiceTypeByName(String serviceType) {
64         SelectOption.byTestIdAndVisibleText(serviceType, Constants.SERVICE_TYPE_SELECT_TESTS_ID);
65         return this;
66     }
67     public VidBasePage selectFromDropdownByTestId(String itemTestId, String dropdownButtonTestId) {
68         GeneralUIUtils.clickOnElementByTestId(dropdownButtonTestId, 60);
69         Assert.assertTrue(String.format(Constants.ViewEdit.OPTION_IN_DROPDOWN_NOT_EXISTS,itemTestId, dropdownButtonTestId),GeneralUIUtils.getWebElementByTestID(itemTestId) != null );
70         GeneralUIUtils.clickOnElementByTestId(itemTestId, 60);
71         return this;
72     }
73     public VidBasePage noOptionDropdownByTestId( String dropdownButtonTestId) {
74         List<WebElement> selectList= SelectOption.getList(dropdownButtonTestId);
75         Assert.assertTrue("The Select Input "+ dropdownButtonTestId+" should be empty",selectList.size()==1);
76         return this;
77     }
78
79     public static void selectSubscriberById(String subscriberId) {
80         SelectOption.byValue(subscriberId, Constants.SUBSCRIBER_NAME_SELECT_TESTS_ID);
81     }
82
83     public VidBasePage selectSubscriberByName(String subscriberName) {
84         SelectOption.byTestIdAndVisibleText(subscriberName, Constants.SUBSCRIBER_NAME_SELECT_TESTS_ID);
85         return this;
86     }
87
88     public VidBasePage selectProductFamily(String productFamily) {
89         SelectOption.byValue(productFamily, Constants.ViewEdit.PRODUCT_FAMILY_SELECT_TESTS_ID);
90         return this;
91     }
92
93     public VidBasePage selectSuppressRollback(String shouldSuppress) {
94         SelectOption.byTestIdAndVisibleText(shouldSuppress, Constants.SUPPRESS_ROLLBACK_SELECT_TESTS_ID);
95         return this;
96     }
97
98     public VidBasePage clickDeployServiceButtonByServiceUUID(String serviceUUID) {
99         Input.replaceText(serviceUUID, Constants.BROWSE_SEARCH);
100         String elementTestId = Constants.DEPLOY_BUTTON_TESTS_ID_PREFIX + serviceUUID;
101         GeneralUIUtils.clickOnElementByTestId(elementTestId, 30);
102         GeneralUIUtils.ultimateWait();
103
104         screenshotDeployDialog(serviceUUID);
105
106         return this;
107     }
108
109     public void screenshotDeployDialog(String serviceUUID) {
110         try {
111             GeneralUIUtils.ultimateWait();
112             GeneralUIUtils.ultimateWait(); // better screenshot
113             String screenshotName = "deployService-" + serviceUUID;
114             ExtentTestActions.addScreenshot(Status.INFO, screenshotName, screenshotName);
115         } catch (IOException e) {
116             throw new RuntimeException(e);
117         }
118     }
119
120
121     public VidBasePage clickEditViewByInstanceId(String instanceId) {
122         String elementTestId = Constants.VIEW_EDIT_TEST_ID_PREFIX + instanceId;
123         GeneralUIUtils.clickOnElementByTestId(elementTestId, 100);
124
125         return this;
126     }
127
128     public Boolean checkEditOrViewExistsByInstanceId(String instanceId) {
129         String elementTestId = Constants.VIEW_EDIT_TEST_ID_PREFIX + instanceId;
130         return Exists.byTestId(elementTestId);
131     }
132
133
134
135     public VidBasePage clickSubmitButton() {
136         GeneralUIUtils.clickOnElementByText(Constants.SUBMIT_BUTTON_TEXT, 30);
137         return this;
138     }
139
140     public VidBasePage clickCancelButton() {
141         Click.byId(Constants.generalCancelButtonId);
142         return this;
143     }
144
145     public VidBasePage clickCancelButtonByTestID() {
146         GeneralUIUtils.clickOnElementByTestId(Constants.CANCEL_BUTTON_TEST_ID, 30);
147         return this;
148     }
149
150
151     public VidBasePage clickConfirmButton() {
152         GeneralUIUtils.clickOnElementByTestId(Constants.CONFIRM_BUTTON_TESTS_ID, 30);
153         return this;
154     }
155
156     public VidBasePage clickConfirmButtonInResumeDelete() {
157         GeneralUIUtils.clickOnElementByTestId(Constants.CONFIRM_RESUME_DELETE_TESTS_ID);
158         return this;
159     }
160
161     public VidBasePage clickCommitCloseButton() {
162         GeneralUIUtils.clickOnElementByTestId(Constants.COMMIT_CLOSE_BUTTON_ID, 30);
163         return this;
164     }
165
166     public VidBasePage clickCloseButton() {
167         return clickCloseButton(30);
168     }
169
170     public VidBasePage clickCloseButton(int customTimeout) {
171         GeneralUIUtils.clickOnElementByText(Constants.CLOSE_BUTTON_TEXT, customTimeout);
172         return this;
173     }
174
175
176     public VidBasePage selectLcpRegion(String lcpRegion) {
177         SelectOption.byValue(lcpRegion, Constants.ViewEdit.LCP_REGION_SELECT_TESTS_ID);
178         return this;
179     }
180
181     public VidBasePage selectTenant(String tenant) {
182         SelectOption.byValue(tenant, Constants.ViewEdit.TENANT_SELECT_TESTS_ID);
183         return this;
184     }
185
186     public VidBasePage selectAicZone(String aicZone) {
187         SelectOption.byValue(aicZone, Constants.ViewEdit.AIC_ZONE_TEST_ID);
188         return this;
189     }
190
191     public VidBasePage selectRollbackOption(boolean rollback) {
192         SelectOption.byValue(String.valueOf(rollback) , Constants.ViewEdit.ROLLBACK_TEST_ID);
193         return this;
194     }
195
196     public VidBasePage selectPlatform(String platform) {
197         SelectOption.byValue(platform, Constants.OwningEntity.PLATFORM_SELECT_TEST_ID);
198         return this;
199     }
200
201     public VidBasePage selectLineOfBusiness(String lob) {
202         SelectOption.byValue(lob, Constants.OwningEntity.LOB_SELECT_TEST_ID);
203         return this;
204     }
205
206     public void assertButtonState(String dataTestId, boolean shouldBeEnabled) {
207         assertButtonStateInternal(dataTestId, shouldBeEnabled,
208                 (dataTestIdInner) -> GeneralUIUtils.getWebElementByTestID(dataTestIdInner, 60));
209     }
210
211     public void assertButtonStateEvenIfButtonNotVisible(String dataTestId, boolean shouldBeEnabled) {
212         // getInputElement is quite similar to getWebElementByTestID, but doesn't use
213         // the visibility predicate, so button is reachable bhind the grayed-out panel
214         assertButtonStateInternal(dataTestId, shouldBeEnabled,
215                 (dataTestIdInner) -> GeneralUIUtils.getInputElement(dataTestIdInner));
216     }
217
218     protected void assertButtonStateInternal(String dataTestId, boolean shouldBeEnabled, Function<String,WebElement> strategy) {
219         GeneralUIUtils.ultimateWait();
220         boolean enabledElement= strategy.apply(dataTestId).getAttribute("disabled") == null;
221         if(shouldBeEnabled) {
222             Assert.assertTrue(String.format(Constants.ViewEdit.DISABLE_ERROR_MESSAGE,dataTestId), enabledElement);
223         }else{
224             Assert.assertFalse(String.format(Constants.ViewEdit.ENABLE_ERROR_MESSAGE,dataTestId),enabledElement);
225         }
226
227     }
228     public VidBasePage assertMsoRequestModal(String statusMsg) {
229         boolean waitForTextResult = Wait.waitByClassAndText("status", statusMsg, 20);
230         Assert.assertTrue(statusMsg + " message didn't appear on time", waitForTextResult);
231
232         return this;
233     }
234
235     public VidBasePage refreshPage() {
236         getDriver().navigate().refresh();
237         return this;
238     }
239
240     public String navigateTo(String path) {
241         String envUrl = System.getProperty("ENV_URL");
242         URI uri;
243         try {
244             uri = new URI(envUrl);
245         } catch (URISyntaxException e) {
246             throw new RuntimeException(e);
247         }
248         String target = uri.resolve(path).toString();
249
250         getDriver().navigate().to(target);
251         GeneralUIUtils.ultimateWait();
252
253         return target;
254     }
255
256     public String getTextByTestID(String testId){
257         WebElement webElement= GeneralUIUtils.getWebElementByTestID(testId);
258         return webElement.getText();
259     }
260
261     public void checkAndCloseAlert(String expectedText) {
262        String alertText= Get.alertText();
263        Assert.assertEquals(expectedText, alertText);
264        Click.acceptAlert();
265     }
266     public void goToIframe() {
267         final long start = System.currentTimeMillis();
268         goOutFromIframe();
269         GeneralUIUtils.ultimateWait();
270         System.out.println("ultimateWait waited " + (System.currentTimeMillis() - start));
271         final WebDriver iframeReady = new WebDriverWait(getDriver(), 10).until(
272                 ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.tagName("iframe"))
273         );
274         Assert.assertNotNull("failed going into iframe", iframeReady);
275
276         final long start2 = System.currentTimeMillis();
277         GeneralUIUtils.ultimateWait();
278         System.out.println("ultimateWait waited " + (System.currentTimeMillis() - start2));
279     }
280
281     public void goOutFromIframe(){
282         getDriver().switchTo().defaultContent();
283     }
284
285
286
287
288
289     public static WebDriverWait waitUntilDriverIsReady(int time) {
290         return new WebDriverWait(getDriver(), (long)time);
291     }
292
293
294 }