-
Notifications
You must be signed in to change notification settings - Fork 4
/
test.js
249 lines (221 loc) · 10.2 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
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
const Scrappey = require('.');
const crypto = require("node:crypto");
const fs = require("node:fs");
const path = require("node:path")
// Replace 'YOUR_API_KEY' with your Scrappey API key
const apiKey = '';
const showData = false;
// Most error types look like this:
// testMethod-region-errorIdentifier
// Examples:
// test-client-get_request
// advancedtest-server-get_request
function getQueryString(object) {
//console.log("getQueryString Method was called.")
const queryString = Object.keys(object)
.map(key => encodeURIComponent(key) + '=' + encodeURIComponent(object[key]))
.join('&');
//console.log(`getQueryString Method returned: ${queryString}`)
return queryString;
}
function saveError(error, type = "undefined") {
if (!fs.existsSync(path.join(__dirname, "errors"))) {
fs.mkdirSync(path.join(__dirname, "errors"));
}
const errorId = crypto.randomUUID().slice(-5);
const errorDate = Date.now();
let errorObject;
if (typeof error === "string") {
errorObject = {
type,
errorId,
errorDate,
message: error
};
} else {
errorObject = {
type,
errorId,
errorDate,
name: error.name,
message: error.message,
stack: error.stack
};
}
const fileName = `${errorDate}-${errorId}.json`;
fs.writeFileSync(path.join(__dirname, 'errors', fileName), JSON.stringify(errorObject, null, 2));
console.log(`[~] Saved Error to: ./errors/${fileName}`);
return errorObject;
}
async function runTest() {
// Create an instance of Scrappey
const scrappey = new Scrappey(apiKey, false);
console.log("[test] [✅] Successfully created Scrappey Class.")
try {
let sessionId = `testSession-${crypto.randomUUID().slice(-5)}`;
try {
console.log("[test] [~] Create Session")
const sessionRequest = await scrappey.createSession();
console.log(`[test] [✅-client] Successfully created Session.`)
if (typeof sessionRequest?.error !== "undefined") {
console.log(`[test] [❌] Failed Creating Session.`)
saveError(
JSON.stringify(sessionRequest, null, 2),
"test-server-create_session"
)
} else {
const { session } = sessionRequest;
console.log("[test] [✅-server] Created Session: " + session)
sessionId = session;
}
if (showData) console.log(sessionRequest);
} catch (error) {
console.log("[test] [❌-client] Failed Creating Session")
saveError(error, "test-client-create_session");
}
try {
// Make a GET request
console.log("[test] [~] Get Request")
const getRequestResult = await scrappey.getRequest({
url: 'https://reqres.in/api/users',
session: sessionId
});
console.log(`[test] [✅-client] Successfully got Request.`)
if (showData) console.log(getRequestResult);
if (typeof getRequestResult?.error !== "undefined") {
console.log(`[test] [❌-server] Failed Getting Request.`)
saveError(
JSON.stringify(getRequestResult, null, 2),
"test-server-get_request"
)
} else {
if (!scrappey.isJSON(getRequestResult.solution.innerText)) {
console.log(`[test] [❌-server] Failed Getting Request. Response is not JSON.`)
saveError(
JSON.stringify(getRequestResult, null, 2),
"test-server-get_request"
)
} else if (getRequestResult.solution.innerText.includes("[email protected]")) {
console.log(`[test] [✅-server] Successfully got Request.`)
} else {
console.log(`[test] [❌-server] Failed Getting Request. Response does not contain the correct data.`)
saveError(
JSON.stringify(getRequestResult, null, 2),
"test-server-get_request"
)
}
}
} catch (error) {
console.log("[test] [❌-client] Failed Getting Request")
saveError(error, "test-client-get_request");
}
try {
// Make a POST request using FormData
console.log("[test] [~] Post Request (FormData)")
const postFormData = { username: 'user123', password: 'pass456' };
const postRequestResult = await scrappey.postRequest({
url: 'https://reqres.in/api/users',
postData: getQueryString(postFormData), // The getQueryString Method will convert the Object to FormData. This will automatically be sent with the application/x-www-form-urlencoded header.
session: sessionId
});
console.log(`[test] [✅-client] Successfully posted Request (FormData).`)
if (showData) console.log(postRequestResult);
if (typeof postRequestResult?.error !== "undefined") {
console.log(`[test] [❌-server] Failed Posting Request (FormData).`)
saveError(
JSON.stringify(postRequestResult, null, 2),
"test-server-post_request_formdata"
)
} else {
if (!scrappey.isJSON(postRequestResult.solution.innerText)) {
console.log(`[test] [❌-server] Failed Posting Request (FormData). Response is not JSON.`)
saveError(
JSON.stringify(postRequestResult, null, 2),
"test-server-post_request_formdata"
)
} else if (postRequestResult.solution.innerText.includes("user123")) {
console.log(`[test] [✅-server] Successfully posted Request (FormData).`)
} else {
console.log(`[test] [❌-server] Failed Posting Request (FormData). Response does not contain the correct data.`)
saveError(
JSON.stringify(postRequestResult, null, 2),
"test-server-post_request_formdata"
)
}
}
} catch (error) {
console.log("[test] [❌-client] Failed Posting Request (FormData).")
saveError(error, "test-post_request_formdata");
}
try {
// If you dont want to use formData, scrappey also supports pure JSON requests.
console.log("[test] [~] Post Request (JSON)")
const postDataJson = JSON.stringify({ email: "[email protected]", password: "password" })
const postRequestResultJson = await scrappey.postRequest({
url: "https://reqres.in/api/users",
postData: postDataJson,
customHeaders: {
'Content-Type': 'application/json', // This is optional, the wrapper should automatically add this if it detects JSON data.
},
session: sessionId
});
console.log(`[test] [✅-client] Successfully posted Request (JSON).`)
if (showData) console.log(postRequestResultJson);
if (typeof postRequestResultJson?.error !== "undefined") {
console.log(`[test] [❌-server] Failed Posting Request (JSON).`)
saveError(
JSON.stringify(postRequestResultJson, null, 2),
"test-server-post_request_json"
)
} else {
if (!scrappey.isJSON(postRequestResultJson.solution.innerText)) {
console.log(`[test] [❌-server] Failed Posting Request (JSON). Response is not JSON.`)
saveError(
JSON.stringify(postRequestResultJson, null, 2),
"test-server-post_request_json"
)
} else if (postRequestResultJson.solution.innerText.includes("[email protected]")) {
console.log(`[test] [✅-server] Successfully posted Request (JSON).`)
} else {
console.log(`[test] [❌-server] Failed Posting Request (JSON). Response does not contain the correct data.`)
saveError(
JSON.stringify(postRequestResultJson, null, 2),
"test-server-post_request_json"
)
}
}
} catch (error) {
console.log("[test] [❌-client] Failed Posting Request (JSON).")
saveError(error, "test-post_request_json");
}
// ! Important ! Either formData or JSON needs to be used.
try {
// Destroy the session
console.log('[test] [~] Session Destruction')
const destroySession = await scrappey.destroySession(sessionId);
console.log(`[test] [✅-client] Successfully destroyed Session.`)
if (showData) console.log(destroySession);
if (typeof destroySession?.error !== "undefined") {
console.log(`[test] [❌-server] Failed Destroying Session.`)
saveError(
JSON.stringify(destroySession, null, 2),
"test-server-destroy_session"
)
}
} catch (error) {
console.log("[test] [❌-client] Failed Destroying Session.")
saveError(error, "test-destroy_session");
}
console.log("[test] All tests have been now executed.")
} catch (error) {
console.error(
"[test] [❌] An error has occured while running the tests."
);
saveError(error, "test");
}
}
async function advancedTests() {
// soon
}
runTest();
advancedTests();