-
Notifications
You must be signed in to change notification settings - Fork 0
/
express-run
executable file
·214 lines (191 loc) · 5.75 KB
/
express-run
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
#!/bin/bash
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[0;33m'
WHITE='\033[0;37m'
PINK='\033[0;35m'
NC='\033[0m' # No Color
function help() {
echo -e "${PINK} _____ __ __ ____${NC}"
echo -e "${PINK} | ____| \ \/ / | _ \\ ${NC}"
echo -e "${PINK} | _| \ / | |_) |${NC}"
echo -e "${PINK} | |___ / \ | __/${NC}"
echo -e "${PINK} |_____| /_/\_\ |_|${NC} The Express Framework for Selenium"
echo -e ""
echo -e "${WHITE}USAGE:${NC}"
echo -e " express-run -n|--number <tests_in_parallel> -H|--headless -b|--browser <browser1> -b|--browser <browser2> <test_file.py> <file.rsc>"
echo -e "${WHITE}EXAMPLES:${NC}"
echo -e ""
echo -e "${WHITE}Listing:${NC}"
echo -e ""
echo -e " List all available tests."
echo -e ""
echo -e " express-run -l"
echo -e ""
echo -e "${WHITE}Execution:${NC}"
echo -e ""
echo -e " If you are not in a rush, you can run the tests sequentially on a single browser."
echo -e ""
echo -e " express-run --browser chrome test/test_example.py"
echo -e ""
echo -e " If you want to run the tests in parallel on a single browser, you can use the -n option."
echo -e ""
echo -e " express-run -n 2 --browser chrome test/test_example.py"
echo -e ""
echo -e " If you want to run the tests in parallel on multiple browsers, you can use the --browser option multiple times."
echo -e ""
echo -e " express-run -n 2 --browser chrome --browser firefox test/test_example.py"
echo -e ""
echo -e " If you want to run the tests in parallel on multiple browsers in headless mode, you can use the --headless option."
echo -e ""
echo -e " express-run -n 2 --browser chrome --browser firefox --headless test/test_example.py"
echo -e ""
echo -e " If you want to ignore a test, you can use the --ignore option."
echo -e ""
echo -e " express-run -n 9 --browser chrome --browser firefox --ignore '*recaptcha_v2*' test/"
echo -e ""
}
# Check directory using which express-run
DIR=$(dirname $(which express-run))
pushd "$DIR" >/dev/null || exit 1
# Check we are executed within express directory
BASEDIR=$(basename $(pwd))
if [ "$BASEDIR" != "express" ]; then
echo -e "${RED}Please run this script from the express directory.${NC}"
exit 1
fi
# Check if virtual environment exists
if [ ! -d venv ]; then
echo -e "${RED}Virtual environment does not exist. Please run setup.sh first.${NC}"
exit 1
fi
if [ $# -eq 0 ]; then
help
exit 1
fi
while [[ $# -gt 0 ]]; do
key="$1"
case $key in
-n|--number)
NUMBER="$2"
shift
shift
;;
-h|--help)
HELP=true
shift
;;
-l|--list)
LIST=true
shift
;;
-b|--browser)
BROWSER+=("$2")
shift
shift
;;
-i|--ignore)
IGNORE+=("$2")
shift
shift
;;
-H|--headless)
HEADLESS=true
shift
;;
*)
TEST="$1"
shift
;;
esac
done
if [ $HELP ]; then
help
exit 0
fi
echo -e "${PINK} _____ __ __ ____${NC}"
echo -e "${PINK} | ____| \ \/ / | _ \\ ${NC}"
echo -e "${PINK} | _| \ / | |_) |${NC}"
echo -e "${PINK} | |___ / \ | __/${NC}"
echo -e "${PINK} |_____| /_/\_\ |_|${NC} The Express Framework for Selenium"
echo -e ""
if [ $LIST ]; then
echo -e "${YELLOW}Listing test files...${NC}"
find test -name "test_*.py" -type f
find test -name "*.rsc" -type f
echo -e ""
echo -e "To run a test file, use the following command:"
echo -e ""
echo -e " express-run <test_file.py>|<file.rsc>"
echo -e ""
exit 0
fi
# Activate virtual environment
echo -e "${YELLOW}Activating virtual environment...${NC}"
if ! source venv/bin/activate
then
echo -e "${RED}Failed to activate virtual environment.${NC}"
exit 1
fi
echo -e "${GREEN}Virtual environment activated.${NC}"
# Set default variables
if [ -z "$HEADLESS" ]; then
HEADLESS=false
fi
if [ -z "$BROWSER" ]; then
BROWSER=("chrome")
fi
# Print variables
echo -e "${YELLOW}Running tests...${NC}"
echo -e "${GREEN}Number of cores allocated: $NUMBER${NC}"
echo -e "${GREEN}Headless: $HEADLESS${NC}"
echo -e "${GREEN}Browsers: ${BROWSER[@]}${NC}"
echo -e "${GREEN}Ignore: ${IGNORE[@]}${NC}"
echo -e "${GREEN}Test file: $TEST${NC}"
# Check if the test file ends with rsc
if [[ "$TEST" == *.rsc ]]; then
# Run code-gen to create a test file
if ! python3 code-gen.py "$TEST"
then
echo -e "${RED}Failed to generate test file.${NC}"
exit 1
fi
echo -e "${GREEN}Test file generated.${NC}"
# Set test file to generated file
TEST="test/test_$(basename "$TEST" .rsc).py"
fi
# Check if test file is directory
if [ -d "$TEST" ]; then
echo -e "${GREEN}Test file is a directory.${NC}"
elif [ ! -f "$TEST" ]; then
# Check if test file exists inside test directory
if [ -f "test/$TEST" ]; then
TEST="test/$TEST"
else
echo -e "${RED}Test file does not exist.${NC}"
exit 1
fi
fi
# Construct pytest command
PYTEST_CMD="python3 -m pytest"
if [ -n "$NUMBER" ]; then
PYTEST_CMD="$PYTEST_CMD -n $NUMBER"
fi
if $HEADLESS; then
PYTEST_CMD="$PYTEST_CMD --headless"
fi
for browser in "${BROWSER[@]}"; do
PYTEST_CMD="$PYTEST_CMD --browser=$browser"
done
for ignore in "${IGNORE[@]}"; do
PYTEST_CMD="$PYTEST_CMD --ignore-glob=$ignore"
done
PYTEST_CMD="$PYTEST_CMD $TEST"
echo -e "${YELLOW}Pytest command: $PYTEST_CMD${NC}"
if ! $PYTEST_CMD
then
echo -e "${RED}Failed to run tests.${NC}"
exit 1
fi
echo -e "${GREEN}Tests completed successfully.${NC}"
exit 0