Best Python code snippet using SeleniumBase
database_testing.py
Source:database_testing.py
...13 self.driver.get(siteURL)14 # attempts SQL injection attack15 def testInjectSQLTestA(self):16 self.elem = self.driver.find_element_by_name("username")17 self.elem.send_keys("joe'); DROP TABLE login")18 self.elem = self.driver.find_element_by_name("password")19 self.elem.send_keys("BLAH")20 self.elem = self.driver.find_element_by_xpath("/html/body/form/div[2]/button")21 self.elem.click()22 # assertion23 self.assertEqual(self.driver.current_url, "http://localhost:7777/login")24 @classmethod25 def tearDown(self):26 self.driver.quit()27class inputValidationChecking(unittest.TestCase):28 @classmethod29 def setUp(self):30 self.driver = webdriver.Chrome(chromeLocation)31 self.driver.get(siteURL)32 self.elem = self.driver.find_element_by_link_text("Sign Up")33 self.elem.click()34 def testCreateUserValid(self):35 # Fills in signup options to test inputs36 self.elem = self.driver.find_element_by_name("email")37 self.elem.send_keys("email@email.com")38 self.elem = self.driver.find_element_by_name("firstName")39 self.elem.send_keys("Timmy")40 self.elem = self.driver.find_element_by_name("lastName")41 self.elem.send_keys("Little")42 self.elem = self.driver.find_element_by_name("type")43 self.elem.click()44 self.elem = self.driver.find_element_by_name("password")45 self.elem.send_keys("password")46 self.elem = self.driver.find_element_by_name("psw2")47 self.elem.send_keys("password")48 49 self.elem = Select(self.driver.find_element_by_name("securityQuestion"))50 self.elem.select_by_index[0]51 self.elem = self.driver.find_element_by_name("answer")52 self.elem.send_keys("Momma Harp")53 self.elem = driver.driver.find_element_by_name("signupButton")54 self.elem.click()55 time.sleep(2)56 self.elem.send_keys(Keys.ENTER)57 #assertion58 self.assertEqual(self.driver.current_url, "http://localhost:7777/courses")59 def testEmailValidationDot(self):60 # Fills in signup options to test inputs61 self.elem = self.driver.find_element_by_name("email")62 self.elem.send_keys("email@emailcom")63 self.elem = self.driver.find_element_by_name("firstName")64 self.elem.send_keys("Timmy")65 self.elem = self.driver.find_element_by_name("lastName")66 self.elem.send_keys("Little")67 self.elem = self.driver.find_element_by_name("type")68 self.elem.click()69 self.elem = self.driver.find_element_by_name("password")70 self.elem.send_keys("password")71 self.elem = self.driver.find_element_by_name("psw2")72 self.elem.send_keys("password")73 74 self.elem = Select(self.driver.find_element_by_name("securityQuestion"))75 self.elem.select_by_index[0]76 self.elem = self.driver.find_element_by_name("answer")77 self.elem.send_keys("Momma Harp")78 self.elem = driver.driver.find_element_by_name("signupButton")79 self.elem.click()80 self.assertEqual(self.driver.current_url, "http://localhost:7777/signup")81 def testEmailValidationAt(self):82 # Fills in signup options to test inputs83 self.elem = self.driver.find_element_by_name("email")84 self.elem.send_keys("emailemail.com")85 self.elem = self.driver.find_element_by_name("firstName")86 self.elem.send_keys("Timmy")87 self.elem = self.driver.find_element_by_name("lastName")88 self.elem.send_keys("Little")89 self.elem = self.driver.find_element_by_name("type")90 self.elem.click()91 self.elem = self.driver.find_element_by_name("password")92 self.elem.send_keys("password")93 self.elem = self.driver.find_element_by_name("psw2")94 self.elem.send_keys("password")95 self.elem = Select(self.driver.find_element_by_name("securityQuestion"))96 self.elem.select_by_index[0]97 self.elem = self.driver.find_element_by_name("answer")98 self.elem.send_keys("Momma Harp")99 self.elem = self.driver.find_element_by_name("signupButton")100 self.elem.click()101 self.assertEqual(self.driver.current_url, "http://localhost:7777/signup")102 def testPasswordLength(self):103 # Fills in signup options to test inputs104 self.elem = self.driver.find_element_by_name("email")105 self.elem.send_keys("email@email.com")106 self.elem = self.driver.find_element_by_name("firstName")107 self.elem.send_keys("Timmy")108 self.elem = self.driver.find_element_by_name("lastName")109 self.elem.send_keys("Little")110 self.elem = self.driver.find_element_by_name("type")111 self.elem.click()112 self.elem = self.driver.find_element_by_name("password")113 self.elem.send_keys("passwor")114 self.elem = self.driver.find_element_by_name("psw2")115 self.elem.send_keys("passwor")116 self.elem = Select(self.driver.find_element_by_name("securityQuestion"))117 self.elem.select_by_index[0]118 self.elem = self.driver.find_element_by_name("answer")119 self.elem.send_keys("Momma Harp")120 self.elem = self.driver.find_element_by_name("signupButton")121 self.elem.click()122 self.assertEqual(self.driver.current_url, "http://localhost:7777/signup")123 def testPasswordTypePunct(self):124 # Fills in signup options to test inputs125 self.elem = self.driver.find_element_by_name("email")126 self.elem.send_keys("email@email.com")127 self.elem = self.driver.find_element_by_name("firstName")128 self.elem.send_keys("Timmy")129 self.elem = self.driver.find_element_by_name("lastName")130 self.elem.send_keys("Little")131 self.elem = self.driver.find_element_by_name("type")132 self.elem.click()133 self.elem = self.driver.find_element_by_name("password")134 self.elem.send_keys("password!")135 self.elem = self.driver.find_element_by_name("psw2")136 self.elem.send_keys("password!")137 self.elem = Select(self.driver.find_element_by_name("securityQuestion"))138 self.elem.select_by_index[0]139 self.elem = self.driver.find_element_by_name("answer")140 self.elem.send_keys("Momma Harp")141 self.elem = self.driver.find_element_by_name("signupButton")142 self.elem.click()143 self.assertEqual(self.driver.current_url, "http://localhost:7777/signup")144 def testPasswordTypeSpace(self):145 # Fills in signup options to test inputs146 self.elem = self.driver.find_element_by_name("email")147 self.elem.send_keys("email@email.com")148 self.elem = self.driver.find_element_by_name("firstName")149 self.elem.send_keys("Timmy")150 self.elem = self.driver.find_element_by_name("lastName")151 self.elem.send_keys("Little")152 self.elem = self.driver.find_element_by_name("type")153 self.elem.click()154 self.elem = self.driver.find_element_by_name("password")155 self.elem.send_keys("pass word")156 self.elem = self.driver.find_element_by_name("psw2")157 self.elem.send_keys("pass word")158 self.elem = Select(self.driver.find_element_by_name("securityQuestion"))159 self.elem.select_by_index[0]160 self.elem = self.driver.find_element_by_name("answer")161 self.elem.send_keys("Momma Harp")162 self.elem = self.driver.find_element_by_name("signupButton")163 self.elem.click()164 self.assertEqual(self.driver.current_url, "http://localhost:7777/signup")165 def TestPasswordMatch(self):166 # Fills in signup options to test inputs167 self.elem = self.driver.find_element_by_name("email")168 self.elem.send_keys("email@email.com")169 self.elem = self.driver.find_element_by_name("firstName")170 self.elem.send_keys("Timmy")171 self.elem = self.driver.find_element_by_name("lastName")172 self.elem.send_keys("Little")173 self.elem = self.driver.find_element_by_name("type")174 self.elem.click()175 self.elem = self.driver.find_element_by_name("password")176 self.elem.send_keys("password")177 self.elem = self.driver.find_element_by_name("psw2")178 self.elem.send_keys("passwrod")179 self.elem = Select(self.driver.find_element_by_name("securityQuestion"))180 self.elem.select_by_index[0]181 self.elem = self.driver.find_element_by_name("answer")182 self.elem.send_keys("Momma Harp")183 self.elem = self.driver.find_element_by_name("signupButton")184 self.elem.click()185 self.assertEqual(self.driver.current_url, "http://localhost:7777/signup")186 @classmethod187 def tearDown(self):188 self.driver.close()189class normalFunctions(unittest.TestCase):190 @classmethod191 def setUp(self):192 self.driver = webdriver.Chrome(chromeLocation)193 self.driver.get(siteURL)194 def testNormalSignIn(self):195 self.elem = self.driver.find_element_by_name("username")196 self.elem.send_keys("jacobsc@gmail.com")197 self.elem = self.driver.find_element_by_name("password")198 self.elem.send_keys("password")199 self.elem = self.driver.find_element_by_tag_name("button")200 self.elem.click()201 #assertion202 self.assertEqual(self.driver.current_url, "http://localhost:7777/courses")203 def testNormalAssignment(self):204 pass205 @classmethod206 def tearDown(self):207 self.driver.close()208class testSandboxFunctionality(unittest.TestCase):209 @classmethod210 def setUp(self):211 # Get to the sandbox page212 self.driver = webdriver.Chrome(chromeLocation)213 self.driver.get(siteURL)214 self.elem = self.driver.find_element_by_name("username")215 self.elem.send_keys("jacobsc@gmail.com")216 self.elem = self.driver.find_element_by_name("password")217 self.elem.send_keys("password")218 self.elem = self.driver.find_element_by_tag_name("button")219 self.elem.click()220 self.elem = self.driver.find_element_by_id("sandbox")221 self.elem.click()222 def testSandboxNormal(self):223 self.elem = self.driver.find_element_by_name("code")224 self.elem.send_keys("#include <iostream>").send_keys(Keys.ENTER)225 self.elem.send_keys("using namespace std;").send_keys(Keys.ENTER)226 self.elem.send_keys(Keys.ENTER)227 self.elem.send_keys("int main() {").send_keys(Keys.ENTER)228 self.elem.send_keys("cout << 'hello world!' << endl;").send_keys(Keys.ENTER)229 self.elem.send_keys("return 0;").send_keys(Keys.ENTER)230 self.elem.send_keys("}").send_keys(Keys.ENTER)231 self.elem = self.driver.find_element_by_link_text("Compile")232 self.elem.click()233 time.sleep(6)234 self.elem = self.driver.find_element_by_link_text("Run")235 self.elem.click()236 time.sleep(6)237 #assertion238 self.assertTrue(self.driver.find_element_by_name("output").text == 'hello world')239 def testSandboxSystem(self):240 self.elem = self.driver.find_element_by_name("code")241 self.elem.send_keys("#include <iostream>").send_keys(Keys.ENTER)242 self.elem.send_keys("using namespace std;").send_keys(Keys.ENTER)243 self.elem.send_keys(Keys.ENTER)244 self.elem.send_keys("int main() {").send_keys(Keys.ENTER)245 self.elem.send_keys("system('ls -l')").send_keys(Keys.ENTER)246 self.elem.send_keys("return 0;").send_keys(Keys.ENTER)247 self.elem.send_keys("}").send_keys(Keys.ENTER)248 self.elem = self.driver.find_element_by_link_text("Compile")249 self.elem.click()250 time.sleep(6)251 self.elem = self.driver.find_element_by_link_text("Run")252 self.elem.click()253 time.sleep(6)254 #assertion255 self.assertTrue(self.driver.find_element_by_name("output").text == "")256 @classmethod257 def tearDown(self):258 self.driver.close()259if __name__ == "__main__":260 print("Welcome to GLaDOS")261 unittest.main()
test_dualmode_arrows.py
Source:test_dualmode_arrows.py
...3def test_dualmode_arrows(notebook):4 # Tests in command mode.5 # Setting up the cells to test the keys to move up.6 notebook.to_command_mode()7 [notebook.body.send_keys("b") for i in range(3)]8 # Use both "k" and up arrow keys to moving up and enter a value.9 # Once located on the top cell, use the up arrow keys to prove the top cell is still selected.10 notebook.body.send_keys("k")11 notebook.body.send_keys(Keys.ENTER)12 notebook.body.send_keys("2")13 notebook.to_command_mode()14 notebook.body.send_keys(Keys.UP)15 notebook.body.send_keys(Keys.ENTER)16 notebook.body.send_keys("1")17 notebook.to_command_mode()18 notebook.body.send_keys("k")19 notebook.body.send_keys(Keys.UP)20 notebook.body.send_keys(Keys.ENTER)21 notebook.body.send_keys("0")22 notebook.to_command_mode()23 assert notebook.get_cells_contents() == ["0", "1", "2", ""]24 # Use the "k" key on the top cell as well25 notebook.body.send_keys("k")26 notebook.body.send_keys(Keys.ENTER)27 notebook.body.send_keys(" edit #1")28 notebook.to_command_mode()29 assert notebook.get_cells_contents() == ["0 edit #1", "1", "2", ""]30 # Setting up the cells to test the keys to move down31 [notebook.body.send_keys("j") for i in range(3)]32 [notebook.body.send_keys("a") for i in range(2)]33 notebook.body.send_keys("k")34 # Use both "j" key and down arrow keys to moving down and enter a value.35 # Once located on the bottom cell, use the down arrow key to prove the bottom cell is still selected.36 notebook.body.send_keys(Keys.DOWN)37 notebook.body.send_keys(Keys.ENTER)38 notebook.body.send_keys("3")39 notebook.to_command_mode()40 notebook.body.send_keys("j")41 notebook.body.send_keys(Keys.ENTER)42 notebook.body.send_keys("4")43 notebook.to_command_mode()44 notebook.body.send_keys("j")45 notebook.body.send_keys(Keys.DOWN)46 notebook.body.send_keys(Keys.ENTER)47 notebook.body.send_keys("5")48 notebook.to_command_mode()49 assert notebook.get_cells_contents() == ["0 edit #1", "1", "2", "3", "4", "5"]50 # Use the "j" key on the top cell as well51 notebook.body.send_keys("j")52 notebook.body.send_keys(Keys.ENTER)53 notebook.body.send_keys(" edit #1")54 notebook.to_command_mode()55 assert notebook.get_cells_contents() == ["0 edit #1", "1", "2", "3", "4", "5 edit #1"]56 # On the bottom cell, use both left and right arrow keys to prove the bottom cell is still selected.57 notebook.body.send_keys(Keys.LEFT)58 notebook.body.send_keys(Keys.ENTER)59 notebook.body.send_keys(", #2")60 notebook.to_command_mode()61 assert notebook.get_cells_contents() == ["0 edit #1", "1", "2", "3", "4", "5 edit #1, #2"]62 notebook.body.send_keys(Keys.RIGHT)63 notebook.body.send_keys(Keys.ENTER)64 notebook.body.send_keys(" and #3")65 notebook.to_command_mode()66 assert notebook.get_cells_contents() == ["0 edit #1", "1", "2", "3", "4", "5 edit #1, #2 and #3"]67 # Tests in edit mode.68 # First, erase the previous content and then setup the cells to test the keys to move up.69 [notebook.browser.find_element_by_class_name("fa-cut.fa").click() for i in range(6)]70 [notebook.body.send_keys("b") for i in range(2)]71 notebook.body.send_keys("a")72 notebook.body.send_keys(Keys.ENTER)73 # Use the up arrow key to move down and enter a value.74 # We will use the left arrow key to move one char to the left since moving up on last character only moves selector to the first one.75 # Once located on the top cell, use the up arrow key to prove the top cell is still selected.76 notebook.body.send_keys(Keys.UP)77 notebook.body.send_keys("1")78 notebook.body.send_keys(Keys.LEFT)79 [notebook.body.send_keys(Keys.UP) for i in range(2)]80 notebook.body.send_keys("0")81 # Use the down arrow key to move down and enter a value.82 # We will use the right arrow key to move one char to the right since moving down puts selector to the last character.83 # Once located on the bottom cell, use the down arrow key to prove the bottom cell is still selected. 84 notebook.body.send_keys(Keys.DOWN)85 notebook.body.send_keys(Keys.RIGHT)86 notebook.body.send_keys(Keys.DOWN)87 notebook.body.send_keys("2")88 [notebook.body.send_keys(Keys.DOWN) for i in range(2)]89 notebook.body.send_keys("3")90 notebook.to_command_mode()...
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!!