|
| 1 | +import ast |
| 2 | +import random |
| 3 | +import requests |
| 4 | +from seleniumbase import __version__ |
| 5 | +from seleniumbase import BaseCase |
| 6 | + |
| 7 | + |
| 8 | +class WordleTests(BaseCase): |
| 9 | + |
| 10 | + word_list = [] |
| 11 | + |
| 12 | + def initalize_word_list(self): |
| 13 | + js_file = "https://www.powerlanguage.co.uk/wordle/main.e65ce0a5.js" |
| 14 | + req_text = requests.get(js_file).text |
| 15 | + start = req_text.find("var La=") + len("var La=") |
| 16 | + end = req_text.find("],", start) + 1 |
| 17 | + word_string = req_text[start:end] |
| 18 | + self.word_list = ast.literal_eval(word_string) |
| 19 | + |
| 20 | + def modify_word_list(self, word, letter_status): |
| 21 | + new_word_list = [] |
| 22 | + correct_letters = [] |
| 23 | + present_letters = [] |
| 24 | + for i in range(len(word)): |
| 25 | + if letter_status[i] == "correct": |
| 26 | + correct_letters.append(word[i]) |
| 27 | + for w in self.word_list: |
| 28 | + if w[i] == word[i]: |
| 29 | + new_word_list.append(w) |
| 30 | + self.word_list = new_word_list |
| 31 | + new_word_list = [] |
| 32 | + for i in range(len(word)): |
| 33 | + if letter_status[i] == "present": |
| 34 | + present_letters.append(word[i]) |
| 35 | + for w in self.word_list: |
| 36 | + if word[i] in w and word[i] != w[i]: |
| 37 | + new_word_list.append(w) |
| 38 | + self.word_list = new_word_list |
| 39 | + new_word_list = [] |
| 40 | + for i in range(len(word)): |
| 41 | + if ( |
| 42 | + letter_status[i] == "absent" |
| 43 | + and word[i] not in correct_letters |
| 44 | + and word[i] not in present_letters |
| 45 | + ): |
| 46 | + for w in self.word_list: |
| 47 | + if word[i] not in w: |
| 48 | + new_word_list.append(w) |
| 49 | + self.word_list = new_word_list |
| 50 | + new_word_list = [] |
| 51 | + |
| 52 | + def skip_if_incorrect_env(self): |
| 53 | + if self.headless: |
| 54 | + message = "This test doesn't run in headless mode!" |
| 55 | + print(message) |
| 56 | + self.skip(message) |
| 57 | + version = [int(i) for i in __version__.split(".") if i.isdigit()] |
| 58 | + if version < [2, 4, 0]: |
| 59 | + message = "This test requires SeleniumBase 2.4.0 or newer!" |
| 60 | + print(message) |
| 61 | + self.skip(message) |
| 62 | + |
| 63 | + def test_wordle(self): |
| 64 | + self.skip_if_incorrect_env() |
| 65 | + self.open("https://www.powerlanguage.co.uk/wordle/") |
| 66 | + self.click("game-app::shadow game-modal::shadow game-icon") |
| 67 | + self.initalize_word_list() |
| 68 | + keyboard_base = "game-app::shadow game-keyboard::shadow " |
| 69 | + word = random.choice(self.word_list) |
| 70 | + total_attempts = 0 |
| 71 | + success = False |
| 72 | + for attempt in range(6): |
| 73 | + total_attempts += 1 |
| 74 | + word = random.choice(self.word_list) |
| 75 | + letters = [] |
| 76 | + for letter in word: |
| 77 | + letters.append(letter) |
| 78 | + button = 'button[data-key="%s"]' % letter |
| 79 | + self.click(keyboard_base + button) |
| 80 | + button = 'button[data-key="↵"]' |
| 81 | + self.click(keyboard_base + button) |
| 82 | + self.sleep(1) # Time for the animation |
| 83 | + row = 'game-app::shadow game-row[letters="%s"]::shadow ' % word |
| 84 | + tile = row + "game-tile:nth-of-type(%s)" |
| 85 | + letter_status = [] |
| 86 | + for i in range(1, 6): |
| 87 | + letter_eval = self.get_attribute(tile % str(i), "evaluation") |
| 88 | + letter_status.append(letter_eval) |
| 89 | + if letter_status.count("correct") == 5: |
| 90 | + success = True |
| 91 | + break |
| 92 | + self.word_list.remove(word) |
| 93 | + self.modify_word_list(word, letter_status) |
| 94 | + |
| 95 | + self.save_screenshot_to_logs() |
| 96 | + print('\nWord: "%s"\nAttempts: %s' % (word.upper(), total_attempts)) |
| 97 | + if not success: |
| 98 | + self.fail("Unable to solve for the correct word in 6 attempts!") |
| 99 | + self.sleep(3) |
0 commit comments