def sign_out(self): """功能: 登出的主过程:点击更多按钮--检查登录态--点击设置按钮--上划--点击退出--点击确认退出 返回值: 0:当前还在登录态,登出失败 1:当前不在登录态,登出成功 2:等待某控件超时,测试失败""" log_utils.C_INFO('开始登出') if self.wait_element_by_mode(base_data.wait_time_long,By.ID,self.more_button_id,'更多按钮') == 1: return 2 self.click_more_button() if self.wait_element_by_mode(base_data.wait_time_mid,By.ID,self.setting_container_id,'更多菜单') == 1: return 2 log_utils.C_STEP('检查登录态') if self.check_is_login_in_more_page() == 0: log_utils.F_ERROR('没有在登陆态,先去退出登录') self.reset_app() if self.login_test() == 0: log_utils.F_ERROR('登录功能已经失效!无法继续测试!') self.tear_down() sys.exit(-1) else: log_utils.C_INFO('登录成功,继续测试!') self.reset_app() log_utils.C_INFO('开始登出') if self.wait_element_by_mode(base_data.wait_time_long,By.ID,self.more_button_id,'更多按钮') == 1: return 2 self.click_more_button() if self.wait_element_by_mode(base_data.wait_time_mid,By.ID,self.setting_container_id,'更多菜单') == 1: return 2 self.click_setting_container() self.my_sleep(3) log_utils.C_STEP('上划屏幕') self.my_swipe('up') self.screenshot('设置页面截图') self.click_signout_button_first() self.my_sleep() self.click_signout_button_second() self.my_sleep() self.click_yes_button() if self.wait_element_by_mode(base_data.wait_time_mid,By.ID,self.more_button_id,'回到主页') == 1: return 2 log_utils.C_STEP('验证是否登出成功,点击更多按钮') self.click_more_button() self.screenshot('登录状态截图') if self.check_is_login_in_more_page() == 1: log_utils.F_FAIL('在登录态,登出失败') return 0 else: log_utils.P_PASS('不在登录态,登出成功') return 1
def click_play_list_page_first_song_more_download_button(self): if not self.find_element_and_action( By.XPATH, self.play_list_page_more_button_download_button_xpath, self.action.click, '更多栏的缓存按钮') == 0: log_utils.F_ERROR('点击失败') return 1
def click_play_list_page_third_song_to_play(self): """点击第三首歌曲条目来播放第三首歌曲""" if not self.find_element_and_action( By.XPATH, self.play_list_page_third_song_name_xpath, self.action.click, '第三首歌曲条目') == 0: log_utils.F_ERROR('点击失败') return 1
def click_comment_page_title_bar_back_button(self): """点击评论页面的返回按钮""" if not self.find_element_and_action( By.ID, self.comment_page_title_bar_back_button_id, self.action.click, '返回按钮') == 0: log_utils.F_ERROR('点击失败') return 1
def my_swipe(self, direction='', t=500, x=0.5): """功能: 模仿手指在屏幕上的滑动动作 参数: direction:方向,有四个选项(up,down,left,right) t:滑动的时间,t越小代表滑的越快,滑的距离越大,但是t不可以过小,想快速滑动最小设置为500,t的单位是毫秒。 x:向上下滑动时手指在屏幕的横向位置,向左右滑动时的竖向位置 例如x=0.5,手指就在屏幕中间向下滑,x=0.25,就在屏幕四分之一的地方向下滑""" size = self.get_size() if direction == 'up': x1 = int(size[0] * x) y1 = int(size[1] * 0.75) y2 = int(size[1] * 0.25) self.driver.swipe(x1, y1, x1, y2, t) elif direction == 'down': x1 = int(size[0] * x) y1 = int(size[1] * 0.25) y2 = int(size[1] * 0.75) self.driver.swipe(x1, y1, x1, y2, t) elif direction == 'left': x1 = int(size[0] * 0.9) x2 = int(size[0] * 0.1) y1 = int(size[1] * x) self.driver.swipe(x1, y1, x2, y1, t) elif direction == 'right': x1 = int(size[0] * 0.1) x2 = int(size[0] * 0.9) y1 = int(size[1] * x) self.driver.swipe(x1, y1, x2, y1, t) else: log_utils.F_ERROR('error,please inset right paremater!') sys.exit()
def find_element_and_action(self, mode, element='', action=None, thing='', keys=''): """功能: 在当前页面上以某种方式查找一个控件,并且对其进行某种操作 参数: mode:以哪种方式寻找控件,必须是以下几种:By.ID, By.CLASS_NAME, By.XPATH, By.NAME element:控件的id或者xpath等,要跟前一项对应, action:对查找元素进行的操作:“is_displayed”、“click”、“send_keys”、“get_text” keys:假如要进行send_keys的操作,就要发送一个keys thing:元素描述 返回值: 0:查找的控件显示\点击成功\输入成功 1:没有输入action参数,错误 2:查找不到要查找的控件,抛出异常 tmpstr:要获取的控件中文本信息 例子: """ try: element_info = self.driver.find_element(mode, element) if action == "is_displayed": element_info.is_displayed() log_utils.C_INFO(thing + '显示正常') return 0 elif action == "click": element_info.click() log_utils.C_STEP('点击' + thing + ' 点击成功') return 0 elif action == "send_keys": element_info.send_keys(keys) log_utils.C_STEP(thing + ' ' + action + ' ' + keys + ' 输入信息成功') return 0 elif action == "get_text": tmp = element_info.text log_utils.C_STEP('获取' + thing + '的文本信息成功') return tmp else: log_utils.F_ERROR(thing + action + ' error') return 1 except: log_utils.F_ERROR(thing + '不在当前页面') self.screenshot('错误截图:要查找控件不在当前页面') return 2
def click_song_list_play_all_button(self): """功能: 点击榜单页面的播放全部按钮""" if not self.find_element_and_action( By.ID, self.song_list_page_play_all_button_id, self.action.click, '点击榜单页面的播放全部按钮') == 0: log_utils.F_ERROR('点击榜单页面的播放全部按钮失败') return 1
def go_to_account_login_page(self): """功能: 点击账号密码登录页按钮,去账号密码登录页""" if not self.find_element_and_action( By.XPATH, self.go_to_account_login_button_xpath, self.action.click, '去账号密码登录页') == 0: log_utils.F_ERROR('点击失败') return 1
def go_to_baidu_login_page(self): """功能: 点击百度账号登录按钮,去百度登录页""" if not self.find_element_and_action( By.XPATH, self.go_to_baidu_login_button_xpath, self.action.click, '百度账号登录') == 0: log_utils.F_ERROR('点击失败') return 1
def go_to_other_login_page(self): """功能: 点击其他方式登录按钮""" if not self.find_element_and_action( By.XPATH, self.go_to_other_login_button_xpath, self.action.click, '其他方式登录') == 0: log_utils.F_ERROR('点击失败') return 1
def click_third_song_to_play_third_song(self): """功能: 通过点击第三首歌的布局来开始播放第三首歌""" if not self.find_element_and_action( By.XPATH, self.song_list_page_third_song_xpath, self.action.click, '第三首歌的布局') == 0: log_utils.F_ERROR('点击第三首歌的布局失败') return 1
def click_login_button_in_more_page(self): """功能: 点击立即登录按钮 返回值: 1:点击失败""" if not self.find_element_and_action(By.ID, self.go_login_button_id, self.action.click, '立即登录按钮') == 0: log_utils.F_ERROR('点击失败') return 1
def click_setting_container(self): """功能: 点击设置进入设置页面 返回值: 0:点击失败""" if not self.find_element_and_action(By.ID, self.setting_container_id, self.action.click, '设置') == 0: log_utils.F_ERROR('点击失败') return 0
def __check_result(dic): """功能: 验证错误码,打印对应信息 返回值: 0:正常 1:参数错误""" error_code = dic['error_code'] log_utils.C_INFO('错误码为:'+str(error_code)) if error_code == 22000: log_utils.C_INFO('获取信息成功') return 0 elif error_code == 22005: log_utils.F_ERROR('获取信息失败。') log_utils.F_ERROR('error_message:'+dic['error_message']) return 1 else: log_utils.F_ERROR('未知错误码') return 2
def click_mine_button(self): """功能: 点击我的按钮 返回值: 1:点击失败""" if not self.find_element_and_action(By.ID,self.i_mine_id,self.action.click,'我的按钮') == 0: log_utils.F_ERROR('点击失败') return 1
def click_anylisten_button(self): """功能: 点击随身听按钮 返回值: 1:点击失败""" if not self.find_element_and_action(By.ID,self.i_anylisten_id,self.action.click,'随身听按钮') == 0: log_utils.F_ERROR('点击失败') return 1
def click_login_button(self): """功能: 点击登录按钮 返回值: 1:点击失败""" if not self.find_element_and_action(By.XPATH, self.login_button_xpath, self.action.click, '登录按钮') == 0: log_utils.F_ERROR('点击失败') return 1
def get_cookie(): url = 'http://passport.qianqian.com/login?login_id=13522113807&password=29eea60d91f9216d0ce950889eb637c9&device_id=v2pcweb-zkagfyuhly15132404684581&tpl=baidu_music&login_type=1' page = urlopen(url) data = page.read() dic = json.loads(data) if not dic['error_code'] == 0: log_utils.F_ERROR('获取token失败!错误码为:'+dic['error_code']) return 1 token = dic['token'] return token
def __get_first_song_have_mv_code(play_list_url): """根据url获取对应歌单的第一首歌的has_mv的code 返回值: 1:获取失败 “1”:有mv “0”:没有mv""" dic = __get_info(play_list_url) if dic == 1: log_utils.F_ERROR('获取信息失败') return 1 has_mv_code = dic['result']['songlist'][0]['has_mv'] return has_mv_code
def after_login(self): """功能: 登录前的步骤,主页--点击更多按钮--验证是否在登录态--点击登录进入登录页""" log_utils.C_INFO('开始登陆') self.click_more_button() #点击更多按钮 log_utils.C_INFO('查看登录态开始') self.my_sleep() if self.check_is_login_in_more_page() == 1: log_utils.F_ERROR('已经在登录态了!先去退出登录') self.screenshot('错误--已经在登录态截图') self.reset_app() if self.sign_out() == 0: log_utils.F_ERROR('登出功能已经失效!无法继续测试!程序退出!') self.tear_down() sys.exit(-1) else: log_utils.C_INFO('登出成功!继续测试登录功能!') self.reset_app() log_utils.C_INFO('开始登陆') self.click_more_button() #点击更多按钮 self.click_login_button_in_more_page() #点击立即登录
def __get_collector_user_id_list(url = ''): """获取收藏此歌单的用户id""" dic = __get_info(url) if dic == 1: log_utils.F_ERROR('获取信息失败') return 1 collect_num = dic['result']['collect_num'] i=0 user_id_list = {} if collect_num > 7: while i < collect_num: user_id_list[i] = dic['result']['collector'][i]['userid'] i += 1 else: while i < 7: user_id_list[i] = dic['result']['collector'][i]['userid'] i += 1 return user_id_list
def login_test(self): """功能: 太合登录,正常情况,输入正确的账号密码 返回值: 1:登录成功 0:登录失败""" log_utils.C_INFO('开始测试登录功能') self.after_login() tmp = self.taihe_login(1) if tmp == 1: log_utils.P_PASS('登录成功!测试通过') return 1 elif tmp == 0: log_utils.F_FAIL('登录失败!测试失败') return 0 else: log_utils.F_ERROR('等待超时!测试失败') return 0
def __get_first_list_id_from_playlist(mode,url = ''): """功能: 获取对应歌单列表的一些信息 参数: mode:返回的内容:1:获取对应歌单列表的第一个歌单list_id 2:获取对应歌单列表的第一个歌单的title 返回值: 1:获取失败 """ dic = __get_info(url) if dic == 1: log_utils.F_ERROR('获取信息失败') return 1 if mode == 1: list_id = dic['diyInfo'][0]['list_id'] log_utils.C_INFO('获取的list_id为:'+list_id) return list_id elif mode == 2: list_name = dic['diyInfo'][0]['title'] return list_name
def __get_info(url = '', number = 0): """功能: 根据url获取页面json 返回值: dic:正常的返回信息 1:获取多次失败""" i = number page = urlopen(url) data = page.read() dic = json.loads(data) result = __check_result(dic) if result == 0: return dic elif (not result == 0) and i<3: i += 1 log_utils.C_INFO('获取信息失败,第 %d 次重新获取信息' %i) if __get_info(url,i) == 1: return 1 else: log_utils.F_ERROR('重新获取信息多次失败,请检查接口和url。URL:'+url) return 1
def __get_first_three_song_name_from_songlist(number,url = ''): """功能: 获取新歌榜前number首歌曲名 参数: url: number:获取的歌曲名数量 返回值: 1:获取失败""" dic = __get_info(url) if dic == 1: log_utils.F_ERROR('获取信息失败') return 1 log_utils.C_INFO('该榜单的前 %d 首歌曲名为:'% number) song_name_list={} i=0 while i < number: song_name_list[i] = dic['song_list'][i]['title'] log_utils.C_INFO(str(i+1)+':'+song_name_list[i]) i += 1 return song_name_list
def wait_element_by_mode(self, t, mode, element='', thing=''): """功能: 等待某控件出现,等待一段规定的时间,假如没有出现就会返回1 参数: t:设置等待的时间 mode:查找模式,例如By,ID,By.XPATH element:等待的控件查找依据,比如id,xpath,必须要跟mode的模式相匹配 thing:等待的元素的描述 返回值: 0:等待成功 1:等待失败""" try: WebDriverWait(self.driver, t).until( expected_conditions.presence_of_element_located( (mode, element))) except: log_utils.F_ERROR(thing + '超时') self.screenshot('错误截图:' + thing + '等待超时') return 1 log_utils.C_INFO('等待' + thing + '成功') return 0
def __get_first_three_song_name_from_playlist(number,url = ''): """功能: 获取对应url歌单的前number首歌曲名 参数: number:获取的歌曲名数量 返回值: 1:获取失败""" dic = __get_info(url) if dic == 1: log_utils.F_ERROR('获取信息失败') return 1 song_list_name = dic['result']['info']['list_title'] log_utils.C_INFO('该歌单名为:'+song_list_name) song_name_list={} log_utils.C_INFO('该歌单的前 %d 首歌名为:'%number) i=0 while i < number: song_name_list[i] = dic['result']['songlist'][i]['title'] log_utils.C_INFO(str(i+1)+':'+song_name_list[i]) i += 1 return song_name_list
def click_play_history_page_third_song_layout_to_play(self): if not self.find_element_and_action( By.XPATH, self.play_history_page_third_song_layout_xpath, self.action.click, '第三首歌曲') == 0: log_utils.F_ERROR('点击第三首歌曲失败') return 1
def click_unlogin_container(self): """功能: 点击非登录的头像,进入太合登录页""" if not self.find_element_and_action(By.ID, self.unlogin_container_id, self.action.click, '头像') == 0: log_utils.F_ERROR('点击失败')
def click_play_history_button(self): """点击最近播放""" if not self.find_element_and_action(By.ID, self.play_history_id, self.action.click, '最近播放') == 0: log_utils.F_ERROR('点击失败')