def get_discount(): #获取提交订单时计算的优惠金额 discount_value = PageAction.getelement_text( read_file.get_option('workbench', 'discount_ele')) discount = discount_value[0:-1] return discount PageAction.pause(2)
def test_cash_payment(setup_module): enter_workbench.workbench_payment() # 输入手机号码, enter_workbench.workbench_inputPhone() enter_workbench.input_oilgunNo(enter_workbench.get_diesegunNo()) oilPrice=round(random.random()*100,2) #加油金额随机数字,保留2个小数位 enter_workbench.input_oilPrice(oilPrice) #输入加油金额 enter_workbench.clear_coupon() # 获取提交订单时计算的实收金额 receivepayment = enter_workbench.get_receivepayment() print('实收金额:', receivepayment) # 获取提交订单时计算的优惠金额 discount = enter_workbench.get_discount() print('提交订单时计算的优惠金额', discount) # enter_workbench.input_cash_receipt() Amount_Collected=enter_workbench.get_cash_receipt() print('提交订单时计算的收款金额', Amount_Collected) PageAction.capture_screen() enter_workbench.Cash_payment_click() enter_workbench.click_pay() if PageAction.assert_word('现金支付成功'): enter_workbench.close_success_pay_Popup() enter_workbench.close_payment_windows() print('现金支付流程成功') else: PageAction.assert_word('现金支付失败') enter_workbench.close_fail_pay_Popup() enter_workbench.close_payment_windows() print('现金支付流程失败') error('现金支付流程失败')
def close_payment_windows(): # 关闭收款弹窗 PageAction.click_SvgelementXpath( read_file.get_option('workbench', 'close_pay')) # Action.click_SvgelementXpath(locatorMethod='xpath', # localExpression='//span[@class="ant-modal-close-x"]/i/*[name()="svg"]/*[name()="path"]') PageAction.pause(3)
def get_order_discount(): #获取订单列表优惠金额 # table_tagname=find.getElement_tag_name('table') #获取表格标签 table_tagname = find.getElement( read_file.get_option('workbench', 'table_real_pay')) table_element = Table.Table(table_tagname).getCell(rowNo=2, colNo=7) #获取表格指定的单元格对象 return table_element.text #返回指定单元格对象的文本 PageAction.pause(2)
def get_order_realpay(): #获取订单列表实收金额 # 方法 1:return Action.getelement_text(locatorMethod='xpath',localExpression='//tbody[@class="ant-table-tbody"]/tr[1]/td[6]') # table_tagname=find.getElement_tag_name('table') #获取表格标签 table_tagname = find.getElement( read_file.get_option('workbench', 'table_real_pay')) table_element = Table.Table(table_tagname).getCell(rowNo=2, colNo=6) #获取表格指定的单元格对象 return table_element.text #返回指定单元格对象的文本 PageAction.pause(2)
def get_receiptpaymoney(): #获取提交订单时计算的实付金额 receipt_value = PageAction.getelement_attribute(read_file.get_option( 'workbench', 'real_receipt'), attr='value') # receipt = float(receipt_value) # 现金支付模块:实付金额浮点型 print('订单实付金额:%s' % (receipt_value)) PageAction.click(read_file.get_option('workbench', 'cash_pay_button')) PageAction.pause(2) return receipt_value
def getElement(localtion, seconds=None): time.sleep(1) if isinstance(localtion, str) and ',' in localtion: selector_by = localtion.split(',')[0] localExpression = localtion.split(',')[1] # print(selector_by,localExpression) if not seconds: seconds = 5 if selector_by == 'x' or selector_by == 'xpath': localType = 'xpath' elif selector_by == 'i' or selector_by == 'id': localType = 'id' elif selector_by == 'n' or selector_by == 'name': localType = 'name' elif selector_by == 't' or selector_by == 'tag name': localType = 'tag name' elif selector_by == "l" or selector_by == 'link': localType = 'link text' elif selector_by == "p" or selector_by == 'partial_link_text': localType = 'partial link text' elif selector_by == "c" or selector_by == 'css_selector': localType = 'css selector' elif selector_by == "c" or selector_by == 'class_name': localType = 'class selector' else: raise NameError("Please enter a valid type of targeting elements.") try: ele = WebDriverWait(get_selenium(), seconds).until( lambda x: x.find_element(by=localType, value=localExpression)) return ele except Exception as e: raise PageAction.JYHException("找不到元素") else: print('当前输入错误,要求文本类型且带逗号分隔符')
def test_Balance_payment(setup_module): enter_workbench.workbench_payment() #输入手机号码, enter_workbench.Balance_payment_click() # 支付方式按钮:余额支付 enter_workbench.input_oilgunNo(enter_workbench.get_diesegunNo()) # 输入汽油枪号 #[0::2]-->取奇数位包含列表末位,[0:-1:2]-->取奇数位不包含末位 before_dieselBalance, before_commonlBalance = enter_workbench.workbench_inputPhone( )[0::2] #加油前-获取汽油余额+通用余额 if not (before_dieselBalance and before_commonlBalance): oilPrice = random.randint(2, 10) else: oilPrice = float(before_dieselBalance) + float( before_commonlBalance) + 1 #加油金额>余额 enter_workbench.input_oilPrice(oilPrice) #输入加油金额 enter_workbench.clear_coupon() # 获取提交订单时计算的实收金额 receivepayment = enter_workbench.get_receivepayment() print('实收金额:', receivepayment) # 获取提交订单时计算的优惠金额 discount = enter_workbench.get_discount() print('提交订单时计算的优惠金额', discount) PageAction.capture_screen() # 收款操作 PageAction.roll_bottom() enter_workbench.click_pay() if PageAction.assert_word('余额支付失败'): print('余额不足支付失败,与预期一致') enter_workbench.close_fail_pay_Popup() enter_workbench.close_payment_windows() else: PageAction.assert_word('余额支付成功') enter_workbench.close_payment_windows() print('余额不足不该支持成功,与预期不一致') error('余额不足不该支持成功,与预期不一致')
def login(): # helper.open_browser('chrome') # helper.open_url() #从配置读取用户信息 userName = Parse_JYH_Config().get_option(sectionName='account_info', option='station_username') #输入用户名 PageAction.input_string(read_file.get_option('login', 'userName'), content=userName) password = Parse_JYH_Config().get_option(sectionName='account_info', option='station_password') # 输入密码 PageAction.input_string(read_file.get_option('login', 'password'), content=password) # 输入验证码 txtCode = Parse_JYH_Config().get_option(sectionName='account_info', option='txtCode') PageAction.input_string(read_file.get_option('login', 'txtCode'), content=txtCode) #点击登录 PageAction.click(read_file.get_option('login', 'login_button')) #校验页面元素,左侧侧边栏文本包含'工作台' find.getElement(read_file.get_option('login', 'workbench'), seconds=14) print('账号%s登录成功!' % (userName))
def workbench_payment(): enter_workbench.enter_workbenchpage() PageAction.click(locatorMethod='xpath',localExpression='//div[.="收款"]') while True: PageAction.input_string(locatorMethod='id',localExpression='cardNoPhone',content='15806049674') # cardNoPhone_elem=find.getElement(localType='id', localExpression='cardNoPhone') cardNoPhone_value=PageAction.getelement_attribute(locatorMethod='id', localExpression='cardNoPhone',attr='value') if len(cardNoPhone_value)==11: print('成功输入11位手机号码:',cardNoPhone_value) break else: print('手机号码位数错误,重输',cardNoPhone_value) PageAction.pause(1) PageAction.press_enter_key() PageAction.pause(2) #定位枪号元素且输入汽油枪号10 PageAction.input_string(locatorMethod='id',localExpression="gunNo",content=config_handler.Parse_JYH_Config().get_option('gunNo_info','diese_gunNo')) PageAction.press_enter_key() PageAction.pause(5) #优惠券内容 # coupon =find.getElement(localType='xpath',localExpression='//label[@title="优惠券"]/parent ::*/following-sibling::*/div/span/div').text #清空优惠券 PageAction.click_SvgelementXpath(locatorMethod='xpath',localExpression="//span[@class='ant-form-item-children']/i/*[name()='svg']/*[name()='path'][2]") PageAction.pause(2)
def test_balance_payment(setup_module): try: enter_workbench.workbench_payment() #输入手机号码, enter_workbench.Balance_payment_click() # 支付方式按钮:余额支付 enter_workbench.input_oilgunNo( enter_workbench.get_diesegunNo()) # 输入汽油枪号 # 加油前-获取汽油余额和通用余额 before_dieselBalance, before_commonlBalance = enter_workbench.workbench_inputPhone( )[0::2] print('加油前-获取汽油余额和通用余额', before_dieselBalance, before_commonlBalance) if float(before_dieselBalance) > 0: oilPrice = round((float(before_dieselBalance) / 8) * 100 / 100.0, 2) #取加油金额=汽油余额的1/4 elif float(before_commonlBalance) > 0: oilPrice = round((float(before_commonlBalance) / 8) * 100 / 100.0, 2) # 取加油金额=通用余额的1/4 # oilPrice=0.1 enter_workbench.input_oilPrice(oilPrice) #输入加油金额 enter_workbench.clear_coupon() # 获取提交订单时计算的实收金额 receivepayment = enter_workbench.get_receivepayment() print('实收金额:', receivepayment) # 获取提交订单时计算的优惠金额 discount = enter_workbench.get_discount() print('提交订单时计算的优惠金额', discount) # PageAction.capture_screen() # 收款操作 PageAction.roll_bottom() enter_workbench.click_pay() if PageAction.assert_word('余额支付成功') == True: enter_workbench.close_success_pay_Popup() #断言部分: map(lambda x :float(x) ,L) Balance_list = enter_workbench.workbench_inputPhone( ) #返回list包含 会员各种类型的余额文本 print('Balance_list', Balance_list) after_dieselBalance, after_gaslBalance, after_commonlBalance = map( lambda x: float(x), Balance_list) #金额文本转成浮点金额 except_remain_Balance = round( (((float(before_dieselBalance) - oilPrice) * 100) / 100.0), 2) # 预期剩余汽油余额 print('原汽油余额:%s,应收金额:%s,预期剩余汽油金额:%s' % (before_dieselBalance, receivepayment, except_remain_Balance)) # 断言会员余额加油前后变化 if not except_remain_Balance == after_dieselBalance: PageAction.capture_error_screen() error('实际剩余汽油余额: %s 跟预期剩余汽油余额: %s不一致' % ( after_dieselBalance, except_remain_Balance, )) enter_workbench.close_payment_windows() payed = enter_workbench.get_order_realpay() print('订单记录的实收金额:', payed) if not receivepayment == payed: PageAction.capture_error_screen() error('订单记录的实收金额:%s与付款实付金额:%s不一致' % (payed, receivepayment)) order_discount = enter_workbench.get_order_discount() print('订单记录的优惠金额:', order_discount) if not order_discount == discount: PageAction.capture_error_screen() error('订单的优惠金额:%s与付款优惠金额:%s不一致' % (order_discount, discount)) else: PageAction.assert_word('余额支付失败') print('余额支付失败') enter_workbench.close_fail_pay_Popup() enter_workbench.close_payment_windows() error('余额支付失败') else: print('通用余额,汽油余额都不足无法进行余额支付') PageAction.capture_error_screen() error('通用余额,汽油余额都不足无法进行余额支付') except Exception as e: raise e print('过程执行失败')
def Balance_payment_click(): #支付方式按钮:余额支付 return PageAction.click( read_file.get_option('workbench', 'Balance_pay_button')) PageAction.pause(10)
def Cash_payment_click(): #支付方式按钮:现金支付 return PageAction.click( read_file.get_option('workbench', 'cash_pay_button')) PageAction.pause(2)
def get_receivepayment(): # 获取提交订单时计算的应收金额 receivepayment = PageAction.getelement_text( read_file.get_option('workbench', 'receiver_pay')) return receivepayment[0:-1] PageAction.pause(3)
def input_oilgunNo(gunNo): # 定位枪号元素且输入汽油枪号 PageAction.input_string(read_file.get_option('workbench', 'gunNo_ele'), content=gunNo) PageAction.press_enter_key() PageAction.pause(2)
def clear_coupon(): # 清空优惠券 PageAction.click_SvgelementXpath( read_file.get_option('workbench', 'clear_coupon_button')) PageAction.pause(3)
def close_success_pay_Popup(): PageAction.pause(2) #关闭支付成功确定按钮 PageAction.click(read_file.get_option('workbench', 'close_enter'))
def min_payment_windows(): #最小化收款弹窗 PageAction.click(read_file.get_option('workbench', 'min_pay')) # Action.click(locatorMethod='xpath', localExpression='//button[@aria-label="Close"]/following-sibling::*[1]/div') PageAction.pause(3)
def workbench_inputPhone(): while True: PageAction.input_string(localtion=read_file.get_option( 'workbench', 'number_ele'), content='15806049674') cardNoPhone_value = PageAction.getelement_attribute( read_file.get_option('workbench', 'number_ele'), attr='value') PageAction.pause(2) if len(cardNoPhone_value) == 11: print('成功输入11位手机号码:', cardNoPhone_value) break else: print('手机号码位数错误,重输', cardNoPhone_value) PageAction.pause(1) PageAction.press_enter_key() PageAction.pause(5) # 定位汽油余额元素 dieselBalance_content = PageAction.getelement_text( read_file.get_option('workbench', 'dieselBalance_ele')) if dieselBalance_content.find('汽油余额') != -1: str_dieselBalance = dieselBalance_content.split(":")[1] # 汽油余额 else: str_dieselBalance = 0 # 定位柴油余额元素 gaslBalance_content = PageAction.getelement_text( read_file.get_option('workbench', 'gaslBalance_ele')) if dieselBalance_content.find('柴油余额') != -1: str_gaslBalance = gaslBalance_content.split(":")[1] # 柴油余额 else: str_gaslBalance = 0 # 定位通用余额元素 commonlBalance_content = PageAction.getelement_text( read_file.get_option('workbench', 'commonlBalance_ele')) str_commonlBalance = commonlBalance_content.split(":")[1] # 通用余额 print('当前汽油余额:%s,柴油余额%s,通用余额:%s' % (str_dieselBalance, str_gaslBalance, str_commonlBalance)) return [str_dieselBalance, str_gaslBalance, str_commonlBalance]
def input_cash_receipt(receipt): Manual_input_cash = PageAction.input_string(read_file.get_option( 'workbench', 'Manual_input_cash'), content=receipt)
def get_diesegunNo(): gunNo = config_handler.Parse_JYH_Config().get_option( 'gunNo_info', 'diese_gunNo') return gunNo PageAction.pause(1)
def find_pop(): PageAction.pause(2) find.getElement(read_file.get_option('workbench', 'tan'))
def getElemtns(localType, localExpression, driver=None): try: elements = WebDriverWait(open_browser('chrome'), 7).until( lambda x: x.find_element(by=localType, value=localExpression)) return elements except Exception as e: raise e # 等待菜单到可点击状态 def elementToBeClickable(localType, localExpression): try: get_selenium().execute_script("arguments[0].scrollIntoView(true);", getElement(localType, localExpression)) WebDriverWait(get_selenium(), 0.7).until( EC.element_to_be_clickable((localType, localExpression))) return True except Exception as e: raise e if __name__ == '__main__': helper.open_browser('chrome') helper.open_url() # print(getElement(localType='id', localExpression="userName")) PageAction.input_string(locatorMethod="id", localExpression="userName", content="1223") # Action.input_string(locatorMethod="id",localExpression="kw",content="123")
def click_pay(): # 收款操作 PageAction.click(read_file.get_option('workbench', 'submit_pay')) PageAction.pause(3)
def close_fail_pay_Popup(): #关闭支付失败弹窗 PageAction.pause(2) PageAction.click(read_file.get_option('workbench', 'cancle_button'))
def input_oilPrice(price): #输入加油金额 PageAction.input_string(read_file.get_option('workbench', 'price_ele'), content=str(price)) PageAction.press_enter_key() PageAction.pause(3)
def get_cash_receipt(): Manual_input_cash = PageAction.getelement_attribute(read_file.get_option( 'workbench', 'Manual_input_cash'), attr='value') if Manual_input_cash: return float(Manual_input_cash)
def workbench_payment(): enter_workbenchpage() PageAction.click(localtion=read_file.get_option('workbench', 'pay_button')) PageAction.pause(3)