-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathparties-stress.js
227 lines (205 loc) · 6.97 KB
/
parties-stress.js
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
// Testing the parties example with an automated ghost
var casper = require('casper').create();
// get variables from command line or use defaults as a fallback
if (casper.cli.has("email")) {
var email = casper.cli.get("email");
}
else {
var email = '[email protected]';
}
if (casper.cli.has("password")) {
var password = casper.cli.get("password");
}
else {
var password = 'password';
}
if (casper.cli.has("url")) {
var url = casper.cli.get("url");
}
else {
var url = 'http://localhost:3000/';
}
var partyTitles = [
"Alive and kicking",
"Accelerate",
"Beerfest",
"Breakfast at Tiffany's",
"Browncoats unite",
"Burning down the House",
"BYOB",
"City Shag",
"Cloud city",
"Das Fest",
"Dude, where's my car?",
"Exterminate!",
"Have you met Ted?",
"House Party",
"Kelly's Call",
"Kwanzaa",
"La Boum",
"Lollapalooza",
"Meet the Feebles",
"No siesta fiesta",
"Out all night",
"Party PEOPLE!!!!",
"Risky Business",
"Rock Da House",
"Rockin' the Night Away",
"Slayerfest '98",
"Slurms MacKenzie's Frenzy",
"Stan's previously owned party",
"The more the merrier",
"The Party",
"TurboDiesel",
"Woo! Party!"
];
var rsvpChoices = ["rsvp_yes", "rsvp_no", "rsvp_maybe"];
var rsvpChoices_weight = [5, 2, 1];
var rsvpChoices_totalWeight = eval(rsvpChoices_weight.join("+"));
var weighedChoices=new Array() //new array to hold "weighted" fruits
var currentChoice=0
while (currentChoice<rsvpChoices.length){ //step through each fruit[] element
for (i=0; i<rsvpChoices_weight[currentChoice]; i++)
weighedChoices[weighedChoices.length]=rsvpChoices[currentChoice];
currentChoice++;
}
// setting the options for our run
casper.options.viewportSize = {width: 1280, height: 768};
casper.options.verbose = true;
//casper.options.logLevel = 'debug'; // debug needs you to be verbose, hence the line above!
casper.start(url, function () {
this.echo('Connected to "' + this.getTitle() + '"');
});
// LOG OUT IF ALREADY LOGGED IN
casper.then(function () {
casper.echo('First make sure this instance is not logged in already');
logOut(this);
});
// LOG IN/SIGN UP A USER
casper.then(function () {
casper.echo('Now I will sign up as a new user');
signUp(this);
casper.waitForSelector('#login-dropdown-list > div.login-form.login-password-form > div.message.error-message',
function then() { // if we find the selector we got an error
if (getErrorMsg(this) === "Email already exists.") {
this.echo("Let's try if I can log in");
logIn(this);
}
else { // for all other errors, e.g. wrong password, we have to stop here
this.echo('Sorry, I do not know how to authenticate.');
}
},
function () { // this precents us from
this.echo('No errors encountered. Time for Party!');
});
});
// CREATE A PARTY
createParty();
// RSVP TO A PARTY - 2 ACTUALLY
rsvpParty();
rsvpParty();
// WAIT AND TAKE A PICTURE
// ONLY WORKS WELL, IF NOT TOO MANY INSTANCES RUN IN PARALLEL
//casper.then(function () {
// this.wait(1000, function () {
// this.capture('captures/done', undefined, {
// format: 'png'
// });
// });
//});
casper.then(function () {
this.echo("I'm done. I quit");
this.exit();
});
// RUN THE CASPER
casper.run();
// HELPFUL FUNCTIONS
function getErrorMsg(obj) {
return obj.getHTML('#login-dropdown-list > div.login-form.login-password-form > div.message.error-message');
}
function signUp(obj) {
obj.echo("Performing sign up for " + email + " (password: " + password + ") at " + url);
obj.waitForSelector('#login-sign-in-link', function () {
obj.click('#login-sign-in-link');
obj.click('#signup-link');
});
obj.waitForSelector('.login-form', function () {
obj.fillSelectors('.login-form', {
'#login-email': email,
'#login-password': password
}, true);
obj.click('#login-buttons-password');
});
}
function logIn(obj) {
obj.echo('Performing Log in');
if (obj.exists('#login-dropdown-list > a')) {
obj.click('#login-dropdown-list > a');
}
casper.waitForSelector('#login-sign-in-link', function () {
this.click('#login-sign-in-link');
this.click('#signup-link');
});
casper.waitForSelector('.login-form', function () {
this.fillSelectors('.login-form', {
'#login-email': email,
'#login-password': password
}, true);
if (obj.exists('#back-to-login-link')) {
obj.click('#back-to-login-link');
}
this.click('#login-buttons-password');
});
obj.waitForSelector('#login-name-link', function () {
obj.echo('Logged in as ' + obj.getHTML('#login-name-link'));
});
}
function logOut(obj) {
obj.echo('Performing Log out');
if (obj.exists('#login-name-link')) {
obj.echo('logging out user ' + obj.getHTML('#login-name-link'));
obj.click('#login-name-link');
obj.click('#login-buttons-logout');
} else {
obj.echo("Nobody was logged in.");
}
}
function createParty() {
casper.then(function () {
this.echo('Start a new party!');
this.mouse.doubleclick(randomCoordinates()[0], randomCoordinates()[1]);
});
casper.waitForSelector('body > div.modal > div.modal-footer > a.btn.btn-primary.save', function () {
this.echo('Adding a party');
this.fillSelectors('.modal', {
'body > div.modal > div.modal-body > input': returnRandomChoice(partyTitles),
'body > div.modal > div.modal-body > textarea': "This party was brought to you by the power of automated testing and Mr or Ms " + email
}, true);
this.click('body > div.modal > div.modal-footer > a.btn.btn-primary.save');
});
}
function rsvpParty() {
casper.then(function () {
// return a random party ID, but never an empty one
// TODO: There is still an issue with *some* Caspers but only with certain *IDs (reproducable)
//this.echo(this.getElementsAttribute('circle', 'id').filter(function(e){return e}));
var partyIdSelector = 'circle#' + returnRandomChoice(this.getElementsAttribute('circle', 'id').filter(function(e){return e}));
var rsvpSelector = '.' + returnRandomChoice(weighedChoices);
this.echo('For ' + partyIdSelector + ' I will ' + rsvpSelector)
casper.waitForSelector(partyIdSelector, function () {
this.click(partyIdSelector);
});
casper.waitForSelector(rsvpSelector, function () {
this.click(rsvpSelector);
});
});
}
function randomCoordinates() {
// watch out - these may only work with a viewport of 1280x768
var x = Math.floor(Math.random() * (650 - 155 + 1) + 155);
var y = Math.floor(Math.random() * (590 - 90 + 1) + 90);
return [x, y];
}
function returnRandomChoice(array) {
return array[Math.floor(Math.random() * array.length)];
}