Best Python code snippet using robotframework-pageobjects_python
auth.py
Source:auth.py
1import uuid2from datetime import datetime, timedelta3from db import selectFromTable,insertIntoTable,updateTable4def stringToDateTime(value):5 new_value = datetime.strptime(value, '%Y-%m-%d %H:%M:%S.%f')6 return new_value7def authUser(username, password):8 rows, table_name, find_by_row, value = "user_id,password", "users", "username", username9 row = selectFromTable(rows, table_name, find_by_row, value)10 if row:11 user_id, passwd = row[0], row[1]12 if passwd == password:13 token = uuid.uuid1().hex14 time = datetime.now()15 rows, table_name, find_by_row, value = "*", "users_token", "user_id", user_id16 row = selectFromTable(rows, table_name, find_by_row, value)17 # -------- INSERT INTO TABLE WHEN TOKEN IS NOT PRESENT -------18 if not row:19 user = (user_id, token, time,2)20 table_name, total_values, values = "users_token", "(?,?,?,?)", user21 insertIntoTable(table_name, total_values, values)22 return True, {"token": token}23 # -------- UPDATE THE TOKEN IF TOKEN IS ALREADY PRESENT -------24 new_value = (time, token,2,user_id)25 table_name, rows_to_update, where_cond, values = "users_token", "expire_time = ?,token = ?,time_limit = ?", "user_id = ?", new_value26 updateTable(table_name, rows_to_update, where_cond, values)27 return True, {"token": token}28 return False, {"msg": "Incorrect Password"}29 return False, {"msg": "Username not found"}30def tokenCheck(token, time, reqRoute):31 rows, table_name, find_by_row, value = "expire_time,time_limit", "users_token", "token", token32 row = selectFromTable(rows, table_name, find_by_row, value)33 if not row:34 return False, {"msg": "Token Not Found"}35 str_expire_time,time_limit = row[0],row[1]36 expire_time = stringToDateTime(str_expire_time)37 timeDiff = time - expire_time38 if timeDiff.seconds <= time_limit*60:39 # for requested route we will increase the expire time by 3 min40 if reqRoute:41 # ---------- UPDATE THE TOKEN EXPIRE TIME ------------42 new_expire_time = expire_time + timedelta(minutes=3)43 new_values = (time_limit+3, token)44 table_name, rows_to_update, where_cond, values = "users_token", "time_limit = ?", "token = ?", new_values45 updateTable(table_name, rows_to_update, where_cond, values)46 return True, {"msg": "Success"}...
test_tableelementfinder.py
Source:test_tableelementfinder.py
...18 '//tbody/tr/td[position()=last()-(2-1)]']19 when(self.finder)._search_in_locators('id:table', xpath,20 'content').thenReturn(element)21 self.finder.find_by_col('id:table', 1, 'content')22 def test_find_by_row(self):23 element = mock()24 xpath = ['//tr[2]//*']25 when(self.finder)._search_in_locators('xpath=//table', xpath,26 'content').thenReturn(element)27 self.finder.find_by_row('xpath=//table', 2, 'content')28 xpath = ['//tbody/tr[position()=last()-(3-1)]']29 when(self.finder)._search_in_locators('xpath=//table', xpath,30 'content').thenReturn(element)31 self.finder.find_by_row('xpath=//table', -3, 'content')32 def test_by_search_in_locators(self):33 xpath = ['//th']34 table = mock()35 element1 = mock()36 element2 = mock()37 element1.text = 'not here'38 element2.text = 'content'39 table_elements = [element1, element2]40 when(self.ctx.element_finder).find(41 'css=table', None, True, True, None).thenReturn(table)42 when(self.ctx.element_finder).find(43 xpath[0], None, False, False, table).thenReturn(table_elements)...
db.py
Source:db.py
1import sqlite32def connect_to_db():3 connection = sqlite3.connect('data.db')4 return connection5def selectFromTable(rows, table_name, find_by_row, value):6 connection = connect_to_db()7 cursor = connection.cursor()8 query = f"select {rows} from {table_name} where {find_by_row}=?"9 result = cursor.execute(query, (value,))10 row = result.fetchone()11 connection.close()12 return row13def insertIntoTable(table_name, total_values, values):14 connection = connect_to_db()15 cursor = connection.cursor()16 insert_query = f"INSERT OR IGNORE INTO {table_name} VALUES{total_values}"17 cursor.execute(insert_query, values)18 connection.commit()19 connection.close()20def updateTable(table_name, rows_to_update, where_cond, values):21 connection = connect_to_db()22 cursor = connection.cursor()23 update_query = f"UPDATE {table_name} SET {rows_to_update} where {where_cond}"24 cursor.execute(update_query, values)25 connection.commit()...
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!!