{"flag":true,"single":true,"pageTitle":"Playwright Tutorial , installation > Record tests, Run Tests","post":{"id":290,"user_id":"1","slug":"playwright-tutorial-fkst","title":"Playwright Tutorial , installation > Record tests, Run Tests","body":"<p>Playwright enables reliable end-to-end testing for modern web apps.<\/p>\r\n<p style=\"text-align: center;\"><span style=\"font-size: 18pt;\"><strong>1. Installation:<\/strong><\/span><\/p>\r\n<pre class=\"language-markup\"><code>pip install pytest-playwright<\/code><\/pre>\r\n<p><strong>Browsers: <a href=\"https:\/\/playwright.dev\/python\/docs\/browsers\">https:\/\/playwright.dev\/python\/docs\/browsers<\/a><\/strong><br>playwright install #INSTALL default browsers<br>playwright install --help<br>Custom browsers: <strong>chromium, chromium-headless-shell, chromium-tip-of-tree-headless-shell, chrome, chrome-beta, msedge, msedge-beta, msedge-dev, bidi-chromium, firefox, webkit.<\/strong><br>playwright install --with-deps chrome<\/p>\r\n<pre class=\"language-markup\"><code>playwright install # install browsers<\/code><\/pre>\r\n<p style=\"text-align: center;\"><span style=\"font-size: 18pt;\"><strong>2.RECORD TESTS<\/strong><\/span><\/p>\r\n<p style=\"text-align: left;\">Run the below command in cmd and do some actions with opened url , Auto code will be generated you can copy that code for use<\/p>\r\n<pre class=\"language-markup\"><code>playwright codegen https:\/\/wellguider.com <\/code><\/pre>\r\n<p style=\"text-align: center;\"><strong><span style=\"font-size: 18pt;\">3.Tests Run<\/span><\/strong><\/p>\r\n<p style=\"text-align: left;\"><span style=\"font-size: 12pt;\">Create any file with name test_ ie <strong>test_example.py&nbsp;<\/strong><\/span><span style=\"font-size: 12pt;\"> Also need to do this with functions ie <strong>test_has_title<\/strong><\/span><\/p>\r\n<pre class=\"language-markup\"><code>def test_has_title():\r\n    with sync_playwright() as p:\r\n        browser = p.firefox.launch(headless=False)  # Set headless=False\r\n        page = browser.new_page()\r\n        page.goto(\"https:\/\/playwright.dev\/\")\r\n        browser.close()<\/code><\/pre>\r\n<p>In current working directory type <strong>pytest <\/strong>to runn all tests inside all files,&nbsp;<\/p>\r\n<pre class=\"language-markup\"><code>pytest # run all files, all test functions auto\r\npytest test_login.py # only test_login.py\r\npytest -k test_add_a_todo_item # specific file and function<\/code><\/pre>\r\n<p>4.<\/p>\r\n<p><strong>open sepecific profile:<\/strong><\/p>\r\n<pre class=\"language-markup\"><code>from playwright.sync_api import sync_playwright\r\nimport os\r\n\r\nclass Init:\r\n    def __init__(self):\r\n        self.playwright = sync_playwright().start()\r\n        self.browser = None\r\n        self.page = None\r\n\r\n    def open_profile(self, profile=1, headless=False, chrome_path=None, user_data_dir=None):\r\n        try:\r\n            # Default Chrome path for different OS\r\n            if not chrome_path:\r\n                chrome_path = (\r\n                    'C:\/Program Files\/Google\/Chrome\/Application\/chrome.exe'\r\n                    if os.name == 'nt' else\r\n                    '\/usr\/bin\/google-chrome'  # Example for Linux\r\n                )\r\n\r\n            # Use a fixed user data directory to persist history and cookies\r\n            if not user_data_dir:\r\n                # Set a fixed directory in the user's home directory\r\n                user_data_dir = os.path.join(os.path.expanduser(\"~\"), \"playwright_chrome_profile\")\r\n            \r\n            # Ensure the directory exists\r\n            os.makedirs(user_data_dir, exist_ok=True)\r\n            \r\n            profile_name = f'Profile {profile}'\r\n\r\n            # Launch browser with persistent context to maintain history and cookies\r\n            self.browser = self.playwright.chromium.launch_persistent_context(\r\n                user_data_dir=user_data_dir,\r\n                executable_path=chrome_path if os.path.exists(chrome_path) else None,\r\n                headless=headless,\r\n                args=[\r\n                    '--no-sandbox',  # Be cautious with this\r\n                    '--test-type',\r\n                    '--disable-extensions',\r\n                    '--start-maximized',\r\n                    '--disable-dev-shm-usage',\r\n                    f'--profile-directory={profile_name}'\r\n                ]\r\n            )\r\n            self.page = self.browser.pages[0] if self.browser.pages else self.browser.new_page()\r\n\r\n            self.page.set_viewport_size({\"width\": 1280, \"height\": 800})\r\n            return self.page\r\n        except Exception as e:\r\n            print(f\"Error opening browser: {e}\")\r\n            raise\r\n\r\n    def close(self):\r\n        if self.browser:\r\n            self.browser.close()\r\n        if self.playwright:\r\n            self.playwright.stop()\r\n\r\n# Example usage\r\nif __name__ == \"__main__\":\r\n    init = Init()\r\n    try:\r\n        page = init.open_profile(profile=1, headless=False)\r\n        page.goto(\"https:\/\/maps.app.goo.gl\/CA7AwydAzZQ9rMi19\")\r\n        print(page.title())\r\n        input(\"Press Enter to close the browser...\")\r\n    finally:\r\n        init.close()<\/code><\/pre>\r\n<p>all chrome flags here:<br>https:\/\/peter.sh\/experiments\/chromium-command-line-switches\/<\/p>","category_id":"37","is_private":"0","created_at":"2025-03-05T22:34:18.000000Z","updated_at":"2025-07-14T10:13:55.000000Z","category":{"id":37,"user_id":"1","name":"Playwright","slug":"playwright-c2zf","parent_id":null,"created_at":"2025-03-05T22:35:12.000000Z","updated_at":"2025-03-05T22:35:12.000000Z"},"user":{"id":1,"name":"R GONDAL","email":"rizikmw@gmail.com","email_verified_at":null,"two_factor_confirmed_at":null,"current_team_id":"1","profile_photo_path":null,"created_at":"2023-03-12T10:49:33.000000Z","updated_at":"2025-01-10T12:59:00.000000Z","profile_photo_url":"https:\/\/ui-avatars.com\/api\/?name=R+G&color=7F9CF5&background=EBF4FF"}},"pageDesc":"Playwright enables reliable end-to-end testing for modern web apps. 1. Installation: pip install pytest-playwright Browsers: https:\/\/playwri - Playwright Tutorial , installation > Record tests, Run Tests (Updated: July 14, 2025) - Read more about Playwright Tutorial , installation > Record tests, Run Tests at my programming site [SITE]","categories":[]}