Python3 生成并且按照顺序保存图片
1 # -*- coding:utf-8 -*- 2 from selenium import webdriver 3 #导入webself.driver 4 import requests 5 # 导入request模块 6 from selenium.webdriver.support.ui import Select 7 #用于定位下拉框 8 import time 9 #导入时间控件 10 import unittest 11 #导入unittest框架 12 from HTMLTestRunner import HTMLTestRunner 13 #d导入HTMLTestRunner模块,用于生成测试报告 14 from selenium.webdriver.common.keys import Keys 15 # 导入键盘机制 16 import qrcode 17 import datetime 18 import os 19 20 21 22 23 class Deposit_Test(unittest.TestCase): 24 num = 100 25 @classmethod 26 def setUpClass(self): 27 chrome_options = webdriver.ChromeOptions() 28 chrome_options.add_experimental_option('useAutomationExtension', False) 29 chrome_options.add_experimental_option("excludeSwitches", ['enable-automation']) 30 self.driver = webdriver.Chrome(executable_path="C:\Download\chromedriver.exe", options=chrome_options) 31 _url = "http://bdms.XXX.com/" 32 self.driver.get(_url) 33 time.sleep(2) 34 name = ("92510521MA649F3333") 35 password = name[-8:] 36 print("密码:",password) 37 self.driver.find_element_by_id("loginName").send_keys(name) 38 self.driver.find_element_by_xpath("/html/body/div[1]/div/div[2]/div[2]/input").send_keys(password) 39 self.driver.find_element_by_xpath("/html/body/div[1]/div/div[2]/div[3]/input").click() 40 time.sleep(2) 41 self.driver.find_element_by_xpath("/html/body/div[1]/div/div[2]/input").click() 42 self.driver.maximize_window() 43 self.driver.fullscreen_window() 44 browser_size = self.driver.get_window_size() 45 browser_name = self.driver.name 46 title = self.driver.title 47 print(title) 48 time.sleep(2) 49 try: 50 self.driver.find_element_by_xpath("/html/body/div[1]/div/div[5]/div[2]/div[3]/span[1]").click() 51 print("关闭未签约弹窗") 52 except: 53 pass 54 # 未签约用户,关闭弹窗 55 print("浏览器名称:" + browser_name) 56 print("浏览器版本 :", self.driver.capabilities["browserVersion"]) 57 print("浏览器尺寸:", browser_size) 58 @classmethod 59 def tearDownClass(self): 60 self.driver.quit() 61 print("执行完成关闭浏览器") 62 63 def test_001(self): 64 '''销售→退押金整体流程''' 65 self.driver.find_element_by_xpath("/html/body/div[1]/div/div[1]/div[2]/div[1]/div").click() 66 # 售卖农药→退押金 67 count = 10 68 salelist = [] 69 # 打开*个页面,售卖个数= count*10 70 for i in range(count): 71 result = requests.get('http://10.2.178.11:8099/test?pagenum=' + str(i)) 72 # 遍历*个页面 73 resultJson = result.json() 74 # 把数据导出为json字符串 75 print("解析结果", resultJson['data']) 76 # 打印结果 77 for nyObj in resultJson['data']: 78 # 遍历导出的json数据 79 print("农药对象为:", nyObj) 80 print("编号:", nyObj[0]) 81 print("名称:", nyObj[1]) 82 print("二维码:", nyObj[2]) 83 salelist.append(nyObj[2]) 84 # 每次遍历后提取nyobj[2]的数据,并且放入salelist数组内 85 print("已销售农药二维码:", salelist) 86 # 打印list 87 self.driver.find_element_by_id("myInputCode").send_keys( nyObj[2]) 88 # 把农药二维码/追溯码当做参数传入输入框内 89 # time.sleep(1) 90 self.driver.find_element_by_id("myInputCode").send_keys(Keys.ENTER) 91 time.sleep(1) 92 # 输入农药二维码/追溯码后,点击键盘回车键 93 time.sleep(1) 94 self.driver.find_element_by_class_name("btnDefault").click() 95 # 点击销售按钮 96 time.sleep(2) 97 self.driver.find_element_by_xpath("/html/body/div[1]/div/div[2]/div/div[3]/div[1]/div[2]/div/div[2]/span/button[1]").click() 98 time.sleep(3) 99 print("销售农药个数:",count*10) 100 # 展示销售个数 101 print(salelist) 102 try: 103 self.driver.find_element_by_xpath("/html/body/div[1]/div/div[2]/div/div[3]/div[1]/div[2]/div/div[2]/span/button[1]").click() 104 except: 105 pass 106 # 打印list:把nyobj[2]的数据放入list数组中 107 time.sleep(1) 108 self.driver.find_element_by_xpath("/html/body/div[1]/div/div[1]/div[2]/div[2]").click() 109 time.sleep(6) 110 # 点击退押金按钮 111 returned1 = self.driver.find_element_by_xpath("/html/body/div[1]/div/div[3]/div[2]/span").text[0:-4] 112 returned1 = int(returned1) 113 print("退押金前退回押金总额:",returned1) 114 time.sleep(60) 115 # 等待60s:销售60s后方可退押金 116 for returnlist in salelist: 117 # 遍历 118 print("已销售二维码:",returnlist) 119 # 打印 120 img = qrcode.make(returnlist) 121 # t = time.strftime('%Y-%m-%d %H_%M_%S', time.localtime()) 122 Deposit_Test.num = Deposit_Test.num + 1 123 img.save(str(Deposit_Test.num)+".jpg") 124 self.driver.find_element_by_xpath("/html/body/div[1]/div/div[2]/div/div[2]/div[2]/div[1]/input").send_keys(returnlist) 125 # 把已销售二维码输入输入框 126 self.driver.find_element_by_xpath("/html/body/div[1]/div/div[2]/div/div[2]/div[2]/div[1]/input").send_keys(Keys.ENTER) 127 time.sleep(1) 128 self.driver.find_element_by_class_name("btnDefault").click() 129 # 点击退押金 130 time.sleep(1) 131 returned2 = self.driver.find_element_by_xpath("/html/body/div[1]/div/div[3]/div[2]/span").text[0:-4] 132 returned2 = int(returned2) 133 print("退押金后退回押金总额",returned2) 134 returned3 = returned1 + int(count*10) 135 print(returned3) 136 if int(returned2) == returned3: 137 print("退还押金成功,已销售农药退押金成功个数:",count*10) 138 else: 139 print("退还押金失败") 140 141 142 143 144 if __name__ == '__main__': 145 unittest.main() 146 147 # testunit = unittest.TestSuite() 148 # # 制定一个容器,生成 test_001的测试报告 149 # testunit.addTest(Deposit_Test("test_001")) 150 # filename = "C:\\Users\\jiaow\\PycharmProjects\\nongzi\\Test_reports\\report_Tuiyajin.html" 151 # fp = open(filename, "wb") 152 # runner = HTMLTestRunner.HTMLTestRunner( 153 # stream=fp, 154 # title="农药押金收退终端测试报告", 155 # description="用例执行情况:" ) 156 # 157 # runner.run(testunit) 158 # fp.close() 159 160 161 162 163