Best Python code snippet using autotest_python
base_page.py
Source:base_page.py
1# encoding: utf-82"""3@author: å§è¡4@contact: zyl140640@163.com5@time: 2022/5/31 17:066@file: base_page.py7@desc: åºç¡ç±»ï¼å°è£
å
ç´ å®ä½æä½8å®ä½ä½¿ç¨ä¼å
级:9Androidï¼AndroidUIAutomator > className = id = AccessibilityId > xpathã10iOSï¼iOSNsPredicateString > className = AccessibilityId> xpathã11"""12import logging13import time14import allure15from appium.webdriver.webdriver import WebDriver16from selenium.webdriver.common.by import By17from selenium.webdriver.support.wait import WebDriverWait18class BasePage:19 def __init__(self, driver: WebDriver):20 """21 åå§åï¼å®ä¹driverçç±»å为WebDriver22 :param driver: web驱å¨å¨23 """24 self.driver = driver25 self.logger = logging26 self.time = time27 def find_element(self, locate, img_doc, timeout=10, frequency=0.5):28 """29 æ£æµå®ä½å
ç´ æ¯å¦åå¨ å®ä½å个å
ç´ 30 :param locate: å
ç´ å®ä½æ¹å¼+è·¯å¾31 :param img_doc: æªå¾è¯´æ32 :param timeout: çå¾
çè¶
æ¶æ¶é´33 :param frequency: 轮询é¢ç34 :return: WebElementå
ç´ å°å35 """36 try:37 el = None38 wait = WebDriverWait(self.driver, timeout, frequency)39 if locate[0] == 'id':40 el = wait.until(lambda diver: self.driver.find_element(By.ID, locate[1]), message='没æ¾å°è¯¥å
ç´ ')41 elif locate[0] == 'xpath':42 el = wait.until(lambda diver: self.driver.find_element(By.XPATH, locate[1]), message='没æ¾å°è¯¥å
ç´ ')43 elif locate[0] == 'name':44 el = wait.until(lambda diver: self.driver.find_element(By.NAME, locate[1]), message='没æ¾å°è¯¥å
ç´ ')45 elif locate[0] == 'css_selector':46 el = wait.until(lambda diver: self.driver.find_element(By.CSS_SELECTOR, locate[1]),47 message='没æ¾å°è¯¥å
ç´ ')48 elif locate[0] == 'tag_name':49 el = wait.until(lambda diver: self.driver.find_element(By.TAG_NAME, locate[1]), message='没æ¾å°è¯¥å
ç´ ')50 elif locate[0] == 'partial_link_text':51 el = wait.until(lambda diver: self.driver.find_element(By.PARTIAL_LINK_TEXT, locate[1]),52 message='没æ¾å°è¯¥å
ç´ ')53 elif locate[0] == 'link_text':54 el = wait.until(lambda diver: self.driver.find_element(By.LINK_TEXT, locate[1]), message='没æ¾å°è¯¥å
ç´ ')55 elif locate[0] == 'class_name':56 el = wait.until(lambda diver: self.driver.find_element(By.CLASS_NAME, locate[1]), message='没æ¾å°è¯¥å
ç´ ')57 elif locate[0] == 'predicate':58 el = wait.until(lambda diver: self.driver.find_element_by_ios_predicate(locate[1]), message='没æ¾å°è¯¥å
ç´ ')59 elif locate[0] == 'accessibility_id':60 el = wait.until(lambda diver: self.driver.find_element_by_accessibility_id(locate[1]), message='没æ¾å°è¯¥å
ç´ ')61 if el is not None:62 return el63 self.logger.info("<{}>,å
ç´ <{}>å®ä½æå".format(img_doc, locate[1]))64 except Exception as e:65 self.logger.error("<{}>å
ç´ <{}>å®ä½å¤±è´¥ï¼".format(img_doc, locate[1]))66 raise e67 def find_elements(self, locate, img_doc, timeout=10, frequency=0.5):68 """69 æ£æµå®ä½å
ç´ æ¯å¦åå¨ å¤ä¸ªå
ç´ 70 :param locate: å
ç´ å®ä½æ¹å¼+è·¯å¾71 :param img_doc: æªå¾è¯´æ72 :param timeout: çå¾
çè¶
æ¶æ¶é´73 :param frequency: 轮询é¢ç74 :return: WebElementå
ç´ å°å è¿ålistç±»å75 """76 try:77 el = None78 wait = WebDriverWait(self.driver, timeout, frequency)79 if locate[0] == 'id':80 el = wait.until(lambda diver: self.driver.find_elements(By.ID, locate[1]), message='没æ¾å°è¯¥å
ç´ ')81 elif locate[0] == 'xpath':82 el = wait.until(lambda diver: self.driver.find_elements(By.XPATH, locate[1]), message='没æ¾å°è¯¥å
ç´ ')83 elif locate[0] == 'name':84 el = wait.until(lambda diver: self.driver.find_elements(By.NAME, locate[1]), message='没æ¾å°è¯¥å
ç´ ')85 elif locate[0] == 'css_selector':86 el = wait.until(lambda diver: self.driver.find_elements(By.CSS_SELECTOR, locate[1]),87 message='没æ¾å°è¯¥å
ç´ ')88 elif locate[0] == 'tag_name':89 el = wait.until(lambda diver: self.driver.find_elements(By.TAG_NAME, locate[1]), message='没æ¾å°è¯¥å
ç´ ')90 elif locate[0] == 'partial_link_text':91 el = wait.until(lambda diver: self.driver.find_elements(By.PARTIAL_LINK_TEXT, locate[1]),92 message='没æ¾å°è¯¥å
ç´ ')93 elif locate[0] == 'link_text':94 el = wait.until(lambda diver: self.driver.find_elements(By.LINK_TEXT, locate[1]), message='没æ¾å°è¯¥å
ç´ ')95 elif locate[0] == 'class_name':96 el = wait.until(lambda diver: self.driver.find_elements(By.CLASS_NAME, locate[1]), message='没æ¾å°è¯¥å
ç´ ')97 elif locate[0] == 'predicate':98 el = wait.until(lambda diver: self.driver.find_elements_by_ios_predicate(locate[1]), message='没æ¾å°è¯¥å
ç´ ')99 elif locate[0] == 'accessibility_id':100 el = wait.until(lambda diver: self.driver.find_elements_by_accessibility_id(locate[1]),101 message='没æ¾å°è¯¥å
ç´ ')102 if el is not None:103 return el104 self.logger.info("<{}>,å
ç´ <{}>å®ä½æå".format(img_doc, locate[1]))105 except Exception as e:106 self.logger.error("<{}>å
ç´ <{}>å®ä½å¤±è´¥ï¼".format(img_doc, locate[1]))107 raise e108 def click(self, locate, img_doc):109 """110 ç¹å»æé®111 :param locate: å
ç´ å®ä½æ¹å¼+è·¯å¾ å
ç»ç±»åä¼ å
¥112 :param img_doc: æªå¾è¯´æ113 """114 try:115 if len(locate) == 2:116 el = self.find_element(locate, img_doc)117 el.click()118 self.logger.info("ç¹å»<{}>æå,å
ç´ åå®ä½æ¹å¼:{}".format(img_doc, locate))119 else:120 el = self.find_elements(locate, img_doc)121 el[locate[2]].click()122 self.logger.info("ç¹å»<{}>æå,å
ç´ åå®ä½æ¹å¼:{}".format(img_doc, locate))123 except Exception as e:124 self.logger.error("ç¹å»<{}>失败,å
ç´ åå®ä½æ¹å¼:{}".format(img_doc, locate))125 self.add_allure_attach(img_doc)126 raise e127 def input_data(self, locate, img_doc, text):128 """129 对è¾å
¥æ¡è¾å
¥ææ¬å
容130 :param locate: å
ç´ å®ä½æ¹å¼131 :param text: è¾å
¥çææ¬å
容132 :param img_doc: æªå¾è¯´æ133 """134 try:135 if len(locate) == 2:136 el = self.find_element(locate, img_doc)137 self.logger.info("å¨<{}>åè½ç<{}>å
ç´ ä¸è¾å
¥å
容为{}: ".format(img_doc, locate[1], text))138 el.send_keys(text)139 else:140 el = self.find_elements(locate, img_doc)141 self.logger.info("å¨<{}>åè½ç<{}>å
ç´ ä¸è¾å
¥å
容为{}: ".format(img_doc, locate[1], text))142 el[locate[2]].send_keys(text)143 except Exception as e:144 self.logger.error("å¨å
ç´ <{}>ä¸è¾å
¥å
容{}失败ï¼".format(locate[1], text))145 self.add_allure_attach(img_doc)146 raise e147 def assert_text(self, locate, img_doc, expect_text):148 """149 è·åWebElement对象çææ¬å¼150 :param locate: å
ç´ å®ä½æ¹å¼151 :param expect_text: é¢æææ¬152 :param img_doc: æªå¾è¯´æ153 :return: WebElement对象çææ¬å¼154 """155 try:156 self.logger.info("å¨{}ä¸è·åå
ç´ <{}>çææ¬å¼".format(img_doc, locate[1]))157 el = self.find_element(locate, img_doc)158 assert el.text == expect_text, "{}æè¨å¤±è´¥".format(img_doc)159 return el.text160 except Exception as e:161 self.logger.error("å¨{}ä¸è·åå
ç´ <{}>çææ¬å¼å¤±è´¥ï¼".format(img_doc, locate[1]))162 self.add_allure_attach(img_doc)163 raise e164 def get_text_click(self, locate, img_doc, text=None):165 """166 è·åWebElement对象çææ¬å
容并è¿è¡ç¹å»æä½ ä¸å¡«åtextåè¿å å
ç´ å¯¹è±¡å表167 :param text: 对象çææ¬å
容 é»è®¤å¯ä»¥ä¸å¡«å168 :param locate: å
ç´ å®ä½æ¹å¼+è·¯å¾169 :param img_doc: æªå¾è¯´æ170 :return: WebElement对象çå±æ§å¼ listç±»å171 """172 try:173 el = self.find_elements(locate, img_doc)174 for lists in el:175 self.logger.info("å
ç´ å表è¿åç{}".format(lists.text))176 if lists.text == text:177 lists.click()178 break179 return el180 except Exception as e:181 self.logger.error("å¨{}ä¸è·åå
ç´ <{}>çå±æ§çææ¬å
容失败ï¼".format(img_doc, locate))182 self.add_allure_attach(img_doc)183 raise e184 def get_elements_text(self, locate, img_doc):185 """186 è·åWebElement对象çææ¬å
容并è¿å187 :param locate: å
ç´ å®ä½æ¹å¼+è·¯å¾188 :param img_doc: æªå¾è¯´æ189 :return: WebElement对象çtext listç±»å190 """191 try:192 element = self.find_elements(locate, img_doc)193 text_list = []194 for el in element:195 text_list.append(el.text)196 return text_list197 except Exception as e:198 self.logger.error("å¨{}ä¸è·åå
ç´ <{}>çå±æ§çææ¬å
容失败ï¼".format(img_doc, locate))199 self.add_allure_attach(img_doc)200 raise e201 def get_size(self):202 """203 è·åçé¢å¤§å°204 :return: è¿åçé¢å¤§å°205 """206 size = self.driver.get_window_size()207 self.logger.info("å±å¹å®½åº¦: {},å±å¹é«åº¦: {}".format(size['width'], size['height']))208 return size209 def add_allure_attach(self, img_doc):210 """211 allure æ¥åä¸æ·»å 失败æªå¾é件212 :param img_doc: æªå¾è¯´æ213 """214 with allure.step("æ·»å 失败æªå¾"):215 file = self.driver.get_screenshot_as_png()216 allure.attach(file, img_doc, allure.attachment_type.PNG)217 def scroll_to_element(self, locate, img_doc):218 """219 seleniuméè¿JSæ»å¨å°æå®å
ç´ 220 :param locate: å
ç´ å®ä½æ¹å¼+è·¯å¾221 :param img_doc: æªå¾è¯´æ222 :return: WebElementå
ç´ å°å223 """224 try:225 el = self.find_element(locate, img_doc)226 self.driver.execute_script("arguments[0].scrollIntoView();", el)227 self.logger.info("<{}>,<{}>å®ä½æå".format(img_doc, locate[1]))228 return el229 except Exception as e:230 self.logger.error("<{}>页é¢å
ç´ <{}>å®ä½å¤±è´¥ï¼å¼å¸¸å
容: <{}>".format(img_doc, locate[1], e))231 raise e232 def target_click(self, x1, y1, img_doc): # x1,y1ä¸ºä½ ç¼åèæ¬æ¶éç¨è®¾å¤çå®é
åæ 233 x_1 = x1 / 375 # 计ç®åæ å¨æ¨ªåæ ä¸çæ¯ä¾ï¼å
¶ä¸375为iphone6sç宽234 y_1 = y1 / 667 # 计ç®åæ å¨çºµåæ 667为iphone6sçé«235 x = self.driver.get_window_size()['width'] # è·å设å¤çå±å¹å®½åº¦236 y = self.driver.get_window_size()['height'] # è·å设å¤å±å¹çé«åº¦237 print(x_1 * x, y_1 * y) # æå°åºç¹å»çåæ ç¹238 try:239 self.driver.tap([(x_1 * x, y_1 * y)], 500) # 模æåæç¹å»æä½240 except Exception as e:241 self.logger.error("å¨{}ä¸è·åå
ç´ <{}>çå±æ§{}çå¼å¤±è´¥ï¼,å¼å¸¸å
容: <{}>".format(img_doc, x1, y1, e))242 self.add_allure_attach(img_doc)...
pybooklet.python.vignette.py
Source:pybooklet.python.vignette.py
1def python_vignette():2 from tests import pyunit_utils3 story1 = [pyunit_utils.locate("Python_Vignette_code_examples/python_add_common_column_to_orig_dataframe.py")]4 story2 = [pyunit_utils.locate("Python_Vignette_code_examples/python_apply_funct_to_columns.py")]5 story3 = [pyunit_utils.locate("Python_Vignette_code_examples/python_apply_funct_to_rows.py")]6 story4 = [pyunit_utils.locate("Python_Vignette_code_examples/python_change_missing_values_to_new_value.py")]7 story5 = [pyunit_utils.locate("Python_Vignette_code_examples/python_column_is_anyfactor.py")]8 story6 = [pyunit_utils.locate("Python_Vignette_code_examples/python_combine_frames_append_one_as_columns.py")]9 story7 = [pyunit_utils.locate("Python_Vignette_code_examples/python_combine_frames_append_one_as_rows.py")]10 story8 = [pyunit_utils.locate("Python_Vignette_code_examples/python_create_categorical_interaction_features.py")]11 story9 = [pyunit_utils.locate("Python_Vignette_code_examples/python_create_frame_from_ordered_dict.py")]12 story10 = [pyunit_utils.locate("Python_Vignette_code_examples/python_create_frame_from_python_dict.py")]13 story11 = [pyunit_utils.locate("Python_Vignette_code_examples/python_create_frame_from_python_list.py")]14 story12 = [pyunit_utils.locate("Python_Vignette_code_examples/python_create_frame_from_python_tuple.py")]15 story13 = [pyunit_utils.locate("Python_Vignette_code_examples/python_create_frame_with_missing_elements.py")]16 story14 = [pyunit_utils.locate("Python_Vignette_code_examples/python_descriptive_stats_entire_frame.py")]17 story15 = [pyunit_utils.locate("Python_Vignette_code_examples/python_descriptive_stats_single_column.py")]18 story16 = [pyunit_utils.locate("Python_Vignette_code_examples/python_determine_string_count_in_element.py")]19 story17 = [pyunit_utils.locate("Python_Vignette_code_examples/python_display_column_names.py")]20 story18 = [pyunit_utils.locate("Python_Vignette_code_examples/python_display_column_types.py")]21 story19 = [pyunit_utils.locate("Python_Vignette_code_examples/python_display_compressiondistributionsummary.py")]22 story20 = [pyunit_utils.locate("Python_Vignette_code_examples/python_display_day_of_month.py")]23 story21 = [pyunit_utils.locate("Python_Vignette_code_examples/python_display_day_of_week.py")]24 story22 = [pyunit_utils.locate("Python_Vignette_code_examples/python_find_missing_data_in_frame.py")]25 story23 = [pyunit_utils.locate("Python_Vignette_code_examples/python_find_rows_missing_data_for_a_column.py")]26 story24 = [pyunit_utils.locate("Python_Vignette_code_examples/python_group_and_apply_function.py")]27 story25 = [pyunit_utils.locate("Python_Vignette_code_examples/python_group_by_multiple_columns.py")]28 story26 = [pyunit_utils.locate("Python_Vignette_code_examples/python_histogramming_data.py")]29 story27 = [pyunit_utils.locate("Python_Vignette_code_examples/python_initializeh2o_example.py")]30 story28 = [pyunit_utils.locate("Python_Vignette_code_examples/python_injest_time_natively.py")]31 story29 = [pyunit_utils.locate("Python_Vignette_code_examples/python_join_results.py")]32 story30 = [pyunit_utils.locate("Python_Vignette_code_examples/python_merge_frames_by_column_name.py")]33 story31 = [pyunit_utils.locate("Python_Vignette_code_examples/python_replace_first_l_with_x.py")]34 story32 = [pyunit_utils.locate("Python_Vignette_code_examples/python_retain_common_categories.py")]35 story33 = [pyunit_utils.locate("Python_Vignette_code_examples/python_select_column_index.py")]36 story34 = [pyunit_utils.locate("Python_Vignette_code_examples/python_select_column_name.py")]37 story35 = [pyunit_utils.locate("Python_Vignette_code_examples/python_select_multiple_column_names.py")]38 story36 = [pyunit_utils.locate("Python_Vignette_code_examples/python_select_multiple_columns_by_index.py")]39 story37 = [pyunit_utils.locate("Python_Vignette_code_examples/python_select_rows_boolean.py")]40 story38 = [pyunit_utils.locate("Python_Vignette_code_examples/python_select_rows_by_slicing.py")]41 story39 = [pyunit_utils.locate("Python_Vignette_code_examples/python_show_column_types.py")]42 story40 = [pyunit_utils.locate("Python_Vignette_code_examples/python_split_using_regex.py")]43 story41 = [pyunit_utils.locate("Python_Vignette_code_examples/python_view_categorical_levels_in_column.py")]44 story42 = [pyunit_utils.locate("Python_Vignette_code_examples/python_view_top_and_bottom_of_frame.py")]45 approved_py_code_examples = story1+story2+story3+story4+story5+story6+story7+story8+story9+story10+story11+story12+story13+story14+story15+story16+story17+story18+story19+story20+story21+story22+story23+story24+story25+story26+story27+story28+story29+story30+story31+story32+story33+story34+story35+story36+story37+story38+story39+story40+story41+story4246 pybooklet_utils.check_code_examples_in_dir(approved_py_code_examples,47 pyunit_utils.locate("Python_Vignette_code_examples"))48 pybooklet_utils.check_story("story1",story1)49 pybooklet_utils.check_story("story2",story2)50 pybooklet_utils.check_story("story3",story3)51 pybooklet_utils.check_story("story4",story4)52 pybooklet_utils.check_story("story5",story5)53 pybooklet_utils.check_story("story6",story6)54 pybooklet_utils.check_story("story7",story7)55 pybooklet_utils.check_story("story8",story8)56 pybooklet_utils.check_story("story9",story9)57 pybooklet_utils.check_story("story10",story10)58 pybooklet_utils.check_story("story11",story11)59 pybooklet_utils.check_story("story12",story12)60 pybooklet_utils.check_story("story13",story13)61 pybooklet_utils.check_story("story14",story14)...
buy_smartbid.py
Source:buy_smartbid.py
1# -*- coding:utf-8 -*-2from selenium import webdriver3import time4from selenium.webdriver.support.ui import WebDriverWait5from selenium.webdriver.common.by import By6from selenium.common.exceptions import TimeoutException, NoSuchElementException7import traceback8from Util.ParsePageObjectRepository import *9from PageObject.LoginPage import *10class Buy_smart:11 def __init__(self,dr):12 self.dr = dr13 self.wait = WebDriverWait(self.dr,10,0.2)14 self.parse_cofig_file = parsepageobjectrepository()15 self.recharge_items = self.parse_cofig_file.getItemSection('smart_buy')16 def get_frame(self):17 locateType, locateExpression = self.recharge_items['buy_smartbid.frame'].split('?')18 smart_frame = getElement(self.dr,locateType,locateExpression)19 return smart_frame20 def get_joinsmart(self):21 locateType, locateExpression = self.recharge_items['buy_smartbid.joinsmart'].split('?')22 joinsmart = getElement(self.dr,locateType,locateExpression)23 return joinsmart24 def get_smartamout(self):#è¾å
¥éé¢æ¡25 locateType, locateExpression = self.recharge_items['buy_smartbid.smart_amount'].split('?')26 smart_amount = getElement(self.dr,locateType,locateExpression)27 return smart_amount28 def get_remaining(self):29 locateType, locateExpression = self.recharge_items['buy_smartbid.remaining'].split('?')30 remaining = getElement(self.dr,locateType,locateExpression)31 return remaining32 def get_amount_submit(self):33 locateType, locateExpression = self.recharge_items['buy_smartbid.amount_submit'].split('?')34 amount_submit = getElement(self.dr,locateType, locateExpression)35 return amount_submit36 def get_buy_now(self):37 locateType, locateExpression = self.recharge_items['buy_smartbid.buy_now'].split('?')38 buy_now =getElement(self.dr,locateType, locateExpression )39 return buy_now40 def get_one_transfer(self):41 locateType, locateExpression = self.recharge_items['buy_smartbid.one_transfer'].split('?')42 one_transfer = getElement(self.dr,locateType,locateExpression)43 return one_transfer44 def get_greet(self):45 locateType, locateExpression = self.recharge_items['buy_smartbid.gree'].split('?')46 gree = getElement(self.dr,locateType, locateExpression)47 return gree48 def get_onesure(self):49 locateType, locateExpression = self.recharge_items['buy_smartbid.onesure'].split('?')50 onesure = getElement(self.dr,locateType, locateExpression)51 return onesure52 def get_recharge(self):53 locateType, locateExpression = self.recharge_items['buy_smartbid.recharge'].split('?')54 recharge = getElement(self.dr, locateType, locateExpression)55 return recharge56 def get_i_know(self):57 locateType, locateExpression = self.recharge_items['buy_smartbid.iknown'].split('?')58 iknow = getElement(self.dr, locateType, locateExpression)59 return iknow60 def get_phoneV(self):61 locateType, locateExpression = self.recharge_items['buy_smartbid.phonev'].split('?')62 phoneV = getElement(self.dr, locateType, locateExpression)63 return phoneV64 def get_passwd(self):65 locateType, locateExpression = self.recharge_items['buy_smartbid.passwd'].split('?')66 passwd= getElement(self.dr, locateType, locateExpression)67 return passwd68 def get_isAgreeReg(self):69 locateType, locateExpression = self.recharge_items['buy_smartbid.reg'].split('?')70 reg = getElement(self.dr, locateType, locateExpression)71 return reg72 def get_renextButton(self):73 locateType, locateExpression = self.recharge_items['buy_smartbid.re_nextbutton'].split('?')74 renextButton = getElement(self.dr, locateType, locateExpression)75 return renextButton76 def get_check_order(self):77 locateType, locateExpression = self.recharge_items['buy_smartbid.check_order'].split('?')78 check_order = getElement(self.dr, locateType, locateExpression)79 return check_order80if __name__=='__main__':81 dr = webdriver.Chrome()82 l = Buy_smart(dr)...
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!!