summaryrefslogtreecommitdiffstats
path: root/tests/acceptance/tests/pages/apps.page.js
blob: eb018979c90db6679166a07480ec7768c3c19168 (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
/*
 * Copyright (c) 2014
 *
 * This file is licensed under the Affero General Public License version 3
 * or later.
 *
 * See the COPYING-README file.
 *
 */

/* global module, protractor, element, by, browser, require */
(function() {
	var Page = require('../helper/page.js');

	var AppsPage = function(baseUrl) {
		this.baseUrl = baseUrl;
		this.path = 'index.php/settings/apps';
		this.url = baseUrl + this.path;

		this.appList = element(by.css('#app-navigation .applist'));
	};

	//================ LOCATOR FUNCTIONS ====================================//
	AppsPage.prototype.appId = function(appId) {
		return by.css('#app-navigation .applist [data-id=\'' + appId + '\']');
	};

	AppsPage.prototype.enableButtonId = function() {
		return by.css('#app-content .appinfo .enable');
	};

	AppsPage.prototype.groupsEnableCheckboxId = function() {
		return by.id('groups_enable');
	};

	AppsPage.prototype.groupsEnableListId = function() {
		return by.css('#app-content .multiselect.button');
	};
	//================ SHARED ===============================================//

	AppsPage.prototype.get = function() {
		browser.get(this.url);

		var appList = this.appList;
		browser.wait(function() {
			return appList.isDisplayed();
		}, 5000, 'load app page');
	};

	/**
	* Enables or disables the given app.
	*
	* @param {String} appId app id
	* @param {bool} [state] true (default) to enable the app, false otherwise
	* @param {Array} [groups] groups for which to enable the app or null to disable
	* group selection. If not specified (undefined), the group checkbox, if it exists,
	* will be left as is.
	*/
	AppsPage.prototype.enableApp = function(appId, state, groups) {
		var d = protractor.promise.defer();
		if (state === undefined) {
			state = true;
		}

		var enableButton = element(this.enableButtonId());

		element(this.appId(appId)).click();
		browser.wait(function() {
			return enableButton.isPresent();
		}, 800);

		// an app is already enabled if the button value is "Disable"
		enableButton.getAttribute('value').then(function(attr) {
			if (state !== (attr === 'Disable')) {
				enableButton.click();
			}
		});

		// wait for the button to change its attribute
		browser.wait(function() {
			return enableButton.getAttribute('value').then(function(attr) {
				return attr === state ? 'Disable' : 'Enable';
			});
		}, 800);

		if (state && groups !== undefined) {
			var groupsCheckbox = element(this.groupsEnableCheckboxId());
			var hasGroups = false;

			if (groups && groups.length > 0) {
				hasGroups = true;
			}

			// check/uncheck checkbox to match desired state
			groupsCheckbox.isSelected().then(function(checkboxState) {
				if (hasGroups !== checkboxState) {
					groupsCheckbox.click();
				}
			});

			// wait for checkbox to change state
			browser.wait(function() {
				return groupsCheckbox.isSelected().then(function(checkboxState) {
					return hasGroups === checkboxState;
				});
			}, 800);

			if (hasGroups) {
				var groupsList = element(this.groupsEnableListId());
				Page.multiSelectSetSelection(groupsList, groups).then(function() {
					d.fulfill(true);
				});
			} else {
				d.fulfill(true);
			}
		}
		return d.promise;
	};

	module.exports = AppsPage;
})();