Best Python code snippet using SeleniumBase
test_ex124.py
Source:test_ex124.py
1def initialize_word_list() :2 """This subroutine initializes the list of words. If a word is in this list,3then it is a word"""4 word_list = []5 f = open("words.txt","r")6 for line in f:7 word = line.strip()8 word_list.append ( word )9 f.close()10 return word_list11def string_to_tuple( word ) :12 """Convert string word into a tuple suitable for use as a key to a dictionary13of anagrams"""14 this_word_list = list(word) # convert the string to a list so we can sort it15 this_word_list.sort()16 this_word_tuple = tuple( this_word_list ) # Conver the list to a tuple which can be the key to a dictionary17 return this_word_tuple18def make_dict_of_anagrams ( word_list ) :19 """This function returns a dictionary of lists of words which are anagrams.20The keys to the dictionaries are tuples of the letters in the words, sorted"""21 anagram_dict = dict()22 for word in word_list:23 this_word_tuple = string_to_tuple ( word )24 # If key is in the dictionary, return its value. If not, insert key with a25 # value of default and return default. default defaults to None.26 # See exercise 11-227 anagram_dict[this_word_tuple] = \28 anagram_dict.setdefault( this_word_tuple, [] ) + [word]29 return anagram_dict30def main() :31 word_list = initialize_word_list()32 anagram_dict=make_dict_of_anagrams( word_list )33 i = 034 for word in word_list :35 key = string_to_tuple(word)36 anagram_list = anagram_dict[key]37# if there are anagrams, then print them. Most words have no anagrams 38 if len ( anagram_list ) > 1 :39 print anagram_list40 i += 141 if i > 200 : # I just want 200 anagrams as a proof of concept42 break43# Part 2 of the problem: output the list sorted by length44 anagram_list_length_list = []45 for key in anagram_dict.keys() :46 anagram_list_length_list.append( (len(anagram_dict[key]), key) )47 anagram_list_length_list.sort()48 for i in range( len ( anagram_list_length_list ) ) :49 key = anagram_list_length_list[i][1]50 anagram = anagram_dict[key]51 if len(anagram) > 1 :52 print anagram53DEBUG = False54# To memoize the function children_of_word, we remember which words we already55# know to be children56children_dict = {}57def children_of_word ( word ) :58 """This function returns a list of children of the word. A word is a "child"59of this word if it has 1 letter removed from this word and is in the word list"""60 global word_list # word_list doesn't change and it is huge, so it is okay61 # to pass globally62 children_of_word_list = []63# Here are some corner cases. 64 if word == "I" or word == "a" or word == "A" or word == "":65 return [word]66 if word in children_dict :67 return children_dict[word]68 for i in range( 0, len ( word ) ) :69 if i == 0 :70 potential_child_word = word[1:]71 elif i==len(word)-1 :72 potential_child_word = word[:-1]73 else :74 potential_child_word = word[:i] + word[i+1:]75 if potential_child_word in word_list : # Would this be faster in a dict?76 if DEBUG :77 print potential_child_word,78# since potential_child_word is known to be in the word list, technically, it is79# now a child_word and not a potential_child_word80 children_of_word_list.extend ( \81 children_of_word ( potential_child_word ) )82# momoize this list of children words of potential_child_word83 children_dict[potential_child_word] = children_of_word_list84 return children_of_word_list85if __name__ == "__main__" :86 word_list = initialize_word_list()87 solutions={}88 for word in word_list :89 solutions[word] = children_of_word( word )90 for word in solutions.keys() :...
wordle_test.py
Source:wordle_test.py
...5from seleniumbase import __version__6from seleniumbase import BaseCase7class WordleTests(BaseCase):8 word_list = []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)...
initialize_game.py
Source:initialize_game.py
...11 word_length = int(input())12 except ValueError:13 print("Sorry, you have to enter an integer, e.g. \"5\"")14 return word_length15def initialize_word_list(filename):16 """Initializes word list"""17 # Get word_file18 try:19 word_file = lzma.open(filename, mode="rt")20 except FileNotFoundError:21 print("Error: " + filename + " not found.")22 word_list = []23 # Iterate over word_file and build list24 for word in word_file:25 word_list.append(word.strip().lower())26 word_file.close()27 return word_list28def get_game_file(word_length):29 """Returns appropriate filename for set difficulty"""30 return "resources/word_lists/" + str(word_length) + "-words.txt.xz"31def initialize_game():32 """Uses above functions to initialize all game data"""33 hangman_art = """34 _35 | |36 | |__ __ _ _ __ __ _ _ __ ___ __ _ _ __37 | '_ \\ / _` | '_ \\ / _` | '_ ` _ \\ / _` | '_ \\38 | | | | (_| | | | | (_| | | | | | | (_| | | | |39 |_| |_|\\__,_|_| |_|\\__, |_| |_| |_|\\__,_|_| |_|40 __/ |41 |___/42 """43 # Greet the player44 print(hangman_art)45 # Get word length from player46 word_length = get_word_length()47 # Load correct game file48 filename = get_game_file(word_length)49 # Initialize word_list50 word_list = initialize_word_list(filename)...
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!!