aboutsummaryrefslogtreecommitdiffstats
path: root/vid-automation/src/main/java/org/onap/sdc/ci/tests/execute/setup/SetupCDTest.java
blob: bb6a19a36fa5977b6ecb3b16e36a541886779fdd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
/*-
 * ============LICENSE_START=======================================================
 * SDC
 * ================================================================================
 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ============LICENSE_END=========================================================
 */

package org.onap.sdc.ci.tests.execute.setup;

import java.io.File;
import java.io.IOException;
import java.util.UUID;

import org.onap.sdc.ci.tests.datatypes.Configuration;
import org.onap.sdc.ci.tests.datatypes.User;
import org.onap.sdc.ci.tests.datatypes.UserCredentials;
import org.onap.sdc.ci.tests.datatypes.UserRoleEnum;
import org.onap.sdc.ci.tests.execute.setup.ExtentManager.suiteNameXml;
import org.onap.sdc.ci.tests.run.StartTest;
import org.onap.sdc.ci.tests.utilities.FileHandling;
import org.onap.sdc.ci.tests.utilities.GeneralUIUtils;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebElement;
import org.testng.Assert;
import org.testng.ITestContext;
import org.testng.ITestResult;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.AfterSuite;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.BeforeSuite;
import org.testng.annotations.Test;

import com.aventstack.extentreports.ExtentTest;
import com.aventstack.extentreports.Status;

import net.lightbody.bmp.core.har.Har;

public abstract class SetupCDTest extends DriverFactory {

	private static final String RE_RUN = "<html><font color=\"red\">ReRun - </font></html>";

	/**************** PRIVATES ****************/
	private static String url;

	protected static ITestContext myContext;

	/**************** METHODS ****************/
	public static ExtentTest getExtendTest() {
		return ExtentTestManager.getTest();
	}

	public static WindowTest getWindowTest() {
		return WindowTestManager.getWindowMap();
	}

	public static String getScreenshotFolder() {
		return getConfiguration().getScreenshotFolder();
	}

	public static String getHarFilesFolder() {
		return getConfiguration().getHarFilesFolder();
	}
	
	public static String getReportFolder(){
		return getConfiguration().getReportFolder();
	}
	
	public static String getReportFilename(){
		return getConfiguration().getReportFileName();
	}
	
	protected abstract UserCredentials getUserCredentials();
	protected abstract Configuration getEnvConfiguration();
	protected abstract void loginToLocalSimulator(UserCredentials userCredentials);
	
	

	/**************** BEFORE ****************/

	@BeforeSuite(alwaysRun = true)
	public void setupBeforeSuite(ITestContext context) throws Exception {
		setUrl();
		initReport(context);
	}

	private void initReport(ITestContext context) throws Exception {
		myContext = context;
		ExtentManager.initReporter(getConfiguration(), context);
	}

	@BeforeMethod(alwaysRun = true)
	public void setBrowserBeforeTest(java.lang.reflect.Method method, ITestContext context) throws Exception {
		boolean emptyDataProvider = isDataProviderEmpty(method);
		if (emptyDataProvider) {
			System.out.println("ExtentReport instance started from BeforeMethod...");
			String suiteName = ExtentManager.getSuiteName(context);
			if (suiteName.equals(suiteNameXml.TESTNG_FAILED_XML_NAME.getValue())) {
				ExtentTestManager.startTest(RE_RUN + method.getName());
			} else {
				ExtentTestManager.startTest(method.getName());
			}

			ExtentTestManager.assignCategory(this.getClass());
			setBrowserBeforeTest(getUserCredentials());
		} else {
			System.out.println("ExtentReport instance started from Test...");
		}

		getConfiguration().setWindowsDownloadDirectory(getWindowTest().getDownloadDirectory());

		if (getConfiguration().isCaptureTraffic()) {
			try {
				MobProxy.getPoxyServer().newHar(method.getName() + ".har");
			} catch (Throwable e) {
				e.printStackTrace();
			}
		}
	}

	private boolean isDataProviderEmpty(java.lang.reflect.Method method) {
		return method.getAnnotation(Test.class).dataProvider().isEmpty();
	}

	/**************** AFTER ****************/
	@AfterMethod(alwaysRun = true)
	public void quitAfterTest(ITestResult result, ITestContext context) throws Exception {

		try {
			ReportAfterTestManager.report(result, context);
			GeneralUIUtils.closeErrorMessage();
		} finally {

			if (getConfiguration().isCaptureTraffic()) {
//				addTrafficFileToReport(result);
			}

			ExtentTestManager.endTest();
			ExtentManager.closeReporter();
			FileHandling.cleanCurrentDownloadDir();
		}

	}

	@AfterSuite(alwaysRun = true)
	public void afterSuite() throws Exception {
		if (getConfiguration().isUseBrowserMobProxy()) {
			MobProxy.getPoxyServer().stop();
		}
	}

	protected static String setUrl() {
		url = getConfiguration().getUrl();
		if (url == null) {
			String message = "no URL found";
			System.out.println(message);
			Assert.fail(message);
		} 
		return url;
	}

	public static void navigateToUrl(String url) throws Exception {
		try {
			System.out.println("Deleting cookies...");
			deleteCookies();

			System.out.println("Navigating to URL : " + url);
			getDriver().navigate().to(url);
			GeneralUIUtils.waitForLoader();

			System.out.println("Zooming out...");
			GeneralUIUtils.windowZoomOutUltimate();

		} catch (Exception e) {
			String msg = "Browser is unreachable";
			System.out.println(msg);
			e.printStackTrace();
			getExtendTest().log(Status.ERROR, msg);
			Assert.fail(msg);
		}
	}

	private static void deleteCookies() throws Exception {
		getDriver().manage().deleteAllCookies();
		Thread.sleep(1000);

		int attempts = 0;
		final int max_attempts = 3;

		while (!getDriver().manage().getCookies().isEmpty() && attempts < max_attempts) {
			getExtendTest().log(Status.INFO,
					"Trying to delete cookies one more time - " + (attempts + 1) + "/" + max_attempts + "attempts");
			String deleteCookiesJS = "document.cookie.split(';').forEach(function(c) { document.cookie = c.replace(/^ +/, '').replace(/=.*/, '=;expires=' + new Date().toUTCString() + ';path=/'); });";
			((JavascriptExecutor) getDriver()).executeScript(deleteCookiesJS);
			attempts++;

			if (attempts == max_attempts) {
				String msg = "Did not delete cookies, can't login with the userId "
						+ WindowTestManager.getWindowMap().getUserCredentials().getUserId();
				System.out.println(msg);
				getExtendTest().log(Status.ERROR, msg);
				Assert.fail(msg);
			}
		}
	}

	protected void loginToSystem(UserCredentials userCredentials) throws Exception {
		if (getConfiguration().isUseCustomLogin()) {
			loginToLocalSimulator(userCredentials);
		} 
		else {
			sendUserAndPasswordKeys(userCredentials);
			WebElement submitButton = GeneralUIUtils.getWebElementBy(By.name("btnSubmit"), 30);
			submitButton.click();
			WebElement buttonOK = GeneralUIUtils.getWebElementBy(By.name("successOK"), 30);
			Assert.assertTrue(buttonOK.isDisplayed(), "OK button is not displayed.");
			buttonOK.click();
		}
		GeneralUIUtils.ultimateWait();
		getWindowTest().setUserCredentials(userCredentials);
	}

	private void setRefreshAttempts(int refreshAttempts) {
		getWindowTest().setRefreshAttempts(refreshAttempts);
	}

	private void sendUserAndPasswordKeys(UserCredentials userCredentials) {
		System.out.println("Login with the userId : " + userCredentials.getUserId());
		WebElement userNameTextbox = GeneralUIUtils.getWebElementBy(By.name("userid"));
		userNameTextbox.sendKeys(userCredentials.getUserId());
		WebElement passwordTextbox = GeneralUIUtils.getWebElementBy(By.name("password"));
		passwordTextbox.sendKeys(userCredentials.getPassword());
	}

	public void loginWithUser(UserCredentials userCredentials) {
		try {
			getExtendTest().log(Status.INFO, String.format("Login with the userId %s", userCredentials.getUserId()));
			loginToSystem(userCredentials);
		} catch (Exception e) {
			throw new RuntimeException(e);
		} finally {
			getWindowTest().setPreviousUser(getWindowTest().getUserCredentials().getUserId());
		}
	}

	private void setUser(UserCredentials userCredentials) {
		User user = new User();
		user.setUserId(userCredentials.getUserId());
		user.setFirstName(userCredentials.getFirstName());
		user.setRole(userCredentials.getRole());
		user.setLastName(userCredentials.getLastName());
		getWindowTest().setUserCredentials(userCredentials);
	}

	public User getUser() {
		return getWindowTest().getUserCredentials();
	}

	private void setBrowserBeforeTest(UserCredentials userCredentials) {
		System.out.println(String.format("Setup before test with the userId %s.", userCredentials.getUserId()));
		try {
			System.out.println("Previous userId is : " + getWindowTest().getPreviousUser() + " ; Current userId is : " + userCredentials.getUserId());
			if (!getWindowTest().getPreviousUser().toLowerCase().equals(userCredentials.getUserId())) {
				System.out.println("User IDs are different. navigating and login.");
				navigateAndLogin(userCredentials);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public void navigateAndLogin(UserCredentials userCredentials) throws Exception {
		int refreshAttempts = getWindowTest().getRefreshAttempts() != 0 ? getWindowTest().getRefreshAttempts() : 0;
		setRefreshAttempts(refreshAttempts);
		setUser(userCredentials);
		navigateToUrl(url);
		loginWithUser(userCredentials);
		GeneralUIUtils.ultimateWait();
	}

	public User getUser(UserRoleEnum role) {
		User user = new User();
		user.setUserId(role.getUserId());
		user.setFirstName(role.getFirstName());
		user.setLastName(role.getLastName());
		user.setRole(role.name());
		return user;
	}

	protected void reloginWithNewRole(UserCredentials userCredentials) throws Exception {
		System.out.println(String.format("Setup before relogin with the userId %s", userCredentials.getUserId()));
		navigateAndLogin(userCredentials);
	}

	public void addTrafficFileToReport(ITestResult result) {
		try {
			// Get the HAR data
			Har har = MobProxy.getPoxyServer().getHar();
			String shortUUID = UUID.randomUUID().toString().split("-")[0];
			File harFile = new File(getHarFilesFolder() + result.getName() + shortUUID + ".har");
			new File(getHarFilesFolder()).mkdirs();

			har.writeTo(harFile);

			String pathToFileFromReportDirectory = getReportFolder() + File.separator + harFile.getName();
			ExtentTestActions.addFileToReportAsLink(harFile, pathToFileFromReportDirectory,
					"File with captured traffic");
		} catch (IOException ioe) {
			ioe.printStackTrace();
		}
	}

	/*
	 * * Start section of test in ExtentReport with DataProvider parameters,
	 * should be started from test method, see example in onboardVNFTest
	 */
	public void setLog(String fromDataProvider) {

		String suiteName = ExtentManager.getSuiteName(myContext);
		if (suiteName.equals(suiteNameXml.TESTNG_FAILED_XML_NAME.getValue())) {
			ExtentTestManager.startTest(RE_RUN + Thread.currentThread().getStackTrace()[2].getMethodName()
					+ "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;" + fromDataProvider);
		} else {
			ExtentTestManager.startTest(Thread.currentThread().getStackTrace()[2].getMethodName()
					+ "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;" + fromDataProvider);
		}

		getWindowTest().setAddedValueFromDataProvider(fromDataProvider);
		ExtentTestManager.assignCategory(this.getClass());
		setBrowserBeforeTest(getUserCredentials());
	}
	
	public static void main(String[] args) {
		System.out.println("---------------------");
		System.out.println("running test from CLI");
		System.out.println("---------------------");

		String testSuite = System.getProperty("testSuite");
		String[] testSuiteArr = {testSuite};
		StartTest.main(testSuiteArr);
	}
	
}