Best Python code snippet using SeleniumBase
wordle_test.py
Source:wordle_test.py
...9 def initialize_word_list(self):10 txt_file = "https://seleniumbase.io/cdn/txt/wordle_words.txt"11 word_string = requests.get(txt_file).text12 self.word_list = ast.literal_eval(word_string)13 def modify_word_list(self, word, letter_status):14 new_word_list = []15 correct_letters = []16 present_letters = []17 for i in range(len(word)):18 if letter_status[i] == "correct":19 correct_letters.append(word[i])20 for w in self.word_list:21 if w[i] == word[i]:22 new_word_list.append(w)23 self.word_list = new_word_list24 new_word_list = []25 for i in range(len(word)):26 if letter_status[i] == "present":27 present_letters.append(word[i])28 for w in self.word_list:29 if word[i] in w and word[i] != w[i]:30 new_word_list.append(w)31 self.word_list = new_word_list32 new_word_list = []33 for i in range(len(word)):34 if (35 letter_status[i] == "absent"36 and word[i] not in correct_letters37 and word[i] not in present_letters38 ):39 for w in self.word_list:40 if word[i] not in w:41 new_word_list.append(w)42 self.word_list = new_word_list43 new_word_list = []44 def skip_if_incorrect_env(self):45 if self.headless:46 message = "This test doesn't run in headless mode!"47 print(message)48 self.skip(message)49 version = [int(i) for i in __version__.split(".") if i.isdigit()]50 if version < [2, 4, 4]:51 message = "This test requires SeleniumBase 2.4.4 or newer!"52 print(message)53 self.skip(message)54 def test_wordle(self):55 self.skip_if_incorrect_env()56 self.open("https://www.nytimes.com/games/wordle/index.html")57 self.click("game-app::shadow game-modal::shadow game-icon")58 self.initialize_word_list()59 keyboard_base = "game-app::shadow game-keyboard::shadow "60 word = random.choice(self.word_list)61 total_attempts = 062 success = False63 for attempt in range(6):64 total_attempts += 165 word = random.choice(self.word_list)66 letters = []67 for letter in word:68 letters.append(letter)69 button = 'button[data-key="%s"]' % letter70 self.click(keyboard_base + button)71 button = "button.one-and-a-half"72 self.click(keyboard_base + button)73 row = 'game-app::shadow game-row[letters="%s"]::shadow ' % word74 tile = row + "game-tile:nth-of-type(%s)"75 self.wait_for_element(tile % "5" + '::shadow [data-state*="e"]')76 letter_status = []77 for i in range(1, 6):78 letter_eval = self.get_attribute(tile % str(i), "evaluation")79 letter_status.append(letter_eval)80 if letter_status.count("correct") == 5:81 success = True82 break83 self.word_list.remove(word)84 self.modify_word_list(word, letter_status)85 self.save_screenshot_to_logs()86 print('\nWord: "%s"\nAttempts: %s' % (word.upper(), total_attempts))87 if not success:88 self.fail("Unable to solve for the correct word in 6 attempts!")...
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!