-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.js
127 lines (102 loc) · 3.49 KB
/
test.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
// Set Defaults
var selectedCandidate = false;
var numerations = 1;
var randomCandidate = false;
var url= 'http://localhost:8000';
// Get Console Args
var chalk = require('chalk');
for( i = 0; i <= process.argv.length-1; i++) {
switch ( process.argv[i] ){
case '--candidate':
case '-c':
selectedCandidate = process.argv[i+1];
break;
case '--numerations':
case '-n':
numerations = process.argv[i+1];
break;
case '--thousands':
case '-k':
numerations = process.argv[i+1]*1000;
break;
case '--millions':
case '-m':
numerations = process.argv[i+1]*1000000;
break;
case '--random':
case '-rand':
case '-r':
randomCandidate = true;
break;
case '-url':
url = process.argv[i+1];
break;
case '--help':
case '-h':
showHelp();
process.exit();
break;
}
}
// default:
// console.log(chalk.green.bold('Specify the candidate and amount of votes to run test.'));
// console.log(chalk.bold('node test.js --candidate 1 --numerations 100'));
// break;
// Gather requirements
var async = require('async');
var webdriver = require('selenium-webdriver'),
By = webdriver.By,
size = webdriver.size,
until = webdriver.until;
var chai = require('chai');
var chaiAsPromised = require('chai-as-promised');
chai.use(chaiAsPromised);
var expect = chai.expect;
// Start the webdriver
var driver = new webdriver.Builder()
.forBrowser('chrome')
.usingServer('http://localhost:4444/wd/hub')
.build();
// Begin the voting loop
for (var i = numerations - 1; i >= 0; i--) {
// Load the front-end
driver.get(url);
// Select a random candidate if needed
if (randomCandidate) {
driver.findElements(webdriver.By.css('.candidates li')).then(function(candidates){
// Select a random candidate
selectedCandidate = Math.floor((Math.random() * candidates.length) + 1);
// Select a candidate
var candidates = driver.findElement(By.css('.candidates li:nth-child('+selectedCandidate+')')).click()
// Submit the vote
driver.findElement(By.id('submit')).click();
});
// Or select the candidate in the command line argument
} else if (selectedCandidate) {
// Select a candidate
var candidates = driver.findElement(By.css('.candidates li:nth-child('+selectedCandidate+')')).click()
// Submit the vote
driver.findElement(By.id('submit')).click();
// Show help text
} else {
showHelp();
}
// Load the statistics
driver.get(url+'/statistics');
};
// Quit the driver, end the test
driver.quit();
function showHelp() {
console.log('Welcome to...');
console.log(chalk.bold('\n┬┌┐┌┌─┐┬─┐┌─┐┌┬┐┌─┐┌┐┌┌┬┐\n│││││ ├┬┘├┤ │││├┤ │││ │ \n┴┘└┘└─┘┴└─└─┘┴ ┴└─┘┘└┘ ┴ \n '));
console.log('\nFor example uses only. Incrementing votes to a legitimate poll using this program is a violation of federal and local laws.\n');
console.log(chalk.bold('You can add any of these command arguments to change the results of the test.'));
console.log('-c --candidate 1 Select the candidate number by number');
console.log('-n --numerations 100 Set the number of votes');
console.log('-r --random Choose a random candidate for each vote');
console.log('-url The URL of the vote server (default: http://localhost:8000)');
console.log('\nExample Commands:');
console.log(chalk.bold('node test.js --candidate 1 --numerations 100'));
console.log(chalk.bold('node test.js -c 1 -n 50'));
console.log(chalk.bold('node test.js --random -n 1000 \n'));
}