How to use browser_factory method in Playwright Python

Best Python code snippet using playwright-python

__main__.py

Source:__main__.py Github

copy

Full Screen

1#!/usr/bin/env python32import logging3import argparse4import enum5import os6import os.path7import sqlite38import signal9from time import sleep, time, ctime10from random import randrange, random11import collections12from heapq import merge13from selenium import webdriver14from selenium.webdriver.chrome.options import Options15from selenium.webdriver.common.by import By16from selenium.webdriver.common.action_chains import ActionChains17from selenium.webdriver.support.ui import WebDriverWait18from selenium.webdriver.support import expected_conditions as EC19from selenium.common.exceptions import (TimeoutException,20 StaleElementReferenceException,21 NoSuchElementException,22 ElementClickInterceptedException)23from webdriver_manager.chrome import ChromeDriverManager24from webdriver_manager.utils import ChromeType25RESUME_LIST_URL = "https://account.rabota.ua/jobsearch/notepad/cvs"26RESUME_LIST_URL_PATTERN = r"^https://account\.rabota\.ua/(ua/)?jobsearch/notepad/cvs/?$"27LOGIN_URL = "https://rabota.ua/jobsearch/login"28POST_LOGIN_URL_PATTERN = r"^https://account\.rabota\.ua/(ua/)?jobsearch/notepad/vacancies_profile/?$"29UPDATE_BUTTON_XPATH = "//div[contains(@class, 'cv-item-container')]"\30 "//button[contains(@data-bind, 'updateDate') and "\31 "(contains(text(), 'Обновить') or contains(text(), 'Оновити'))]"32UPDATE_INTERVAL = 30 * 6033UPDATE_INTERVAL_MIN_DRIFT = 1034UPDATE_INTERVAL_MAX_DRIFT = 6035MANUAL_LOGIN_TIMEOUT = 360036POST_UPDATE_PAUSE = 3037DB_INIT = [38 "CREATE TABLE IF NOT EXISTS update_ts (\n"39 "name TEXT PRIMARY KEY,\n"40 "value REAL NOT NULL DEFAULT 0)\n"41]42def wall_clock_wait(when, precision=1.):43 """ Sleep variation which is doesn't increases44 sleep duration when computer enters suspend/hybernation45 """46 while time() < when:47 sleep(precision)48def setup_logger(name, verbosity):49 logger = logging.getLogger(name)50 logger.setLevel(verbosity)51 handler = logging.StreamHandler()52 handler.setLevel(verbosity)53 handler.setFormatter(logging.Formatter("%(asctime)s "54 "%(levelname)-8s "55 "%(name)s: %(message)s",56 "%Y-%m-%d %H:%M:%S"))57 logger.addHandler(handler)58 return logger59class LogLevel(enum.IntEnum):60 debug = logging.DEBUG61 info = logging.INFO62 warn = logging.WARN63 error = logging.ERROR64 fatal = logging.FATAL65 crit = logging.CRITICAL66 def __str__(self):67 return self.name68class Command(enum.Enum):69 login = 170 update = 271 def __str__(self):72 return self.name73class BrowserType(enum.Enum):74 chrome = ChromeType.GOOGLE75 chromium = ChromeType.CHROMIUM76 def __str__(self):77 return self.name78class ScheduledEvent(enum.Enum):79 REFRESH = 180 UPDATE = 281ScheduleEntry = collections.namedtuple('ScheduleEntry', ('when', 'what'))82button_wait_condition = EC.presence_of_element_located((By.XPATH, UPDATE_BUTTON_XPATH))83def update(browser, timeout):84 logger = logging.getLogger("UPDATE")85 browser.get(RESUME_LIST_URL)86 WebDriverWait(browser, timeout).until(87 button_wait_condition88 )89 update_buttons = browser.find_elements_by_xpath(UPDATE_BUTTON_XPATH)90 logger.info("Located %d update buttons", len(update_buttons))91 for elem in update_buttons:92 sleep(1 + 2 * random())93 elem.click()94 logger.debug("click!")95 # There is no easy reliable way to make sure all outstanding request are96 # complete. So, just give it enough time.97 sleep(POST_UPDATE_PAUSE)98 logger.info('Updated!')99def login(browser, timeout):100 logger = logging.getLogger("LOGIN")101 browser.get(LOGIN_URL)102 WebDriverWait(browser, timeout).until(103 EC.url_matches(POST_LOGIN_URL_PATTERN)104 )105 logger.info('Successfully logged in!')106def parse_args():107 def check_loglevel(arg):108 try:109 return LogLevel[arg]110 except (IndexError, KeyError):111 raise argparse.ArgumentTypeError("%s is not valid loglevel" % (repr(arg),))112 def check_command(arg):113 try:114 return Command[arg]115 except (IndexError, KeyError):116 raise argparse.ArgumentTypeError("%s is not valid command" % (repr(arg),))117 def check_browser_type(arg):118 try:119 return BrowserType[arg]120 except (IndexError, KeyError):121 raise argparse.ArgumentTypeError("%s is not valid browser type" % (repr(arg),))122 def check_positive_float(arg):123 def fail():124 raise argparse.ArgumentTypeError("%s is not valid positive float" % (repr(arg),))125 try:126 fvalue = float(arg)127 except ValueError:128 fail()129 if fvalue <= 0:130 fail()131 return fvalue132 parser = argparse.ArgumentParser(133 description="Python script to update your CV",134 formatter_class=argparse.ArgumentDefaultsHelpFormatter)135 parser.add_argument("-t", "--timeout",136 help="webdriver wait timeout",137 type=check_positive_float,138 default=10.)139 parser.add_argument("-b", "--browser",140 help="browser type",141 type=check_browser_type,142 choices=BrowserType,143 default=BrowserType.chromium)144 parser.add_argument("-v", "--verbosity",145 help="logging verbosity",146 type=check_loglevel,147 choices=LogLevel,148 default=LogLevel.info)149 parser.add_argument("cmd", help="command",150 type=check_command,151 choices=Command)152 parser.add_argument("-d", "--data-dir",153 default=os.path.join(os.path.expanduser("~"),154 '.config',155 'rabotaua-cv-updater'),156 help="application datadir location",157 metavar="FILE")158 return parser.parse_args()159class BrowserFactory:160 def __init__(self, profile_dir, browser_type, headless=True):161 chrome_options = Options()162 # option below causes webdriver process remaining in memory163 # chrome_options.add_argument('--no-sandbox')164 chrome_options.add_argument('--disable-dev-shm-usage')165 chrome_options.add_argument('--disable-gpu')166 chrome_options.add_argument('user-data-dir=' + profile_dir)167 if headless:168 chrome_options.add_argument('--headless')169 self._options = chrome_options170 self._driver = ChromeDriverManager(chrome_type=browser_type).install()171 def new(self):172 return webdriver.Chrome(173 self._driver,174 options=self._options)175class UpdateTracker:176 def __init__(self, dbpath):177 conn = sqlite3.connect(dbpath)178 cur = conn.cursor()179 try:180 for q in DB_INIT:181 cur.execute(q)182 conn.commit()183 cur.execute("SELECT 1 FROM update_ts WHERE name = ?", ("last",))184 if cur.fetchone() is None:185 cur.execute("INSERT INTO update_ts (name, value) VALUES (?,?)",186 ("last", 0.))187 conn.commit()188 cur.execute("SELECT 1 FROM update_ts WHERE name = ?", ("login",))189 if cur.fetchone() is None:190 cur.execute("INSERT INTO update_ts (name, value) VALUES (?,?)",191 ("login", 0.))192 conn.commit()193 finally:194 cur.close()195 self._conn = conn196 def last_update(self):197 cur = self._conn.cursor()198 try:199 cur.execute("SELECT value FROM update_ts WHERE name = ?",200 ("last",))201 return cur.fetchone()[0]202 finally:203 cur.close()204 def last_login(self):205 cur = self._conn.cursor()206 try:207 cur.execute("SELECT value FROM update_ts WHERE name = ?",208 ("login",))209 return cur.fetchone()[0]210 finally:211 cur.close()212 def update(self, ts):213 c = self._conn214 with c:215 c.execute("UPDATE update_ts SET value = ? WHERE name = ? AND value < ?",216 (float(ts), "last", float(ts)))217 def login(self, ts):218 c = self._conn219 with c:220 c.execute("UPDATE update_ts SET value = ? WHERE name = ? AND value < ?",221 (float(ts), "login", float(ts)))222 def close(self):223 self._conn.close()224 self._conn = None225def random_interval(base, min_drift, max_drift):226 return base + min_drift + random() * (max_drift - min_drift)227class Scheduler:228 def __init__(self, last_login, last_update):229 self._it = self._iter_events(last_login, last_update)230 def __iter__(self):231 return self232 def __next__(self):233 return next(self._it)234 @staticmethod235 def _event_stream(token, last_occured, base, min_drift, max_drift):236 t = max(last_occured + random_interval(base, min_drift, max_drift), time())237 yield ScheduleEntry(when=t, what=token)238 while True:239 t += random_interval(base, min_drift, max_drift)240 yield ScheduleEntry(when=t, what=token)241 @staticmethod242 def _iter_events(last_login, last_update):243 return Scheduler._event_stream(ScheduledEvent.UPDATE,244 last_update,245 UPDATE_INTERVAL,246 UPDATE_INTERVAL_MIN_DRIFT,247 UPDATE_INTERVAL_MAX_DRIFT)248def do_login(browser_factory, timeout):249 browser = browser_factory.new()250 try:251 login(browser, timeout)252 finally:253 browser.quit()254def do_update(browser_factory, timeout):255 browser = browser_factory.new()256 try:257 update(browser, timeout)258 finally:259 browser.quit()260def update_loop(browser_factory, tracker, timeout):261 logger = logging.getLogger("EVLOOP")262 last_update = tracker.last_update()263 last_login = tracker.last_login()264 logger.info("Starting scheduler. "265 "Last update @ %.3f (%s); last refresh @ %.3f (%s).",266 last_update, ctime(last_update),267 last_login, ctime(last_login))268 for ev in Scheduler(last_login, last_update):269 logger.info("Next event is %s @ %.3f (%s)",270 ev.what.name, ev.when, ctime(ev.when))271 wall_clock_wait(ev.when)272 try:273 if ev.what is ScheduledEvent.REFRESH:274 logger.info("Refreshing session now!")275 do_login(browser_factory, timeout)276 tracker.login(time())277 elif ev.what is ScheduledEvent.UPDATE:278 logger.info("Updating CVs now!")279 do_update(browser_factory, timeout)280 tracker.update(time())281 except KeyboardInterrupt:282 raise283 except Exception as exc:284 logger.exception("Event %s handling failed: %s", ev.what.name, str(exc))285def sig_handler(signum, frame):286 raise KeyboardInterrupt287def main():288 args = parse_args()289 mainlogger = setup_logger("MAIN", args.verbosity)290 setup_logger("UPDATE", args.verbosity)291 setup_logger("LOGIN", args.verbosity)292 setup_logger("EVLOOP", args.verbosity)293 os.makedirs(args.data_dir, mode=0o700, exist_ok=True)294 profile_dir = os.path.join(args.data_dir, 'profile')295 browser_factory = BrowserFactory(profile_dir,296 args.browser.value,297 args.cmd is Command.update)298 db_path = os.path.join(args.data_dir, 'updater.db')299 tracker = UpdateTracker(db_path)300 signal.signal(signal.SIGTERM, sig_handler)301 try:302 if args.cmd is Command.login:303 mainlogger.info("Login mode. Please enter your credentials in opened "304 "browser window.")305 try:306 do_login(browser_factory, MANUAL_LOGIN_TIMEOUT)307 tracker.login(time())308 except KeyboardInterrupt:309 mainlogger.warning("Interrupted!")310 elif args.cmd is Command.update:311 mainlogger.info("Update mode. Running headless browser.")312 try:313 update_loop(browser_factory, tracker, args.timeout)314 except KeyboardInterrupt:315 pass316 finally:317 mainlogger.info("Shutting down...")318 finally:319 tracker.close()320if __name__ == "__main__":...

Full Screen

Full Screen

test_testcases.py

Source:test_testcases.py Github

copy

Full Screen

...113 with self.assertRaisesRegex(NoBrowserFactoryError, r'^Browser brand "EDGE"'):114 super().setUp()115 def tearDown(self):116 self.assertFalse(hasattr(self, 'driver'))117 def test_no_browser_factory(self):118 """ NoBrowserFactoryError was raised and the "browser_factory" attribute is None """119 self.assertIsNone(self.browser_factory)120class NoInternetExplorerWebGUITests(SeleniumWebGUITestCase):121 """ No MS Internet Explorer Browser Web GUI Tests.122 Prove that unit tests raise NoBrowserFactoryError.123 """124 # Settings: Missing Browser125 BROWSER_BRAND = 'INTERNETEXPLORER'126 def setUp(self):127 with self.assertRaisesRegex(NoBrowserFactoryError, r'^Browser brand "INTERNETEXPLORER"'):128 super().setUp()129 def tearDown(self):130 self.assertFalse(hasattr(self, 'driver'))131 def test_no_browser_factory(self):132 """ NoBrowserFactoryError was raised and the "browser_factory" attribute is None """133 self.assertIsNone(self.browser_factory)134class NoSafariWebGUITests(SeleniumWebGUITestCase):135 """ No Mac Safari Browser Web GUI Tests.136 Prove that unit tests raise NoBrowserFactoryError.137 """138 # Settings: Missing Browser139 BROWSER_BRAND = 'SAFARI'140 def setUp(self):141 with self.assertRaisesRegex(NoBrowserFactoryError, r'^Browser brand "SAFARI"'):142 super().setUp()143 def tearDown(self):144 self.assertFalse(hasattr(self, 'driver'))145 def test_no_browser_factory(self):146 """ NoBrowserFactoryError was raised and the "browser_factory" attribute is None """...

Full Screen

Full Screen

py_quality_services.py

Source:py_quality_services.py Github

copy

Full Screen

...30 """Choose interface for future browser.31 :return: Browser instance.32 :rtype: Browser.33 """34 return cls.get_browser_factory().get_browser()35 @classmethod36 def get_browser_factory(cls) -> BF:37 """Select a factory for browsers if it does not exist, install it.38 :return: Factory that implements BaseBrowserFactory.39 :rtype: BF.40 """41 if getattr(cls.get_instance(), 'browser_factory', None) is None:42 cls.set_default_browser_factory()43 return cls.get_instance().browser_factory44 @classmethod45 def set_browser_factory(cls, browser_factory: BF) -> None:46 """Set factory in current instance that implements BaseBrowserFactory.47 :param browser_factory: Factory implementing the interface BaseBrowserFactory.48 """49 setattr(cls.get_instance().__class__, 'browser_factory', browser_factory)50 @classmethod51 def set_default_browser_factory(cls) -> None:52 """Choose interface for browser factory."""53 browser_factory = cls.get_local_browser_factory()54 cls.set_browser_factory(browser_factory=browser_factory)55 @classmethod56 def get_instance(cls) -> T:57 """Get or create current instance.58 :return: Get or create current class.59 :rtype: T.60 """61 if getattr(LOCAL, '_instance_container', None) is None:62 setattr(LOCAL, '_instance_container', cls())63 return getattr(LOCAL, '_instance_container', None)64 @classmethod65 def get_service_provider(cls) -> Injector:66 """Get injector instance.67 :return: Injector object.68 :rtype: Injector.69 """70 return cls.get_instance().get_injector()71 @classmethod72 def get(cls, clz: ty.Any) -> ty.Any:73 """Get instance from injector instance.74 :param clz: Class that is associated.75 :return: Instance which is registered in injector.76 :rtype: ty.Any.77 """78 return cls.get_service_provider().get(clz)79 @classmethod80 def get_local_browser_factory(cls) -> LocalBaseBrowserFactory:81 """Get LocalBaseBrowserFactory from injector instance which implements BaseBrowserFactory.82 :return: Get LocalBaseBrowserFactory instance.83 :rtype: LocalBaseBrowserFactory.84 """85 return cls.get(LocalBaseBrowserFactory)86 @classmethod87 def get_element_factory(cls) -> ElementFactory:88 return cls.get(ElementFactory)89 @classmethod90 def get_browser_profile(cls) -> BrowserProfile:91 """Get BrowserProfile from injector instance which implements IBrowserProfile.92 :return: Get BrowserProfile instance.93 :rtype: BrowserProfile.94 """...

Full Screen

Full Screen

testcases.py

Source:testcases.py Github

copy

Full Screen

1""" Selenium Web GUI Test Harness for Python unittest2Use these unittest.TestCase subclasses to run Web GUI tests.3"""4import logging5import unittest6from browser_emulators.chrome.grid_session import ChromeBrowserGridSession7from browser_emulators.chrome.local_session import ChromeBrowserSession8from browser_emulators.firefox.grid_session import FirefoxBrowserGridSession9from browser_emulators.firefox.local_session import FirefoxBrowserSession10# log package name.11log = logging.getLogger('.'.join(__name__.split('.')[:-1]))12# BRAND Key => (HEADLESS=True, Headless=False)13BROWSER_SESSION_CLASS = {14 'ANDROID': None,15 'CHROME': (ChromeBrowserGridSession, ChromeBrowserSession),16 'EDGE': None,17 'FIREFOX': (FirefoxBrowserGridSession, FirefoxBrowserSession),18 'HTMLUNIT': None,19 'HTMLUNITWITHJS': None,20 'INTERNETEXPLORER': None,21 'IPAD': None,22 'IPHONE': None,23 'OPERA': None,24 'PHANTOMJS': None,25 'SAFARI': None,26 'WEBKITGTK': None,27}28class NoBrowserFactoryError(NotImplementedError):29 """ No Browser Factory Error.30 This browser brand has no get_driver() factory class.31 """32 def __init__(self, browser_brand):33 super().__init__(34 'Browser brand "{}" has no Web Driver factory class.'.format(35 browser_brand))36class SeleniumWebGUITestCase(unittest.TestCase):37 """ Selenium Web GUI Test Case.38 This Test Case provides a QA or Regression testing plan.39 This Selenium client describes a Web GUI interface.40 A Selenium Grid service is deployed to emulate browsers already.41 Each test case method should start a browser session and traverse42 its GUI to validate its health.43 """44 # Change these constants in the subclass to set the browser instance.45 GRID_URL = 'http://127.0.0.1:4444/wd/hub'46 BROWSER_BRAND = 'Chrome' # Chrome, FireFox, ...47 PLATFORM = 'ANY' # "ANY" or Linux, Windows, ...48 VERSION = None # Browser version (None means ANY)49 HEADLESS = True50 PAGE_LOAD_TIMEOUT = 20.0 # seconds51 LOG_VERBOSE = True52 LOG_TO_FILE = None53 ACCEPT_SSL_CERTS = False54 HTTP_PROXY_HOST = None55 HTTP_PROXY_PORT = 8056 @classmethod57 def _select_browser_brand_factory(cls, browser_brand):58 """ Select Browser Brand Factory Class.59 :param str browser_brand: match BROWSER_BRAND to its class60 :returns: BrowserSession factory or None when not available61 """62 browser_brand = browser_brand.upper()63 browser_factory = BROWSER_SESSION_CLASS.get(browser_brand, None)64 if browser_factory is not None:65 try:66 if cls.HEADLESS:67 browser_factory = browser_factory[0] # grid session68 else:69 browser_factory = browser_factory[1] # local session70 except IndexError:71 browser_factory = None72 return browser_factory73 @classmethod74 def setUpClass(cls):75 """76 Create a browser factory to get_driver() objects.77 Each browser session has the "desired capabilities" from the constants.78 """79 browser_factory_class = cls._select_browser_brand_factory(cls.BROWSER_BRAND)80 if browser_factory_class is not None:81 cls.browser_factory = browser_factory_class(82 command_executor=cls.GRID_URL,83 platform=cls.PLATFORM,84 browser_version=cls.VERSION,85 headless=cls.HEADLESS,86 page_load_timeout=cls.PAGE_LOAD_TIMEOUT,87 log_verbose=cls.LOG_VERBOSE,88 log_to_file=cls.LOG_TO_FILE,89 accept_ssl_certs=cls.ACCEPT_SSL_CERTS,90 proxy_host=cls.HTTP_PROXY_HOST,91 proxy_port=cls.HTTP_PROXY_PORT,92 raise_errors=True)93 else:94 cls.browser_factory = None95 def setUp(self):96 """ Set Up.97 Create a new browser session "driver" for the current tests.98 """99 super().setUp()100 if self.browser_factory is not None:101 self.driver = self.browser_factory.get_driver()102 else:103 raise NoBrowserFactoryError(self.BROWSER_BRAND)104 def tearDown(self):105 """ Tear Down.106 Quit browser session and release resources.107 """108 super().tearDown()109 if self.driver:110 self.driver.quit()...

Full Screen

Full Screen

Playwright tutorial

LambdaTest’s Playwright tutorial will give you a broader idea about the Playwright automation framework, its unique features, and use cases with examples to exceed your understanding of Playwright testing. This tutorial will give A to Z guidance, from installing the Playwright framework to some best practices and advanced concepts.

Chapters:

  1. What is Playwright : Playwright is comparatively new but has gained good popularity. Get to know some history of the Playwright with some interesting facts connected with it.
  2. How To Install Playwright : Learn in detail about what basic configuration and dependencies are required for installing Playwright and run a test. Get a step-by-step direction for installing the Playwright automation framework.
  3. Playwright Futuristic Features: Launched in 2020, Playwright gained huge popularity quickly because of some obliging features such as Playwright Test Generator and Inspector, Playwright Reporter, Playwright auto-waiting mechanism and etc. Read up on those features to master Playwright testing.
  4. What is Component Testing: Component testing in Playwright is a unique feature that allows a tester to test a single component of a web application without integrating them with other elements. Learn how to perform Component testing on the Playwright automation framework.
  5. Inputs And Buttons In Playwright: Every website has Input boxes and buttons; learn about testing inputs and buttons with different scenarios and examples.
  6. Functions and Selectors in Playwright: Learn how to launch the Chromium browser with Playwright. Also, gain a better understanding of some important functions like “BrowserContext,” which allows you to run multiple browser sessions, and “newPage” which interacts with a page.
  7. Handling Alerts and Dropdowns in Playwright : Playwright interact with different types of alerts and pop-ups, such as simple, confirmation, and prompt, and different types of dropdowns, such as single selector and multi-selector get your hands-on with handling alerts and dropdown in Playright testing.
  8. Playwright vs Puppeteer: Get to know about the difference between two testing frameworks and how they are different than one another, which browsers they support, and what features they provide.
  9. Run Playwright Tests on LambdaTest: Playwright testing with LambdaTest leverages test performance to the utmost. You can run multiple Playwright tests in Parallel with the LammbdaTest test cloud. Get a step-by-step guide to run your Playwright test on the LambdaTest platform.
  10. Playwright Python Tutorial: Playwright automation framework support all major languages such as Python, JavaScript, TypeScript, .NET and etc. However, there are various advantages to Python end-to-end testing with Playwright because of its versatile utility. Get the hang of Playwright python testing with this chapter.
  11. Playwright End To End Testing Tutorial: Get your hands on with Playwright end-to-end testing and learn to use some exciting features such as TraceViewer, Debugging, Networking, Component testing, Visual testing, and many more.
  12. Playwright Video Tutorial: Watch the video tutorials on Playwright testing from experts and get a consecutive in-depth explanation of Playwright automation testing.

Run Playwright Python automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful