-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathmanual-test.py
More file actions
670 lines (530 loc) · 22.1 KB
/
Copy pathmanual-test.py
File metadata and controls
670 lines (530 loc) · 22.1 KB
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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
import os
from scrapingbee import ScrapingBeeClient
API_KEY = os.environ.get("SCRAPINGBEE_API_KEY")
client = ScrapingBeeClient(API_KEY)
# ============================================
# Helper Functions
# ============================================
def assert_test(condition, message):
if not condition:
raise AssertionError(message)
# ============================================
# HTML API Tests (Legacy)
# ============================================
def test_html_get():
print("=== Testing HTML API - GET ===")
try:
response = client.get(
url="https://httpbingo.org/get",
params={"render_js": False}
)
assert_test(response.status_code == 200, f"Expected status 200, got {response.status_code}")
assert_test(response.text, "Response is empty")
assert_test(
"httpbin" in response.text or "headers" in response.text.lower(),
"Response does not contain expected content",
)
print(f"Status: {response.status_code}")
print("✅ HTML GET test passed!\n")
except Exception as e:
print(f"❌ HTML GET test failed: {e}\n")
raise
def test_html_post():
print("=== Testing HTML API - POST ===")
try:
response = client.post(
url="https://httpbingo.org/post",
params={"render_js": False},
data={"test": "data"}
)
assert_test(response.status_code == 200, f"Expected status 200, got {response.status_code}")
assert_test(response.text, "Response is empty")
assert_test("test" in response.text, "Response does not contain posted data")
print(f"Status: {response.status_code}")
print("✅ HTML POST test passed!\n")
except Exception as e:
print(f"❌ HTML POST test failed: {e}\n")
raise
# ============================================
# HTML API Tests (New)
# ============================================
def test_html_api_get():
print("=== Testing HTML API (New) - GET ===")
try:
response = client.html_api(
url="https://httpbingo.org/get",
method="GET",
params={"render_js": False}
)
assert_test(response.status_code == 200, f"Expected status 200, got {response.status_code}")
assert_test(response.text, "Response is empty")
assert_test(
"httpbin" in response.text or "headers" in response.text.lower(),
"Response does not contain expected content",
)
print(f"Status: {response.status_code}")
print("✅ HTML API GET test passed!\n")
except Exception as e:
print(f"❌ HTML API GET test failed: {e}\n")
raise
def test_html_api_post():
print("=== Testing HTML API (New) - POST ===")
try:
response = client.html_api(
url="https://httpbingo.org/post",
method="POST",
params={"render_js": False},
data={"test": "data"}
)
assert_test(response.status_code == 200, f"Expected status 200, got {response.status_code}")
assert_test(response.text, "Response is empty")
assert_test("test" in response.text, "Response does not contain posted data")
print(f"Status: {response.status_code}")
print("✅ HTML API POST test passed!\n")
except Exception as e:
print(f"❌ HTML API POST test failed: {e}\n")
raise
def test_html_api_extract_rules():
print("=== Testing HTML API - Extract Rules ===")
try:
response = client.html_api(
url="https://www.scrapingbee.com/blog/",
params={
"render_js": False,
"extract_rules": {
"title": "h1",
"posts": {
"selector": ".container > div > div > div",
"type": "list",
"output": {
"title": "h4",
"link": "a@href"
}
}
}
}
)
assert_test(response.status_code == 200, f"Expected status 200, got {response.status_code}")
data = response.json()
assert_test(data.get("title"), "Extracted title is missing")
assert_test(isinstance(data.get("posts"), list), "Extracted posts is not a list")
assert_test(len(data.get("posts", [])) > 0, "No posts extracted")
print(f"Status: {response.status_code}")
print(f"Extracted title: {data.get('title')}")
print(f"Extracted posts count: {len(data.get('posts', []))}")
print("✅ HTML API Extract Rules test passed!\n")
except Exception as e:
print(f"❌ HTML API Extract Rules test failed: {e}\n")
raise
def test_html_api_js_scenario():
print("=== Testing HTML API - JS Scenario ===")
try:
response = client.html_api(
url="https://www.scrapingbee.com",
params={
"render_js": True,
"js_scenario": {
"instructions": [
{"wait": 1000},
{"scroll_y": 500},
{"wait": 500}
]
}
}
)
assert_test(response.status_code == 200, f"Expected status 200, got {response.status_code}")
assert_test(response.text, "Response is empty")
print(f"Status: {response.status_code}")
print(f"Content: {response.text[:300]}") # ← Fixed: Match Node.js output
print("✅ HTML API JS Scenario test passed!\n")
except Exception as e:
print(f"❌ HTML API JS Scenario test failed: {e}\n")
raise
def test_html_api_screenshot():
print("=== Testing HTML API - Screenshot ===")
try:
response = client.html_api(
url="https://www.scrapingbee.com",
params={
"render_js": True,
"screenshot": True,
"window_width": 1920,
"window_height": 1080
}
)
assert_test(response.status_code == 200, f"Expected status 200, got {response.status_code}")
assert_test(response.content, "Response is empty")
assert_test(len(response.content) > 10000, "Screenshot seems too small")
# Check PNG signature
png_signature = b'\x89PNG\r\n\x1a\n'
assert_test(response.content[:8] == png_signature, "Response is not a valid PNG")
print(f"Status: {response.status_code}")
print(f"Screenshot size: {len(response.content)} bytes")
print("✅ HTML API Screenshot test passed!\n")
except Exception as e:
print(f"❌ HTML API Screenshot test failed: {e}\n")
raise
def test_html_api_json_response():
print("=== Testing HTML API - JSON Response ===")
try:
response = client.html_api(
url="https://httpbingo.org/get",
params={
"render_js": False,
"json_response": True
}
)
assert_test(response.status_code == 200, f"Expected status 200, got {response.status_code}")
data = response.json()
assert_test(data.get("body") is not None, "JSON response missing body field")
assert_test(data.get("xhr") is not None, "JSON response missing xhr field")
# Handle body as string or object
body = data.get("body")
body_preview = body[:300] if isinstance(body, str) else str(body)[:300]
print(f"Status: {response.status_code}")
print(f"Content: {body_preview}")
print("✅ HTML API JSON Response test passed!\n")
except Exception as e:
print(f"❌ HTML API JSON Response test failed: {e}\n")
raise
def test_html_api_with_headers():
print("=== Testing HTML API - Custom Headers ===")
try:
response = client.html_api(
url="https://httpbingo.org/headers",
params={"render_js": False},
headers={"X-Custom-Header": "CustomValue123"}
)
assert_test(response.status_code == 200, f"Expected status 200, got {response.status_code}")
assert_test("CustomValue123" in response.text, "Custom header not forwarded")
print(f"Status: {response.status_code}")
print("✅ HTML API Custom Headers test passed!\n")
except Exception as e:
print(f"❌ HTML API Custom Headers test failed: {e}\n")
raise
def test_html_api_with_cookies():
print("=== Testing HTML API - Custom Cookies ===")
try:
response = client.html_api(
url="https://httpbingo.org/cookies",
params={"render_js": False},
cookies={"session_id": "abc123", "user_token": "xyz789"}
)
assert_test(response.status_code == 200, f"Expected status 200, got {response.status_code}")
assert_test("abc123" in response.text or "xyz789" in response.text, "Cookies not forwarded")
print(f"Status: {response.status_code}")
print("✅ HTML API Custom Cookies test passed!\n")
except Exception as e:
print(f"❌ HTML API Custom Cookies test failed: {e}\n")
raise
def test_html_api_post_with_headers_and_cookies():
print("=== Testing HTML API - POST with Headers + Cookies ===")
try:
response = client.html_api(
url="https://httpbingo.org/post",
method="POST",
params={"render_js": False},
headers={"X-Test-Header": "TestValue"},
cookies={"session": "mysession123"},
data={"action": "submit"}
)
assert_test(response.status_code == 200, f"Expected status 200, got {response.status_code}")
assert_test("submit" in response.text, "Posted data not in response")
print(f"Status: {response.status_code}")
print("✅ HTML API POST with Headers + Cookies test passed!\n")
except Exception as e:
print(f"❌ HTML API POST with Headers + Cookies test failed: {e}\n")
raise
# ============================================
# Google Search API
# ============================================
def test_google_search():
print("=== Testing Google Search API ===")
try:
response = client.google_search(
search="scrapingbee",
params={"language": "en", "country_code": "us"}
)
assert_test(response.status_code == 200, f"Expected status 200, got {response.status_code}")
data = response.json()
assert_test(data.get("organic_results"), "Missing organic_results in response")
assert_test(isinstance(data.get("organic_results"), list), "organic_results is not a list")
assert_test(len(data.get("organic_results", [])) > 0, "No organic results found")
print(f"Status: {response.status_code}")
print(f"Results found: {len(data.get('organic_results', []))}")
print("✅ Google Search test passed!\n")
except Exception as e:
print(f"❌ Google Search test failed: {e}\n")
raise
# ============================================
# Fast Search API
# ============================================
def test_fast_search():
print("=== Testing Fast Search API ===")
try:
response = client.fast_search(
search="scrapingbee",
params={"country_code": "us", "language": "en"}
)
assert_test(response.status_code == 200, f"Expected status 200, got {response.status_code}")
data = response.json()
assert_test(data.get("organic"), "Missing organic in response")
assert_test(isinstance(data.get("organic"), list), "organic is not a list")
assert_test(len(data.get("organic", [])) > 0, "No organic results found")
print(f"Status: {response.status_code}")
print(f"Results found: {len(data.get('organic', []))}")
print("✅ Fast Search test passed!\n")
except Exception as e:
print(f"❌ Fast Search test failed: {e}\n")
raise
# ============================================
# Amazon API
# ============================================
def test_amazon_search():
print("=== Testing Amazon Search API ===")
try:
response = client.amazon_search(
query="laptop",
params={"domain": "com", "pages": 1}
)
assert_test(response.status_code == 200, f"Expected status 200, got {response.status_code}")
data = response.json()
assert_test(data.get("products"), "Missing products in response")
assert_test(isinstance(data.get("products"), list), "products is not a list")
assert_test(len(data.get("products", [])) > 0, "No products found")
print(f"Status: {response.status_code}")
print(f"Results found: {len(data.get('products', []))}")
print("✅ Amazon Search test passed!\n")
except Exception as e:
print(f"❌ Amazon Search test failed: {e}\n")
raise
def test_amazon_product():
print("=== Testing Amazon Product API ===")
try:
response = client.amazon_product(
query="B0D2Q9397Y",
params={"domain": "com"}
)
assert_test(response.status_code == 200, f"Expected status 200, got {response.status_code}")
data = response.json()
assert_test(data.get("title"), "Missing product title in response")
print(f"Status: {response.status_code}")
print(f"Product title: {data.get('title', '')[:50]}")
print("✅ Amazon Product test passed!\n")
except Exception as e:
print(f"❌ Amazon Product test failed: {e}\n")
raise
def test_amazon_pricing():
print("=== Testing Amazon Pricing API ===")
try:
response = client.amazon_pricing(
asin="B0DPDRNSXV",
params={"domain": "com"}
)
assert_test(response.status_code == 200, f"Expected status 200, got {response.status_code}")
data = response.json()
assert_test(data.get("pricing") is not None or data.get("asin"), "Missing pricing data in response")
print(f"Status: {response.status_code}")
print(f"ASIN: {data.get('asin')}")
print(f"Offers: {len(data.get('pricing', []))}")
print("✅ Amazon Pricing test passed!\n")
except Exception as e:
print(f"❌ Amazon Pricing test failed: {e}\n")
raise
# ============================================
# Walmart API
# ============================================
def test_walmart_search():
print("=== Testing Walmart Search API ===")
try:
response = client.walmart_search(
query="laptop",
params={"device": "desktop", "sort_by": "best_match"}
)
assert_test(response.status_code == 200, f"Expected status 200, got {response.status_code}")
data = response.json()
assert_test(data.get("products"), "Missing products in response")
assert_test(isinstance(data.get("products"), list), "products is not a list")
assert_test(len(data.get("products", [])) > 0, "No products found")
print(f"Status: {response.status_code}")
print(f"Results found: {len(data.get('products', []))}")
print("✅ Walmart Search test passed!\n")
except Exception as e:
print(f"❌ Walmart Search test failed: {e}\n")
raise
def test_walmart_product():
print("=== Testing Walmart Product API ===")
try:
response = client.walmart_product(
product_id="454408250",
params={"device": "desktop"}
)
assert_test(response.status_code == 200, f"Expected status 200, got {response.status_code}")
data = response.json()
assert_test(data.get("title"), "Missing product title in response")
print(f"Status: {response.status_code}")
print(f"Product title: {data.get('title', '')[:50]}")
print("✅ Walmart Product test passed!\n")
except Exception as e:
print(f"❌ Walmart Product test failed: {e}\n")
raise
# ============================================
# YouTube API
# ============================================
def test_youtube_search():
print("=== Testing YouTube Search API ===")
try:
response = client.youtube_search(
search="web scraping tutorial",
params={"sort_by": "relevance", "type": "video"}
)
assert_test(response.status_code == 200, f"Expected status 200, got {response.status_code}")
data = response.json()
assert_test(data.get("results"), "Missing results in response")
assert_test(isinstance(data.get("results"), list), "results is not a list")
assert_test(len(data.get("results", [])) > 0, "No results found")
print(f"Status: {response.status_code}")
print(f"Results found: {len(data.get('results', []))}")
print("✅ YouTube Search test passed!\n")
except Exception as e:
print(f"❌ YouTube Search test failed: {e}\n")
raise
def test_youtube_metadata():
print("=== Testing YouTube Metadata API ===")
try:
response = client.youtube_metadata(video_id="dQw4w9WgXcQ")
assert_test(response.status_code == 200, f"Expected status 200, got {response.status_code}")
data = response.json()
assert_test(data.get("title") or data.get("like_count") is not None, "Missing expected metadata fields")
print(f"Status: {response.status_code}")
print(f"Like count: {data.get('like_count')}")
print("✅ YouTube Metadata test passed!\n")
except Exception as e:
print(f"❌ YouTube Metadata test failed: {e}\n")
raise
def test_youtube_subtitles():
print("=== Testing YouTube Subtitles API ===")
try:
response = client.youtube_subtitles(
video_id="dQw4w9WgXcQ",
params={"language": "en"}
)
assert_test(response.status_code == 200, f"Expected status 200, got {response.status_code}")
data = response.json()
assert_test(data.get("subtitles"), "Missing subtitles in response")
print(f"Status: {response.status_code}")
print(f"Subtitle origins: {list(data.get('subtitles', {}).keys())}")
print("✅ YouTube Subtitles test passed!\n")
except Exception as e:
print(f"❌ YouTube Subtitles test failed: {e}\n")
raise
# ============================================
# ChatGPT API
# ============================================
def test_chatgpt():
print("=== Testing ChatGPT API ===")
try:
response = client.chatgpt(
prompt="What is web scraping? Answer in one sentence.",
params={"search": True}
)
assert_test(response.status_code == 200, f"Expected status 200, got {response.status_code}")
data = response.json()
assert_test(data.get("results_text") or data.get("results_markdown"), "Missing response text")
response_text = (data.get("results_text") or data.get("results_markdown", ""))[:100]
print(f"Status: {response.status_code}")
print(f"Response: {response_text}")
print("✅ ChatGPT test passed!\n")
except Exception as e:
print(f"❌ ChatGPT test failed: {e}\n")
raise
# ============================================
# Gemini API
# ============================================
def test_gemini():
print("=== Testing Gemini API ===")
try:
response = client.gemini(
prompt="What is web scraping? Answer in one sentence.",
params={"country_code": "us"}
)
assert_test(response.status_code == 200, f"Expected status 200, got {response.status_code}")
data = response.json()
assert_test(data.get("results_text") or data.get("results_markdown"), "Missing response text")
response_text = (data.get("results_text") or data.get("results_markdown", ""))[:100]
print(f"Status: {response.status_code}")
print(f"Response: {response_text}")
print("✅ Gemini test passed!\n")
except Exception as e:
print(f"❌ Gemini test failed: {e}\n")
raise
# ============================================
# Usage API
# ============================================
def test_usage():
print("=== Testing Usage API ===")
try:
response = client.usage()
assert_test(response.status_code == 200, f"Expected status 200, got {response.status_code}")
data = response.json()
assert_test(data.get("max_api_credit") is not None, "Missing max_api_credit")
assert_test(data.get("used_api_credit") is not None, "Missing used_api_credit")
assert_test(data.get("max_concurrency") is not None, "Missing max_concurrency")
print(f"Status: {response.status_code}")
print(f"Max API credits: {data.get('max_api_credit')}")
print(f"Used API credits: {data.get('used_api_credit')}")
print(f"Max concurrency: {data.get('max_concurrency')}")
print("✅ Usage test passed!\n")
except Exception as e:
print(f"❌ Usage test failed: {e}\n")
raise
# ============================================
# Run All Tests
# ============================================
def run_tests():
print("\n🚀 Starting ScrapingBee Python SDK Tests\n")
tests = [
# Legacy HTML API
test_html_get,
test_html_post,
# New HTML API
test_html_api_get,
test_html_api_post,
test_html_api_extract_rules,
test_html_api_js_scenario,
test_html_api_screenshot,
test_html_api_json_response,
test_html_api_with_headers,
test_html_api_with_cookies,
test_html_api_post_with_headers_and_cookies,
# Other APIs
test_google_search,
test_fast_search,
test_amazon_search,
test_amazon_product,
test_amazon_pricing,
test_walmart_search,
test_walmart_product,
test_youtube_search,
test_youtube_metadata,
test_youtube_subtitles,
test_chatgpt,
test_gemini,
test_usage,
]
passed = 0
failed = 0
for test in tests:
try:
test()
passed += 1
except Exception:
failed += 1
print("🏁 All tests completed!")
print(f"✅ Passed: {passed}")
print(f"❌ Failed: {failed}")
print(f"📊 Total: {len(tests)}\n")
if failed > 0:
exit(1)
if __name__ == "__main__":
run_tests()