def __create_driver(self, name, *args, **kwargs): folder_with_drivers = os.path.expanduser('~') driver = None if name == self.IE: path_to_iedriver = os.path.join( folder_with_drivers, 'IEDriverServer.exe' ) if os.path.exists(path_to_iedriver): driver = webdriver.Ie(executable_path=path_to_iedriver, **kwargs) else: raise Exception("IEDriver.exe wasn't found in " + path_to_iedriver) elif name == self.GC: path_to_chromedriver = os.path.join( folder_with_drivers, 'chromedriver.exe' if is_windows() else 'chromedriver' ) if os.path.exists(path_to_chromedriver): driver = webdriver.Chrome(executable_path=path_to_chromedriver, **kwargs) else: raise Exception("Chromedriver wasn't found in " + path_to_chromedriver) elif name == self.OP: path_to_selenium_server = os.path.join( folder_with_drivers, 'operadriver.exe' if is_windows else 'operadriver' ) if os.path.exists(path_to_selenium_server): capabilities = DesiredCapabilities.OPERA.copy() capabilities['engine'] = 2 driver = webdriver.Opera(desired_capabilities=capabilities, executable_path=path_to_selenium_server, **kwargs) else: raise Exception("Selenium server jar file wasn't found in " + path_to_selenium_server) elif name == self.FF: path_to_driver = os.path.join( folder_with_drivers, 'geckodriver.exe' if is_windows() else 'geckodriver' ) if os.path.exists(path_to_driver): driver = webdriver.Firefox(executable_path=path_to_driver, **kwargs) else: raise Exception("Geckoriver wasn't found in " + path_to_driver) else: raise ValueError( "Unsupported browser '%s', " "supported browsers: ['%s']" % (name, "', '".join(self.__BROWSERS)) ) if driver and not self.is_gc() and not self.is_op(): driver.maximize_window() return driver
def __create_driver(self, name, *args, **kwargs): driver_and_constructor = { self.FF: ('geckodriver', webdriver.Firefox), self.IE: ('IEDriverServer', webdriver.Ie), self.GC: ('chromedriver', webdriver.Chrome), self.OP: ('operadriver', webdriver.Opera), self.PHANTOMJS: ('phantomjs', webdriver.PhantomJS), }.get(name, None) if driver_and_constructor is None: raise ValueError( "Unsupported browser '%s', " "supported browsers: ['%s']" % (name, "', '".join(self.__BROWSERS)) ) driver, constructor = driver_and_constructor if is_windows(): driver += '.exe' home_dir = os.path.expanduser('~') driver_in_home = os.path.join(home_dir, driver) if os.path.exists(driver_in_home): driver = constructor(executable_path=driver_in_home, **kwargs) else: if self.logger: self.logger.warn("Driver for '%s' wasn't found in %s.\n" "Using driver from PATH environment variable.", name, driver_in_home) driver = constructor(**kwargs) if driver and not self.is_gc() and not self.is_op(): driver.maximize_window() return driver
def _get_fixed_position(self, position): fix_for_scrolling = 0 if is_windows() else self.MIN_SCROLL scroll_offset = (self.GetScrollPos(HORIZONTAL), self.GetScrollPos(VERTICAL)) return ( position[0] + scroll_offset[0] * fix_for_scrolling, position[1] + scroll_offset[1] * fix_for_scrolling )
def test_get_file_content(self): if is_windows(): self.skipTest( 'Unsupported in Windows because temp directory is hardcoded to \'/tmp\' in pickled object.' ) else: expected_string = read_file(self.expected_duckduckgo_class_path) self.assertEqual(self.po_class_object._get_file_content().strip(), expected_string.strip())
def test_get_file_content(self): if is_windows(): self.skipTest( 'Unsupported in Windows because temp directory is hardcoded to \'/tmp\' in pickled object.' ) else: expected_string = read_file(self.expected_duckduckgo_class_path) self.assertEqual( self.po_class_object._get_file_content().strip(), expected_string.strip() )
def __fix_class_initialization(self, po_class): class_name = po_class.name field_name = class_name.lower() field_line = u' cls.{field_name} = {class_name}(cls.browser, cls.logger)'.format( field_name=field_name, class_name=class_name) text = self.txt_content.GetValue() if field_line not in text: class_def_end = u'cls).setUpClass()' shift = 13 if is_windows() else len(LINESEP) # windows hack pos = text.index(class_def_end) + len(class_def_end) + shift self.insert_text(field_line, pos)
def __fix_class_initialization(self, po_class): class_name = po_class.name field_name = class_name.lower() field_line = u' cls.{field_name} = {class_name}(cls.browser, cls.logger)'.format(field_name=field_name, class_name=class_name) text = self.txt_content.GetValue() if field_line not in text: class_def_end = u'cls).setUpClass()' shift = 13 if is_windows() else len(LINESEP) # windows hack pos = text.index(class_def_end) + len(class_def_end) + shift self.insert_text(field_line, pos)
def __fix_imports(self, po_class): root_folder = os.path.normpath( self.GetTopLevelParent().get_root_folder()) path, _ = os.path.splitext(po_class.file_path) path = path.replace(root_folder, '').strip() paths = [p for p in os.path.normpath(path).split(os.sep) if len(p) > 0] correct_import = u'.'.join(paths) class_name = po_class.name import_line = u'from {relative_import} import {class_name}'.format( relative_import=correct_import, class_name=class_name) text = self.txt_content.GetValue() if import_line not in text: base_test_import = u'import BaseTest' shift = 6 if is_windows() else len(LINESEP) # windows hack pos = text.index(base_test_import) + len(base_test_import) + shift self.insert_text(import_line, pos)
def test_save_po_object_class(self): if is_windows(): self.skipTest( 'Unsupported in Windows because temp directory is hardcoded to \'/tmp\' in pickled object.' ) else: for path in (self.po_class_object.file_path, self.po_class_object.img_path): safe_remove_path(path) self.po_class_object.save() check_if_path_exists(self.po_class_object.file_path) self.assertGreater(os.path.getsize(self.po_class_object.file_path), 0) check_if_path_exists(self.po_class_object.img_path) self.assertGreater(os.path.getsize(self.po_class_object.img_path), 0)
def __fix_imports(self, po_class): root_folder = os.path.normpath(self.GetTopLevelParent().get_root_folder()) path, _ = os.path.splitext(po_class.file_path) path = path.replace(root_folder, '').strip() paths = [p for p in os.path.normpath(path).split(os.sep) if len(p) > 0] correct_import = u'.'.join(paths) class_name = po_class.name import_line = u'from {relative_import} import {class_name}'.format( relative_import=correct_import, class_name=class_name ) text = self.txt_content.GetValue() if import_line not in text: base_test_import = u'import BaseTest' shift = 6 if is_windows() else len(LINESEP) # windows hack pos = text.index(base_test_import) + len(base_test_import) + shift self.insert_text(import_line, pos)
def test_get_formatted_fields(self): if is_windows(): self.skipTest( 'Unsupported in Windows because temp directory is hardcoded to \'/tmp\' in pickled object.' ) else: expected_fields = u''' BAD_NAME = (By.LINK_TEXT, u'⇶') # location: (1261, 23) dimensions: (33, 33) LOGO_HOMEPAGE_LINK = (By.ID, u'logo_homepage_link') # location: (526, 117) dimensions: (250, 200) SEARCH_FORM_INPUT_HOMEPAGE = (By.ID, u'search_form_input_homepage') # location: (347, 347) dimensions: (562, 46) SEARCH_BUTTON_HOMEPAGE = (By.ID, u'search_button_homepage') # location: (912, 350) dimensions: (52, 40) TAG_HOME_NAV_TAG_HOME_NAV_ITEM = (By.CSS_SELECTOR, u'.tag-home__nav > .tag-home__nav__item') # location: (625, 458) dimensions: (7, 7) TAG_HOME_NAV_ITEM_IS_ACTIVE = (By.CSS_SELECTOR, u'.tag-home__nav__item.is-active') # location: (658, 458) dimensions: (7, 7) ADD_TO_BROWSER_BADGE_ICON_BROWSER_FIREFOX = (By.CSS_SELECTOR, u'.add-to-browser-badge__icon.browser--firefox') # location: (518, 521) dimensions: (64, 64) ID_FOOTER_HOMEPAGE_DIV_1_SPAN_1_BR_1 = (By.XPATH, u'id("footer_homepage")/DIV[1]/SPAN[1]/BR[1]') # location: (747, 572) dimensions: (0, 20) DDGSI_ADD_TO_BROWSER_BADGE_CLOSE_JS_ADD_TO_BROWSER_CLOSE = (By.CSS_SELECTOR, u'.ddgsi.add-to-browser-badge__close.js-add-to-browser-close') # location: (754, 559) dimensions: (30, 38) ''' self.assertEqual( self.po_class_object._get_fields_as_code().strip(), expected_fields.strip())
def test_save_po_object_class(self): if is_windows(): self.skipTest( 'Unsupported in Windows because temp directory is hardcoded to \'/tmp\' in pickled object.' ) else: for path in (self.po_class_object.file_path, self.po_class_object.img_path): safe_remove_path(path) self.po_class_object.save() check_if_path_exists(self.po_class_object.file_path) self.assertGreater( os.path.getsize(self.po_class_object.file_path), 0 ) check_if_path_exists(self.po_class_object.img_path) self.assertGreater( os.path.getsize(self.po_class_object.img_path), 0 )
def test_parse_string_to_po_class(self): if is_windows(): self.skipTest( "Unsupported in Windows because temp directory is hardcoded to '/tmp' in pickled object." ) else: expected_string = read_file(self.expected_duckduckgo_class_path) self.assertEqual(self.po_class_object._get_file_content().strip(), expected_string.strip()) po_class = PageObjectClass.parse_string_to_po_class( read_file(self.expected_duckduckgo_class_path)) self.assertEqual(po_class.name, self.po_class_object.name) self.assertEqual(po_class.url, self.po_class_object.url) self.assertEqual(po_class.fields, self.po_class_object.fields) self.assertEqual(po_class.area, self.po_class_object.area) self.assertEqual(po_class.file_path, self.po_class_object.file_path) self.assertEqual(po_class.img_path, self.po_class_object.img_path) self.assertEqual(po_class, self.po_class_object)
def test_get_formatted_fields(self): if is_windows(): self.skipTest( 'Unsupported in Windows because temp directory is hardcoded to \'/tmp\' in pickled object.' ) else: expected_fields = u''' BAD_NAME = (By.LINK_TEXT, u'⇶') # location: (1261, 23) dimensions: (33, 33) LOGO_HOMEPAGE_LINK = (By.ID, u'logo_homepage_link') # location: (526, 117) dimensions: (250, 200) SEARCH_FORM_INPUT_HOMEPAGE = (By.ID, u'search_form_input_homepage') # location: (347, 347) dimensions: (562, 46) SEARCH_BUTTON_HOMEPAGE = (By.ID, u'search_button_homepage') # location: (912, 350) dimensions: (52, 40) TAG_HOME_NAV_TAG_HOME_NAV_ITEM = (By.CSS_SELECTOR, u'.tag-home__nav > .tag-home__nav__item') # location: (625, 458) dimensions: (7, 7) TAG_HOME_NAV_ITEM_IS_ACTIVE = (By.CSS_SELECTOR, u'.tag-home__nav__item.is-active') # location: (658, 458) dimensions: (7, 7) ADD_TO_BROWSER_BADGE_ICON_BROWSER_FIREFOX = (By.CSS_SELECTOR, u'.add-to-browser-badge__icon.browser--firefox') # location: (518, 521) dimensions: (64, 64) ID_FOOTER_HOMEPAGE_DIV_1_SPAN_1_BR_1 = (By.XPATH, u'id("footer_homepage")/DIV[1]/SPAN[1]/BR[1]') # location: (747, 572) dimensions: (0, 20) DDGSI_ADD_TO_BROWSER_BADGE_CLOSE_JS_ADD_TO_BROWSER_CLOSE = (By.CSS_SELECTOR, u'.ddgsi.add-to-browser-badge__close.js-add-to-browser-close') # location: (754, 559) dimensions: (30, 38) ''' self.assertEqual( self.po_class_object._get_fields_as_code().strip(), expected_fields.strip() )
def test_parse_string_to_po_class(self): if is_windows(): self.skipTest( "Unsupported in Windows because temp directory is hardcoded to '/tmp' in pickled object." ) else: expected_string = read_file(self.expected_duckduckgo_class_path) self.assertEqual( self.po_class_object._get_file_content().strip(), expected_string.strip() ) po_class = PageObjectClass.parse_string_to_po_class( read_file(self.expected_duckduckgo_class_path) ) self.assertEqual(po_class.name, self.po_class_object.name) self.assertEqual(po_class.url, self.po_class_object.url) self.assertEqual(po_class.fields, self.po_class_object.fields) self.assertEqual(po_class.area, self.po_class_object.area) self.assertEqual(po_class.file_path, self.po_class_object.file_path) self.assertEqual(po_class.img_path, self.po_class_object.img_path) self.assertEqual(po_class, self.po_class_object)
def _get_fixed_position(self, position): fix_for_scrolling = 0 if is_windows() else self.MIN_SCROLL scroll_offset = (self.GetScrollPos(HORIZONTAL), self.GetScrollPos(VERTICAL)) return (position[0] + scroll_offset[0] * fix_for_scrolling, position[1] + scroll_offset[1] * fix_for_scrolling)