def wait_for(func, timeout, first=0.0, step=1.0, text=None): """ If func() evaluates to True before timeout expires, return the value of func(). Otherwise return None. @brief: Wait until func() evaluates to True. :param timeout: Timeout in seconds :param first: Time to sleep before first attempt :param steps: Time to sleep between attempts in seconds :param text: Text to print while waiting, for debug purposes """ start_time = time.time() end_time = time.time() + timeout time.sleep(first) while time.time() < end_time: if text: PD_DebugLog.debug_print("%s (%f secs)" % ( text, (time.time() - start_time))) output = func() if output: return output time.sleep(step) return None
def wait_for(func, timeout, first=0.0, step=1.0, text=None): """ If func() evaluates to True before timeout expires, return the value of func(). Otherwise return None. @brief: Wait until func() evaluates to True. :param timeout: Timeout in seconds :param first: Time to sleep before first attempt :param steps: Time to sleep between attempts in seconds :param text: Text to print while waiting, for debug purposes """ start_time = time.time() end_time = time.time() + timeout time.sleep(first) while time.time() < end_time: if text: PD_DebugLog.debug_print("%s (%f secs)" % (text, (time.time() - start_time))) output = func() if output: return output time.sleep(step) return None
def get_current_node_id(self): id_attr = self.node.get_attribute('id') if DEBUG_LOG_PRINT: PD_DebugLog.debug_print(self.__class__.__name__ + " The node is: " + id_attr) return id_attr
def pd_login(self, driver, username, password): ''' Use default configurations in shared_config file to login PD ''' pd_browser_client = driver PD_DebugLog.stepinfo("Login PowerDirector") PD_DebugLog.debug_print("client browser title is " + pd_browser_client.title) assert "PowerDirector" in pd_browser_client.title PD_DebugLog.info_print("Page title: " + pd_browser_client.title) uid_input = pd_browser_client.find_element_by_id("uid") uid_input.send_keys(username) p_input = pd_browser_client.find_element_by_id("pword") p_input.send_keys(password) login_button = pd_browser_client.find_element_by_xpath("/html/body/div/form/div/p[4]/input") login_button.click() expect_welcome_panel = pd_browser_client.find_element_by_id("user_panel") welcome_text = "PowerDirector" try: if welcome_text in expect_welcome_panel.text: PD_DebugLog.debug_print(expect_welcome_panel.text) except: raise Login_Error("Login fails") pd_browser_client.quit() return pd_browser_client
def click(self): xpath = '//*[@id="%s"]/a/span[1]' % self.get_current_node_id() if DEBUG_LOG_PRINT: PD_DebugLog.debug_print(self.__class__.__name__ + "The xpath is : " + xpath) elm = self.node.find_element(By.XPATH, xpath) elm.click()
def get_node_title(self): xpath = '//*[@id="%s"]/a/span[2]' % self.get_current_node_id() if DEBUG_LOG_PRINT: PD_DebugLog.debug_print(self.__class__.__name__ + "The xpath is : " + xpath) return self.node.find_element(By.XPATH, xpath).text
def check_if_platform_ip_existing(self, driver, platform_ip, platform_name): ''' To get platform node from the web page And enter into the page, and get the elements info. ''' cur_platform_ip = '' cur_platform_name = '' default_main_browser = Main_Browser_UIMap(driver) subtree = default_main_browser.get_platform_sub_menu_tree() platform_subtree = Platform_Tree(driver, subtree) count = platform_subtree.get_platform_count() for index in range(count): #elem = platform_subtree.get_platform_item(index) #platformNode.switch_to_page() platform_subtree.enter_platform_summary_page(index) # elem.click() # switch into the platform tree node # elem_page_driver = Platform_Summary_UIMap(default_main_browser) cur_platform_name = platform_subtree.get_platform_name(index) cur_platform_ip = platform_subtree.get_platform_ip() #index if platform_name == cur_platform_name: break PD_DebugLog.debug_print("") if platform_ip != cur_platform_ip or platform_name != cur_platform_name: return False else: return True
def save_screenshot(driver, path, filename): ''' To save the screenshot for the failed case ''' screenshot_filename = os.path.join(path, filename) PD_DebugLog.debug_print("The saved file is " + screenshot_filename) driver.save_screenshot(screenshot_filename)
def get_submenu_tree_id(self): #ul_elm = self.tree.find_element(By.XPATH, '//li/ul') #if DEBUG_LOG_PRINT: # PD_DebugLog.debug_print("The sub tree is: " + ul_elm.text) id_attr = self.tree.get_attribute('id') if DEBUG_LOG_PRINT: PD_DebugLog.debug_print("The tree id is: " + id_attr) return id_attr
def get_submenu_tree(self, submenu_idx): ##platform_tree //*[@id="pane"]/div[1] # //*[@id="pane"]/div[1]/ul/li left_panel = self.driver.find_element(By.ID, Submenu_Tree.LeftPanelID) xpath = '//div[%d]/ul/li/ul' % (submenu_idx) submenu = left_panel.find_element(By.XPATH, xpath) if DEBUG_LOG_PRINT: PD_DebugLog.debug_print("The sub menu text is: " + submenu.text) return submenu
def get_platform_ip(self): '''Get the platform ip from the ip page ''' ip_text = '' if self.platform_summary: ip_text = self.platform_summary.get_platform_ip() if PD_DebugLog.DEBUG_LOG_PRINT: PD_DebugLog.debug_print("The IP text is: " + ip_text) return ip_text
def is_closed(self): # check if the switch is closed xpath = '//*[@id="%s"]/span' % self.get_current_node_id() if DEBUG_LOG_PRINT: PD_DebugLog.debug_print(self.__class__.__name__ + "The xpath is : " + xpath) elem = self.node.find_element(By.XPATH, xpath) attr = elem.get_attribute('class') node_closed = attr.endwith('_close') return node_closed
def get_vm_static_label_text(self, label_xpath): """ get the static label text """ self._common_enter_summary_frame_proc() elem = self.driver.find_element(By.XPATH, label_xpath) text = elem.text PD_DebugLog.debug_print("Get the element title: " + elem.text) self._common_exit_summary_frame_proc() return text
def get_vm_static_label_text(self, label_xpath): ''' get the static label text ''' self._common_enter_summary_frame_proc() elem = self.driver.find_element(By.XPATH, label_xpath) text = elem.text PD_DebugLog.debug_print("Get the element title: " + elem.text) self._common_exit_summary_frame_proc() return text
def get_all_child_nodes(self): xpath = '//*[@id="%s"]/li' % self.get_current_node_id() if self.has_child_node(): xpath = '//*[@id="%s"]/ul/li' % self.get_current_node_id() if DEBUG_LOG_PRINT: PD_DebugLog.debug_print(self.__class__.__name__ + ":" + \ "get_all_child_nodes" + \ "The xpath is : " + xpath) elemts = self.node.find_elements(By.XPATH, xpath) return elemts
def readconfig(classname): config = ConfigParser.ConfigParser() config_file = 'cfg\\' + classname + '.cfg' PD_DebugLog.debug_print("The self.config_file is " + config_file) config_file = os.path.join(Basejob.TESTCASE_DIR, config_file) PD_DebugLog.debug_print("The self.config_file is " + config_file) if config_file and os.path.exists(config_file): config.read(config_file) return config else: return None
def get_loss_ratio(output): """ Get the packet loss ratio from the output of ping. :param output: Ping output. """ try: # packet loss return int(re.findall(u'(\d+)% 丢失', output)[0]) except IndexError: PD_DebugLog.debug_print(output) return -1
def is_tab_current_selected(self, tab_idx): ''' return bool value to represent whether the tab is current selected ''' cur_selected = False tab_elm = self.get_tab_from_leftpanel(tab_idx) attr = tab_elm.get_attribute('class') if PD_DebugLog.DEBUG_LOG_PRINT: PD_DebugLog.debug_print("The current tab attribute is: " + attr) if 'current' == attr: cur_selected = True return cur_selected
def get_platform_name(self): ''' get the platform name ''' self._common_enter_summary_frame_proc() # /html/body/div/fieldset/legend xpath = '/html/body/div/fieldset/legend' elem = self.driver.find_element(By.XPATH, xpath) text = elem.text PD_DebugLog.debug_print("Get the element title: " + elem.text) self._common_exit_summary_frame_proc() return text
def get_task_count(self): ''' get the task table element ''' summary_text = self.get_task_summary() if DEBUG_LOG_PRINT: PD_DebugLog.debug_print("Summary text is: " + summary_text) ptn = re.compile(r'\d+') result = re.findall(ptn, summary_text) count = result[0] if DEBUG_LOG_PRINT: PD_DebugLog.debug_print("The count in summary is: " + count) return count
def get_history_tasks_count(self): ''' get all the history tasks count ''' task_status_text = self.driver.find_element(By.XPATH, "/html/body/div/div[2]/div[6]/div[1]/div[6]/div[6]/span").text if (DEBUG_LOG_PRINT): PD_DebugLog.debug_print("History task status:" + task_status_text) ptn = re.compile(r'\d+') count = re.findall(ptn, task_status_text) PD_DebugLog.debug_print("count is: " + count) return int(count)
def get_tab_from_leftpanel(self, tab_idx): ''' get the tab element for platform resource ''' # //*[@id="tab"]/li[2]/a tab = self.driver.find_element(By.XPATH, '//*[@id="tab"]/li[%d]/a' % tab_idx ) #tab = tab_elemts.find_elements(By.XPATH, '//li[%d]' % (tab_idx)) #xpath_ptn = '//li[%d]' % (tab_idx) # /html/body/div[2]/div/ul/li[4] # tab_elmt = ta#tab_elemts[tab_idx-1] if PD_DebugLog.DEBUG_LOG_PRINT: PD_DebugLog.debug_print("The tab text is: " + tab.text) return tab
def has_child_node(self): child_node_existed = False try: xpath = '//*[@id="%s"]/ul' % self.get_current_node_id() if DEBUG_LOG_PRINT: PD_DebugLog.debug_print("The xpath is : " + xpath) elemts = self.node.find_elements(By.XPATH, xpath) if elemts: child_node_existed = True except: child_node_existed = False return child_node_existed
def get_iso_table(self): ''' get the iso table ''' table = self.driver.find_element(By.ID, 'rdo_from_iso') ##iso_table > tbody > tr:nth-child(1) #table_records = table.find_elements_by_tag_name("tr") #PD_DebugLog.debug_print("table count is : %d " % len(table_records)) table_records = self.driver.find_elements_by_xpath('//*[@id="iso_table"]/tbody/tr') PD_DebugLog.debug_print("table count is : %d " % len(table_records)) for tr in table_records: field2_elm = self.driver.find_element_by_xpath('//*[@id="iso_table"]/tbody/tr[2]/td[2]/div') field2_txt = field2_elm.text PD_DebugLog.debug_print("The table field[1] is :" + field2_txt)
def get_task_percentage(self, task_idx): ''' get the task name for the table row0 field 0 ''' table_elm = self.get_task_table() xpath_str = '//tr[%d]/td[3]/div/div[2]' % (task_idx) elem = table_elm.find_element(By.XPATH, xpath_str) percentage_text = elem.text ptn = re.compile(r'\d+') result = re.findall(ptn, percentage_text) percentage = result[0] if DEBUG_LOG_PRINT: PD_DebugLog.debug_print("the percentage text is " + percentage_text) return percentage
def get_task_description(self, task_type, task_target): ''' get the task name for the table row0 field 0 ''' table_elm = self.get_task_table() result = '' desc = '' for rowid in range(1, 10): xpath_str = '//tbody/tr[%d]/td[1]/div' % (rowid) elem = table_elm.find_element(By.XPATH, xpath_str) task_type_text = elem.text PD_DebugLog.debug_print("Task type is: " + task_type_text) xpath_str = '//tbody/tr[%d]/td[4]/div' % (rowid) elem = table_elm.find_element(By.XPATH, xpath_str) task_target_text = elem.text PD_DebugLog.debug_print("Task type is: " + task_target_text) if task_type == task_type_text and task_target == task_target_text: xpath_str = '//tbody/tr[%d]/td[6]/div' % (rowid) elem = table_elm.find_element(By.XPATH, xpath_str) result = elem.text PD_DebugLog.debug_print("Get the task status: " + result) desc_xpath_str = '//tbody/tr[%d]/td[7]/div' % (rowid) elem = table_elm.find_element(By.XPATH, desc_xpath_str) desc = elem.text PD_DebugLog.debug_print("Get the task status description: " + desc) return result, desc
def get_submenu_tree(self, submenu_idx): ##platform_tree //*[@id="pane"]/div[1] # //*[@id="pane"]/div[1]/ul/li left_panel = self.driver.find_element(By.ID, Main_Browser_UIMap.LeftPanelID) xpath = '//*[@id="pane"]/div[%d]/ul/li/ul/li' % (submenu_idx) submenu = left_panel.find_element(By.XPATH, xpath) if PD_DebugLog.DEBUG_LOG_PRINT: if submenu is list: for menu in submenu: PD_DebugLog.debug_print("The sub menu text is: " + menu.text) else: PD_DebugLog.debug_print("The sub menu text is: " + submenu.text) return submenu
def get_sub_menu_tree_title(self, tabid): # //*[@id="pane"]/div[%d]/ul/li/span # /html/body/div[2]/div/div[2]/div[%d]/ul/li/span left_panel = self.driver.find_element(By.ID, Main_Browser_UIMap.LeftPanelID) xpath = '//*[@id="pane"]/div[%d]/ul/li/span' % (tabid) submenu = left_panel.find_element(By.XPATH, xpath) if PD_DebugLog.DEBUG_LOG_PRINT: if submenu is list: for menu in submenu: PD_DebugLog.debug_print("The sub menu text is: " + menu.text) else: PD_DebugLog.debug_print("The sub menu text is: " + submenu.text) return submenu.text
def get_tab_from_leftpanel(self, tab_idx): ''' get the tab element for platform resource ''' # //*[@id="tab"]/li[2]/a tab = self.driver.find_element(By.XPATH, '//*[@id="tab"]/li[%d]/a' % tab_idx) #tab = tab_elemts.find_elements(By.XPATH, '//li[%d]' % (tab_idx)) #xpath_ptn = '//li[%d]' % (tab_idx) # /html/body/div[2]/div/ul/li[4] # tab_elmt = ta#tab_elemts[tab_idx-1] if PD_DebugLog.DEBUG_LOG_PRINT: PD_DebugLog.debug_print("The tab text is: " + tab.text) return tab
def __init__(self, driver, root_elemt): ''' Constructor ''' self.driver = driver self.root = root_elemt expected_root_title = "DataCenter" PD_DebugLog.debug_print("The expected root title is: " + expected_root_title) self.platform_total_num = 0 self.platform_summary = None root_elemt_title = self.get_tree_node_elemt_title(self.root) PD_DebugLog.debug_print("The fetched root title is: " + root_elemt_title) assert(expected_root_title == root_elemt_title)
def __init__(self, driver, root_elemt): ''' Constructor ''' self.driver = driver self.root = root_elemt expected_root_title = "DataCenter" PD_DebugLog.debug_print("The expected root title is: " + expected_root_title) self.platform_total_num = 0 self.platform_summary = None root_elemt_title = self.get_tree_node_elemt_title(self.root) PD_DebugLog.debug_print("The fetched root title is: " + root_elemt_title) assert (expected_root_title == root_elemt_title)
def get_platform_ip(self): ''' get the platform ip ''' self._common_enter_summary_frame_proc() # /html/body/div/fieldset/table/tbody/tr/td[2]/ul/li[2]/span[2] # //*[@id="wrapper"]/fieldset/table/tbody/tr/td[2]/ul/li[2]/span[2] xpath_from_Chrome = '//*[@id="wrapper"]/fieldset/table/tbody/tr/td[2]/ul/li[2]/span[2]' xpath_from_firefox = '/html/body/div/fieldset/table/tbody/tr/td[2]/ul/li/span[2]' xpath_str = xpath_from_firefox elem = self.driver.find_element(By.XPATH, xpath_str) PD_DebugLog.debug_print("Get the element (platform ip) title: " + elem.text) text = elem.text self._common_exit_summary_frame_proc() return text
def select_template_from_table_by_name(self, name): # //*[@id="templates_table"]/tbody/tr[2]/td[2]/div table_records = self.driver.find_elements_by_xpath('//*[@id="templates_table"]/tbody/tr') PD_DebugLog.debug_print("table count is : %d " % len(table_records)) # select the table record and click the radio button for nid in range(len(table_records)): field2_elm = self.driver.find_element_by_xpath('//*[@id="templates_table"]/tbody/tr[%d]/td[2]/div' % (nid + 1) ) field2_txt = field2_elm.text PD_DebugLog.debug_print("table element name is :" + field2_txt) if name in field2_txt: #self.driver.find_element_by_xpath('//*[@id="templates_table"]/tbody/tr[%d]/td[1]/div/[@id="imagePcid"]' % (nid + 1) ).click() table_records[nid].find_element(By.ID, 'imagePcid').click() PD_DebugLog.debug_print("Hit") PD_DebugLog.debug_print("The id is %d" % nid) #break PD_DebugLog.debug_print("The table field[%d] is : %s" % (nid+1, field2_txt))
def expand_more_actions_bar(self): """ Make the more actions bar is expanded, if not expand it """ self._common_enter_summary_frame_proc() xpath = "/html/body/div/form/div/table/tbody/tr/td[2]/div/h3/a" more_actions_bar_elemt = self.driver.find_element(By.XPATH, xpath) class_attribute_value = more_actions_bar_elemt.get_attribute("class") PD_DebugLog.debug_print("more actions bar class attribute is : " + class_attribute_value) if class_attribute_value != "expand": more_actions_bar_elemt.click() class_attribute_value = more_actions_bar_elemt.get_attribute("class") PD_DebugLog.debug_print("more actions bar class attribute is : " + class_attribute_value) self._common_exit_summary_frame_proc()
def click_register_image_link(self): ''' get the register image link should use the right xpath expression end with 'a' element. ''' self._common_enter_summary_frame_proc() xpath_from_Chrome = '//*[@id="wrapper"]/div[2]/table/tbody/tr/td/div/ul/li[2]/a' #xpath_from_firefox = '/html/body/div/fieldset/table/tbody/tr/td[2]/ul/li/span[2]' xpath_str = xpath_from_Chrome elem = self.frame_summary_wrapper.find_element(By.XPATH, xpath_str) #elem = self.frame_summary_wrapper.find_element_by_link_text(u'注册镜像') PD_DebugLog.debug_print("Get the element (register image) title: " + elem.text) elem.click() self._common_exit_summary_frame_proc()
def expand_more_actions_bar(self): ''' Make the more actions bar is expanded, if not expand it ''' self._common_enter_summary_frame_proc() xpath = '/html/body/div/form/div/table/tbody/tr/td[2]/div/h3/a' more_actions_bar_elemt = self.driver.find_element(By.XPATH, xpath) class_attribute_value = more_actions_bar_elemt.get_attribute("class") PD_DebugLog.debug_print("more actions bar class attribute is : " + class_attribute_value) if class_attribute_value != 'expand': more_actions_bar_elemt.click() class_attribute_value = more_actions_bar_elemt.get_attribute("class") PD_DebugLog.debug_print("more actions bar class attribute is : " + class_attribute_value) self._common_exit_summary_frame_proc()
def get_task_status_from_history_task(self, task_type, vm_name, host_name, taskstarttime): ''' Get task status from the history task list ''' self.pd_browser_main_map.switch_to_default_content() current_tasks_btn = self.pd_browser_main_map.get_current_task_btn() current_tasks_btn.click() #time.sleep(TestSpeedControl.TEST_STEP_INTERVAL) tasks_popup = self.pd_browser_main_map.get_popup_tasks() tasks_popup_cls_attr = tasks_popup.get_attribute("class") PD_DebugLog.debug_print("The current task: " + tasks_popup_cls_attr) if not "open-state" in tasks_popup_cls_attr: # click the button again current_tasks_btn.click() time.sleep(TestSpeedControl.TEST_STEP_INTERVAL) # Step 13: show the task name pd_browser_current_task_map = PD_CurrentTasks_UIMap( self.pd_client_browser) history_link_btn = pd_browser_current_task_map.get_history_task_link() now_handle = self.pd_client_browser.current_window_handle #得到当前窗口句柄 history_link_btn.click() all_handles = self.pd_client_browser.window_handles #获取所有窗口句柄 task_result = 'SUCCESS' for handle in all_handles: if handle != now_handle: self.pd_client_browser.switch_to_window(handle) time.sleep(1) # check the history log pd_browser_history_task_map = PD_HistoryTasks_UIMap( self.pd_client_browser) time.sleep(1) task_target = vm_name task_result, task_desc = pd_browser_history_task_map.get_task_description( task_type, task_target) time.sleep(1) self.pd_client_browser.switch_to_window(now_handle) return task_result, task_desc
def get_task_id(self, vm_name): ''' ''' table_elm = self.get_task_table() ret_val = 0 task_count = int(self.get_task_count()) for tid in range(1, task_count+1): vm_name_in_task = table_elm.find_element(By.XPATH, '//tr[1]/td[2]/span[1]').text vm_name_in_task = vm_name_in_task.split(':')[1].strip() if DEBUG_LOG_PRINT: PD_DebugLog.debug_print("task name " + vm_name_in_task) if vm_name == vm_name_in_task: ret_val = tid + 1 break return ret_val
def get_task_id(self, vm_name): ''' ''' table_elm = self.get_task_table() ret_val = 0 task_count = int(self.get_task_count()) for tid in range(1, task_count + 1): vm_name_in_task = table_elm.find_element( By.XPATH, '//tr[1]/td[2]/span[1]').text vm_name_in_task = vm_name_in_task.split(':')[1].strip() if DEBUG_LOG_PRINT: PD_DebugLog.debug_print("task name " + vm_name_in_task) if vm_name == vm_name_in_task: ret_val = tid + 1 break return ret_val
def get_task_status_from_history_task(self, task_type, vm_name, host_name, taskstarttime): ''' Get task status from the history task list ''' self.pd_browser_main_map.switch_to_default_content() current_tasks_btn = self.pd_browser_main_map.get_current_task_btn() current_tasks_btn.click() #time.sleep(TestSpeedControl.TEST_STEP_INTERVAL) tasks_popup = self.pd_browser_main_map.get_popup_tasks() tasks_popup_cls_attr = tasks_popup.get_attribute("class") PD_DebugLog.debug_print("The current task: " + tasks_popup_cls_attr) if not "open-state" in tasks_popup_cls_attr: # click the button again current_tasks_btn.click() time.sleep(TestSpeedControl.TEST_STEP_INTERVAL) # Step 13: show the task name pd_browser_current_task_map = PD_CurrentTasks_UIMap(self.pd_client_browser) history_link_btn = pd_browser_current_task_map.get_history_task_link() now_handle = self.pd_client_browser.current_window_handle #得到当前窗口句柄 history_link_btn.click() all_handles = self.pd_client_browser.window_handles #获取所有窗口句柄 task_result = 'SUCCESS' for handle in all_handles: if handle != now_handle: self.pd_client_browser.switch_to_window(handle) time.sleep(1) # check the history log pd_browser_history_task_map = PD_HistoryTasks_UIMap(self.pd_client_browser) time.sleep(1) task_target = vm_name task_result, task_desc = pd_browser_history_task_map.get_task_description( task_type, task_target) time.sleep(1) self.pd_client_browser.switch_to_window(now_handle) return task_result, task_desc