-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_html_api.py
More file actions
1050 lines (860 loc) · 43.1 KB
/
test_html_api.py
File metadata and controls
1050 lines (860 loc) · 43.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
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# coding: utf-8
"""
--------------------------------------------------------------------------------------------------------------------
<copyright company="Aspose" file="test_html_api.py">
Copyright (c) 2018 Aspose.HTML for Cloud
</copyright>
<summary>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
</summary>
--------------------------------------------------------------------------------------------------------------------
"""
from __future__ import absolute_import
import unittest
from asposehtmlcloud.api_client import ApiClient as Client
from asposehtmlcloud.api.html_api import HtmlApi
from asposehtmlcloud.configuration import Configuration
from asposehtmlcloud.rest import ApiException
from test.test_helper import TestHelper
class TestHtmlApi(unittest.TestCase):
@classmethod
def setUpClass(cls):
configuration = Configuration(apiKey="60487a72d6325241191177e37ae52146",
appSid="80e32ca5-a828-46a4-9d54-7199dfd3764a",
basePath="https://api-qa.aspose.cloud/v1.1",
authPath="https://api-qa.aspose.cloud/oauth2/token",
debug=True)
cls.api = HtmlApi(configuration)
###############################################################
# Conversion test
###############################################################
def test_get_convert_document_to_image(self):
"""Test case for get_convert_document_to_image
Convert the HTML document from the storage by its name to the specified image format.
param async bool
param str name: Document name. (required)
param str out_format: Resulting image format. (required)
param int width: Resulting image width.
param int height: Resulting image height.
param int left_margin: Left resulting image margin.
param int right_margin: Right resulting image margin.
param int top_margin: Top resulting image margin.
param int bottom_margin: Bottom resulting image margin.
param int x_resolution: Horizontal resolution of resulting image.
param int y_resolution: Vertical resolution of resulting image.
param str folder: The source document folder.
param str storage: The source document storage.
return: file
If the method is called asynchronously,
returns the request thread.
"""
name = "test1.html"
try:
# Upload file to storage
res = TestHelper.upload_file(name)
self.assertEqual(res.Code, 200, "Error upload file to server")
# Convert document to image
res = self.api.get_convert_document_to_image(
name, out_format="png", width=800, height=1000, left_margin=50, right_margin=100,
top_margin=150, bottom_margin=200, x_resolution=300, y_resolution=300,
folder=TestHelper.folder, storage=""
)
self.assertTrue(isinstance(res, str), "Error convert document to image")
# Move to test folder
TestHelper.move_file(str(res), TestHelper.test_dst)
except ApiException as ex:
print("Exception")
print("Info: " + str(ex))
raise ex
def test_get_convert_document_to_image_by_url(self):
"""Test case for get_convert_document_to_image_by_url
Convert the HTML page from the web by its URL to the specified image format.
param async bool
param str source_url: Source page URL. (required)
param str out_format: Resulting image format. (required)
param int width: Resulting image width.
param int height: Resulting image height.
param int left_margin: Left resulting image margin.
param int right_margin: Right resulting image margin.
param int top_margin: Top resulting image margin.
param int bottom_margin: Bottom resulting image margin.
param int x_resolution: Horizontal resolution of resulting image.
param int y_resolution: Vertical resolution of resulting image.
param str folder: The document folder.
param str storage: The document storage.
return: file
If the method is called asynchronously,
returns the request thread.
"""
source_url = "https://stallman.org/articles/anonymous-payments-thru-phones.html"
try:
# Convert url to image
res = self.api.get_convert_document_to_image_by_url(
source_url, out_format="jpeg", width=800, height=1000, left_margin=50, right_margin=100,
top_margin=150, bottom_margin=200, x_resolution=300, y_resolution=300,
folder=TestHelper.folder, storage=""
)
self.assertTrue(isinstance(res, str), "Error convert url to image")
# Move to test folder
TestHelper.move_file(str(res), TestHelper.test_dst)
except ApiException as ex:
print("Exception")
print("Info: " + str(ex))
raise ex
def test_get_convert_document_to_pdf(self):
"""Test case for get_convert_document_to_pdf
Convert the HTML document from the storage by its name to PDF.
param async bool
param str name: Document name. (required)
param int width: Resulting image width.
param int height: Resulting image height.
param int left_margin: Left resulting image margin.
param int right_margin: Right resulting image margin.
param int top_margin: Top resulting image margin.
param int bottom_margin: Bottom resulting image margin.
param str folder: The document folder.
param str storage: The document storage.
return: file
If the method is called asynchronously,
returns the request thread.
"""
name = "test1.html"
try:
# Upload file to storage
res = TestHelper.upload_file(name)
self.assertEqual(res.Code, 200, "Error upload file to server")
# Convert document to pdf
res = self.api.get_convert_document_to_pdf(
name, width=800, height=1000, left_margin=50, right_margin=100, top_margin=150, bottom_margin=200,
folder=TestHelper.folder, storage=""
)
self.assertTrue(isinstance(res, str), "Error convert document to pdf")
# Move to test folder
TestHelper.move_file(str(res), TestHelper.test_dst)
except ApiException as ex:
print("Exception")
print("Info: " + str(ex))
raise ex
def test_get_convert_document_to_pdf_by_url(self):
"""Test case for get_convert_document_to_pdf_by_url
Convert the HTML page from the web by its URL to PDF.
param async bool
param str source_url: Source page URL. (required)
param int width: Resulting image width.
param int height: Resulting image height.
param int left_margin: Left resulting image margin.
param int right_margin: Right resulting image margin.
param int top_margin: Top resulting image margin.
param int bottom_margin: Bottom resulting image margin.
param str folder: The document folder.
param str storage: The document storage.
return: file
If the method is called asynchronously,
returns the request thread.
"""
source_url = "https://stallman.org/articles/anonymous-payments-thru-phones.html"
try:
# Convert url to pdf
res = self.api.get_convert_document_to_pdf_by_url(
source_url, width=800, height=1000, left_margin=50, right_margin=100, top_margin=150,
bottom_margin=200,
folder=TestHelper.folder, storage=""
)
self.assertTrue(isinstance(res, str), "Error convert url to pdf")
# Move to test folder
TestHelper.move_file(str(res), TestHelper.test_dst)
except ApiException as ex:
print("Exception")
print("Info: " + str(ex))
raise ex
def test_get_convert_document_to_xps(self):
"""Test case for get_convert_document_to_xps
Convert the HTML document from the storage by its name to XPS.
param async bool
param str name: Document name. (required)
param int width: Resulting image width.
param int height: Resulting image height.
param int left_margin: Left resulting image margin.
param int right_margin: Right resulting image margin.
param int top_margin: Top resulting image margin.
param int bottom_margin: Bottom resulting image margin.
param str folder: The document folder.
param str storage: The document storage.
return: file
If the method is called asynchronously,
returns the request thread.
"""
name = "test1.html"
try:
# Upload file to storage
res = TestHelper.upload_file(name)
self.assertEqual(res.Code, 200, "Error upload file to server")
# Convert document to xps
res = self.api.get_convert_document_to_xps(
name, width=800, height=1000, left_margin=50, right_margin=100, top_margin=150, bottom_margin=200,
folder=TestHelper.folder, storage=""
)
self.assertTrue(isinstance(res, str), "Error convert document to xps")
# Move to test folder
TestHelper.move_file(str(res), TestHelper.test_dst)
except ApiException as ex:
print("Exception")
print("Info: " + str(ex))
raise ex
def test_get_convert_document_to_xps_by_url(self):
"""Test case for get_convert_document_to_xps_by_url
Convert the HTML page from the web by its URL to XPS.
param async bool
param str source_url: Source page URL. (required)
param int width: Resulting image width.
param int height: Resulting image height.
param int left_margin: Left resulting image margin.
param int right_margin: Right resulting image margin.
param int top_margin: Top resulting image margin.
param int bottom_margin: Bottom resulting image margin.
param str folder: The document folder.
param str storage: The document storage.
return: file
If the method is called asynchronously,
returns the request thread.
"""
source_url = "https://stallman.org/articles/anonymous-payments-thru-phones.html"
try:
# Convert url to xps
res = self.api.get_convert_document_to_xps_by_url(
source_url, width=800, height=1000, left_margin=50, right_margin=100, top_margin=150,
bottom_margin=200,
folder=TestHelper.folder, storage=""
)
self.assertTrue(isinstance(res, str), "Error convert url to xps")
# Move to test folder
TestHelper.move_file(str(res), TestHelper.test_dst)
except ApiException as ex:
print("Exception")
print("Info: " + str(ex))
raise ex
def test_put_convert_document_in_request_to_image(self):
"""Test case for put_convert_document_in_request_to_image
:param async bool
:param str out_path: Full resulting filename (ex. /folder1/folder2/result.jpg) (required)
:param str out_format: (required)
:param file file: A file to be converted. (required)
:param int width: Resulting document page width in points (1/96 inch).
:param int height: Resulting document page height in points (1/96 inch).
:param int left_margin: Left resulting document page margin in points (1/96 inch).
:param int right_margin: Right resulting document page margin in points (1/96 inch).
:param int top_margin: Top resulting document page margin in points (1/96 inch).
:param int bottom_margin: Bottom resulting document page margin in points (1/96 inch).
:param int resolution: Resolution of resulting image. Default is 96 dpi.
:return: file
If the method is called asynchronously,
returns the request thread.
"""
name = "putConvertToImage.png"
test_out_path = "HtmlTestDoc/" + name
test_file = TestHelper.test_src + "test1.html"
try:
# Convert document to image
self.api.put_convert_document_in_request_to_image(
out_path=test_out_path, out_format="png", file=test_file, width=800, height=1000, left_margin=50,
right_margin=100, top_margin=150, bottom_margin=200, resolution=300)
# Download result
res = TestHelper.download_file(name)
save_file = TestHelper.test_dst + name
# Save to test folder
with open(save_file, "wb") as file:
file.write(res.InputStream)
except ApiException as ex:
print("Exception")
print("Info: " + str(ex))
raise ex
def test_put_convert_document_in_request_to_pdf(self):
"""Test case for put_convert_document_in_request_to_pdf
:param async bool
:param str out_path: Full resulting filename (ex. /folder1/folder2/result.pdf) (required)
:param file file: A file to be converted. (required)
:param int width: Resulting document page width in points (1/96 inch).
:param int height: Resulting document page height in points (1/96 inch).
:param int left_margin: Left resulting document page margin in points (1/96 inch).
:param int right_margin: Right resulting document page margin in points (1/96 inch).
:param int top_margin: Top resulting document page margin in points (1/96 inch).
:param int bottom_margin: Bottom resulting document page margin in points (1/96 inch).
:return: file
If the method is called asynchronously,
returns the request thread.
"""
name = "putConvertToPdf.pdf"
test_out_path = "HtmlTestDoc/" + name
test_file = TestHelper.test_src + "test1.html"
try:
# Upload and convert document to pdf
self.api.put_convert_document_in_request_to_pdf(
out_path=test_out_path, file=test_file, width=800, height=1000, left_margin=50, right_margin=100,
top_margin=150, bottom_margin=200)
# Download result
res = TestHelper.download_file(name)
save_file = TestHelper.test_dst + name
# Save to test folder
with open(save_file, "wb") as file:
file.write(res.InputStream)
except ApiException as ex:
print("Exception")
print("Info: " + str(ex))
raise ex
def test_put_convert_document_in_request_to_xps(self):
"""Test case for put_convert_document_in_request_to_xps
:param async bool
:param str out_path: Full resulting filename (ex. /folder1/folder2/result.xps) (required)
:param file file: A file to be converted. (required)
:param int width: Resulting document page width in points (1/96 inch).
:param int height: Resulting document page height in points (1/96 inch).
:param int left_margin: Left resulting document page margin in points (1/96 inch).
:param int right_margin: Right resulting document page margin in points (1/96 inch).
:param int top_margin: Top resulting document page margin in points (1/96 inch).
:param int bottom_margin: Bottom resulting document page margin in points (1/96 inch).
:return: file
If the method is called asynchronously,
returns the request thread.
"""
name = "putConvertToXps.xps"
test_out_path = "HtmlTestDoc/" + name
test_file = TestHelper.test_src + "test1.html"
try:
# Upload and convert document to xps
self.api.put_convert_document_in_request_to_xps(
out_path=test_out_path, file=test_file, width=800, height=1000, left_margin=50, right_margin=100,
top_margin=150, bottom_margin=200)
# Download result
res = TestHelper.download_file(name)
save_file = TestHelper.test_dst + name
# Save to test folder
with open(save_file, "wb") as file:
file.write(res.InputStream)
except ApiException as ex:
print("Exception")
print("Info: " + str(ex))
raise ex
def test_put_convert_document_to_image(self):
"""Test case for put_convert_document_to_image
:param async bool
:param str name: Document name. (required)
:param str out_path: Full resulting filename (ex. /folder1/folder2/result.jpg) (required)
:param str out_format: (required)
:param int width: Resulting document page width in points (1/96 inch).
:param int height: Resulting document page height in points (1/96 inch).
:param int left_margin: Left resulting document page margin in points (1/96 inch).
:param int right_margin: Right resulting document page margin in points (1/96 inch).
:param int top_margin: Top resulting document page margin in points (1/96 inch).
:param int bottom_margin: Bottom resulting document page margin in points (1/96 inch).
:param int resolution: Resolution of resulting image. Default is 96 dpi.
:param str folder: The source document folder.
:param str storage: The source and resulting document storage.
:return: file
If the method is called asynchronously,
returns the request thread.
"""
# Already in storage
name = "test1.html"
result_name = "putConvertDocToImg.tiff"
test_folder = "HtmlTestDoc"
test_out_path = test_folder + "/" + result_name
try:
# Convert document to image in storage
self.api.put_convert_document_to_image(
name, out_path=test_out_path, out_format="tiff", width=800, height=1000, left_margin=50, right_margin=100,
top_margin=150, bottom_margin=200, resolution=300, folder=test_folder, storage="")
# Download result
res = TestHelper.download_file(result_name)
save_file = TestHelper.test_dst + result_name
# Save to test folder
with open(save_file, "wb") as f:
f.write(res.InputStream)
except ApiException as ex:
print("Exception")
print("Info: " + str(ex))
raise ex
def test_put_convert_document_to_pdf(self):
"""Test case for put_convert_document_to_pdf
:param async bool
:param str name: Document name. (required)
:param str out_path: Full resulting filename (ex. /folder1/folder2/result.pdf) (required)
:param int width: Resulting document page width in points (1/96 inch).
:param int height: Resulting document page height in points (1/96 inch).
:param int left_margin: Left resulting document page margin in points (1/96 inch).
:param int right_margin: Right resulting document page margin in points (1/96 inch).
:param int top_margin: Top resulting document page margin in points (1/96 inch).
:param int bottom_margin: Bottom resulting document page margin in points (1/96 inch).
:param str folder: The source document folder.
:param str storage: The source and resulting document storage.
:return: file
If the method is called asynchronously,
returns the request thread.
"""
# Already in storage
name = "test1.html"
result_name = "putConvertDocToPdf.pdf"
test_folder = "HtmlTestDoc"
test_out_path = test_folder + "/" + result_name
try:
# Convert document to pdf in storage
self.api.put_convert_document_to_pdf(
name, out_path=test_out_path, width=800, height=1000, left_margin=50, right_margin=100,
top_margin=150, bottom_margin=200, folder=test_folder, storage="")
# Download result
res = TestHelper.download_file(result_name)
save_file = TestHelper.test_dst + result_name
# Save to test folder
with open(save_file, "wb") as f:
f.write(res.InputStream)
except ApiException as ex:
print("Exception")
print("Info: " + str(ex))
raise ex
def test_put_convert_document_to_xps(self):
"""Test case for put_convert_document_to_xps
:param async bool
:param str name: Document name. (required)
:param str out_path: Full resulting filename (ex. /folder1/folder2/result.xps) (required)
:param int width: Resulting document page width in points (1/96 inch).
:param int height: Resulting document page height in points (1/96 inch).
:param int left_margin: Left resulting document page margin in points (1/96 inch).
:param int right_margin: Right resulting document page margin in points (1/96 inch).
:param int top_margin: Top resulting document page margin in points (1/96 inch).
:param int bottom_margin: Bottom resulting document page margin in points (1/96 inch).
:param str folder: The source document folder.
:param str storage: The source and resulting document storage.
:return: file
If the method is called asynchronously,
returns the request thread.
"""
# Already in storage
name = "test1.html"
result_name = "putConvertDocToXps.xps"
test_folder = "HtmlTestDoc"
test_out_path = test_folder + "/" + result_name
try:
# Convert document to pdf in storage
self.api.put_convert_document_to_xps(
name, out_path=test_out_path, width=800, height=1000, left_margin=50, right_margin=100,
top_margin=150, bottom_margin=200, folder=test_folder, storage="")
# Download result
res = TestHelper.download_file(result_name)
save_file = TestHelper.test_dst + result_name
# Save to test folder
with open(save_file, "wb") as f:
f.write(res.InputStream)
except ApiException as ex:
print("Exception")
print("Info: " + str(ex))
raise ex
###############################################################
# Document test
###############################################################
def test_get_document_fragment_by_x_path(self):
"""Test case for get_document_fragment_by_x_path
Return list of HTML fragments matching the specified XPath query.
param async bool
param str name: The document name. (required)
param str x_path: XPath query string. (required)
param str out_format: Output format. Possible values: 'plain' and 'json'. (required)
param str storage: The document storage.
param str folder: The document folder.
return: file
If the method is called asynchronously,
returns the request thread.
"""
name = "test2.html.zip"
x_path = ".//p"
out_format = "plain"
try:
# Upload file to storage
res = TestHelper.upload_file(name)
self.assertEqual(res.Code, 200, "Error upload file to server")
# Get fragment document from remote storage
res = self.api.get_document_fragment_by_x_path(name=name, x_path=x_path, out_format=out_format,
storage="", folder=TestHelper.folder)
self.assertTrue(isinstance(res, str), "Error get fragment document from remote storage")
# Move to test folder
TestHelper.move_file(str(res), TestHelper.test_dst)
except ApiException as ex:
print("Exception")
print("Info: " + str(ex))
raise ex
def test_get_document_fragment_by_x_path_by_url(self):
"""Return list of HTML fragments matching the specified XPath query by the source page URL.
This method makes a synchronous HTTP request by default. To make an
asynchronous HTTP request, please pass async=True
:param bool async: Asynchronous request
:param str source_url: Source page URL. (required)
:param str x_path: XPath query string. (required)
:param str out_format: Output format. Possible values: 'plain' and 'json'. (required)
:return: File. If the method is called asynchronously, returns the request thread.
"""
source_url = "https://stallman.org/articles/anonymous-payments-thru-phones.html"
x_path = ".//p"
out_format = "plain"
try:
# Get fragment document from remote storage by url
res = self.api.get_document_fragment_by_x_path_by_url(source_url=source_url, x_path=x_path, out_format=out_format)
self.assertTrue(isinstance(res, str), "Error get fragment document from remote storage by url")
# Move to test folder
TestHelper.move_file(str(res), TestHelper.test_dst)
except ApiException as ex:
print("Exception")
print("Info: " + str(ex))
raise ex
def test_get_document_fragments_by_css_selector(self):
"""Return list of HTML fragments matching the specified CSS selector.
This method makes a synchronous HTTP request by default. To make an
asynchronous HTTP request, please pass async=True
:param bool async: Asynchronous request
:param str name: The document name. (required)
:param str selector: CSS selector string. (required)
:param str out_format: Output format. Possible values: 'plain' and 'json'. (required)
:param str folder: The document folder.
:param str storage: The document storage.
:return: File. If the method is called asynchronously, returns the request thread.
"""
name = "test2.html.zip"
selector = "div p"
out_format = "plain"
try:
# Upload file to storage
res = TestHelper.upload_file(name)
self.assertEqual(res.Code, 200, "Error upload file to server")
# Get fragment document from remote storage by css
res = self.api.get_document_fragments_by_css_selector(name=name, selector=selector, out_format=out_format,
storage="", folder=TestHelper.folder)
self.assertTrue(isinstance(res, str), "Error get fragment document from remote storage")
# Move to test folder
TestHelper.move_file(str(res), TestHelper.test_dst)
except ApiException as ex:
print("Exception")
print("Info: " + str(ex))
raise ex
def test_get_document_fragments_by_css_selector_by_url(self):
"""Return list of HTML fragments matching the specified CSS selector by the source page URL.
This method makes a synchronous HTTP request by default. To make an
asynchronous HTTP request, please pass async=True
:param bool async: Asynchronous request
:param str source_url: Source page URL. (required)
:param str selector: CSS selector string. (required)
:param str out_format: Output format. Possible values: 'plain' and 'json'. (required)
:return: File. If the method is called asynchronously, returns the request thread.
"""
source_url = "https://www.w3schools.com/cssref/css_selectors.asp"
selector = 'a[href$=".asp"]' # Get all asp links
out_format = "plain"
try:
# Get fragment matching the specified CSS selector by url
res = self.api.get_document_fragments_by_css_selector_by_url(source_url=source_url, selector=selector, out_format=out_format)
self.assertTrue(isinstance(res, str), "Error get fragment matching the specified CSS selector by url")
# Move to test folder
TestHelper.move_file(str(res), TestHelper.test_dst)
except ApiException as ex:
print("Exception")
print("Info: " + str(ex))
raise ex
def test_get_document_images(self):
"""Test case for get_document_images
Return all HTML document images packaged as a ZIP archive.
param async bool
param str name: The document name. (required)
param str folder: The document folder.
param str storage: The document storage.
return: file
If the method is called asynchronously,
returns the request thread.
"""
name = "test3.html.zip"
try:
# Upload file to storage
res = TestHelper.upload_file(name)
self.assertEqual(res.Code, 200, "Error upload file to server")
# Get images from document on remote storage
res = self.api.get_document_images(name=name, storage="", folder=TestHelper.folder)
self.assertTrue(isinstance(res, str), "Error get images from document")
# Move to test folder
TestHelper.move_file(str(res), TestHelper.test_dst)
except ApiException as ex:
print("Exception")
print("Info: " + str(ex))
raise ex
def test_get_document_images_by_url(self):
"""Test case for get_document_images
Return all HTML document images packaged as a ZIP archive.
param async bool
param str name: The document name. (required)
param str folder: The document folder.
param str storage: The document storage.
return: file
If the method is called asynchronously,
returns the request thread.
"""
source_url = "https://www.google.com/"
try:
# Get images from document by url
res = self.api.get_document_images_by_url(source_url=source_url)
self.assertTrue(isinstance(res, str), "Error get images from url")
# Move to test folder
TestHelper.move_file(str(res), TestHelper.test_dst)
except ApiException as ex:
print("Exception")
print("Info: " + str(ex))
raise ex
###############################################################
# OCR test
###############################################################
def test_get_recognize_and_import_to_html(self):
"""Test case for get_recognize_and_import_to_html
Recognize text from the image file in the storage and import it to HTML format.
param async bool
param str name: The image file name. (required)
param str ocr_engine_lang: OCR engine language - language
param str folder: The source image folder.
param str storage: The source image storage.
return: file
If the method is called asynchronously,
returns the request thread.
"""
file_name = "test_ocr.png"
lang = "en"
try:
# Upload file to storage
res = TestHelper.upload_file(file_name)
self.assertEqual(res.Code, 200, "Error upload file to server")
# Recognize image
res = self.api.get_recognize_and_import_to_html(
file_name, ocr_engine_lang=lang, folder=TestHelper.folder)
self.assertTrue(isinstance(res, str), "Error OCR test")
# Move to test folder
TestHelper.move_file(str(res), TestHelper.test_dst)
except ApiException as ex:
print("Exception")
print("Info: " + str(ex))
raise ex
def test_get_recognize_and_translate_to_html(self):
"""Test case for get_recognize_and_translate_to_html
Recognize text from the image file in the storage, import it to HTML format and translate to specified language.
param async bool
param str name: The image file name. (required)
param str src_lang: Source language - also supposed as the OCR engine language. (required)
param str res_lang: Result language. (required)
param str folder: The source image folder.
param str storage: The source image storage.
return: file
If the method is called asynchronously,
returns the request thread.
"""
file_name = "test_ocr.jpg"
src_lang = "en"
res_lang = "de"
try:
# Upload file to storage
res = TestHelper.upload_file(file_name)
self.assertEqual(res.Code, 200, "Error upload file to server")
# Recognize and translate image
res = self.api.get_recognize_and_translate_to_html(file_name, src_lang, res_lang, folder=TestHelper.folder)
self.assertTrue(isinstance(res, str), "Error OCR and translate test")
# Move to test folder
TestHelper.move_file(str(res), TestHelper.test_dst)
except ApiException as ex:
print("Exception")
print("Info: " + str(ex))
raise ex
###############################################################
# Translation test
###############################################################
def test_get_translate_document(self):
"""Test case for get_translate_document
Translate the HTML document specified by the name from default or specified storage.
param async bool
param str name: Document name. (required)
param str src_lang: Source language. (required)
param str res_lang: Result language. (required)
param str storage: The source document storage.
param str folder: The source document folder.
return: file
If the method is called asynchronously,
returns the request thread.
"""
file_name = "test_en.html"
src_lang = "en"
res_lang = "de"
try:
# Upload file to storage
res = TestHelper.upload_file(file_name)
self.assertEqual(res.Code, 200, "Error upload file to server")
# Translate document
res = self.api.get_translate_document(file_name, src_lang, res_lang,
folder=TestHelper.folder)
self.assertTrue(isinstance(res, str), "Error translate html document")
# Move to test folder
TestHelper.move_file(str(res), TestHelper.test_dst)
except ApiException as ex:
print("Exception")
print("Info: " + str(ex))
raise ex
def test_get_translate_document_by_url(self):
"""Test case for get_translate_document_by_url
Translate the HTML document from Web specified by its URL.
param async bool
param str source_url: Source document URL. (required)
param str src_lang: Source language. (required)
param str res_lang: Result language. (required)
return: file
If the method is called asynchronously,
returns the request thread.
"""
source_url = "https://www.le.ac.uk/oerresources/bdra/html/page_02.htm"
src_lang = "en"
res_lang = "fr"
try:
# Translate url
res = self.api.get_translate_document_by_url(source_url, src_lang, res_lang)
self.assertTrue(isinstance(res, str), "Error translate document from url")
# Move to test folder
TestHelper.move_file(res, TestHelper.test_dst)
except ApiException as ex:
print("Exception")
print("Info: " + str(ex))
raise ex
###############################################################
# Summarization test
###############################################################
def test_get_detect_html_keywords(self):
"""Get the HTML document keywords using the keyword detection service.
This method makes a synchronous HTTP request by default. To make an
asynchronous HTTP request, please pass async=True
:param async bool
:param str name: Document name. (required)
:param str folder: Document folder.
:param str storage: Document storage.
:return: file
If the method is called asynchronously,
returns the request thread.
"""
file_name = "test_en.html"
try:
# Upload file to storage
res = TestHelper.upload_file(file_name)
self.assertEqual(res.Code, 200, "Error upload file to server")
# Get keywords from document
res = self.api.get_detect_html_keywords(file_name, folder=TestHelper.folder)
self.assertTrue(isinstance(res, str), "Error get keywords from html document")
# Move to test folder
TestHelper.move_file(str(res), TestHelper.test_dst + 'keywordsDoc.json')
except ApiException as ex:
print("Exception")
print("Info: " + str(ex))
raise ex
def test_get_detect_html_keywords_by_url(self):
"""Get the keywords from HTML document from Web specified by its URL using the keyword detection service
This method makes a synchronous HTTP request by default. To make an
asynchronous HTTP request, please pass async=True
:param async bool
:param str source_url: Source document URL. (required)
:return: file
If the method is called asynchronously,
returns the request thread.
"""
source_url = "https://www.le.ac.uk/oerresources/bdra/html/page_02.htm"
try:
# Get keyword from url
res = self.api.get_detect_html_keywords_by_url(source_url)
self.assertTrue(isinstance(res, str), "Error get keyword from url")
# Move to test folder
TestHelper.move_file(res, TestHelper.test_dst + 'keywordsUrl.json')
except ApiException as ex:
print("Exception")
print("Info: " + str(ex))
raise ex
###############################################################
# TemplateMerge test
###############################################################
# @unittest.skip("skipping")
def test_get_merge_html_template(self):
"""Test case for put_convert_document_in_request_to_image
This method makes a synchronous HTTP request by default. To make an
asynchronous HTTP request, please pass async=True
:param bool async: Asynchronous request
:param str template_name: Template document name. Template document is HTML or zipped HTML. (required)
:param str data_path: Data source file path in the storage. Supported data format: XML (required)
:param str options: Template merge options: reserved for further implementation.
:param str folder: The template document folder.
:param str storage: The template document and data source storage.
:return: File. If the method is called asynchronously, returns the request thread.
"""
template_name = "HtmlTemplate.html"
template_data = "XmlSourceData.xml"
result_name = "GetTemplateMergePython.html"
options = ""
folder = "HtmlTestDoc"
storage = ""
data_path = folder + "/" + template_data
try:
res = TestHelper.upload_file(template_name)
self.assertEqual(res.Code, 200, "Error upload file to server")
res = TestHelper.upload_file(template_data)
self.assertEqual(res.Code, 200, "Error upload file to server")
# Get result file from storage
res = self.api.get_merge_html_template(template_name=template_name,data_path=data_path,options=options,
folder=folder, storage=storage)
self.assertTrue(isinstance(res, str), "Error merge template")
# Move to test folder
TestHelper.move_file(str(res), TestHelper.test_dst + result_name)
except ApiException as ex: