def getLocationForElementXpath(self, xPath): if AbstractPage.flag: try: element = self.getElementByXPath(xPath) return element.location['x'], element.location['y'] except Exception, e: Log.logError(str(e))
def selectDropdownListItem(self, id, value): if AbstractPage.flag: try: select = Select(self.getElementByID(id)) select.select_by_visible_text(value) except Exception, e: Log.logError(str(e))
def mergeUserTwitterWithFacebook(self, usernameMerged, passwordMerged): if AbstractPage.flag: try: if(self.checkControlExistedById(self.link_unlinkFacebook_id, 5)==False): if self.checkControlExistedByXpath(self.btn_connectNowFb_xpath) == True: # Click connect now to facebook self.facebookLoginPage = self.connectNowFacebook() # Login facebook self.facebookLoginPage.login(usernameMerged, passwordMerged) # Click on Allow button if show message warning if self.facebookLoginPage.checkControlExistedByXpath(self.facebookLoginPage.btn_allow_xpath,timeout=5) == True: self.facebookLoginPage.clickElementByXPath(self.facebookLoginPage.btn_allow_xpath) # Close dialog self.closePopup() # Click on Merge Account button if self.checkControlExistedByXpath(self.btn_mergeAccount_xpath,timeout=5) == True: self.clickElementByXPath(self.btn_mergeAccount_xpath) else: Log.logInfo("Account merged.") except Exception, e: Log.logError(str(e)) Log.logInfo("Account is not merged.")
def getNameOfPicture(self): if AbstractPage.flag: try: return self.getAttributeByXPath(self.img_myProfile_xpath, "title") except Exception, e: Log.logError(str(e))
def selectDropdownListItemXpath(self, xpath, xpathItem): if AbstractPage.flag: try: self.getSettingPage.getElementByXPath(xpath).click() self.getSettingPage.getElementByXPath(xpathItem).click() except Exception, e: Log.logError(str(e))
def clickElementById(self, id): if AbstractPage.flag: try: element = self.getElementByID(id) element.click() except Exception, e: Log.logError(str(e))
def clickElementByXPath(self, xpath): if AbstractPage.flag: try: element = self.getElementByXPath(xpath) element.click() except Exception, e: Log.logError(str(e))
def getLocationForElementById(self, id): if AbstractPage.flag: try: element = self.getElementByID(id) return element.location['x'], element.location['y'] except Exception, e: Log.logError(str(e))
def sendKeyByxPath(self, xPath, value): if AbstractPage.flag: try: element = self.getElementByXPath(xPath) element.clear() element.send_keys(value) except Exception, e: Log.logError(str(e))
def sendKeyById(self, id, value): if AbstractPage.flag: try: element = self.getElementByID(id) element.clear() element.send_keys(value) except Exception, e: Log.logError(str(e))
def getTextById(self, id): if AbstractPage.flag: try: element = self.getElementByID(id) return element.text except Exception, e: Log.logError(str(e)) AbstractPage.flag = False
def getTextByXPath(self, xpath): if AbstractPage.flag: try: element = self.getElementByXPath(xpath) return element.text except Exception, e: Log.logError(str(e)) AbstractPage.flag = False
def getAttributeById(self, id, attributeName): if AbstractPage.flag: try: element = self.getElementByID(id) return element.get_attribute(attributeName) except Exception, e: Log.logError(str(e)) AbstractPage.flag = False
def mouseMoveToElementById(self, elementID): if AbstractPage.flag: try: element = self.getElementByID(elementID) hov = ActionChains(AbstractPage.Browser()).move_to_element(element) hov.perform() except Exception, e: Log.logError(str(e))
def getNameOfPicture(self): if AbstractPage.flag: try: return self.getAttributeByXPath(self.img_myProfile_xpath, "title") except Exception, e: Log.logError(str(e))
def getElementByXPath(self, xPath, timeout = 30): if AbstractPage.flag: try: WebDriverWait(AbstractPage.Browser, timeout).until(lambda driver: AbstractPage.Browser().find_element_by_xpath(xPath)) return AbstractPage.Browser().find_element_by_xpath(xPath) except Exception, e: Log.logError(str(e)) AbstractPage.flag = False
def closeBrowser(): try: AbstractPage.Browser().quit() AbstractPage.browser = None AbstractPage.flag = True Log.logInfo("Close browser") except Exception, e: Log.logError(str(e))
def checkControlExistedByXpath(self,element_xpath, timeout = 60): if AbstractPage.flag: try: WebDriverWait(AbstractPage.Browser, timeout).until(lambda driver: AbstractPage.Browser().find_element_by_xpath(element_xpath)) return True except Exception, e: Log.logError(str(e)) return False
def selectRadioButtonById(self, id): if AbstractPage.flag: try: element = self.getElementByID(id) status = self.getAttributeById(id, "checked") if status == None: element.click() except Exception, e: Log.logError(str(e))
def getListNameFromPopUp(self): if AbstractPage.flag: try: AbstractPage.Browser().switch_to_alert() txt_shareNewPopup = self.getTextByXPath(self.txt_shareNewPopup_xpath) txt_listname = str(txt_shareNewPopup).split('"') return txt_listname[1] except Exception, e: Log.logError(str(e)) return str(e)
def getAccountName(self): if AbstractPage.flag: try: # Get first name and last name self.FirstName = self.getTextByXPath(self.lbl_firstName_xpath) self.LastName = self.getTextByXPath(self.lbl_lastName_xpath) return self.FirstName, self.LastName except Exception, e: Log.logError(str(e))
def getAccountName(self): if AbstractPage.flag: try: # Get first name and last name self.FirstName = self.getTextByXPath(self.lbl_firstName_xpath) self.LastName = self.getTextByXPath(self.lbl_lastName_xpath) return self.FirstName, self.LastName except Exception, e: Log.logError(str(e))
def getListNameFromPopUp(self): if AbstractPage.flag: try: AbstractPage.Browser().switch_to_alert() txt_shareNewPopup = self.getTextByXPath( self.txt_shareNewPopup_xpath) txt_listname = str(txt_shareNewPopup).split('"') return txt_listname[1] except Exception, e: Log.logError(str(e)) return str(e)
def Browser(cls): if cls.browser == None: # open browser try: if AbstractPage.browser == None: AbstractPage.browser = Browser.initBrowser() Log.logInfo("Open browser successfully") except Exception, e: Log.logError(str(e)) Log.logError("Open browser unsuccessfully") AbstractPage.flag = False
def openAccountSettingPage(self): if AbstractPage.flag: # click dropdown menu self.clickElementById(self.btn_dropMenu_id) # Open Account Setting page self.clickElementByXPath(self.link_accountSetting_xpath) Log.logError("Open account setting successfully") return AccountSettingPageFB()
def openAccountSettingPage(self): if AbstractPage.flag: # click dropdown menu self.clickElementById(self.btn_dropMenu_id) # Open Account Setting page self.clickElementByXPath(self.link_accountSetting_xpath) Log.logError("Open account setting successfully") return AccountSettingPageFB()
def navigateToWeb(url): if AbstractPage.flag: try: AbstractPage.Browser().get(url) AbstractPage.Browser().implicitly_wait(60) Log.logInfo("Navigate to %s successfully" % url) return AbstractPage.Browser() except Exception, e: AbstractPage.flag = False Log.logError(str(e)) Log.logError("Navigate to %s unsuccessfully" % url)
def switchToPopUpWindow(self): if AbstractPage.flag: try: # get current window parentWindow = AbstractPage.browser.current_window_handle handles = AbstractPage.browser.window_handles handles.remove(parentWindow) # handle pop up window AbstractPage.browser.switch_to_window(handles.pop()) except Exception, e: Log.logError(str(e)) AbstractPage.flag = False
def closePopup(self): if AbstractPage.flag: try: AbstractPage.Browser().switch_to_alert() # Click on 'x' link self.clickElementByXPath(self.btn_closeConnectPageDialog_xpath) Log.logInfo("The pop up closed successfully") return SettingPageKlout() except Exception, e: Log.logError(str(e)) Log.logError("The pop up doesnot close successfully")
def isProfileOfSelectedUserIsReturned(self, username): if AbstractPage.flag: try: fullName = self.getTextByXPath(self.lbl_firstName_xpath)+self.getTextByXPath(self.lbl_lastName_xpath) if str(fullName).__contains__(username)== True: Log.logInfo("Profile of selected user is returned.") return True else: Log.logInfo("Profile of selected user is not returned.") return False except Exception, e: Log.logError(str(e)) return False
def isListNameOnPopup(self, listName): if AbstractPage.flag: try: AbstractPage.Browser().switch_to_alert() txt_shareNewPopup = self.getTextByXPath(self.txt_shareNewPopup_xpath) if str(txt_shareNewPopup).__contains__(listName) == True: Log.logInfo("List name is on the pop up.") return True else: Log.logInfo("List name is not on the pop up.") return False except Exception, e: Log.logError(str(e)) return False
def isListNameOnPopup(self, listName): if AbstractPage.flag: try: AbstractPage.Browser().switch_to_alert() txt_shareNewPopup = self.getTextByXPath( self.txt_shareNewPopup_xpath) if str(txt_shareNewPopup).__contains__(listName) == True: Log.logInfo("List name is on the pop up.") return True else: Log.logInfo("List name is not on the pop up.") return False except Exception, e: Log.logError(str(e)) return False
def isProfileOfSelectedUserIsReturned(self, username): if AbstractPage.flag: try: fullName = self.getTextByXPath( self.lbl_firstName_xpath) + self.getTextByXPath( self.lbl_lastName_xpath) if str(fullName).__contains__(username) == True: Log.logInfo("Profile of selected user is returned.") return True else: Log.logInfo("Profile of selected user is not returned.") return False except Exception, e: Log.logError(str(e)) return False
def checkControlStatus(self, id, status): if AbstractPage.flag: try: controlStatus = self.getAttributeById(id, status) if controlStatus != None and status == "disabled": Log.logInfo("Control status is disabled") return True elif controlStatus == None and status == "enabled": Log.logInfo("Control status is enabled") return True else: Log.logInfo("Cannot find control") return False except Exception, e: Log.logError(str(e)) return False
def logoutKlout(): if AbstractPage.flag: try: # Log out if the page is the home (already logged in) then log out before logging in page = AbstractPage() if page.checkControlExistedById(page.btn_dropdownMenu_id): # Select Log out menu item # wait for dropdown menu displayed successful time.sleep(1) page.mouseMoveToElementById(page.btn_dropdownMenu_id) # wait for dropdown menu displayed successful time.sleep(1) page.mouseMoveToElementById(page.btn_home_id) page.mouseMoveToElementById(page.btn_dropdownMenu_id) page.clickElementByXPath(page.link_logout_xpath) page = None except Exception, e: Log.logError(str(e))
def logoutFacebook(): if AbstractPage.flag: try: # Log out if the page is the home (already logged in) then log out before logging in page = AbstractPage() if page.checkControlExistedById(page.btn_homeFacebook_id): # Click dropdown menu page.clickElementById(page.btn_dropMenuFacebook_id) # wait for dropdown menu displayed successful time.sleep(2) # Click Logout page.clickElementByXPath(page.link_logoutFacebook_xpath) time.sleep(2) page = None except Exception, e: Log.logError(str(e))
def logoutTwitter(): if AbstractPage.flag: try: # Log out if the page is the home (already logged in) then log out before logging in page = AbstractPage() if page.checkControlExistedById(page.btn_dropMenuTwitter_id): # Click dropdown menu time.sleep(5) page.clickElementById(page.btn_dropMenuTwitter_id) time.sleep(2) # Click Logout page.clickElementByXPath(page.link_logoutTW_xpath) time.sleep(2) page = None except Exception, e: Log.logError(str(e))
def logoutTwitterWhenLoggedIn(self): if AbstractPage.flag: try: element01 = self.getElementByXPath(self.btn_dropMenuTW_xpath) element02 = self.getElementByXPath(self.btn_logoutTW_xpath) # Use ActionChains class to set click action for elements builder = ActionChains(AbstractPage.browser); builder.click(element01); builder.click(element02); # Execute javascript to click on element on Twitter Home page # Parameters: # first argument - The JavaScript to execute # second argument - The arguments to the script. May be empty AbstractPage().browser.execute_script("arguments[0].click();", element01) AbstractPage().browser.execute_script("arguments[0].click();", element02) except Exception, e: Log.logError(str(e)) Log.logError("Log out unsuccessfully") AbstractPage.flag = False