def validate_login(self): driver = self.driver driver.find_element_by_xpath("//a[contains(@href,'swadhikar-c')]" ) # validate if profile link exists log.info( "[LinkedIn] Linked in login successful. Profile link displayed!") sleep(5) return 1
def download_firefox(cls, url, installer): if not file.download_file(url=url, file=installer): log.error( '[Firefox] Failed to download firefox installer from website') return 0 log.info("[Firefox] Downloaded firefox from website successfully!") return 1
def un_mount(cls): cmd = 'sudo hdiutil detach /Volumes/Firefox' if not cls.run(cmd): log.warn("[FireFox] Failed to unmount volume") else: log.info("[Firefox] Firefox volume unmounted successfully!") return 1
def is_installed(cls): cmd = "ls /Applications/ | grep -i firefox" if not Command.execute(cmd): log.info("[FireFox] Firefox not installed in this machine") return 0 log.info("[FireFox] Firefox already installed!") return 1
def uninstall(cls): cmd = 'sudo rm -rf /Applications/Firefox.app/' if not Command.execute(cmd): log.info("[FireFox] Unable to uninstall firefox!") return 0 log.info("[FireFox] Firefox uninstalled successfully!") return 1
def mount(cls, installer): cmd = 'sudo hdiutil attach {}'.format(installer) if not cls.run(cmd): log.error("[FireFox] Failed to mount volume") return 0 log.info("[Firefox] Firefox volume mounted successfully!") return 1
def download_and_install_firefox(cls, url, installer): if not cls.download_firefox(url=url, installer=installer): return 0 if not cls.install_firefox(installer=installer, remove=True): return 0 log.info("[Firefox] Firefox installation successful!") return 1
def install(cls): cmd = 'sudo cp -R /Volumes/Firefox/Firefox.app/ /Applications/Firefox.app' if not cls.run(cmd): log.error( "[FireFox] Failed to copy Firefox.app to Applications folder") return 0 log.info("[Firefox] Firefox installed successfully!") return 1
def capture_screen(driver=None, path=None, filename='screen', timestamp=False): if not path: path = variables.SCREENSHOT_PATH filename = path + filename if timestamp: filename += get_current_timestamp() filename += ".png" driver.get_screenshot_as_file(filename) log.info("[WebDriver] Screenshot captured: '{}'".format(filename)) return 1
def get_output(cmd): """ @brief Executes a command and returns output @param cmd Command to execute @return Command output """ if not cmd: raise Exception("Please enter a valid command to run. You entered '{}'".format(cmd)) log.info("[Command.get_output()] Executing command: '{}'".format(cmd)) list_cmd = str(cmd).split(" ") return check_output(list_cmd)
def capture_screen(path=None, filename='screen_print'): if not path: path = variables.SCREENSHOT_PATH filename = path + filename cmd = 'screencapture {}.png'.format(filename) sleep(1) if not Command.run(cmd): log.error("Failed to capture screen") return 0 log.info("Captured screen: " + filename) return 1
def run(cmd): """ @brief Executes a command @param cmd Command to execute @return 1 Success """ if not cmd: raise Exception("Please enter a valid command to run. You entered '{}'".format(cmd)) log.info("[Command.run()] Executing command: '{}'".format(cmd)) list_cmd = str(cmd).split(" ") call(list_cmd) log.info("[Command.run()] Command executed successfully!") return 1
def login_page(self): driver = self.driver driver.get(FACEBOOK_URL) log.info("Facebook loaded successfully") # Enter credentials field = driver.find_element_by_id('email') field.click() field.clear() field.send_keys(FACEBOOK_USER) field = driver.find_element_by_id('pass') field.click() field.clear() field.send_keys("<your password>") field = driver.find_element_by_id('loginbutton') field.click() driver.implicitly_wait(5) return 1
def check_tabs(browser): list_tab_names = [ 'Home', 'About TCE', 'Academics', 'Admission', 'Departments', 'Research', 'Contact Us' ] for tab in list_tab_names: if not driver_util.wait_for_object(browser, id=tab): raise Exception("Unable to detect tab: '{}'".format(tab)) else: log.info("[TCE.check_tabs()] Found tab: '{}'".format(tab)) time.sleep(1) log.info("[TCE.check_tabs()] Detected all the tabs successfully!") return 1
def login_page(self): driver = self.driver driver.get(LINKEDIN_URL) log.info("[LinkedIn] Linkedin loaded successfully") # Enter credentials field = driver.find_element_by_id('login-email') field.click() field.clear() field.send_keys(LINKEDIN_USER) field = driver.find_element_by_id('login-password') field.click() field.clear() field.send_keys("<your password>") field = driver.find_element_by_id('login-submit') field.click() driver.implicitly_wait(5) return 1
def get_driver(browser_name, chrome_disable_notifications=True): if str(browser_name).lower() == 'chrome': if chrome_disable_notifications: options = Options() options.add_argument('--disable-notifications') return webdriver.Chrome(executable_path=variables.WIN_CHROME_DRIVER, chrome_options=options) return webdriver.Chrome(executable_path=variables.WIN_CHROME_DRIVER) if str(browser_name).lower() == 'firefox': return webdriver.Firefox() if str(browser_name).lower() == 'safari': return webdriver.Safari() log.info("Driver not supported for browser: '{}'".format(browser_name)) return None
def close_current_tab(driver): body = driver.find_element_by_tag_name('body') if not body: log.info("[WebDriver] Unable to find tag: 'body'") body.send_keys(Keys.COMMAND + 'w') log.info("[WebDriver] Current tab closed!") log.info("[WebDriver] Current tab closed successfully!") return 1
def check_installation(cls): found = 0 cmd = 'open -a Firefox' log.info("[Firefox] Starting firefox...") if not cls.run(cmd): log.info("[Firefox] Failed to open firefox") return 0 try: firefox = atomac.getAppRefByBundleId(BID_FIREFOX) found = 1 firefox.activate() sleep(5) capture_screen('firefox_install_success') sleep(2) atomac.terminateAppByBundleId(BID_FIREFOX) log.info("[Firefox] Firefox launched successfully!") except ValueError: log.error("[Firefox] Firefox not running!") return found
def bring_focus(driver): driver.execute_script("window.focus()") sleep(0.5) log.info("[WebDriver] Firefox window brought into focus") return 1
def create_new_tab(driver): driver.find_element_by_tag_name('body').send_keys(Keys.COMMAND + 't') driver.switch_to_window(driver.window_handles[-1]) log.info("[WebDriver] New tab created!") return 1
def toggle_to_main_window(driver): driver.switch_to.window(main_window_handle) log.info("[WebDriver] Toggle to main window successful!") return 1
def validate_login(self): driver = self.driver driver.find_element_by_xpath("//a[contains(@href,'ref=logo')]") log.info("Facebook logo found") sleep(5) return 1
def add(a, b): return a + b def sub(x, y): return x - y def mul(x, y): return x * y def div(x, y): try: res = x / y except ZeroDivisionError: log.exception("Division by zero") else: return res if __name__ == '__main__': x = 10 y = 0 log.debug("Add: {} + {} = {}".format(x, y, add(x, y))) log.debug("Sub: {} - {} = {}".format(x, y, sub(x, y))) log.debug("Mul: {} * {} = {}".format(x, y, mul(x, y))) log.exception("Div: {} / {} = {}".format(x, y, div(x, y))) log.info("Simple calculator successfully implemented!")
def execute_step_validate(func, summary, expected_output=(1,), exception=Exception, exception_msg='', *args, **kwargs): """ @brief Executes the given function and validates result @param func Method to be executed @param summary Step summary @param expected_output Tuple of elements to be validated against result @param exception Name of the exception to be thrown @param exception_msg Exception message while throwing @param args List of arguments @param kwargs Keyword arguments """ stars = (len(summary) + 25) * "*" log.info(stars) log.info("Method : " + func.__name__) log.info("Summary : " + summary) log.info("Arguments : " + str(args)) log.info("Keyword args : " + str(kwargs)) log.info(stars) # Force tuple the expected output if not in type tuple or list if not isinstance(expected_output, tuple) or not isinstance(expected_output, list): expected_output = (expected_output,) try: result = func(*args, **kwargs) if result not in expected_output: log.error("Result '{}' not in expected output '{}'".format(result, expected_output)) if not exception_msg: exception_msg = "Result '{}' not in expected output '{}'".format(result, expected_output) raise exception(exception_msg) log.info(log.error("Result '{}' is present in expected output '{}'".format(result, expected_output))) finally: log.info("Completed step: '{}'".format(summary))
def execute_step(func, summary, *args, **kwargs): """ @brief Executes the given function @param func Method to be executed @param summary Step summary @param args List of arguments @param kwargs Keyword arguments """ stars = (len(summary) + 25) * "*" log.info(stars) log.info("Method : " + func.__name__) log.info("Summary : " + summary) log.info("Arguments : " + str(args)) log.info("Keyword args : " + str(kwargs)) log.info(stars) try: func(*args, **kwargs) finally: log.info("Completed step: '{}'".format(summary))