class Test_Login(unittest.TestCase): # 页面元素定位(获取Config中配置) URL = Config().get('URL') # 获取URL excel = DATA_PATH + '\TestLogin.xlsx' # 所用表格 phone = (By.XPATH, Config().get('phone')) # 用户名输入框 Username = (By.XPATH, Config().get('Username')) # 登录后,用户名 password = (By.XPATH, Config().get('password')) # 密码输入框 # order = (By.XPATH, Config().get('order')) #订单页签 order = (By.XPATH, Config().get('order')) #订单页签 new_order = (By.XPATH, Config().get('new_order')) #新建订单 uesr_search = (By.XPATH, Config().get('uesr_search')) #新建订单,用户输入框 search_user = (By.XPATH, Config().get('search_user')) #新建订单,搜索按钮 digital = Config().get('digital') #表格中行数 @classmethod def sub_setUp(cls): # 调用登录模块中driver cls.driver = webdriver.Chrome(executable_path=DRIVER_PATH + '\chromedriver.exe') cls.driver.maximize_window() cls.driver.get(Test_Login.URL) def sub_tearDown(self): # 关闭游览器、命令框 self.driver.quit() def test_search(self): try: self.sub_setUp() # 调用sub_setUp方法 p = LoGin(self.driver) #调用登录类,并定位driver p.test_search() #调用登录方法 datas = ExcelReader(self.excel).data print(2) WebDriverWait(self.driver, 10).until(lambda driver: #定位设置框 self.driver.find_element(*self.Username)) print(3) WebDriverWait(self.driver, 10).until(lambda driver: # 定位订单框 self.driver.find_element(*self.order)) print(4.1) above = self.driver.find_element(*self.order) #定位加入订单位置 ActionChains(self.driver).move_to_element( above).click().perform() #待元素显示,点击元素 print(4) self.driver.find_element(*self.new_order).click() self.driver.find_element(*self.uesr_search).send_keys( datas[Test_Login.digital]["user"]) logger.info('登录成功') # 登录成功日志 self.sub_tearDown() # 调用退出方法 except Exception as msg: # 代码执行错误时,打印图片 script = os.path.basename(__file__)[:-3] #获取用例名,去除.py nowTime = time.strftime( "%Y.%m.%d.%H.%M.%S") + "." + script # 图片名称格式 self.driver.get_screenshot_as_file( (LOG_PATH + '//%s.png') % nowTime) # 截屏图片 logger.info(script + ".%s" % msg) self.sub_tearDown() # 调用退出方法
def test_search(self): try: self.sub_setUp() # 调用sub_setUp方法 p = LoGin(self.driver) p.test_search() print(10) # LoGin() # print(11) time.sleep(2) self.driver.find_element(*self.member_centre).click() test = self.driver.find_element(*self.prompt).text links = test[3:7] print(links) if links == Config().get('links'): self.sub_tearDown() logger.info('会员中心登录成功') except Exception as msg: # 代码执行错误时,打印图片 script = os.path.basename(__file__)[:-3] #获取用例名,去除.py nowTime = time.strftime( "%Y.%m.%d.%H.%M.%S") + "." + script # 图片名称格式 self.driver.get_screenshot_as_file( (LOG_PATH + '//%s.png') % nowTime) # 截屏图片 logger.info(script + ".%s" % msg) self.sub_tearDown() # 调用退出方法
class Test_Member(unittest.TestCase): member_centre = (By.XPATH, Config().get('member_centre')) prompt = (By.XPATH, Config().get('prompt')) # driver = webdriver.Chrome(executable_path=DRIVER_PATH + '\chromedriver.exe') @classmethod def sub_setUp(cls): # 调用登录模块中driver cls.driver = webdriver.Chrome(executable_path=DRIVER_PATH + '\chromedriver.exe') cls.driver.maximize_window() cls.driver.get(LoGin.URL) print("这段有输出") def sub_tearDown(self): self.driver.quit() def test_search(self): try: self.sub_setUp() # 调用sub_setUp方法 p = LoGin(self.driver) p.test_search() print(10) # LoGin() # print(11) time.sleep(2) self.driver.find_element(*self.member_centre).click() test = self.driver.find_element(*self.prompt).text links = test[3:7] print(links) if links == Config().get('links'): self.sub_tearDown() logger.info('会员中心登录成功') except Exception as msg: # 代码执行错误时,打印图片 script = os.path.basename(__file__)[:-3] #获取用例名,去除.py nowTime = time.strftime( "%Y.%m.%d.%H.%M.%S") + "." + script # 图片名称格式 self.driver.get_screenshot_as_file( (LOG_PATH + '//%s.png') % nowTime) # 截屏图片 logger.info(script + ".%s" % msg) self.sub_tearDown() # 调用退出方法
def test_search(self): try: self.sub_setUp() # 调用sub_setUp方法 # self.driver.maximize_window() # self.driver.get(Test_Login.URL) self.driver.find_element(*self.locator_button).click() # 点击首页登录按钮 datas = ExcelReader(self.excel).data # 获取excell表格中字符串 # 循环导入username、password for index, d in enumerate(datas): time.sleep(3) self.driver.find_element(*self.phone).clear() # 清空用户名输入框 self.driver.find_element(*self.password).clear() # 清空密码输入框 self.driver.find_element(*self.phone).send_keys(d['username']) # 输入用户名(读取表格) self.driver.find_element(*self.password).send_keys(d['password']) # 输入密码(读取表格) self.driver.find_element(*self.login_button).click() # 登录页,点击登录按钮 if index == 0: # 意思就是在10秒内等待某个按钮被定位到。每隔0.5秒内调用一下until里面的表达式或者方法函数, # 要么10秒内表达式执行成功,要么10秒后抛出超时异常 WebDriverWait(self.driver, 10).until(lambda driver: self.driver.find_element(*self.login_account)) elif index == 1: WebDriverWait(self.driver, 10).until(lambda driver: self.driver.find_element(*self.login_password)) elif index == (2, 3): WebDriverWait(self.driver, 10).until(lambda driver: self.driver.find_element(*self.Information)) # elif index == (2, 3): # WebDriverWait(self.driver, 10).until(lambda driver: # self.driver.find_element(*self.Username)) # 判断元素是否消失,是返回Ture,否返回False # is_disappeared = WebDriverWait(self.driver, 30, 1,).\ # until_not(lambda driver: self.driver.find_element(*self.Information).is_displayed()) # print(is_disappeared) WebDriverWait(self.driver, 10).until(lambda driver: self.driver.find_element(*self.Username)) links = self.driver.find_element(*self.Username).text # 通过ID,获得用户名 if links == Config().get('links'): # 验证获得的用户名与配置文件中是否一致 self.driver.find_element(*self.quit_button).click() # 首页(已登录),退出按钮 logger.info('登录成功') # 登录成功日志 self.sub_tearDown() # 调用退出方法 except Exception as msg: # 代码执行错误时,打印图片 nowTime = time.strftime("%Y.%m.%d.%H.%M.%S") + ".test_001_login" # 图片名称格式 self.driver.get_screenshot_as_file( "D:\\TestCase\\Hypweb.Frame\\Test_framework\\log\\%s.png" % nowTime) # 截屏图片 logger.info("test_001_login.%s" % msg) self.sub_tearDown() # 调用退出方法
class LoGin(object): URL = Config().get('URL') excel = DATA_PATH + '\TestLogin.xlsx' locator_button = (By.XPATH, Config().get('loginxpath')) phone = (By.XPATH, Config().get('phone')) Username = (By.ID, Config().get('Username')) password = (By.XPATH, Config().get('password')) # login_button = (By.XPATH, Config().get('login_button')) login_button = (By.XPATH, Config().get('login_button')) # 登录页,登录按钮 quit = (By.XPATH, Config().get('quit')) digital = Config().get('digital') # driver = webdriver.Chrome(executable_path=DRIVER_PATH + '\chromedriver.exe') # driver.maximize_window() # driver.get(URL) def __init__(self, driver): self.driver = driver # @classmethod # def sub_setUp(cls): # cls.driver = driver # cls.driver.maximize_window() # cls.driver.get(LoGin.URL) # # P = self.driver.driver # # self.driver = webdriver.Chrome(executable_path=DRIVER_PATH + '\chromedriver.exe') # self.driver.maximize_window() # self.driver.get(LoGin.URL) # print(1) # # def sub_teardown(self): # self.driver.close() def test_search(self): # self.driver.maximize_window() # self.driver.get(LoGin.URL) datas = ExcelReader(self.excel).data self.driver.find_element(*self.locator_button).click() time.sleep(3) self.driver.find_element(*self.phone).clear() self.driver.find_element(*self.phone).send_keys(datas[LoGin.digital]['username']) self.driver.find_element(*self.password).clear() self.driver.find_element(*self.password).send_keys(datas[LoGin.digital]['password']) self.driver.find_element(*self.login_button).click() print(2)
def test_search(self): try: self.sub_setUp() # 调用sub_setUp方法 p = LoGin(self.driver) p.test_search() print(10) # LoGin() # print(11) time.sleep(2) self.driver.find_element(*self.member_centre).click() test = self.driver.find_element(*self.prompt).text links = test[3:7] print(links) if links == Config().get('links'): self.sub_tearDown() logger.info('会员中心登录成功') except Exception as msg: # 代码执行错误时,打印图片 nowTime = time.strftime("%Y.%m.%d.%H.%M.%S") + ".test_001_member" # 图片名称格式 self.driver.get_screenshot_as_file( "D:\\TestCase\\Hypweb.Yili\\Test_framework\\log\\%s.png" % nowTime) # 截屏图片 logger.info("test_001_login.%s" % msg) self.sub_tearDown() # 调用退出方法
class Test_Order(unittest.TestCase): # 页面元素定位(获取Config中配置) search_goods = Config().get('search_goods') # 搜索商品 search = (By.XPATH, Config().get('search')) # 搜索栏 add_shop_cat = (By.XPATH, Config().get('add_shop_cat')) #加入购物车 search_button = (By.XPATH, Config().get('search_button')) #搜索按钮 purchase_quantity = (By.XPATH, Config().get('purchase_quantity')) #购买数量 increase = (By.XPATH, Config().get('increase')) #数量箭头 @classmethod def sub_setUp(cls): # 调用登录模块中driver cls.driver = webdriver.Chrome(executable_path=DRIVER_PATH + '\chromedriver.exe') cls.driver.maximize_window() cls.driver.get(LoGin.URL) def sub_tearDown(self): # 关闭游览器、命令框 self.driver.quit() def test_search(self): try: self.sub_setUp() # 调用sub_setUp方法 p = LoGin(self.driver) p.test_search() time.sleep(2) self.driver.find_element(*self.search).clear() # 清空搜索栏 self.driver.find_element(*self.search).send_keys( Test_Order.search_goods) #输入文字 self.driver.find_element(*self.search_button).click() #点击搜索按钮 WebDriverWait( self.driver, 10).until(lambda driver: #查询加入购物车按钮 self.driver.find_element(*self.add_shop_cat)) above = self.driver.find_element(*self.add_shop_cat) #定位加入购物车元素位置 ActionChains(self.driver).move_to_element( above).click().perform() #待元素显示,点击元素 WebDriverWait( self.driver, 10).until(lambda driver: # 查询加入购物车按钮 self.driver.find_element(*self.purchase_quantity)) p = self.driver.find_element(*self.purchase_quantity) value = int(p.get_attribute("value")) print(type(value)) f = random.randint(1, 10) print(type(f)) for i in range(f): if f > value: self.driver.find_element(*self.increase).click() print(3) time.sleep(3) logger.info('搜索成功') # 登录成功日志 self.sub_tearDown() # 调用退出方法 except Exception as msg: # 代码执行错误时,打印图片 nowTime = time.strftime( "%Y.%m.%d.%H.%M.%S") + ".test_003_order" # 图片名称格式 # print(type(nowTime)) self.driver.get_screenshot_as_file( "D:\\TestCase\\Hypweb.Frame\\Test_framework\\log\\%s.png" % nowTime) # 截屏图片 # print(type(msg)) age = "test_003_login:"******"%s:%s" % (age, msg)) self.sub_tearDown() # 调用退出方法
def __init__(self, logger_name='framework'): self.logger = logging.getLogger(logger_name) logging.root.setLevel(logging.NOTSET) c = Config().get('log') self.log_file_name = c.get( 'file_name') if c and c.get('file_name') else 'test.log' # 日志文件 self.backup_count = c.get( 'backup') if c and c.get('backup') else 5 # 保留的日志数量 # 日志输出级别 self.console_output_level = c.get( 'console_level') if c and c.get('console_level') else 'WARNING' self.file_output_level = c.get( 'file_level') if c and c.get('file_level') else 'DEBUG' # 日志输出格式 pattern = c.get('pattern') if c and c.get( 'pattern' ) else '%(asctime)s - %(name)s - %(levelname)s - %(message)s' self.formatter = logging.Formatter(pattern)
class Test_Login(unittest.TestCase): # 页面元素定位(获取Config中配置) URL = Config().get('URL') # 获取URL excel = DATA_PATH + '\TestLogin.xlsx' # 所用表格 locator_button = (By.XPATH, Config().get('loginxpath')) # 首页登录按钮 phone = (By.XPATH, Config().get('phone')) # 用户名输入框 Username = (By.ID, Config().get('Username')) # 登录后,用户名 password = (By.XPATH, Config().get('password')) # 密码输入框 login_button = (By.XPATH, Config().get('login_button')) # 登录页,登录按钮 quit_button = (By.XPATH, Config().get('quit')) # 首页,退出按钮 login_account = (By.XPATH, Config().get('login_account')) #未输入账号,提示信息 login_password = (By.XPATH, Config().get('login_password')) #未输入密码,提示信息 Information = (By.XPATH, Config().get('Information')) #登录失败,页面提示信息 @classmethod def sub_setUp(cls): # 调用登录模块中driver cls.driver = webdriver.Chrome(executable_path=DRIVER_PATH + '\chromedriver.exe') cls.driver.maximize_window() cls.driver.get(LoGin.URL) print(1) # self.driver = p # LoGin.sub_setup(self.driver) def sub_tearDown(self): # 关闭游览器、命令框 print(2) self.driver.quit() def test_search(self): try: self.sub_setUp() # 调用sub_setUp方法 # self.driver.maximize_window() # self.driver.get(Test_Login.URL) self.driver.find_element(*self.locator_button).click() # 点击首页登录按钮 datas = ExcelReader(self.excel).data # 获取excell表格中字符串 # 循环导入username、password for index, d in enumerate(datas): time.sleep(3) self.driver.find_element(*self.phone).clear() # 清空用户名输入框 self.driver.find_element(*self.password).clear() # 清空密码输入框 self.driver.find_element(*self.phone).send_keys(d['username']) # 输入用户名(读取表格) self.driver.find_element(*self.password).send_keys(d['password']) # 输入密码(读取表格) self.driver.find_element(*self.login_button).click() # 登录页,点击登录按钮 if index == 0: # 意思就是在10秒内等待某个按钮被定位到。每隔0.5秒内调用一下until里面的表达式或者方法函数, # 要么10秒内表达式执行成功,要么10秒后抛出超时异常 WebDriverWait(self.driver, 10).until(lambda driver: self.driver.find_element(*self.login_account)) elif index == 1: WebDriverWait(self.driver, 10).until(lambda driver: self.driver.find_element(*self.login_password)) elif index == (2, 3): WebDriverWait(self.driver, 10).until(lambda driver: self.driver.find_element(*self.Information)) # elif index == (2, 3): # WebDriverWait(self.driver, 10).until(lambda driver: # self.driver.find_element(*self.Username)) # 判断元素是否消失,是返回Ture,否返回False # is_disappeared = WebDriverWait(self.driver, 30, 1,).\ # until_not(lambda driver: self.driver.find_element(*self.Information).is_displayed()) # print(is_disappeared) WebDriverWait(self.driver, 10).until(lambda driver: self.driver.find_element(*self.Username)) links = self.driver.find_element(*self.Username).text # 通过ID,获得用户名 if links == Config().get('links'): # 验证获得的用户名与配置文件中是否一致 self.driver.find_element(*self.quit_button).click() # 首页(已登录),退出按钮 logger.info('登录成功') # 登录成功日志 self.sub_tearDown() # 调用退出方法 except Exception as msg: # 代码执行错误时,打印图片 nowTime = time.strftime("%Y.%m.%d.%H.%M.%S") + ".test_001_login" # 图片名称格式 self.driver.get_screenshot_as_file( "D:\\TestCase\\Hypweb.Frame\\Test_framework\\log\\%s.png" % nowTime) # 截屏图片 logger.info("test_001_login.%s" % msg) self.sub_tearDown() # 调用退出方法