How to use locate method in autotest

Best Python code snippet using autotest_python

base_page.py

Source:base_page.py Github

copy

Full Screen

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)...

Full Screen

Full Screen

pybooklet.python.vignette.py

Source:pybooklet.python.vignette.py Github

copy

Full Screen

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)...

Full Screen

Full Screen

buy_smartbid.py

Source:buy_smartbid.py Github

copy

Full Screen

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)...

Full Screen

Full Screen

Automation Testing Tutorials

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.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run autotest automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful