1+ import os
2+ import time
3+ import urllib .request
4+ from selenium import webdriver
5+ from selenium .common .exceptions import TimeoutException
6+ from selenium .webdriver .common .by import By
7+ from selenium .webdriver .chrome .service import Service
8+ from selenium .webdriver .support import expected_conditions as EC
9+ from selenium .webdriver .support .ui import WebDriverWait
10+
11+
12+ class text_to_be_changed (object ):
13+ def __init__ (self , locator , original_text ):
14+ self .locator = locator
15+ self .original_text = original_text
16+
17+ def __call__ (self , driver ):
18+ element_text = driver .find_element (* self .locator ).text
19+ return element_text != self .original_text
20+
21+
22+ def main () -> int :
23+ PRIME = "It's prime"
24+ NOTPRIME = "It's not prime"
25+
26+ test_cases = [
27+ ("1" , NOTPRIME ),
28+ ("2" , PRIME ),
29+ ("3" , PRIME ),
30+ ("977" , PRIME ),
31+ ("1021" , PRIME ),
32+ ("7139" , NOTPRIME ),
33+ ("28669" , PRIME ),
34+ ("34860283" , PRIME ),
35+ ("76351879" , NOTPRIME ),
36+ ("93416783" , PRIME ),
37+ ("672541787" , PRIME ),
38+ ("896871567" , NOTPRIME ),
39+ ("3472666597" , PRIME ),
40+ ("8632541253" , NOTPRIME ),
41+ ("9633251477" , NOTPRIME ),
42+ ("77265818857" , PRIME ),
43+ ("99999985837" , PRIME ),
44+ ("99999999997" , NOTPRIME ),
45+ ("172658105531" , PRIME ),
46+ ("267714526633" , NOTPRIME ),
47+ ("201423710699" , PRIME ),
48+ ("314159265359" , PRIME ),
49+ ("496635874121" , NOTPRIME ),
50+ ("588736214479" , NOTPRIME ),
51+ ("786632547711" , NOTPRIME ),
52+ ("538941033881" , PRIME ),
53+ ("6322571574869" , NOTPRIME ),
54+ ("23674586397841" , PRIME ),
55+ ("47763354178559" , NOTPRIME ),
56+ ("99887766554433" , NOTPRIME ),
57+ ("124475899632119" , NOTPRIME ),
58+ ("375593357415211" , NOTPRIME ),
59+ ("923174692024939" , PRIME ),
60+ ("1000000000000007" , NOTPRIME ),
61+ ("10000000000000061" , PRIME ),
62+ ("789663254110256361" , NOTPRIME ),
63+ ("1056325741526369751" , NOTPRIME ),
64+ ("1000000000000000003" , PRIME ),
65+ ("10000000000000000003" , NOTPRIME ),
66+ ("18446744073709551615" , NOTPRIME ),
67+ ]
68+
69+ inp = ""
70+
71+ if not os .path .exists ("wasm_exec.js" ):
72+ print ("\033 [1;31m[ERROR]\033 [0m wasm_exec.js not found!" )
73+ return 1
74+ if not os .path .exists ("wasm/lib.wasm" ):
75+ print ("\033 [1;31m[ERROR]\033 [0m wasm/lib.wasm not found!" )
76+ return 1
77+
78+ try :
79+ service = Service ()
80+ options = webdriver .ChromeOptions ()
81+ options .add_argument ("--headless=new" )
82+ driver = webdriver .Chrome (service = service , options = options )
83+ wait = WebDriverWait (driver , 3 )
84+ url = "http://localhost:8086"
85+
86+ for _ in range (30 ):
87+ try :
88+ code = urllib .request .urlopen (url ).getcode ()
89+ if code == 200 :
90+ break
91+ except Exception as e :
92+ print (
93+ "\033 [1;36m[INFO]\033 [0m Unable to connect to Go server, try again..."
94+ )
95+ time .sleep (2 )
96+ else :
97+ print ("\033 [1;31m[ERROR]\033 [0m Unable to connect to Go server!" )
98+ return 1
99+
100+ driver .get (url )
101+ print ("\033 [1;36m[INFO]\033 [0m Start testing!" )
102+
103+ for tc in test_cases :
104+ input_field = driver .find_element (By .ID , "value" )
105+ input_field .send_keys (tc [0 ])
106+
107+ check_button = driver .find_element (By .ID , "check" )
108+ check_button .click ()
109+
110+ inp = tc [0 ]
111+
112+ wait .until (text_to_be_changed ((By .ID , "answer" ), "" ))
113+ answer = driver .find_element (By .ID , "answer" )
114+ if answer .text != tc [1 ]:
115+ print (
116+ '\033 [1;31m[ERROR]\033 [0m Input: "%s", expected: "%s", got: "%s"'
117+ % (tc [0 ], tc [1 ], answer .text )
118+ )
119+ return 1
120+ driver .refresh ()
121+ return 0
122+
123+ except TimeoutException as e :
124+ print ('\033 [1;31m[ERROR]\033 [0m Time limit excceed!! Last input: "%s"' % inp )
125+ return 1
126+
127+ except Exception as e :
128+ print ("\033 [1;31m[ERROR]\033 [0m" )
129+ print (e )
130+ return 1
131+ finally :
132+ driver .quit ()
133+
134+
135+ if __name__ == "__main__" :
136+ status = main ()
137+ if status == 0 :
138+ print ("\033 [1;32mPASS\033 [0m" )
139+ exit (status )
0 commit comments