class Scrapper: def __init__(self, browser, url): self.pages = Pages(browser=browser) self.pages.navigate(url) self.browser = self.pages.driver locator_type = LocatorType() def snippet(self): self.elem = self.pages.find_all_elements(locator="""//*[@href and @class="result_title_link"]""",type='xpath') self.ref_len = len(self.elem) list =[] for i in range(self.ref_len): self.el = self.elem[i] self.x = self.el.get_attribute('href') self.pages.new_tab(self.x) self.para = self.pages.find_all_elements('p',type='tags') for p in self.para: print(p.text) list.append(p.text) list.append('END') self.pages.end() self.pages.switch_to_main() return list
def test_search_product(self): page = Pages(self.driver) page.main_page.open() page.main_page.accept_cookie() page.main_page.search_product('laptop') page.main_page.switch_to_list_view() page.main_page.select_product_in_list(3) page.main_page.save_screenshot()
def get_pages(browser_driver, url_manager): from pages.pages import Pages return Pages(browser_driver, url_manager)
import requests import logging from pages.pages import Pages logging.basicConfig( format="%(asctime)s %(levelname)-8s [%(filename)s:%(lineno)d] %(message)s", datefmt="%d-%m-%Y %H:%M:%S", level=logging.INFO, filename="logs.txt") logger = logging.getLogger("scraping") logger.info("Loading books list....") page_content = requests.get( "http://books.toscrape.com/").content #it has page 1 page = Pages(page_content) lists_books = page.info_list for page_num in range( 1, page.page_count): #we start from page 2 to page_count which is 50 page_content = requests.get( f"http://books.toscrape.com/catalogue/page-{page_num+1}.html").content logger.debug("Creating All Pages from page content") page = Pages(page_content) lists_books.extend(page.info_list)
def test_order_selling_site_modal_window(browser): page = MainPage(browser) pages = Pages(browser) page.go_to_url(urls.URLS["BASE_URL"]) page.scroll_page() assert page.check_order_selling_site_button_present( ), "Order selling site is not displayed" page.click_order_selling_site_button_present() assert pages.check_order_site_modal_window_present( ), "Order selling site modal window is not displayed" pages.click_order_button() assert pages.check_required_fields_warnings_show( ) == 2, "less then 2 warnings were showed" pages.input_name("test name") pages.click_order_button() assert pages.check_required_fields_warnings_show( ) == 1, "less then 1 warning was showed" pages.input_phone_number("+1111111111") pages.click_order_button() assert pages.check_success_order_block_shows( ), "success order block is not present" pages.close_order_modal_window()
class XMLWrapper: def __init__(self): self.webpage = Pages(browser="chrome") self.init_action() def init_action(self): self.action = { 'open': lambda target, value: self.webpage.navigate(target), 'type': lambda target, value: self.webpage.input_text( self.get_locator(target), self.get_locator_type(target), value ), 'click': lambda target, value: self.webpage.click( self.get_locator(target), self.get_locator_type(target)), 'clear': lambda target, value: self.webpage.cleartext( self.get_locator(target), self.get_locator_type(target)), 'submit': lambda target, value: self.webpage.submit( self.get_locator(target), self.get_locator_type(target)), 'sendKeys': lambda target, value: self.webpage.press_key( self.get_locator(target), self.get_locator_type(target), text=value), 'select': lambda target, value: self.webpage.select( self.get_locator(target), self.get_locator_type(target)), 'selectFrame': lambda target, value: self.webpage.switch_frame( self.get_locator(target)), 'doubleClick': lambda target, value: self.webpage.double_click( self.get_locator(target), self.get_locator_type(target)) } extensions = ('id', 'name', "link", 'xpath', 'css', 'index') def get_locator(self, target): if any(target.startswith(ext) for ext in self.extensions): return target.split("=", 1)[1] else: return target def get_locator_type(self, target): if any(target.startswith(ext) for ext in self.extensions): return target.split("=", 1)[0] else: return 'xpath' def job(self, command, target, value): lambdafunc = self.action.get(command) lambdafunc(target, value) time.sleep(1) def automate_xml(self, location="", df=None, func=None, *args, **kwargs): if df is None: with open(location) as fd: doc = xmltodict.parse(fd.read()) xml_in_json = json.loads(json.dumps(doc)) todo_list = xml_in_json['TestCase']['selenese'] for argument in todo_list: print(argument['command'], argument['target'], argument['value']) self.job(argument['command'], argument['target'], argument['value']) else: with open(location) as fd: template_xml = fd.read() for index, row in df.iterrows(): template_dict = { "Something to ignore": "Ignore information TOFA" } for iterator in range(len(row)): template_dict[df.columns[iterator]] = row[iterator] xml_string = template_xml.format(**template_dict) doc = xmltodict.parse(xml_string) xml_in_json = json.loads(json.dumps(doc)) todo_list = xml_in_json['TestCase']['selenese'] for argument in todo_list: print(argument['command'], argument['target'], argument['value']) self.job(argument['command'], argument['target'], argument['value'])
def __init__(self): self.webpage = Pages(browser="chrome") self.init_action()
def __init__(self, browser, url): self.pages = Pages(browser=browser) self.pages.navigate(url) self.browser = self.pages.driver