def finish_voice_query(self, name, completed): if completed: logger.info("The %s is over" % name) self.engine.endLoop() else: logger.error("There is an exception when %s" % name) self.engine.endLoop()
def test_Xiaowei_Disconnect_Cycle_While_Music_Is_Streaming_01(automation_xiaowei_connect): dut = automation_xiaowei_connect[0] phone = automation_xiaowei_connect[1] assert phone.xiaowei.verify_device_icon(), "Xiaowei connection fail" test_step(1, "Launch QQ music", dut) assert phone.launch_application(PhoneAppType.QQMUSIC), "Failed to launch QQ music" test_step(2, "Play a music in QQ music", dut) assert phone.qqmusic.play_music(), "Failed to play music" assert dut.status.get_sink_state() == SinkStates.A2DP_STREAMING, "The music was not played" test_step(3, "Launch xiaowei", dut) assert phone.launch_application(PhoneAppType.XIAOWEI), "Failed to switch to Xiaowei" cycle = 20 for i in range(cycle): logger.info("Test disconnect xiaowei and reopen for %d times, %d" % (cycle, i + 1)) assert dut.status.get_sink_state() == SinkStates.A2DP_STREAMING, "The music was not played" test_step(4, "Disconnect dut", dut) assert phone.xiaowei.disconnect_device(), "Failed to disconnect dut" test_step(5, "Close xiaowei APP", dut) assert phone.xiaowei.close_app(), "Failed to close xiaowei" test_step(6, "Re-open xiaowei APP", dut) assert phone.xiaowei.launch_app(), "Failed to re-open Xiaowei" test_step(7, "Verify prompt and accept it", dut) assert phone.xiaowei.verify_connection_prompt(), "Failed to verify connection prompt" assert phone.xiaowei.accept_connection_prompt(), "Failed to accept connection prompt" test_step(8, "Verify if xiaowei connect to dut", dut) assert phone.xiaowei.verify_device_icon(), "Failed to connect with dut" test_step(9, "Close QQ music", dut) assert phone.qqmusic.terminate_app(), "Failed to close QQ music" wait(20) assert dut.status.get_sink_state() == SinkStates.CONNECTED, "Failed to close QQ music"
def music_resuming_after_interruption(self, wait_time=5): logger.info('start to resume music') if self.phone_info.phone_type == PhoneType.ANDROID: for i in range(30): try: WebDriverWait(self.driver.appium_driver, wait_time, 1).until( lambda x: x.find_element_by_xpath(self.pause_music_label)) except Exception as e: logger.info('Music was not resumed, try again') wait(1) else: logger.info('Music has been resumed') return True else: return False else: for i in range(30): try: ele_obj = self.driver.appium_driver.find_element_by_xpath(self.play_or_pause_music_button) if ele_obj.text == "暂停": logger.info('Music has been resumed') return True except Exception as e: logger.info('Music was not resumed, try again') wait(1) else: return False
def insure_device_connection(self, wait_time=5): if not self.verify_device_icon(wait_time): result1 = self.verify_connection_prompt(wait_time) result2 = self.accept_connection_prompt() return (result1 and result2) else: logger.info('device is already connected') return True
def download_xiaowei(cls, phone): if phone.phone_type == PhoneType.ANDROID: phone.launch_application(PhoneAppType.BLUETOOTH) driver = phone._current_app.driver.appium_driver logger.info("downloading xiaowei") appUrl = r'https://imtt.dd.qq.com/16891/apk/EE10B0EEF0826192921AF851952191E5.apk?fsname=com.tencent.xw' driver.install_app(appUrl) if phone.phone_type == PhoneType.IOS: bundle_id = 'com.apple.AppStore' app_current_status = driver.execute_script('mobile: queryAppState', {'bundleId': bundle_id}) if app_current_status == const.APPLICATION_NOT_RUNNING or app_current_status == \ const.APPLICATION_RUNNING_IN_BACKGROUND: driver.execute_script('mobile: activateApp', {'bundleId': bundle_id}) else: driver.execute_script('mobile: launchApp', {'bundleId': bundle_id}) search_bar_xpath = '//XCUIElementTypeSearchField' xiaowei_download_xpath = '//XCUIElementTypeButton[contains(@name,"腾讯小微")]//XCUIElementTypeButton' xiaowei_open_xpath = '//XCUIElementTypeButton[contains(@name,"腾讯小微")]//XCUIElementTypeButton[@name="open"]' search_tab_xpath = '//*[@name="Search"]' search_tab = cls.wait_find_element(driver, search_tab_xpath) if search_tab is not None: logger.debug("search tab is found") search_tab.click() else: logger.debug("search tab is not found") return False search_bar = cls.wait_find_element(driver, search_bar_xpath) if search_bar is not None: logger.debug("search bar is found") search_bar.clear() search_bar.send_keys("腾讯小微" + "\n") else: logger.debug("search bar is not found") return False open_btn = cls.wait_find_element(driver, xiaowei_open_xpath) if open_btn is not None: logger.debug("open button is found") return True else: logger.debug("open button is not found") download_btn = cls.wait_find_element(driver, xiaowei_download_xpath) if download_btn is not None: logger.debug("download button is found") download_btn.click() else: logger.debug("download button is not found") return False return True
def close_pop_window(self, wait_time=5): wait(5, 'If there is a pop window, close it') try: WebDriverWait(self.driver.appium_driver, wait_time, 1).until( lambda x: x.find_element_by_xpath(self.close_membership_application_button)) except Exception as e: return True else: logger.info('Succeed to close pop windows') return self._verify_and_click_element(self.close_membership_application_button)
def verify_setting(self, check_setting_xpath): try: logger.info("Verify the required setting") self.driver.appium_driver.find_element_by_xpath( check_setting_xpath) except Exception as e: logger.error("The setting was verified to be wrong") return False else: logger.info("Complete the required setting successfully") return True
def perform_setting(self, perform_setting_xpath): try: logger.info("Perform the required setting") wait(1) self.driver.appium_driver.find_element_by_xpath( perform_setting_xpath).click() except Exception as e: logger.error("Unable to do the required setting") return False else: logger.info("Complete the required setting") return True
def check_qq_music_pause_state(self): wait(1) active_app = self.phone.qqmusic.activate_app() click_no_reminder_button = self.phone.qqmusic.no_reminder_for_music_interruption( ) pause_music = self.phone.qqmusic.pause_music() if active_app and click_no_reminder_button and pause_music: if self.dut.status.get_sink_state() == SinkStates.A2DP_STREAMING: logger.info( "QQ music has been paused, streaming is come from VPA response" ) return True
def lock_screen(self, condition, lock_time): logger.info('Screen will be locked') try: self.driver.appium_driver.lock(lock_time) except Exception as e: logger.error(e) condition.wait() self.unlock_screen() wait(2) self.swip_up(n=2) wait(2) self.unlock_with_password() condition.notify()
def test_Xiaowei_Reopen_Cycle_01(automation_xiaowei_connect): dut = automation_xiaowei_connect[0] phone = automation_xiaowei_connect[1] assert phone.xiaowei.verify_device_icon(), "Failed to connect with dut" cycle = 20 for i in range(cycle): logger.info("Test xiaowei reopen for %d times, %d" % (cycle, i+1)) test_step(1, "Close xiaowei APP", dut) assert phone.xiaowei.close_app(), "Failed to close xiaowei" test_step(2, "Re-open xiaowei APP", dut) assert phone.xiaowei.launch_app(), "Failed to re-open Xiaowei" test_step(3, "Verify if xiaowei connect to dut", dut) assert phone.xiaowei.verify_device_icon(), "Failed to connect with dut"
def get_current_media_name(self, wait_time=5): logger.info('get current media name') for i in range(wait_time): try: time.sleep(1) media_name = self.driver.appium_driver.find_element_by_xpath( self.get_media_name_xpath).text if media_name is not None: return media_name except Exception: pass else: logger.info('Fail to get current media name') return None
def automation_xiaowei_connect(dut_precondition_connected_activated_in_xiaowei): logger.info('--------------starting test case setup---------------') assert dut_precondition_connected_activated_in_xiaowei, "Failed to connect to dut through bluetooth" dut = dut_precondition_connected_activated_in_xiaowei.dut_list[0] phone = dut_precondition_connected_activated_in_xiaowei.phones[0] assert phone.launch_application(PhoneAppType.XIAOWEI), "Failed while going to launch Xiaowei" assert phone.xiaowei.insure_device_connection(), "Failed to connect to dut" yield dut, phone logger.info('--------------starting test case cleanup---------------') assert phone.xiaowei.disconnect_device(), "Failed to disconnect dut from xiaowei" assert phone.xiaowei.quit(), "Failed to close xiaowei" phone._set_current_app_to_none()
def test_Xiaowei_Dut_Power_Cycle_01(automation_xiaowei_connect): dut = automation_xiaowei_connect[0] phone = automation_xiaowei_connect[1] assert phone.xiaowei.verify_device_icon(), "Xiaowei connection fail" cycle = 20 for i in range(cycle): logger.info("Power cycle dut for %d times, %d" % (cycle, i + 1)) test_step(1, "Power off dut", dut) dut.key.press_power_off() test_step(2, "Verify if xiaowei connect to dut", dut) assert not phone.xiaowei.verify_device_icon(wait_time=3), "Xiaowei still connect" test_step(3, "Power on dut", dut) dut.key.press_power_on() test_step(4, "Verify if xiaowei connect to dut", dut) assert phone.xiaowei.verify_device_icon(), "Fail to connect with dut"
def verify_connection_prompt(self, wait_time=5): """ check the connection prompt :return: True """ logger.info('Verify connection prompt') try: WebDriverWait(self.driver.appium_driver, wait_time, 1).until(lambda x: x.find_element_by_xpath( self.connection_prompt_xpath)) return True except Exception as e: logger.error(e) logger.info('There is no xiaowei prompt in current page') return False
def cancel_connection_prompt(self, wait_time=5): """ Tap cancel button :return: True """ logger.info('Cancel connection prompt') try: WebDriverWait(self.driver.appium_driver, wait_time, 1).until( lambda x: x.find_element_by_xpath(self.cancel_button_xpath)) self.driver.appium_driver.find_element_by_xpath( self.cancel_button_xpath).click() return True except Exception as e: logger.error(e) logger.info('There is no cancel button') return False
def disconnect_device(self, count=5): for i in range(count): self.swip_down() device_status = self.go_to_device_page() if device_status: self.swip_up() time.sleep(1) result1 = self._verify_and_click_element( self.disconnect_button_xpath) if result1: result2 = self.verify_device_icon(1) if not result2: logger.info('disconnect device successfully') return True else: logger.info('Fail to disconnect device') return False
def verify_device_icon(self, wait_time=5): """ verify if device icon display, used fro verifying xiaowei connection :return: True """ logger.info('Verify device icon') for i in range(wait_time): try: time.sleep(1) if self.driver.appium_driver.find_element_by_xpath( self.device_icon_xpath).is_displayed(): return True except Exception: pass else: logger.info('There is no device icon in current page') return False
def test_Xiaowei_Disconnect_Cycle_01(automation_xiaowei_connect): dut = automation_xiaowei_connect[0] phone = automation_xiaowei_connect[1] assert phone.xiaowei.verify_device_icon(), "Xiaowei connection fail" cycle = 20 for i in range(cycle): logger.info("Test disconnect xiaowei and reopen for %d times, %d" % (cycle, i + 1)) test_step(1, "Disconnect dut", dut) assert phone.xiaowei.disconnect_device(), "Failed to disconnect dut" test_step(2, "Close xiaowei APP", dut) assert phone.xiaowei.close_app(), "Failed to close xiaowei" test_step(3, "Re-open xiaowei APP", dut) assert phone.xiaowei.launch_app(), "Failed to re-open Xiaowei" test_step(4, "Verify prompt and accept it", dut) assert phone.xiaowei.verify_connection_prompt(), "Failed to verify connection prompt" assert phone.xiaowei.accept_connection_prompt(), "Failed to accept connection prompt" test_step(5, "Verify if xiaowei connect to dut", dut) assert phone.xiaowei.verify_device_icon(), "Failed to connect with dut"
def test_Xiaowei_VPA_Button_Stress_01(automation_xiaowei_connect): dut = automation_xiaowei_connect[0] phone = automation_xiaowei_connect[1] assert phone.xiaowei.verify_device_icon(), "Xiaowei connection fail" cycle = 40 for i in range(cycle): press_time = random.choice([0.35, 1, 2, 3, 5, 8, 13, 21]) logger.info("VPA button stress for %d times, %d" % (cycle, i + 1)) test_step(1, "Power VPA button for %d second" % press_time, dut) dut.key.press_and_release(keys=Buttons.VPA, press_duration_sec=press_time) wait(2) query_response_dict = XiaoweiSupport.xw_query_response_dict() query_text = random.choice(list(query_response_dict["query_weekday"])) response_text = query_response_dict["query_weekday"][query_text] test_step(2, "Test xiaowei voice query the query_weather", dut) XiaoweiSupport.test_vpa(dut, query_text) assert XiaoweiSupport.test_xiaowei_vpa(), "xiaowei recording or streaming failed" test_step(3, "Verify that the text of this query and response in xiaowei APP is correct or not", dut) assert phone.xiaowei.verify_query_response_text(query_text, response_text), "xiaowei query or response failed" phone.xiaowei.swip_down(n=8)
def go_to_device_page(self): logger.info('go to device page') self._verify_and_click_element(self.device_icon_xpath) try: result = self.driver.appium_driver.find_element_by_xpath( self.device_page_xpath) if result: logger.info('Go to device page successfully') else: logger.info('Fail to go to device page') return result except Exception as e: logger.error(e) logger.info('Fail to go to device page') return False
def go_to_home_page(self): logger.info('go to media streaming page') result = self._verify_and_click_element(self.go_to_home_page_xpath) if result: logger.info('Go to home page successfully') else: logger.info('Fail to go to home page page') return result
def test_Xiaowei_Bluetooth_Disable_Cycle_01(automation_xiaowei_connect): dut = automation_xiaowei_connect[0] current_name = dut.bluetooth.get_name() phone = automation_xiaowei_connect[1] assert phone.xiaowei.verify_device_icon(), "Xiaowei connection fail" cycle = 20 for i in range(cycle): logger.info("Test Bluetooth disable and enable cycle for %d times, %d" % (cycle, i + 1)) test_step(1, "Switch to bluetooth settings", dut) assert phone.launch_application(PhoneAppType.BLUETOOTH), "Failed to switch to bluetooth settings" test_step(2, "Disable bluetooth on phone", dut) assert phone.bluetooth.bt_radio(enable='off'), "Failed to disable bluetooth" test_step(3, "Enable bluetooth on phone", dut) assert phone.bluetooth.bt_radio(enable='on'), "Failed to enable bluetooth" # iPhone will not connect with blutooth device after disable/enable bluetooth wait(3) if not phone.bluetooth.bt_is_connected_to(current_name): assert phone.bluetooth.connect_paired_device(current_name), "Failed to dut via bluetooth" wait(0.5) ###iap issue workaround test_step(4, "Switch to xiaowei APP", dut) assert phone.launch_application(PhoneAppType.XIAOWEI), "Failed to switch to Xiaowei" test_step(5, "Verify if xiaowei connect to dut", dut) assert phone.xiaowei.verify_device_icon(), "Fail to connect with dut"
def terminate_app(self): logger.info('The settings app will be terminated') try: self.driver.appium_driver.terminate_app(self.package_name) except Exception as e: logger.info('The settings app cannot be terminated') else: logger.info('The settings app has been terminated successfully') return True
def verify_query_no_response_text(self, query_text): wait(2) logger.info('Get text of this query %s ...' % query_text) if self.phone_info.phone_type == PhoneType.ANDROID: xw_query_in_current_page = self.driver.appium_driver.find_elements_by_xpath( self.get_query_response_text) count = 0 xw_last_query = xw_query_in_current_page[-1].text if xw_last_query == query_text: return xw_last_query else: for xiaowei_item in xw_query_in_current_page: if xiaowei_item.text.find("你好,我是小微") >= 0: try: logger.info( "query text in xiaowei : %s" % xw_query_in_current_page[count + 1].text) if count + 1 == len(xw_query_in_current_page): return "录音失败" # elif len(xw_query_in_current_page) > count + 2: # return "录音失败" else: return xw_query_in_current_page[count + 1].text except Exception as e: return "录音失败" else: count += 1 else: try: xw_query_in_current_page = self.driver.appium_driver.find_element_by_xpath( self.get_query_text) except Exception as e: return "录音失败" else: xw_last_query = xw_query_in_current_page.text return xw_last_query
def terminate_app(self): logger.info('Xiaowei will be terminated') try: self.driver.appium_driver.terminate_app(self.package_name) except Exception as e: logger.error(e) logger.info('Xiaowei cannot be terminated') else: logger.info('Xiaowei has been terminated successfully') return True
def quit_app(self): logger.info('Quit QQ Music driver') try: self.driver.appium_driver.quit() except Exception as e: logger.error(e) logger.info('QQ Music driver cannot be quited') return False else: logger.info('QQ Music driver has been quited successfully') return True
def activate_app(self): logger.info('QQ Music will be brought to foreground') try: self.driver.appium_driver.activate_app(self.package_name) except Exception as e: logger.error(e) logger.info('QQ Music cannot be brought to foreground') return False else: logger.info('QQ Music has been brought to foreground successfully') return True
def activate_activity(self): logger.info('QQ Music app activity will be activate') try: self.driver.appium_driver.start_activity(self.package_name, self.activity_name) except Exception as e: logger.error(e) logger.info('QQ Music app activity cannot be activated') return False else: logger.info('QQ Music app activity has been activated') return True
def close_app(self): logger.info('QQ Music will be closed') try: self.driver.appium_driver.close_app() except Exception as e: logger.error(e) logger.info('QQ Music cannot be closed') return False else: logger.info('QQ Music has been closed successfully') return True