- Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Copy pathwordle_test.py
106 lines (98 loc) · 4.17 KB
/
wordle_test.py
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
"""Solve Wordle with SeleniumBase."""
importast
importrandom
importrequests
fromseleniumbaseimportBaseCase
classWordleTests(BaseCase):
word_list= []
definitialize_word_list(self):
txt_file="https://seleniumbase.github.io/cdn/txt/wordle_words.txt"
word_string=requests.get(txt_file, timeout=3).text
self.word_list=ast.literal_eval(word_string)
defmodify_word_list(self, word, letter_status):
new_word_list= []
correct_letters= []
present_letters= []
foriinrange(len(word)):
ifletter_status[i] =="correct":
correct_letters.append(word[i])
forwinself.word_list:
ifw[i] ==word[i]:
new_word_list.append(w)
self.word_list=new_word_list
new_word_list= []
foriinrange(len(word)):
ifletter_status[i] =="present":
present_letters.append(word[i])
forwinself.word_list:
ifword[i] inwandword[i] !=w[i]:
new_word_list.append(w)
self.word_list=new_word_list
new_word_list= []
foriinrange(len(word)):
ifletter_status[i] =="absent":
if (
word[i] notincorrect_letters
andword[i] notinpresent_letters
):
forwinself.word_list:
ifword[i] notinw:
new_word_list.append(w)
else:
forwinself.word_list:
ifword[i] !=w[i]:
new_word_list.append(w)
self.word_list=new_word_list
new_word_list= []
deftest_wordle(self):
ifself.headless:
self.open_if_not_url("about:blank")
self.skip("Skip this test in headless mode!")
self.open("https://www.nytimes.com/games/wordle/index.html")
self.click_if_visible("button.purr-blocker-card__button", timeout=2)
self.click_if_visible('button:contains("Play")', timeout=2)
self.click_if_visible('svg[data-testid="icon-close"]', timeout=2)
self.remove_elements("div.place-ad")
self.initialize_word_list()
random.seed()
word=random.choice(self.word_list)
num_attempts=0
found_word=False
forattemptinrange(6):
num_attempts+=1
iflen(self.word_list) ==0:
self.fail("Today's word was not found in my dictionary!")
word=random.choice(self.word_list)
letters= []
forletterinword:
letters.append(letter)
button='button[data-key="%s"]'%letter
self.click(button)
button='button[class*="oneAndAHalf"]'
self.click(button)
row= (
'div[class*="Board"] div[class*="Row-module"]:nth-of-type(%s) '
%num_attempts
)
tile=row+'div:nth-child(%s) div[class*="module_tile__"]'
self.wait_for_element(tile%"5"+'[data-state$="t"]')
self.wait_for_element(tile%"5"+'[data-animation="idle"]')
letter_status= []
foriinrange(1, 6):
letter_eval=self.get_attribute(tile%str(i), "data-state")
letter_status.append(letter_eval)
ifletter_status.count("correct") ==5:
found_word=True
break
self.word_list.remove(word)
self.modify_word_list(word, letter_status)
self.save_screenshot_to_logs()
iffound_word:
print('\nWord: "%s"\nAttempts: %s'% (word.upper(), num_attempts))
else:
print('Final guess: "%s" (Not the correct word!)'%word.upper())
self.fail("Unable to solve for the correct word in 6 attempts!")
self.sleep(3)
if__name__=="__main__":
frompytestimportmain
main([__file__, "-s"])