├── .flake8 ├── .github ├── ISSUE_TEMPLATE │ ├── bug_report.md │ ├── feature_request.md │ └── question.md ├── depenabot.yml └── workflows │ └── ci.yml ├── .gitignore ├── LICENSE ├── README.md ├── cdp_patches ├── __init__.py └── input │ ├── __init__.py │ ├── async_input.py │ ├── browsers.py │ ├── exceptions.py │ ├── mouse_trajectory.py │ ├── os_base │ ├── __init__.py │ ├── linux.py │ └── windows.py │ └── sync_input.py ├── docs ├── CONTRIBUTING.md ├── HISTORY.md └── gitbook │ ├── README.md │ ├── SUMMARY.md │ └── input │ ├── README.md │ ├── async-usage.md │ ├── playwright-usage.md │ ├── selenium-usage.md │ └── sync-usage.md ├── py.typed ├── pyproject.toml ├── requirements-test.txt ├── requirements.txt ├── setup.cfg └── tests ├── __init__.py ├── assets ├── input │ ├── button.html │ ├── keyboard.html │ ├── mouse-helper.js │ ├── scrollable.html │ └── textarea.html └── offscreenbuttons.html ├── conftest.py ├── server.py ├── test_async_driverless.py ├── test_async_playwright.py ├── test_selenium.py ├── test_sync_driverless.py └── test_sync_playwright.py /.flake8: -------------------------------------------------------------------------------- 1 | [flake8] 2 | max-line-length = 200 -------------------------------------------------------------------------------- /.github/ISSUE_TEMPLATE/bug_report.md: -------------------------------------------------------------------------------- 1 | --- 2 | name: Bug report 3 | about: Create a report to help us improve 4 | title: '[BUG] ' 5 | labels: bug, help wanted 6 | assignees: '' 7 | 8 | --- 9 | 10 | **Describe the bug** 11 | A clear and concise description of what the bug is. 12 | 13 | **Code Sample** 14 | If applicable, add a code sample to replicate the bug. 15 | 16 | **To Reproduce** 17 | Steps to reproduce the behavior: 18 | 1. Go to '...' 19 | 2. Click on '....' 20 | 3. Scroll down to '....' 21 | 4. See error 22 | 23 | **Expected behavior** 24 | A clear and concise description of what you expected to happen. 25 | 26 | **Screenshots** 27 | If applicable, add screenshots to help explain your problem. 28 | 29 | **Desktop (please complete the following information):** 30 | - OS: [e.g. iOS] 31 | - Version [e.g. 22] 32 | 33 | **Additional context** 34 | Add any other context about the problem here. -------------------------------------------------------------------------------- /.github/ISSUE_TEMPLATE/feature_request.md: -------------------------------------------------------------------------------- 1 | --- 2 | name: Feature request 3 | about: Suggest an idea for this project 4 | title: '' 5 | labels: enhancement, question 6 | assignees: '' 7 | 8 | --- 9 | 10 | **Is your feature request related to a problem? Please describe.** 11 | A clear and concise description of what the problem is. Ex. I'm always frustrated when [...] 12 | 13 | **Describe the solution you'd like** 14 | A clear and concise description of what you want to happen. 15 | 16 | **Describe alternatives you've considered** 17 | A clear and concise description of any alternative solutions or features you've considered. 18 | 19 | **Additional context** 20 | Add any other context or screenshots about the feature request here. -------------------------------------------------------------------------------- /.github/ISSUE_TEMPLATE/question.md: -------------------------------------------------------------------------------- 1 | --- 2 | name: Question 3 | about: Ask a question about Botright 4 | title: '[Question] ' 5 | labels: question, help wanted, documentation 6 | assignees: '' 7 | 8 | --- 9 | 10 | **Describe your quesiton** 11 | A clear and concise question. 12 | 13 | **Expected behavior** 14 | A clear and concise description of what you expected to happen. 15 | 16 | **Screenshots** 17 | If applicable, add screenshots to help explain your problem. 18 | 19 | **Desktop (please complete the following information):** 20 | - OS: [e.g. iOS] 21 | - Version [e.g. 22] 22 | 23 | **Additional context** 24 | Add any other context about the problem here. -------------------------------------------------------------------------------- /.github/depenabot.yml: -------------------------------------------------------------------------------- 1 | # To get started with Dependabot version updates, you'll need to specify which 2 | # package ecosystems to update and where the package manifests are located. 3 | # Please see the documentation for all configuration options: 4 | # https://docs.github.com/github/administering-a-repository/configuration-options-for-dependency-updates 5 | 6 | version: 2 7 | updates: 8 | - package-ecosystem: "pip" # See documentation for possible values 9 | directory: "/" # Location of package manifests 10 | schedule: 11 | interval: "weekly" -------------------------------------------------------------------------------- /.github/workflows/ci.yml: -------------------------------------------------------------------------------- 1 | name: CDP-Patches CI 2 | 3 | on: 4 | - push 5 | - pull_request 6 | 7 | jobs: 8 | Linting: 9 | runs-on: windows-latest 10 | steps: 11 | - uses: actions/checkout@v4 12 | - name: Set up Python 3.11 13 | uses: actions/setup-python@v4 14 | with: 15 | python-version: '3.11' 16 | - name: Install dependencies 17 | run: | 18 | python -m pip install --upgrade pip 19 | pip install -r requirements-test.txt 20 | - name: (Linting) isort 21 | run: isort . --check-only 22 | - name: (Linting) Flake8 23 | run: flake8 . 24 | - name: (Linting) MyPy 25 | run: | 26 | mypy --install-types --non-interactive 27 | mypy . 28 | - name: (Linting) Black 29 | run: black . --check 30 | 31 | Build_Linux: 32 | strategy: 33 | matrix: 34 | os: [ ubuntu-latest ] 35 | python-version: [ '3.9', '3.10', '3.11', '3.12' ] 36 | 37 | runs-on: ${{ matrix.os }} 38 | steps: 39 | - uses: actions/checkout@v4 40 | - name: Set up Python ${{ matrix.python-version }} 41 | uses: actions/setup-python@v4 42 | with: 43 | python-version: ${{ matrix.python-version }} 44 | - name: Install dependencies 45 | run: | 46 | python -m pip install --upgrade pip 47 | pip install -r requirements-test.txt 48 | pip install -e . 49 | python -c "import os; os.environ['TOKENIZERS_PARALLELISM'] = 'false'" 50 | - name: Install Chrome Browser 51 | uses: browser-actions/setup-chrome@v1 52 | - name: Install Chromium Driver 53 | run: python -m playwright install chromium 54 | - name: Test with PyTest 55 | run: | 56 | Xvfb -ac :100 -screen 0 1280x1024x16 > /dev/null 2>&1 & 57 | export DISPLAY=:100.0 58 | pytest 59 | 60 | Build_Windows: 61 | strategy: 62 | matrix: 63 | os: [ windows-latest ] 64 | python-version: [ '3.9', '3.10', '3.11', '3.12' ] 65 | 66 | runs-on: ${{ matrix.os }} 67 | steps: 68 | - uses: actions/checkout@v4 69 | - name: Set up Python ${{ matrix.python-version }} 70 | uses: actions/setup-python@v4 71 | with: 72 | python-version: ${{ matrix.python-version }} 73 | - name: Install dependencies 74 | run: | 75 | python -m pip install --upgrade pip 76 | pip install -r requirements-test.txt 77 | pip install -e . 78 | python -c "import os; os.environ['TOKENIZERS_PARALLELISM'] = 'false'" 79 | - name: Install Chrome Browser 80 | uses: browser-actions/setup-chrome@v1 81 | - name: Install Chromium Driver 82 | run: python -m playwright install chromium 83 | - name: Test with PyTest 84 | run: pytest -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Byte-compiled / optimized / DLL files 2 | __pycache__/ 3 | *.py[cod] 4 | *$py.class 5 | 6 | # C extensions 7 | *.so 8 | 9 | # Distribution / packaging 10 | .Python 11 | build/ 12 | develop-eggs/ 13 | dist/ 14 | downloads/ 15 | eggs/ 16 | .eggs/ 17 | lib/ 18 | lib64/ 19 | parts/ 20 | sdist/ 21 | var/ 22 | wheels/ 23 | share/python-wheels/ 24 | *.egg-info/ 25 | .installed.cfg 26 | *.egg 27 | MANIFEST 28 | 29 | # PyInstaller 30 | # Usually these files are written by a python script from a template 31 | # before PyInstaller builds the exe, so as to inject date/other infos into it. 32 | *.manifest 33 | *.spec 34 | 35 | # Installer logs 36 | pip-log.txt 37 | pip-delete-this-directory.txt 38 | 39 | # Unit test / coverage reports 40 | htmlcov/ 41 | .tox/ 42 | .nox/ 43 | .coverage 44 | .coverage.* 45 | .cache 46 | nosetests.xml 47 | coverage.xml 48 | *.cover 49 | *.py,cover 50 | .hypothesis/ 51 | .pytest_cache/ 52 | cover/ 53 | 54 | # Translations 55 | *.mo 56 | *.pot 57 | 58 | # Django stuff: 59 | *.log 60 | local_settings.py 61 | db.sqlite3 62 | db.sqlite3-journal 63 | 64 | # Flask stuff: 65 | instance/ 66 | .webassets-cache 67 | 68 | # Scrapy stuff: 69 | .scrapy 70 | 71 | # Sphinx documentation 72 | docs/_build/ 73 | 74 | # PyBuilder 75 | .pybuilder/ 76 | target/ 77 | 78 | # Jupyter Notebook 79 | .ipynb_checkpoints 80 | 81 | # IPython 82 | profile_default/ 83 | ipython_config.py 84 | 85 | # pyenv 86 | # For a library or package, you might want to ignore these files since the code is 87 | # intended to run in multiple environments; otherwise, check them in: 88 | # .python-version 89 | 90 | # pipenv 91 | # According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control. 92 | # However, in case of collaboration, if having platform-specific dependencies or dependencies 93 | # having no cross-platform support, pipenv may install dependencies that don't work, or not 94 | # install all needed dependencies. 95 | #Pipfile.lock 96 | 97 | # poetry 98 | # Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control. 99 | # This is especially recommended for binary packages to ensure reproducibility, and is more 100 | # commonly ignored for libraries. 101 | # https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control 102 | #poetry.lock 103 | 104 | # pdm 105 | # Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control. 106 | #pdm.lock 107 | # pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it 108 | # in version control. 109 | # https://pdm.fming.dev/#use-with-ide 110 | .pdm.toml 111 | 112 | # PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm 113 | __pypackages__/ 114 | 115 | # Celery stuff 116 | celerybeat-schedule 117 | celerybeat.pid 118 | 119 | # SageMath parsed files 120 | *.sage.py 121 | 122 | # Environments 123 | .env 124 | .venv 125 | env/ 126 | venv/ 127 | ENV/ 128 | env.bak/ 129 | venv.bak/ 130 | 131 | # Spyder project settings 132 | .spyderproject 133 | .spyproject 134 | 135 | # Rope project settings 136 | .ropeproject 137 | 138 | # mkdocs documentation 139 | /site 140 | 141 | # mypy 142 | .mypy_cache/ 143 | .dmypy.json 144 | dmypy.json 145 | 146 | # Pyre type checker 147 | .pyre/ 148 | 149 | # pytype static type analyzer 150 | .pytype/ 151 | 152 | # Cython debug symbols 153 | cython_debug/ 154 | 155 | # PyCharm 156 | # JetBrains specific template is maintained in a separate JetBrains.gitignore that can 157 | # be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore 158 | # and can be added to the global gitignore or merged into this file. For a more nuclear 159 | # option (not recommended) you can uncomment the following to ignore the entire idea folder. 160 | #.idea/ 161 | /.idea 162 | /.vs 163 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | GNU GENERAL PUBLIC LICENSE 2 | Version 3, 29 June 2007 3 | 4 | Copyright (C) 2007 Free Software Foundation, Inc. 5 | Everyone is permitted to copy and distribute verbatim copies 6 | of this license document, but changing it is not allowed. 7 | 8 | Preamble 9 | 10 | The GNU General Public License is a free, copyleft license for 11 | software and other kinds of works. 12 | 13 | The licenses for most software and other practical works are designed 14 | to take away your freedom to share and change the works. By contrast, 15 | the GNU General Public License is intended to guarantee your freedom to 16 | share and change all versions of a program--to make sure it remains free 17 | software for all its users. We, the Free Software Foundation, use the 18 | GNU General Public License for most of our software; it applies also to 19 | any other work released this way by its authors. You can apply it to 20 | your programs, too. 21 | 22 | When we speak of free software, we are referring to freedom, not 23 | price. Our General Public Licenses are designed to make sure that you 24 | have the freedom to distribute copies of free software (and charge for 25 | them if you wish), that you receive source code or can get it if you 26 | want it, that you can change the software or use pieces of it in new 27 | free programs, and that you know you can do these things. 28 | 29 | To protect your rights, we need to prevent others from denying you 30 | these rights or asking you to surrender the rights. Therefore, you have 31 | certain responsibilities if you distribute copies of the software, or if 32 | you modify it: responsibilities to respect the freedom of others. 33 | 34 | For example, if you distribute copies of such a program, whether 35 | gratis or for a fee, you must pass on to the recipients the same 36 | freedoms that you received. You must make sure that they, too, receive 37 | or can get the source code. And you must show them these terms so they 38 | know their rights. 39 | 40 | Developers that use the GNU GPL protect your rights with two steps: 41 | (1) assert copyright on the software, and (2) offer you this License 42 | giving you legal permission to copy, distribute and/or modify it. 43 | 44 | For the developers' and authors' protection, the GPL clearly explains 45 | that there is no warranty for this free software. For both users' and 46 | authors' sake, the GPL requires that modified versions be marked as 47 | changed, so that their problems will not be attributed erroneously to 48 | authors of previous versions. 49 | 50 | Some devices are designed to deny users access to install or run 51 | modified versions of the software inside them, although the manufacturer 52 | can do so. This is fundamentally incompatible with the aim of 53 | protecting users' freedom to change the software. The systematic 54 | pattern of such abuse occurs in the area of products for individuals to 55 | use, which is precisely where it is most unacceptable. Therefore, we 56 | have designed this version of the GPL to prohibit the practice for those 57 | products. If such problems arise substantially in other domains, we 58 | stand ready to extend this provision to those domains in future versions 59 | of the GPL, as needed to protect the freedom of users. 60 | 61 | Finally, every program is threatened constantly by software patents. 62 | States should not allow patents to restrict development and use of 63 | software on general-purpose computers, but in those that do, we wish to 64 | avoid the special danger that patents applied to a free program could 65 | make it effectively proprietary. To prevent this, the GPL assures that 66 | patents cannot be used to render the program non-free. 67 | 68 | The precise terms and conditions for copying, distribution and 69 | modification follow. 70 | 71 | TERMS AND CONDITIONS 72 | 73 | 0. Definitions. 74 | 75 | "This License" refers to version 3 of the GNU General Public License. 76 | 77 | "Copyright" also means copyright-like laws that apply to other kinds of 78 | works, such as semiconductor masks. 79 | 80 | "The Program" refers to any copyrightable work licensed under this 81 | License. Each licensee is addressed as "you". "Licensees" and 82 | "recipients" may be individuals or organizations. 83 | 84 | To "modify" a work means to copy from or adapt all or part of the work 85 | in a fashion requiring copyright permission, other than the making of an 86 | exact copy. The resulting work is called a "modified version" of the 87 | earlier work or a work "based on" the earlier work. 88 | 89 | A "covered work" means either the unmodified Program or a work based 90 | on the Program. 91 | 92 | To "propagate" a work means to do anything with it that, without 93 | permission, would make you directly or secondarily liable for 94 | infringement under applicable copyright law, except executing it on a 95 | computer or modifying a private copy. Propagation includes copying, 96 | distribution (with or without modification), making available to the 97 | public, and in some countries other activities as well. 98 | 99 | To "convey" a work means any kind of propagation that enables other 100 | parties to make or receive copies. Mere interaction with a user through 101 | a computer network, with no transfer of a copy, is not conveying. 102 | 103 | An interactive user interface displays "Appropriate Legal Notices" 104 | to the extent that it includes a convenient and prominently visible 105 | feature that (1) displays an appropriate copyright notice, and (2) 106 | tells the user that there is no warranty for the work (except to the 107 | extent that warranties are provided), that licensees may convey the 108 | work under this License, and how to view a copy of this License. If 109 | the interface presents a list of user commands or options, such as a 110 | menu, a prominent item in the list meets this criterion. 111 | 112 | 1. Source Code. 113 | 114 | The "source code" for a work means the preferred form of the work 115 | for making modifications to it. "Object code" means any non-source 116 | form of a work. 117 | 118 | A "Standard Interface" means an interface that either is an official 119 | standard defined by a recognized standards body, or, in the case of 120 | interfaces specified for a particular programming language, one that 121 | is widely used among developers working in that language. 122 | 123 | The "System Libraries" of an executable work include anything, other 124 | than the work as a whole, that (a) is included in the normal form of 125 | packaging a Major Component, but which is not part of that Major 126 | Component, and (b) serves only to enable use of the work with that 127 | Major Component, or to implement a Standard Interface for which an 128 | implementation is available to the public in source code form. A 129 | "Major Component", in this context, means a major essential component 130 | (kernel, window system, and so on) of the specific operating system 131 | (if any) on which the executable work runs, or a compiler used to 132 | produce the work, or an object code interpreter used to run it. 133 | 134 | The "Corresponding Source" for a work in object code form means all 135 | the source code needed to generate, install, and (for an executable 136 | work) run the object code and to modify the work, including scripts to 137 | control those activities. However, it does not include the work's 138 | System Libraries, or general-purpose tools or generally available free 139 | programs which are used unmodified in performing those activities but 140 | which are not part of the work. For example, Corresponding Source 141 | includes interface definition files associated with source files for 142 | the work, and the source code for shared libraries and dynamically 143 | linked subprograms that the work is specifically designed to require, 144 | such as by intimate data communication or control flow between those 145 | subprograms and other parts of the work. 146 | 147 | The Corresponding Source need not include anything that users 148 | can regenerate automatically from other parts of the Corresponding 149 | Source. 150 | 151 | The Corresponding Source for a work in source code form is that 152 | same work. 153 | 154 | 2. Basic Permissions. 155 | 156 | All rights granted under this License are granted for the term of 157 | copyright on the Program, and are irrevocable provided the stated 158 | conditions are met. This License explicitly affirms your unlimited 159 | permission to run the unmodified Program. The output from running a 160 | covered work is covered by this License only if the output, given its 161 | content, constitutes a covered work. This License acknowledges your 162 | rights of fair use or other equivalent, as provided by copyright law. 163 | 164 | You may make, run and propagate covered works that you do not 165 | convey, without conditions so long as your license otherwise remains 166 | in force. You may convey covered works to others for the sole purpose 167 | of having them make modifications exclusively for you, or provide you 168 | with facilities for running those works, provided that you comply with 169 | the terms of this License in conveying all material for which you do 170 | not control copyright. Those thus making or running the covered works 171 | for you must do so exclusively on your behalf, under your direction 172 | and control, on terms that prohibit them from making any copies of 173 | your copyrighted material outside their relationship with you. 174 | 175 | Conveying under any other circumstances is permitted solely under 176 | the conditions stated below. Sublicensing is not allowed; section 10 177 | makes it unnecessary. 178 | 179 | 3. Protecting Users' Legal Rights From Anti-Circumvention Law. 180 | 181 | No covered work shall be deemed part of an effective technological 182 | measure under any applicable law fulfilling obligations under article 183 | 11 of the WIPO copyright treaty adopted on 20 December 1996, or 184 | similar laws prohibiting or restricting circumvention of such 185 | measures. 186 | 187 | When you convey a covered work, you waive any legal power to forbid 188 | circumvention of technological measures to the extent such circumvention 189 | is effected by exercising rights under this License with respect to 190 | the covered work, and you disclaim any intention to limit operation or 191 | modification of the work as a means of enforcing, against the work's 192 | users, your or third parties' legal rights to forbid circumvention of 193 | technological measures. 194 | 195 | 4. Conveying Verbatim Copies. 196 | 197 | You may convey verbatim copies of the Program's source code as you 198 | receive it, in any medium, provided that you conspicuously and 199 | appropriately publish on each copy an appropriate copyright notice; 200 | keep intact all notices stating that this License and any 201 | non-permissive terms added in accord with section 7 apply to the code; 202 | keep intact all notices of the absence of any warranty; and give all 203 | recipients a copy of this License along with the Program. 204 | 205 | You may charge any price or no price for each copy that you convey, 206 | and you may offer support or warranty protection for a fee. 207 | 208 | 5. Conveying Modified Source Versions. 209 | 210 | You may convey a work based on the Program, or the modifications to 211 | produce it from the Program, in the form of source code under the 212 | terms of section 4, provided that you also meet all of these conditions: 213 | 214 | a) The work must carry prominent notices stating that you modified 215 | it, and giving a relevant date. 216 | 217 | b) The work must carry prominent notices stating that it is 218 | released under this License and any conditions added under section 219 | 7. This requirement modifies the requirement in section 4 to 220 | "keep intact all notices". 221 | 222 | c) You must license the entire work, as a whole, under this 223 | License to anyone who comes into possession of a copy. This 224 | License will therefore apply, along with any applicable section 7 225 | additional terms, to the whole of the work, and all its parts, 226 | regardless of how they are packaged. This License gives no 227 | permission to license the work in any other way, but it does not 228 | invalidate such permission if you have separately received it. 229 | 230 | d) If the work has interactive user interfaces, each must display 231 | Appropriate Legal Notices; however, if the Program has interactive 232 | interfaces that do not display Appropriate Legal Notices, your 233 | work need not make them do so. 234 | 235 | A compilation of a covered work with other separate and independent 236 | works, which are not by their nature extensions of the covered work, 237 | and which are not combined with it such as to form a larger program, 238 | in or on a volume of a storage or distribution medium, is called an 239 | "aggregate" if the compilation and its resulting copyright are not 240 | used to limit the access or legal rights of the compilation's users 241 | beyond what the individual works permit. Inclusion of a covered work 242 | in an aggregate does not cause this License to apply to the other 243 | parts of the aggregate. 244 | 245 | 6. Conveying Non-Source Forms. 246 | 247 | You may convey a covered work in object code form under the terms 248 | of sections 4 and 5, provided that you also convey the 249 | machine-readable Corresponding Source under the terms of this License, 250 | in one of these ways: 251 | 252 | a) Convey the object code in, or embodied in, a physical product 253 | (including a physical distribution medium), accompanied by the 254 | Corresponding Source fixed on a durable physical medium 255 | customarily used for software interchange. 256 | 257 | b) Convey the object code in, or embodied in, a physical product 258 | (including a physical distribution medium), accompanied by a 259 | written offer, valid for at least three years and valid for as 260 | long as you offer spare parts or customer support for that product 261 | model, to give anyone who possesses the object code either (1) a 262 | copy of the Corresponding Source for all the software in the 263 | product that is covered by this License, on a durable physical 264 | medium customarily used for software interchange, for a price no 265 | more than your reasonable cost of physically performing this 266 | conveying of source, or (2) access to copy the 267 | Corresponding Source from a network server at no charge. 268 | 269 | c) Convey individual copies of the object code with a copy of the 270 | written offer to provide the Corresponding Source. This 271 | alternative is allowed only occasionally and noncommercially, and 272 | only if you received the object code with such an offer, in accord 273 | with subsection 6b. 274 | 275 | d) Convey the object code by offering access from a designated 276 | place (gratis or for a charge), and offer equivalent access to the 277 | Corresponding Source in the same way through the same place at no 278 | further charge. You need not require recipients to copy the 279 | Corresponding Source along with the object code. If the place to 280 | copy the object code is a network server, the Corresponding Source 281 | may be on a different server (operated by you or a third party) 282 | that supports equivalent copying facilities, provided you maintain 283 | clear directions next to the object code saying where to find the 284 | Corresponding Source. Regardless of what server hosts the 285 | Corresponding Source, you remain obligated to ensure that it is 286 | available for as long as needed to satisfy these requirements. 287 | 288 | e) Convey the object code using peer-to-peer transmission, provided 289 | you inform other peers where the object code and Corresponding 290 | Source of the work are being offered to the general public at no 291 | charge under subsection 6d. 292 | 293 | A separable portion of the object code, whose source code is excluded 294 | from the Corresponding Source as a System Library, need not be 295 | included in conveying the object code work. 296 | 297 | A "User Product" is either (1) a "consumer product", which means any 298 | tangible personal property which is normally used for personal, family, 299 | or household purposes, or (2) anything designed or sold for incorporation 300 | into a dwelling. In determining whether a product is a consumer product, 301 | doubtful cases shall be resolved in favor of coverage. For a particular 302 | product received by a particular user, "normally used" refers to a 303 | typical or common use of that class of product, regardless of the status 304 | of the particular user or of the way in which the particular user 305 | actually uses, or expects or is expected to use, the product. A product 306 | is a consumer product regardless of whether the product has substantial 307 | commercial, industrial or non-consumer uses, unless such uses represent 308 | the only significant mode of use of the product. 309 | 310 | "Installation Information" for a User Product means any methods, 311 | procedures, authorization keys, or other information required to install 312 | and execute modified versions of a covered work in that User Product from 313 | a modified version of its Corresponding Source. The information must 314 | suffice to ensure that the continued functioning of the modified object 315 | code is in no case prevented or interfered with solely because 316 | modification has been made. 317 | 318 | If you convey an object code work under this section in, or with, or 319 | specifically for use in, a User Product, and the conveying occurs as 320 | part of a transaction in which the right of possession and use of the 321 | User Product is transferred to the recipient in perpetuity or for a 322 | fixed term (regardless of how the transaction is characterized), the 323 | Corresponding Source conveyed under this section must be accompanied 324 | by the Installation Information. But this requirement does not apply 325 | if neither you nor any third party retains the ability to install 326 | modified object code on the User Product (for example, the work has 327 | been installed in ROM). 328 | 329 | The requirement to provide Installation Information does not include a 330 | requirement to continue to provide support service, warranty, or updates 331 | for a work that has been modified or installed by the recipient, or for 332 | the User Product in which it has been modified or installed. Access to a 333 | network may be denied when the modification itself materially and 334 | adversely affects the operation of the network or violates the rules and 335 | protocols for communication across the network. 336 | 337 | Corresponding Source conveyed, and Installation Information provided, 338 | in accord with this section must be in a format that is publicly 339 | documented (and with an implementation available to the public in 340 | source code form), and must require no special password or key for 341 | unpacking, reading or copying. 342 | 343 | 7. Additional Terms. 344 | 345 | "Additional permissions" are terms that supplement the terms of this 346 | License by making exceptions from one or more of its conditions. 347 | Additional permissions that are applicable to the entire Program shall 348 | be treated as though they were included in this License, to the extent 349 | that they are valid under applicable law. If additional permissions 350 | apply only to part of the Program, that part may be used separately 351 | under those permissions, but the entire Program remains governed by 352 | this License without regard to the additional permissions. 353 | 354 | When you convey a copy of a covered work, you may at your option 355 | remove any additional permissions from that copy, or from any part of 356 | it. (Additional permissions may be written to require their own 357 | removal in certain cases when you modify the work.) You may place 358 | additional permissions on material, added by you to a covered work, 359 | for which you have or can give appropriate copyright permission. 360 | 361 | Notwithstanding any other provision of this License, for material you 362 | add to a covered work, you may (if authorized by the copyright holders of 363 | that material) supplement the terms of this License with terms: 364 | 365 | a) Disclaiming warranty or limiting liability differently from the 366 | terms of sections 15 and 16 of this License; or 367 | 368 | b) Requiring preservation of specified reasonable legal notices or 369 | author attributions in that material or in the Appropriate Legal 370 | Notices displayed by works containing it; or 371 | 372 | c) Prohibiting misrepresentation of the origin of that material, or 373 | requiring that modified versions of such material be marked in 374 | reasonable ways as different from the original version; or 375 | 376 | d) Limiting the use for publicity purposes of names of licensors or 377 | authors of the material; or 378 | 379 | e) Declining to grant rights under trademark law for use of some 380 | trade names, trademarks, or service marks; or 381 | 382 | f) Requiring indemnification of licensors and authors of that 383 | material by anyone who conveys the material (or modified versions of 384 | it) with contractual assumptions of liability to the recipient, for 385 | any liability that these contractual assumptions directly impose on 386 | those licensors and authors. 387 | 388 | All other non-permissive additional terms are considered "further 389 | restrictions" within the meaning of section 10. If the Program as you 390 | received it, or any part of it, contains a notice stating that it is 391 | governed by this License along with a term that is a further 392 | restriction, you may remove that term. If a license document contains 393 | a further restriction but permits relicensing or conveying under this 394 | License, you may add to a covered work material governed by the terms 395 | of that license document, provided that the further restriction does 396 | not survive such relicensing or conveying. 397 | 398 | If you add terms to a covered work in accord with this section, you 399 | must place, in the relevant source files, a statement of the 400 | additional terms that apply to those files, or a notice indicating 401 | where to find the applicable terms. 402 | 403 | Additional terms, permissive or non-permissive, may be stated in the 404 | form of a separately written license, or stated as exceptions; 405 | the above requirements apply either way. 406 | 407 | 8. Termination. 408 | 409 | You may not propagate or modify a covered work except as expressly 410 | provided under this License. Any attempt otherwise to propagate or 411 | modify it is void, and will automatically terminate your rights under 412 | this License (including any patent licenses granted under the third 413 | paragraph of section 11). 414 | 415 | However, if you cease all violation of this License, then your 416 | license from a particular copyright holder is reinstated (a) 417 | provisionally, unless and until the copyright holder explicitly and 418 | finally terminates your license, and (b) permanently, if the copyright 419 | holder fails to notify you of the violation by some reasonable means 420 | prior to 60 days after the cessation. 421 | 422 | Moreover, your license from a particular copyright holder is 423 | reinstated permanently if the copyright holder notifies you of the 424 | violation by some reasonable means, this is the first time you have 425 | received notice of violation of this License (for any work) from that 426 | copyright holder, and you cure the violation prior to 30 days after 427 | your receipt of the notice. 428 | 429 | Termination of your rights under this section does not terminate the 430 | licenses of parties who have received copies or rights from you under 431 | this License. If your rights have been terminated and not permanently 432 | reinstated, you do not qualify to receive new licenses for the same 433 | material under section 10. 434 | 435 | 9. Acceptance Not Required for Having Copies. 436 | 437 | You are not required to accept this License in order to receive or 438 | run a copy of the Program. Ancillary propagation of a covered work 439 | occurring solely as a consequence of using peer-to-peer transmission 440 | to receive a copy likewise does not require acceptance. However, 441 | nothing other than this License grants you permission to propagate or 442 | modify any covered work. These actions infringe copyright if you do 443 | not accept this License. Therefore, by modifying or propagating a 444 | covered work, you indicate your acceptance of this License to do so. 445 | 446 | 10. Automatic Licensing of Downstream Recipients. 447 | 448 | Each time you convey a covered work, the recipient automatically 449 | receives a license from the original licensors, to run, modify and 450 | propagate that work, subject to this License. You are not responsible 451 | for enforcing compliance by third parties with this License. 452 | 453 | An "entity transaction" is a transaction transferring control of an 454 | organization, or substantially all assets of one, or subdividing an 455 | organization, or merging organizations. If propagation of a covered 456 | work results from an entity transaction, each party to that 457 | transaction who receives a copy of the work also receives whatever 458 | licenses to the work the party's predecessor in interest had or could 459 | give under the previous paragraph, plus a right to possession of the 460 | Corresponding Source of the work from the predecessor in interest, if 461 | the predecessor has it or can get it with reasonable efforts. 462 | 463 | You may not impose any further restrictions on the exercise of the 464 | rights granted or affirmed under this License. For example, you may 465 | not impose a license fee, royalty, or other charge for exercise of 466 | rights granted under this License, and you may not initiate litigation 467 | (including a cross-claim or counterclaim in a lawsuit) alleging that 468 | any patent claim is infringed by making, using, selling, offering for 469 | sale, or importing the Program or any portion of it. 470 | 471 | 11. Patents. 472 | 473 | A "contributor" is a copyright holder who authorizes use under this 474 | License of the Program or a work on which the Program is based. The 475 | work thus licensed is called the contributor's "contributor version". 476 | 477 | A contributor's "essential patent claims" are all patent claims 478 | owned or controlled by the contributor, whether already acquired or 479 | hereafter acquired, that would be infringed by some manner, permitted 480 | by this License, of making, using, or selling its contributor version, 481 | but do not include claims that would be infringed only as a 482 | consequence of further modification of the contributor version. For 483 | purposes of this definition, "control" includes the right to grant 484 | patent sublicenses in a manner consistent with the requirements of 485 | this License. 486 | 487 | Each contributor grants you a non-exclusive, worldwide, royalty-free 488 | patent license under the contributor's essential patent claims, to 489 | make, use, sell, offer for sale, import and otherwise run, modify and 490 | propagate the contents of its contributor version. 491 | 492 | In the following three paragraphs, a "patent license" is any express 493 | agreement or commitment, however denominated, not to enforce a patent 494 | (such as an express permission to practice a patent or covenant not to 495 | sue for patent infringement). To "grant" such a patent license to a 496 | party means to make such an agreement or commitment not to enforce a 497 | patent against the party. 498 | 499 | If you convey a covered work, knowingly relying on a patent license, 500 | and the Corresponding Source of the work is not available for anyone 501 | to copy, free of charge and under the terms of this License, through a 502 | publicly available network server or other readily accessible means, 503 | then you must either (1) cause the Corresponding Source to be so 504 | available, or (2) arrange to deprive yourself of the benefit of the 505 | patent license for this particular work, or (3) arrange, in a manner 506 | consistent with the requirements of this License, to extend the patent 507 | license to downstream recipients. "Knowingly relying" means you have 508 | actual knowledge that, but for the patent license, your conveying the 509 | covered work in a country, or your recipient's use of the covered work 510 | in a country, would infringe one or more identifiable patents in that 511 | country that you have reason to believe are valid. 512 | 513 | If, pursuant to or in connection with a single transaction or 514 | arrangement, you convey, or propagate by procuring conveyance of, a 515 | covered work, and grant a patent license to some of the parties 516 | receiving the covered work authorizing them to use, propagate, modify 517 | or convey a specific copy of the covered work, then the patent license 518 | you grant is automatically extended to all recipients of the covered 519 | work and works based on it. 520 | 521 | A patent license is "discriminatory" if it does not include within 522 | the scope of its coverage, prohibits the exercise of, or is 523 | conditioned on the non-exercise of one or more of the rights that are 524 | specifically granted under this License. You may not convey a covered 525 | work if you are a party to an arrangement with a third party that is 526 | in the business of distributing software, under which you make payment 527 | to the third party based on the extent of your activity of conveying 528 | the work, and under which the third party grants, to any of the 529 | parties who would receive the covered work from you, a discriminatory 530 | patent license (a) in connection with copies of the covered work 531 | conveyed by you (or copies made from those copies), or (b) primarily 532 | for and in connection with specific products or compilations that 533 | contain the covered work, unless you entered into that arrangement, 534 | or that patent license was granted, prior to 28 March 2007. 535 | 536 | Nothing in this License shall be construed as excluding or limiting 537 | any implied license or other defenses to infringement that may 538 | otherwise be available to you under applicable patent law. 539 | 540 | 12. No Surrender of Others' Freedom. 541 | 542 | If conditions are imposed on you (whether by court order, agreement or 543 | otherwise) that contradict the conditions of this License, they do not 544 | excuse you from the conditions of this License. If you cannot convey a 545 | covered work so as to satisfy simultaneously your obligations under this 546 | License and any other pertinent obligations, then as a consequence you may 547 | not convey it at all. For example, if you agree to terms that obligate you 548 | to collect a royalty for further conveying from those to whom you convey 549 | the Program, the only way you could satisfy both those terms and this 550 | License would be to refrain entirely from conveying the Program. 551 | 552 | 13. Use with the GNU Affero General Public License. 553 | 554 | Notwithstanding any other provision of this License, you have 555 | permission to link or combine any covered work with a work licensed 556 | under version 3 of the GNU Affero General Public License into a single 557 | combined work, and to convey the resulting work. The terms of this 558 | License will continue to apply to the part which is the covered work, 559 | but the special requirements of the GNU Affero General Public License, 560 | section 13, concerning interaction through a network will apply to the 561 | combination as such. 562 | 563 | 14. Revised Versions of this License. 564 | 565 | The Free Software Foundation may publish revised and/or new versions of 566 | the GNU General Public License from time to time. Such new versions will 567 | be similar in spirit to the present version, but may differ in detail to 568 | address new problems or concerns. 569 | 570 | Each version is given a distinguishing version number. If the 571 | Program specifies that a certain numbered version of the GNU General 572 | Public License "or any later version" applies to it, you have the 573 | option of following the terms and conditions either of that numbered 574 | version or of any later version published by the Free Software 575 | Foundation. If the Program does not specify a version number of the 576 | GNU General Public License, you may choose any version ever published 577 | by the Free Software Foundation. 578 | 579 | If the Program specifies that a proxy can decide which future 580 | versions of the GNU General Public License can be used, that proxy's 581 | public statement of acceptance of a version permanently authorizes you 582 | to choose that version for the Program. 583 | 584 | Later license versions may give you additional or different 585 | permissions. However, no additional obligations are imposed on any 586 | author or copyright holder as a result of your choosing to follow a 587 | later version. 588 | 589 | 15. Disclaimer of Warranty. 590 | 591 | THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY 592 | APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT 593 | HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY 594 | OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, 595 | THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 596 | PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM 597 | IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF 598 | ALL NECESSARY SERVICING, REPAIR OR CORRECTION. 599 | 600 | 16. Limitation of Liability. 601 | 602 | IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING 603 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS 604 | THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY 605 | GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE 606 | USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF 607 | DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD 608 | PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), 609 | EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF 610 | SUCH DAMAGES. 611 | 612 | 17. Interpretation of Sections 15 and 16. 613 | 614 | If the disclaimer of warranty and limitation of liability provided 615 | above cannot be given local legal effect according to their terms, 616 | reviewing courts shall apply local law that most closely approximates 617 | an absolute waiver of all civil liability in connection with the 618 | Program, unless a warranty or assumption of liability accompanies a 619 | copy of the Program in return for a fee. 620 | 621 | END OF TERMS AND CONDITIONS 622 | 623 | How to Apply These Terms to Your New Programs 624 | 625 | If you develop a new program, and you want it to be of the greatest 626 | possible use to the public, the best way to achieve this is to make it 627 | free software which everyone can redistribute and change under these terms. 628 | 629 | To do so, attach the following notices to the program. It is safest 630 | to attach them to the start of each source file to most effectively 631 | state the exclusion of warranty; and each file should have at least 632 | the "copyright" line and a pointer to where the full notice is found. 633 | 634 | 635 | Copyright (C) 636 | 637 | This program is free software: you can redistribute it and/or modify 638 | it under the terms of the GNU General Public License as published by 639 | the Free Software Foundation, either version 3 of the License, or 640 | (at your option) any later version. 641 | 642 | This program is distributed in the hope that it will be useful, 643 | but WITHOUT ANY WARRANTY; without even the implied warranty of 644 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 645 | GNU General Public License for more details. 646 | 647 | You should have received a copy of the GNU General Public License 648 | along with this program. If not, see . 649 | 650 | Also add information on how to contact you by electronic and paper mail. 651 | 652 | If the program does terminal interaction, make it output a short 653 | notice like this when it starts in an interactive mode: 654 | 655 | Copyright (C) 656 | This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'. 657 | This is free software, and you are welcome to redistribute it 658 | under certain conditions; type `show c' for details. 659 | 660 | The hypothetical commands `show w' and `show c' should show the appropriate 661 | parts of the General Public License. Of course, your program's commands 662 | might be different; for a GUI interface, you would use an "about box". 663 | 664 | You should also get your employer (if you work as a programmer) or school, 665 | if any, to sign a "copyright disclaimer" for the program, if necessary. 666 | For more information on this, and how to apply and follow the GNU GPL, see 667 | . 668 | 669 | The GNU General Public License does not permit incorporating your program 670 | into proprietary programs. If your program is a subroutine library, you 671 | may consider it more useful to permit linking proprietary applications with 672 | the library. If this is what you want to do, use the GNU Lesser General 673 | Public License instead of this License. But first, please read 674 | . 675 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 |

2 | CDP-Patches v1.0 3 |

4 | 5 | 6 |

7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | PyPI 15 | 16 | 17 | PyPI 18 | 19 |
20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 |

36 | 37 | ## Install it from PyPI 38 | 39 | ```bash 40 | pip install cdp-patches 41 | ``` 42 |
43 | Or for Full Linting 44 | 45 | #### (Includes: playwright, botright, selenium, selenium_driverless) 46 | ```bash 47 | pip install cdp-patches[automation_linting] 48 | ``` 49 |
50 | 51 | --- 52 | 53 | # Leak Patches 54 |
55 | Input Package 56 | 57 | ### Concept: Input Domain Leaks 58 | Bypass CDP Leaks in [Input](https://chromedevtools.github.io/devtools-protocol/tot/Input/) domains 59 | 60 | [![Brotector Banner](https://github.com/Kaliiiiiiiiii-Vinyzu/CDP-Patches/assets/50874994/fdbe831d-cb39-479d-ba0a-fea7f29fe90a)](https://github.com/kaliiiiiiiiii/brotector) 61 | 62 | For an interaction event `e`, the page coordinates won't ever equal the screen coordinates, unless Chrome is in fullscreen. 63 | However, all `CDP` input commands just set it the same by default (see [crbug#1477537](https://bugs.chromium.org/p/chromium/issues/detail?id=1477537)). 64 | ```js 65 | var is_bot = (e.pageY == e.screenY && e.pageX == e.screenX) 66 | if (is_bot && 1 >= outerHeight - innerHeight){ // fullscreen 67 | is_bot = false 68 | } 69 | ``` 70 | 71 | Furthermore, CDP can't dispatch `CoalescedEvent`'s ([demo](https://omwnk.codesandbox.io/)). 72 | 73 | As we don't want to patch Chromium itsself, let's just dispatch this event at OS-level! 74 | 75 | --- 76 | 77 | ## Usage 78 | 79 | ```py 80 | from cdp_patches.input import SyncInput 81 | 82 | sync_input = SyncInput(pid=pid) 83 | # Or 84 | sync_input = SyncInput(browser=browser) 85 | 86 | # Dispatch Inputs 87 | sync_input.click("left", 100, 100) # Left click at (100, 100) 88 | sync_input.double_click("left", 100, 100) # Left double-click at (100, 100) 89 | sync_input.down("left", 100, 100) # Left mouse button down at (100, 100) 90 | sync_input.up("left", 100, 100) # Left mouse button up at (100, 100) 91 | sync_input.move(100, 100) # Move mouse to (100, 100) 92 | sync_input.scroll("down", 10) # Scroll down by 10 lines 93 | sync_input.type("Hello World!") # Type "Hello World!" 94 | ``` 95 | 96 | ## Async Usage 97 | 98 | ```py 99 | import asyncio 100 | 101 | from cdp_patches.input import AsyncInput 102 | 103 | async def main(): 104 | async_input = await AsyncInput(pid=pid) 105 | # Or 106 | async_input = await AsyncInput(browser=browser) 107 | 108 | # Dispatch Inputs 109 | await async_input.click("left", 100, 100) # Left click at (100, 100) 110 | await async_input.double_click("left", 100, 100) # Left double-click at (100, 100) 111 | await async_input.down("left", 100, 100) # Left mouse button down at (100, 100) 112 | await async_input.up("left", 100, 100) # Left mouse button up at (100, 100) 113 | await async_input.move(100, 100) # Move mouse to (100, 100) 114 | await async_input.scroll("down", 10) # Scroll down by 10 lines 115 | await async_input.type("Hello World!") # Type "Hello World!" 116 | 117 | if __name__ == '__main__': 118 | asyncio.run(main()) 119 | ``` 120 | 121 | ### TODO 122 | - [ ] Improve mouse movement timings. 123 | - [ ] Implement extensive testing. 124 | 125 | #### Owner: [Vinyzu](https://github.com/Vinyzu/) 126 | #### Co-Maintainer: [Kaliiiiiiiiii](https://github.com/kaliiiiiiiiii/) 127 |
128 | 129 | 130 | > [!IMPORTANT] 131 | > By the nature of OS-level events (which can only impact actionable windows), this package can only be used with headful browsers. 132 | 133 | > [!WARNING] 134 | > Pressing `SHIFT` or `CAPSLOCK` manually on Windows affects `input.type(text) as well.` 135 | 136 | > [!WARNING] 137 | > Because Chrome does not recognize Input Events to specific tabs, these methods can only be used on the active tab. 138 | > Chrome Tabs do have their own process with a process id (pid), but these can not be controlled using Input Events as they´re just engines. 139 | 140 | 141 | Read the [Documentation](https://vinyzu.gitbook.io/cdp-patches-documentation) 142 | 143 | --- 144 | 145 | ## Development 146 | 147 | Read the [CONTRIBUTING.md](https://github.com/Vinyzu/Botright/blob/main/docs/CONTRIBUTING.md) file. 148 | 149 | --- 150 | 151 | ## Copyright and License 152 | © [Vinyzu](https://github.com/Vinyzu/) 153 | 154 | [GNU GPL](https://choosealicense.com/licenses/gpl-3.0/) 155 | 156 | (Commercial Usage is allowed, but source, license and copyright has to made available. Botright does not provide and Liability or Warranty) 157 | 158 | --- 159 | 160 | ## Authors 161 | 162 | [Vinyzu](https://github.com/Vinyzu/), 163 | [Kaliiiiiiiiii](https://github.com/kaliiiiiiiiii/) -------------------------------------------------------------------------------- /cdp_patches/__init__.py: -------------------------------------------------------------------------------- 1 | import platform 2 | import warnings 3 | 4 | VERSION = "1.1" 5 | 6 | system_name = platform.system() 7 | if system_name == "Windows": 8 | is_windows = True 9 | elif system_name == "Linux": 10 | is_windows = False 11 | else: 12 | warnings.warn("Unknown system (You´re probably using MacOS, which is currently not supported).", RuntimeWarning) 13 | 14 | __all__ = ["VERSION", "is_windows"] 15 | -------------------------------------------------------------------------------- /cdp_patches/input/__init__.py: -------------------------------------------------------------------------------- 1 | from dataclasses import dataclass 2 | 3 | from cdp_patches import is_windows 4 | 5 | from .async_input import AsyncInput 6 | from .sync_input import SyncInput 7 | 8 | 9 | # From: https://pywinauto.readthedocs.io/en/latest/code/pywinauto.keyboard.html 10 | @dataclass 11 | class WinKeyboardCodes: 12 | SPACE: str = "{SPACE}" 13 | TAB: str = "{TAB}" 14 | ENTER: str = "{ENTER}" 15 | BACKSPACE: str = "{BACKSPACE}" 16 | DELETE: str = "{DELETE}" 17 | ESCAPE: str = "{ESC}" 18 | LEFT_ARROW: str = "{LEFT}" 19 | RIGHT_ARROW: str = "{RIGHT}" 20 | UP_ARROW: str = "{UP}" 21 | DOWN_ARROW: str = "{DOWN}" 22 | HOME: str = "{HOME}" 23 | END: str = "{END}" 24 | PAGE_UP: str = "{PGUP}" 25 | PAGE_DOWN: str = "{PGDN}" 26 | PRINT_SCREEN: str = "{PRTSC}" 27 | SCROLLLOCK: str = "{SCROLLLOCK}" 28 | CAPSLOCK: str = "{CAPSLOCK}" 29 | NUMLOCK: str = "{NUMLOCK}" 30 | F1: str = "{F1}" 31 | F2: str = "{F2}" 32 | F3: str = "{F3}" 33 | F4: str = "{F4}" 34 | F5: str = "{F5}" 35 | F6: str = "{F6}" 36 | F7: str = "{F7}" 37 | F8: str = "{F8}" 38 | F9: str = "{F9}" 39 | F10: str = "{F10}" 40 | F11: str = "{F11}" 41 | F12: str = "{F12}" 42 | F13: str = "{F13}" 43 | F14: str = "{F14}" 44 | F15: str = "{F15}" 45 | F16: str = "{F16}" 46 | F17: str = "{F17}" 47 | F18: str = "{F18}" 48 | F19: str = "{F19}" 49 | F20: str = "{F20}" 50 | F21: str = "{F21}" 51 | F22: str = "{F22}" 52 | F23: str = "{F23}" 53 | F24: str = "{F24}" 54 | INSERT: str = "{INSERT}" 55 | WIN_LEFT: str = "{LWIN}" 56 | WIN_RIGHT: str = "{RWIN}" 57 | CONTROL: str = "{VK_CONTROL}" 58 | SHIFT: str = "{VK_SHIFT}" 59 | ALT: str = "{VK_MENU}" 60 | CANCEL: str = "{VK_CANCEL}" 61 | CLEAR: str = "{VK_CLEAR}" 62 | EXECUTE: str = "{VK_EXECUTE}" 63 | KANA: str = "{VK_KANA}" 64 | KANJI: str = "{VK_KANJI}" 65 | LCONTROL: str = "{VK_LCONTROL}" 66 | LMENU: str = "{VK_LMENU}" 67 | LSHIFT: str = "{VK_LSHIFT}" 68 | NEXT: str = "{VK_NEXT}" 69 | PAUSE: str = "{VK_PAUSE}" 70 | PRIOR: str = "{VK_PRIOR}" 71 | RCONTROL: str = "{VK_RCONTROL}" 72 | RETURN: str = "{VK_RETURN}" 73 | RSHIFT: str = "{VK_RSHIFT}" 74 | SELECT: str = "{VK_SELECT}" 75 | MENU_RIGHT: str = "{RMENU}" 76 | ADD: str = "{VK_ADD}" 77 | SUBTRACT: str = "{VK_SUBTRACT}" 78 | DECIMAL: str = "{VK_DECIMAL}" 79 | MULTIPLY: str = "{VK_MULTIPLY}" 80 | DIVIDE: str = "{VK_DIVIDE}" 81 | SEPARATOR: str = "{VK_SEPARATOR}" 82 | NUMPAD0: str = "{VK_NUMPAD0}" 83 | NUMPAD1: str = "{VK_NUMPAD1}" 84 | NUMPAD2: str = "{VK_NUMPAD2}" 85 | NUMPAD3: str = "{VK_NUMPAD3}" 86 | NUMPAD4: str = "{VK_NUMPAD4}" 87 | NUMPAD5: str = "{VK_NUMPAD5}" 88 | NUMPAD6: str = "{VK_NUMPAD6}" 89 | NUMPAD7: str = "{VK_NUMPAD7}" 90 | NUMPAD8: str = "{VK_NUMPAD8}" 91 | NUMPAD9: str = "{VK_NUMPAD9}" 92 | 93 | 94 | # From: https://github.com/python-xlib/python-xlib/blob/4e8bbf8fc4941e5da301a8b3db8d27e98de68666/Xlib/keysymdef/miscellany.py 95 | @dataclass 96 | class LinuxKeyboardCodes: 97 | SPACE: str = "space" 98 | TAB: str = "Tab" 99 | ENTER: str = "Return" 100 | BACKSPACE: str = "BackSpace" 101 | DELETE: str = "Delete" 102 | ESCAPE: str = "Escape" 103 | LEFT_ARROW: str = "Left" 104 | RIGHT_ARROW: str = "Right" 105 | UP_ARROW: str = "Up" 106 | DOWN_ARROW: str = "Down" 107 | HOME: str = "Home" 108 | END: str = "End" 109 | PAGE_UP: str = "Page_Up" 110 | PAGE_DOWN: str = "Page_Down" 111 | PRINT_SCREEN: str = "Print" 112 | SCROLLLOCK: str = "Scroll_Lock" 113 | CAPSLOCK: str = "Caps_Lock" 114 | NUMLOCK: str = "Num_Lock" 115 | F1: str = "F1" 116 | F2: str = "F2" 117 | F3: str = "F3" 118 | F4: str = "F4" 119 | F5: str = "F5" 120 | F6: str = "F6" 121 | F7: str = "F7" 122 | F8: str = "F8" 123 | F9: str = "F9" 124 | F10: str = "F10" 125 | F11: str = "F11" 126 | F12: str = "F12" 127 | F13: str = "F13" 128 | F14: str = "F14" 129 | F15: str = "F15" 130 | F16: str = "F16" 131 | F17: str = "F17" 132 | F18: str = "F18" 133 | F19: str = "F19" 134 | F20: str = "F20" 135 | F21: str = "F21" 136 | F22: str = "F22" 137 | F23: str = "F23" 138 | F24: str = "F24" 139 | INSERT: str = "Insert" 140 | CONTROL: str = "Control_L" # No normal Control -> Alias 141 | SHIFT: str = "Shift_L" # No normal Shift -> Alias 142 | ALT: str = "Alt_L" # No normal Alt -> Alias 143 | CANCEL: str = "Cancel" 144 | CLEAR: str = "Clear" 145 | EXECUTE: str = "Execute" 146 | KANA: str = "Kana_Lock" 147 | KANJI: str = "Kanji" 148 | LCONTROL: str = "Control_L" 149 | LMENU: str = "Menu" 150 | LSHIFT: str = "Shift_L" 151 | NEXT: str = "Next" 152 | PAUSE: str = "Pause" 153 | PRIOR: str = "Prior" 154 | RCONTROL: str = "Control_R" 155 | RETURN: str = "Return" 156 | RSHIFT: str = "Shift_R" 157 | SELECT: str = "Select" 158 | MENU_RIGHT: str = "Menu" # No right Menu -> Alias 159 | ADD: str = "KP_Add" 160 | SUBTRACT: str = "KP_Subtract" 161 | DECIMAL: str = "KP_Decimal" 162 | MULTIPLY: str = "KP_Multiply" 163 | DIVIDE: str = "KP_Divide" 164 | SEPARATOR: str = "KP_Separator" 165 | NUMPAD0: str = "KP_0" 166 | NUMPAD1: str = "KP_1" 167 | NUMPAD2: str = "KP_2" 168 | NUMPAD3: str = "KP_3" 169 | NUMPAD4: str = "KP_4" 170 | NUMPAD5: str = "KP_5" 171 | NUMPAD6: str = "KP_6" 172 | NUMPAD7: str = "KP_7" 173 | NUMPAD8: str = "KP_8" 174 | NUMPAD9: str = "KP_9" 175 | 176 | 177 | KeyboardCodes = WinKeyboardCodes if is_windows else LinuxKeyboardCodes 178 | __all__ = ["SyncInput", "AsyncInput", "KeyboardCodes", "WinKeyboardCodes", "LinuxKeyboardCodes", "is_windows"] 179 | -------------------------------------------------------------------------------- /cdp_patches/input/async_input.py: -------------------------------------------------------------------------------- 1 | from __future__ import annotations 2 | 3 | import asyncio 4 | import platform 5 | import random 6 | import re 7 | import sys 8 | import time 9 | from typing import Any, Generator, Literal, Optional, Union 10 | 11 | if sys.version_info.minor >= 10: 12 | from typing import TypeAlias 13 | else: 14 | TypeAlias = "TypeAlias" # type: ignore[assignment] 15 | 16 | from cdp_patches import is_windows 17 | from cdp_patches.input.exceptions import WindowClosedException 18 | 19 | if is_windows: 20 | from pywinauto.application import ProcessNotFoundError 21 | from pywinauto.base_wrapper import ElementNotEnabled 22 | 23 | from cdp_patches.input.os_base.windows import WindowsBase # type: ignore[assignment] 24 | 25 | LinuxBase: TypeAlias = WindowsBase # type: ignore[no-redef] 26 | InputBase = WindowsBase # type: ignore 27 | WindowErrors = (ValueError, ElementNotEnabled, ProcessNotFoundError, WindowClosedException) # type: ignore[assignment] 28 | else: 29 | from cdp_patches.input.os_base.linux import LinuxBase # type: ignore[assignment] 30 | 31 | WindowsBase: TypeAlias = LinuxBase # type: ignore[no-redef] 32 | InputBase = LinuxBase # type: ignore 33 | WindowErrors = (AssertionError, ValueError, WindowClosedException) # type: ignore[assignment] 34 | 35 | from .browsers import DriverlessAsyncChrome, async_browsers, get_async_browser_pid, get_async_scale_factor 36 | from .mouse_trajectory import HumanizeMouseTrajectory 37 | 38 | 39 | class AsyncInput: 40 | emulate_behaviour: Optional[bool] = True 41 | pid: Optional[int] 42 | _base: Union[WindowsBase, LinuxBase] 43 | window_timeout: float = 30.0 44 | _scale_factor: float = 1.0 45 | sleep_timeout: float = 0.01 46 | typing_speed: int = 50 47 | last_x: int = 0 48 | last_y: int = 0 49 | selective_modifiers_regex = re.compile(r"{[^{}]*}|.") 50 | 51 | def __init__( 52 | self, pid: Optional[int] = None, browser: Optional[async_browsers] = None, scale_factor: Optional[float] = 1.0, emulate_behaviour: Optional[bool] = True, window_timeout: Optional[float] = 30.0 53 | ) -> None: 54 | if platform.system() not in ("Windows", "Linux"): 55 | raise SystemError("Unknown system (You´re probably using MacOS, which is currently not supported).") 56 | 57 | self.pid = pid 58 | self.browser = browser 59 | self.window_timeout = window_timeout or self.window_timeout 60 | self._scale_factor = scale_factor or self._scale_factor 61 | self.emulate_behaviour = emulate_behaviour or self.emulate_behaviour 62 | self._move_lock = asyncio.Lock() 63 | 64 | def __await__(self) -> Generator[None, Any, AsyncInput]: 65 | yield from self.__ainit__().__await__() 66 | return self 67 | 68 | async def __ainit__(self) -> None: 69 | if self.browser: 70 | self.pid = await get_async_browser_pid(self.browser) 71 | self._scale_factor = float(await get_async_scale_factor(self.browser)) 72 | elif not self.pid: 73 | raise ValueError("You must provide a pid or a browser") 74 | 75 | self._base = InputBase(self.pid, self._scale_factor) # type: ignore 76 | await self._wait_for_window() 77 | 78 | # Include Windows Scale Factor for every browser except DriverlessSyncChrome 79 | if is_windows and not isinstance(self.browser, DriverlessAsyncChrome): 80 | self._base.include_windows_scale_factor() 81 | 82 | @property 83 | def base(self) -> Union[WindowsBase, LinuxBase]: 84 | return self._base 85 | 86 | @property 87 | def scale_factor(self) -> float: 88 | return self._scale_factor 89 | 90 | @scale_factor.setter 91 | def scale_factor(self, scale_value: float) -> None: 92 | self._scale_factor = scale_value 93 | if self._base: 94 | self._base.scale_factor = scale_value 95 | 96 | async def _wait_for_window(self) -> None: 97 | max_wait = time.perf_counter() + self.window_timeout 98 | while time.perf_counter() < max_wait: 99 | try: 100 | if await self.base.async_get_window(): 101 | return 102 | except WindowErrors: 103 | pass 104 | await self._sleep_timeout(0.1) 105 | 106 | raise TimeoutError(f"Chrome Window (PID: {self.pid}) not found in {self.window_timeout} seconds.") 107 | 108 | async def _sleep_timeout(self, timeout: Optional[float] = None) -> None: 109 | timeout = timeout or self.sleep_timeout 110 | if not random.randint(0, 10): 111 | timeout_random = self.sleep_timeout / 10 112 | timeout = timeout or random.uniform(self.sleep_timeout, self.sleep_timeout + timeout_random) 113 | 114 | await asyncio.sleep(timeout) 115 | 116 | async def click(self, button: Literal["left", "right", "middle"], x: Union[int, float], y: Union[int, float], emulate_behaviour: Optional[bool] = True, timeout: Optional[float] = None) -> None: 117 | x, y = int(x), int(y) 118 | 119 | await self.down(button=button, x=x, y=y, emulate_behaviour=emulate_behaviour, timeout=timeout) 120 | if self.emulate_behaviour and emulate_behaviour: 121 | await self._sleep_timeout(timeout=timeout) 122 | await self.up(button=button, x=x, y=y) 123 | self.last_x, self.last_y = x, y 124 | 125 | async def double_click( 126 | self, button: Literal["left", "right", "middle"], x: Union[int, float], y: Union[int, float], emulate_behaviour: Optional[bool] = True, timeout: Optional[float] = None 127 | ) -> None: 128 | x, y = int(x), int(y) 129 | 130 | await self.click(button=button, x=x, y=y, timeout=timeout, emulate_behaviour=emulate_behaviour) 131 | if self.emulate_behaviour and emulate_behaviour: 132 | await self._sleep_timeout(random.uniform(0.14, 0.21)) 133 | # await self._sleep_timeout(timeout=timeout) 134 | await self.click(button=button, x=x, y=y, emulate_behaviour=False, timeout=timeout) 135 | 136 | self.last_x, self.last_y = x, y 137 | 138 | async def down(self, button: Literal["left", "right", "middle"], x: Union[int, float], y: Union[int, float], emulate_behaviour: Optional[bool] = True, timeout: Optional[float] = None) -> None: 139 | x, y = int(x), int(y) 140 | 141 | if self.emulate_behaviour and emulate_behaviour: 142 | await self.move(x=x, y=y, timeout=timeout, emulate_behaviour=emulate_behaviour) 143 | self._base.down(button=button, x=x, y=y) 144 | self.last_x, self.last_y = x, y 145 | 146 | async def up(self, button: Literal["left", "right", "middle"], x: Union[int, float], y: Union[int, float]) -> None: 147 | x, y = int(x), int(y) 148 | 149 | self._base.up(button=button, x=x, y=y) 150 | self.last_x, self.last_y = x, y 151 | 152 | async def move(self, x: Union[int, float], y: Union[int, float], emulate_behaviour: Optional[bool] = True, timeout: Optional[float] = None) -> None: 153 | async with self._move_lock: 154 | x, y = int(x), int(y) 155 | 156 | if self.emulate_behaviour and emulate_behaviour: 157 | humanized_points = HumanizeMouseTrajectory((self.last_x, self.last_y), (x, y)) 158 | 159 | # Move Mouse to new random locations 160 | for i, (human_x, human_y) in enumerate(humanized_points.points): 161 | self._base.move(x=int(human_x), y=int(human_y)) 162 | await self._sleep_timeout(timeout=timeout) 163 | 164 | else: 165 | self._base.move(x=x, y=y) 166 | self.last_x, self.last_y = x, y 167 | 168 | async def scroll(self, direction: Literal["up", "down", "left", "right"], amount: int) -> None: 169 | self._base.scroll(direction=direction, amount=amount) 170 | 171 | async def type(self, text: str, fill: Optional[bool] = False, timeout: Optional[float] = None) -> None: 172 | if self.emulate_behaviour and not fill: 173 | for i, char in enumerate(self.selective_modifiers_regex.findall(text)): 174 | # If new word is started wait some more time 175 | if i != 0 and text[i - 1] == " ": 176 | await self._sleep_timeout(timeout=timeout) 177 | 178 | self._base.send_keystrokes(char) 179 | await self._sleep_timeout((random.random() * 10) / self.typing_speed) 180 | else: 181 | self._base.send_keystrokes(text) 182 | -------------------------------------------------------------------------------- /cdp_patches/input/browsers.py: -------------------------------------------------------------------------------- 1 | import json 2 | import time 3 | from contextlib import suppress 4 | from typing import Dict, List, Type, TypedDict, Union 5 | 6 | import requests 7 | from websockets.sync import client 8 | 9 | try: 10 | from playwright.async_api import Browser as AsyncBrowser 11 | from playwright.async_api import BrowserContext as AsyncContext 12 | from playwright.async_api import Error as AsyncError 13 | from playwright.async_api import Error as SyncError 14 | from playwright.sync_api import Browser as SyncBrowser 15 | from playwright.sync_api import BrowserContext as SyncContext 16 | except ImportError: 17 | AsyncBrowser: Type["AsyncBrowser"] = "AsyncBrowser" # type: ignore[no-redef] 18 | AsyncContext: Type["AsyncContext"] = "AsyncContext" # type: ignore[no-redef] 19 | SyncBrowser: Type["SyncBrowser"] = "SyncBrowser" # type: ignore[no-redef] 20 | SyncContext: Type["SyncContext"] = "SyncContext" # type: ignore[no-redef] 21 | 22 | try: 23 | from botright.extended_typing import BrowserContext as BotrightContext 24 | except ImportError: 25 | BotrightContext: Type["BotrightContext"] = "BotrightContext" # type: ignore[no-redef] 26 | 27 | try: 28 | from selenium.webdriver import Chrome as SeleniumChrome 29 | except ImportError: 30 | SeleniumChrome: Type["SeleniumChrome"] = "SeleniumChrome" # type: ignore[no-redef] 31 | 32 | try: 33 | from selenium_driverless.sync.webdriver import Chrome as DriverlessSyncChrome 34 | from selenium_driverless.webdriver import Chrome as DriverlessAsyncChrome 35 | except ImportError: 36 | DriverlessAsyncChrome: Type["DriverlessAsyncChrome"] = "DriverlessAsyncChrome" # type: ignore[no-redef] 37 | DriverlessSyncChrome: Type["DriverlessSyncChrome"] = "DriverlessSyncChrome" # type: ignore[no-redef] 38 | 39 | all_browsers = Union[AsyncContext, AsyncBrowser, SyncContext, SyncBrowser, BotrightContext, SeleniumChrome, DriverlessAsyncChrome, DriverlessSyncChrome] 40 | sync_browsers = Union[SeleniumChrome, SyncContext, SyncBrowser, DriverlessSyncChrome] 41 | async_browsers = Union[AsyncContext, AsyncBrowser, BotrightContext, DriverlessAsyncChrome] 42 | 43 | 44 | class InternalProcessInfo(TypedDict): 45 | type: str 46 | id: int 47 | cpuTime: float 48 | 49 | 50 | class CDPProcessInfo: 51 | processInfo: List[InternalProcessInfo] 52 | 53 | def __init__(self, process_info: Dict[str, List[InternalProcessInfo]]) -> None: 54 | self.processInfo = process_info["processInfo"] 55 | 56 | def get_main_browser(self) -> InternalProcessInfo: 57 | for process in self.processInfo: 58 | if process.get("type") == "browser": 59 | return process 60 | 61 | raise ValueError("No browser process found.") 62 | 63 | 64 | class SeleniumProcessInfoResponse(TypedDict): 65 | id: int 66 | result: Dict[str, List[InternalProcessInfo]] 67 | 68 | 69 | def process_info_from_ws(ws_url: str) -> Dict[str, List[InternalProcessInfo]]: 70 | with client.connect(ws_url) as websocket: 71 | websocket.send(json.dumps({"id": 1, "method": "SystemInfo.getProcessInfo"})) 72 | for message in websocket: 73 | data: SeleniumProcessInfoResponse = json.loads(message) 74 | if data.get("id") == 1: 75 | return data["result"] 76 | 77 | raise RuntimeError("Couldn't connect to browser.") 78 | 79 | 80 | def ws_url_from_url(url: str, timeout: float = 30) -> str: 81 | if len(url) < 7 or url[:7] != "http://": 82 | url = "http://" + url + "/json/version" 83 | try: 84 | data = requests.get(url, timeout=timeout).json() 85 | except requests.exceptions.Timeout: 86 | raise TimeoutError(f"Couldn't connect to browser within {timeout} seconds") 87 | 88 | websocket_debugger_url: str = data["webSocketDebuggerUrl"] 89 | return websocket_debugger_url 90 | 91 | 92 | def process_info_from_url(url: str) -> Dict[str, List[InternalProcessInfo]]: 93 | ws_url = ws_url_from_url(url) 94 | return process_info_from_ws(ws_url) 95 | 96 | 97 | # Browser PID 98 | # Selenium & Selenium Driverless 99 | def get_sync_selenium_browser_pid(driver: Union[SeleniumChrome, DriverlessSyncChrome]) -> int: 100 | if isinstance(driver, DriverlessSyncChrome): 101 | cdp_system_info = driver.base_target.execute_cdp_cmd(cmd="SystemInfo.getProcessInfo") 102 | elif isinstance(driver, SeleniumChrome): 103 | cdp_system_info = process_info_from_url(driver.capabilities["goog:chromeOptions"]["debuggerAddress"]) 104 | else: 105 | raise ValueError("Invalid browser type.") 106 | process_info = CDPProcessInfo(cdp_system_info) 107 | browser_info = process_info.get_main_browser() 108 | return browser_info["id"] 109 | 110 | 111 | async def get_async_selenium_browser_pid(driver: DriverlessAsyncChrome) -> int: 112 | cdp_system_info = await driver.base_target.execute_cdp_cmd(cmd="SystemInfo.getProcessInfo") 113 | 114 | process_info = CDPProcessInfo(cdp_system_info) 115 | browser_info = process_info.get_main_browser() 116 | return browser_info["id"] 117 | 118 | 119 | # Playwright 120 | def get_sync_playwright_browser_pid(browser: Union[SyncContext, SyncBrowser]) -> int: 121 | if isinstance(browser, SyncContext): 122 | main_browser = browser.browser 123 | assert main_browser 124 | cdp_session = main_browser.new_browser_cdp_session() 125 | elif isinstance(browser, SyncBrowser): 126 | cdp_session = browser.new_browser_cdp_session() 127 | else: 128 | raise ValueError("Invalid browser type.") 129 | 130 | cdp_system_info = cdp_session.send("SystemInfo.getProcessInfo") 131 | 132 | process_info = CDPProcessInfo(cdp_system_info) 133 | browser_info = process_info.get_main_browser() 134 | return browser_info["id"] 135 | 136 | 137 | async def get_async_playwright_browser_pid(browser: Union[AsyncContext, AsyncBrowser, BotrightContext]) -> int: 138 | if isinstance(browser, AsyncContext) or isinstance(browser, BotrightContext): 139 | main_browser = browser.browser 140 | assert main_browser 141 | cdp_session = await main_browser.new_browser_cdp_session() 142 | elif isinstance(browser, AsyncBrowser): 143 | cdp_session = await browser.new_browser_cdp_session() 144 | else: 145 | raise ValueError("Invalid browser type.") 146 | cdp_system_info = await cdp_session.send("SystemInfo.getProcessInfo") 147 | 148 | process_info = CDPProcessInfo(cdp_system_info) 149 | browser_info = process_info.get_main_browser() 150 | return browser_info["id"] 151 | 152 | 153 | def get_sync_browser_pid(browser: sync_browsers) -> int: 154 | if isinstance(browser, SeleniumChrome) or isinstance(browser, DriverlessSyncChrome): 155 | return get_sync_selenium_browser_pid(browser) 156 | elif isinstance(browser, SyncContext) or isinstance(browser, SyncBrowser): 157 | return get_sync_playwright_browser_pid(browser) 158 | 159 | raise ValueError("Invalid browser type.") 160 | 161 | 162 | async def get_async_browser_pid(browser: async_browsers) -> int: 163 | if isinstance(browser, DriverlessAsyncChrome): 164 | return await get_async_selenium_browser_pid(browser) 165 | elif isinstance(browser, AsyncContext) or isinstance(browser, AsyncBrowser) or isinstance(browser, BotrightContext): 166 | return await get_async_playwright_browser_pid(browser) 167 | 168 | raise ValueError("Invalid browser type.") 169 | 170 | 171 | # Scale Factor 172 | # Selenium & Selenium Driverless 173 | def get_sync_selenium_scale_factor(driver: Union[SeleniumChrome, DriverlessSyncChrome]) -> int: 174 | if isinstance(driver, DriverlessSyncChrome): 175 | _scale_factor: int = driver.execute_script("return window.devicePixelRatio", unique_context=True) 176 | return _scale_factor 177 | 178 | scale_factor: int = driver.execute_script("return window.devicePixelRatio") 179 | return scale_factor 180 | 181 | 182 | async def get_async_selenium_scale_factor(driver: DriverlessAsyncChrome) -> int: 183 | scale_factor: int = await driver.execute_script("return window.devicePixelRatio", unique_context=True) 184 | return scale_factor 185 | 186 | 187 | # Playwright with Runtime Patching 188 | def get_sync_playwright_scale_factor(browser: Union[SyncContext, SyncBrowser]) -> int: 189 | close_context, close_page = False, False 190 | if isinstance(browser, SyncContext): 191 | context = browser 192 | elif isinstance(browser, SyncBrowser): 193 | if any(browser.contexts): 194 | context = browser.contexts[0] 195 | else: 196 | context = browser.new_context() 197 | close_context = True 198 | else: 199 | raise ValueError("Invalid browser type.") 200 | 201 | if any(context.pages): 202 | page = context.pages[0] 203 | else: 204 | page = context.new_page() 205 | close_page = True 206 | cdp_session = context.new_cdp_session(page) 207 | 208 | time1 = time.perf_counter() 209 | while (time.perf_counter() - time1) <= 10: 210 | try: 211 | page_frame_tree = cdp_session.send("Page.getFrameTree") 212 | page_id = page_frame_tree["frameTree"]["frame"]["id"] 213 | 214 | isolated_world = cdp_session.send("Page.createIsolatedWorld", {"frameId": page_id, "grantUniveralAccess": True, "worldName": "Shimmy shimmy yay, shimmy yay, shimmy ya"}) 215 | isolated_exec_id = isolated_world["executionContextId"] 216 | break 217 | except SyncError as e: 218 | if e.message == "Protocol error (Page.createIsolatedWorld): Invalid parameters": 219 | pass 220 | else: 221 | raise e 222 | else: 223 | raise TimeoutError("Page.createIsolatedWorld did not initialize properly within 30 seconds.") 224 | 225 | time2 = time.perf_counter() 226 | while (time.perf_counter() - time2) <= 10: 227 | try: 228 | scale_factor_eval = cdp_session.send("Runtime.evaluate", {"expression": "window.devicePixelRatio", "contextId": isolated_exec_id}) 229 | scale_factor: int = scale_factor_eval["result"]["value"] 230 | break 231 | except SyncError as e: 232 | if e.message == "Protocol error (Runtime.evaluate): Cannot find context with specified id": 233 | pass 234 | else: 235 | raise e 236 | else: 237 | raise TimeoutError("Runtime.evaluate did not run properly within 30 seconds.") 238 | 239 | with suppress(SyncError): 240 | if close_page: 241 | page.close() 242 | 243 | with suppress(SyncError): 244 | if close_context: 245 | context.close() 246 | 247 | return scale_factor 248 | 249 | 250 | async def get_async_playwright_scale_factor(browser: Union[AsyncContext, AsyncBrowser, BotrightContext]) -> int: 251 | close_context, close_page = False, False 252 | if isinstance(browser, AsyncContext) or isinstance(browser, BotrightContext): 253 | context = browser 254 | elif isinstance(browser, AsyncBrowser): 255 | if any(browser.contexts): 256 | context = browser.contexts[0] 257 | else: 258 | context = await browser.new_context() 259 | close_context = True 260 | else: 261 | raise ValueError("Invalid browser type.") 262 | 263 | if any(context.pages): 264 | page = context.pages[0] 265 | else: 266 | page = await context.new_page() 267 | close_page = True 268 | cdp_session = await context.new_cdp_session(page) 269 | 270 | time1 = time.perf_counter() 271 | while (time.perf_counter() - time1) <= 10: 272 | try: 273 | page_frame_tree = await cdp_session.send("Page.getFrameTree") 274 | page_id = page_frame_tree["frameTree"]["frame"]["id"] 275 | 276 | isolated_world = await cdp_session.send("Page.createIsolatedWorld", {"frameId": page_id, "grantUniveralAccess": True, "worldName": "Shimmy shimmy yay, shimmy yay, shimmy ya"}) 277 | isolated_exec_id = isolated_world["executionContextId"] 278 | break 279 | except AsyncError as e: 280 | if e.message == "Protocol error (Page.createIsolatedWorld): Invalid parameters": 281 | pass 282 | else: 283 | raise e 284 | else: 285 | raise TimeoutError("Page.createIsolatedWorld did not initialize properly within 30 seconds.") 286 | 287 | time2 = time.perf_counter() 288 | while (time.perf_counter() - time2) <= 10: 289 | try: 290 | scale_factor_eval = await cdp_session.send("Runtime.evaluate", {"expression": "window.devicePixelRatio", "contextId": isolated_exec_id}) 291 | scale_factor: int = scale_factor_eval["result"]["value"] 292 | break 293 | except AsyncError as e: 294 | if e.message == "Protocol error (Runtime.evaluate): Cannot find context with specified id": 295 | pass 296 | else: 297 | raise e 298 | else: 299 | raise TimeoutError("Runtime.evaluate did not run properly within 30 seconds.") 300 | 301 | with suppress(SyncError): 302 | if close_page: 303 | await page.close() 304 | 305 | with suppress(SyncError): 306 | if close_context: 307 | await context.close() 308 | 309 | return scale_factor 310 | 311 | 312 | def get_sync_scale_factor(browser: sync_browsers) -> int: 313 | if isinstance(browser, SeleniumChrome) or isinstance(browser, DriverlessSyncChrome): 314 | return get_sync_selenium_scale_factor(browser) 315 | elif isinstance(browser, SyncContext) or isinstance(browser, SyncBrowser): 316 | return get_sync_playwright_scale_factor(browser) 317 | 318 | raise ValueError("Invalid browser type.") 319 | 320 | 321 | async def get_async_scale_factor(browser: async_browsers) -> int: 322 | if isinstance(browser, DriverlessAsyncChrome): 323 | return await get_async_selenium_scale_factor(browser) 324 | elif isinstance(browser, AsyncContext) or isinstance(browser, AsyncBrowser) or isinstance(browser, BotrightContext): 325 | return await get_async_playwright_scale_factor(browser) 326 | 327 | raise ValueError("Invalid browser type.") 328 | 329 | 330 | __all__ = ["SeleniumChrome", "DriverlessSyncChrome", "DriverlessAsyncChrome"] 331 | -------------------------------------------------------------------------------- /cdp_patches/input/exceptions.py: -------------------------------------------------------------------------------- 1 | from typing import Optional 2 | 3 | 4 | class WindowClosedException(Exception): 5 | def __init__(self, message: Optional[str] = None, pid: Optional[int] = None): 6 | if not message: 7 | if pid: 8 | message = f"Interaction not possible due to stale/closed window with pid {pid}" 9 | else: 10 | message = "Interaction not possible due to stale/closed window" 11 | 12 | super().__init__(message) 13 | -------------------------------------------------------------------------------- /cdp_patches/input/mouse_trajectory.py: -------------------------------------------------------------------------------- 1 | import math 2 | import random 3 | from typing import Any, Callable, List, NoReturn, Tuple, Union 4 | 5 | import numpy as np 6 | 7 | 8 | # From https://github.com/riflosnake/HumanCursor/blob/main/humancursor/utilities/human_curve_generator.py 9 | # Edited by Vinyzu for more realistic mouse movements 10 | class HumanizeMouseTrajectory: 11 | def __init__(self, from_point: Tuple[int, int], to_point: Tuple[int, int]) -> None: 12 | self.from_point = from_point 13 | self.to_point = to_point 14 | 15 | if self.from_point == self.to_point: 16 | self.to_point = (self.from_point[0] + 10, self.from_point[1] + 10) 17 | 18 | self.points = self.generate_curve() 19 | self.points.append(to_point) 20 | 21 | def easeOutQuad(self, n: float) -> float: 22 | if not 0.0 <= n <= 1.0: 23 | raise ValueError("Argument must be between 0.0 and 1.0.") 24 | return -n * (n - 2) 25 | 26 | def generate_curve(self) -> List[Tuple[float, float]]: 27 | """Generates the curve based on arguments below, default values below are automatically modified to cause randomness""" 28 | left_boundary = min(self.from_point[0], self.to_point[0]) - 40 29 | right_boundary = max(self.from_point[0], self.to_point[0]) + 40 30 | down_boundary = min(self.from_point[1], self.to_point[1]) - 40 31 | up_boundary = max(self.from_point[1], self.to_point[1]) + 40 32 | 33 | points_distance = math.sqrt((self.from_point[0] - self.to_point[0]) ** 2 + (self.from_point[1] - self.to_point[1]) ** 2) 34 | internalKnots = self.generate_internal_knots(left_boundary, right_boundary, down_boundary, up_boundary, int(points_distance**0.25)) 35 | points = self.generate_points(internalKnots) 36 | points = self.distort_points(points, 2, 2, 0.6) 37 | 38 | target_points = int(points_distance // 4) if int(points_distance // 4) > 2 else 2 39 | points = self.tween_points(points, target_points) 40 | return points 41 | 42 | def generate_internal_knots( 43 | self, l_boundary: Union[int, float], r_boundary: Union[int, float], d_boundary: Union[int, float], u_boundary: Union[int, float], knots_count: int 44 | ) -> Union[List[Tuple[int, int]], NoReturn]: 45 | """Generates the internal knots of the curve randomly""" 46 | if not (self.check_if_numeric(l_boundary) and self.check_if_numeric(r_boundary) and self.check_if_numeric(d_boundary) and self.check_if_numeric(u_boundary)): 47 | raise ValueError("Boundaries must be numeric values") 48 | if not isinstance(knots_count, int) or knots_count < 0: 49 | knots_count = 0 50 | if l_boundary > r_boundary: 51 | raise ValueError("left_boundary must be less than or equal to right_boundary") 52 | if d_boundary > u_boundary: 53 | raise ValueError("down_boundary must be less than or equal to upper_boundary") 54 | 55 | # knotsX = np.random.choice(range(int(l_boundary), int(r_boundary)), size=knots_count) 56 | # knotsY = np.random.choice(range(int(d_boundary), int(u_boundary)), size=knots_count) 57 | # knots = list(zip(knotsX, knotsY)) 58 | 59 | # Standard deviation for normal distribution 60 | midpoint_x = (l_boundary + r_boundary) / 2 61 | midpoint_y = (d_boundary + u_boundary) / 2 62 | std_deviation = min(midpoint_x - l_boundary, r_boundary - midpoint_x, midpoint_y - d_boundary, u_boundary - midpoint_y) 63 | 64 | # Generate knotsX and knotsY using normal distribution 65 | knotsX = np.random.normal(midpoint_x, std_deviation, size=knots_count).astype(int) 66 | knotsY = np.random.normal(midpoint_y, std_deviation, size=knots_count).astype(int) 67 | 68 | # Clip values to within boundaries 69 | knotsX = np.clip(knotsX, l_boundary, r_boundary) 70 | knotsY = np.clip(knotsY, d_boundary, u_boundary) 71 | knots = list(zip(knotsX, knotsY)) 72 | 73 | # Sort Knots 74 | def distance(knot): 75 | return math.sqrt((self.from_point[0] - knot[0]) ** 2 + (self.from_point[0] - knot[1]) ** 2) 76 | 77 | sorted_knots = sorted(knots, key=lambda knot: distance(knot)) 78 | return sorted_knots 79 | 80 | def generate_points(self, knots: List[Tuple[int, int]]) -> List[Tuple[float, float]]: 81 | """Generates the points from BezierCalculator""" 82 | if not self.check_if_list_of_points(knots): 83 | raise ValueError("knots must be valid list of points") 84 | 85 | midPtsCnt = max( 86 | abs(self.from_point[0] - self.to_point[0]), 87 | abs(self.from_point[1] - self.to_point[1]), 88 | 2, 89 | ) 90 | knots = [self.from_point] + knots + [self.to_point] 91 | return BezierCalculator.calculate_points_in_curve(int(midPtsCnt), knots) 92 | 93 | def distort_points(self, points: List[Tuple[float, float]], distortion_mean: int, distortion_st_dev: int, distortion_frequency: float) -> Union[List[Tuple[float, float]], NoReturn]: 94 | """Distorts points by parameters of mean, standard deviation and frequency""" 95 | if not (self.check_if_numeric(distortion_mean) and self.check_if_numeric(distortion_st_dev) and self.check_if_numeric(distortion_frequency)): 96 | raise ValueError("Distortions must be numeric") 97 | if not self.check_if_list_of_points(points): 98 | raise ValueError("points must be valid list of points") 99 | if not (0 <= distortion_frequency <= 1): 100 | raise ValueError("distortion_frequency must be in range [0,1]") 101 | 102 | distorted: List[Tuple[float, float]] = [] 103 | for i in range(1, len(points) - 1): 104 | x, y = points[i] 105 | delta = int(np.random.normal(distortion_mean, distortion_st_dev) if random.random() < distortion_frequency else 0) 106 | distorted.append((x + delta // 5, y + delta // 5)) 107 | distorted = [points[0]] + distorted + [points[-1]] 108 | return distorted 109 | 110 | def tween_points(self, points: List[Tuple[float, float]], target_points: int) -> Union[List[Tuple[float, float]], NoReturn]: 111 | """Modifies points by tween""" 112 | if not self.check_if_list_of_points(points): 113 | raise ValueError("List of points not valid") 114 | if not isinstance(target_points, int) or target_points < 2: 115 | raise ValueError("target_points must be an integer greater or equal to 2") 116 | 117 | res: List[Tuple[float, float]] = [] 118 | for i in range(target_points): 119 | index = int(self.easeOutQuad(float(i) / (target_points - 1)) * (len(points) - 1)) 120 | res.append(points[index]) 121 | return res 122 | 123 | @staticmethod 124 | def check_if_numeric(val: Any) -> bool: 125 | """Checks if value is proper numeric value""" 126 | return isinstance(val, (float, int, np.integer, np.float32, np.float64)) 127 | 128 | def check_if_list_of_points(self, list_of_points: Union[List[Tuple[int, int]], List[Tuple[float, float]]]) -> bool: 129 | """Checks if list of points is valid""" 130 | try: 131 | 132 | def point(p): 133 | return (len(p) == 2) and self.check_if_numeric(p[0]) and self.check_if_numeric(p[1]) 134 | 135 | return all(map(point, list_of_points)) 136 | except (KeyError, TypeError): 137 | return False 138 | 139 | 140 | class BezierCalculator: 141 | @staticmethod 142 | def binomial(n: int, k: int) -> float: 143 | """Returns the binomial coefficient "n choose k" """ 144 | return math.factorial(n) / float(math.factorial(k) * math.factorial(n - k)) 145 | 146 | @staticmethod 147 | def bernstein_polynomial_point(x: int, i: int, n: int) -> float: 148 | """Calculate the i-th component of a bernstein polynomial of degree n""" 149 | return float(BezierCalculator.binomial(n, i) * (x**i) * ((1 - x) ** (n - i))) # Quirky for mypy :D 150 | 151 | @staticmethod 152 | def bernstein_polynomial(points: List[Tuple[int, int]]) -> Callable[[float], Tuple[float, float]]: 153 | """ 154 | Given list of control points, returns a function, which given a point [0,1] returns 155 | a point in the Bezier described by these points 156 | """ 157 | 158 | def bernstein(t): 159 | n = len(points) - 1 160 | x = y = 0.0 161 | for i, point in enumerate(points): 162 | bern = BezierCalculator.bernstein_polynomial_point(t, i, n) 163 | x += point[0] * bern 164 | y += point[1] * bern 165 | return x, y 166 | 167 | return bernstein 168 | 169 | @staticmethod 170 | def calculate_points_in_curve(n: int, points: List[Tuple[int, int]]) -> List[Tuple[float, float]]: 171 | """ 172 | Given list of control points, returns n points in the Bézier curve, 173 | described by these points 174 | """ 175 | curvePoints: List[Tuple[float, float]] = [] 176 | bernstein_polynomial = BezierCalculator.bernstein_polynomial(points) 177 | for i in range(n): 178 | t = i / (n - 1) 179 | curvePoints += (bernstein_polynomial(t),) 180 | return curvePoints 181 | -------------------------------------------------------------------------------- /cdp_patches/input/os_base/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kaliiiiiiiiii-Vinyzu/CDP-Patches/602b680abf50b8190a5955ec7feb8c2045002e8d/cdp_patches/input/os_base/__init__.py -------------------------------------------------------------------------------- /cdp_patches/input/os_base/linux.py: -------------------------------------------------------------------------------- 1 | import asyncio 2 | import os 3 | import re 4 | import subprocess 5 | import time 6 | from typing import Any, List, Literal, Tuple 7 | 8 | from Xlib import X, display 9 | from Xlib.error import BadWindow 10 | from Xlib.ext.xtest import fake_input 11 | from Xlib.XK import string_to_keysym 12 | from Xlib.xobject.drawable import Window 13 | 14 | from cdp_patches.input.exceptions import WindowClosedException 15 | 16 | # Every Common Symbol on a QWERTY Keyboard, Source: https://github.com/python-xlib/python-xlib/blob/4e8bbf8fc4941e5da301a8b3db8d27e98de68666/Xlib/keysymdef/latin1.py 17 | # Dict Source: https://github.com/svenlr/swift-map/blob/main/mainloop.py#L459 18 | symbol_dict = { 19 | "@": "at", 20 | "`": "grave", 21 | "\t": "Tab", 22 | "|": "bar", 23 | "\n": "Return", 24 | "\r": "Return", 25 | "~": "asciitilde", 26 | "{": "braceleft", 27 | "[": "bracketleft", 28 | "]": "bracketright", 29 | "\\": "backslash", 30 | "_": "underscore", 31 | "^": "asciicircum", 32 | "!": "exclam", 33 | " ": "space", 34 | "#": "numbersign", 35 | '"': "quotedbl", 36 | "%": "percent", 37 | "$": "dollar", 38 | "'": "apostrophe", 39 | "&": "ampersand", 40 | ")": "parenright", 41 | "(": "parenleft", 42 | "+": "plus", 43 | "*": "asterisk", 44 | "-": "minus", 45 | ",": "comma", 46 | "/": "slash", 47 | ".": "period", 48 | "\\e": "Escape", 49 | "}": "braceright", 50 | ";": "semicolon", 51 | ":": "colon", 52 | "=": "equal", 53 | "<": "less", 54 | "?": "question", 55 | ">": "greater", 56 | } 57 | 58 | layouts_shifted_chars = { 59 | "QWERTY": '~!@#$%^&*()_+{}|:"<>?', 60 | "AZERTY": '^¨£$¤%µ§*()_+{}|:"<>?', 61 | "QWERTZ": '°!"§$%&/()=?`´*_:;<>|', 62 | "DVORAK": '~!@#$%^&*()_+{}|:"<>?', 63 | "COLEMAK": '~!@#$%^&*()_+{}|:"<>?', 64 | "WORKMAN": '~!@#$%^&*()_+{}|:"<>?', 65 | "BEPO": '~!@#$%^&*()_+{}|:"<>?', 66 | "NEO": '~!@#$%^&*()_+{}|:"<>?', 67 | "JCUKEN": '!"№;%:?*()_+{}|:"<>?', 68 | "RUSSIAN": '!"№;%:?*()_+{}|:"<>?', 69 | } 70 | 71 | 72 | # Note for someday: `setxkbmap us` 73 | def get_kb_layout_shifted_chars() -> str: 74 | search_regex = r'xkb_keycodes\s+{\s+include\s+"[A-Za-z\+]*\((.*?)\)"' 75 | layout_info = subprocess.check_output(["setxkbmap", "-print"]).decode("utf-8") 76 | matches = re.search(search_regex, layout_info) 77 | if matches: 78 | layout = matches.group(1).upper() 79 | if layout in layouts_shifted_chars: 80 | return layouts_shifted_chars[layout] 81 | raise EnvironmentError(f"Keyboard layout not recognized: {layout}!") 82 | raise EnvironmentError("Keyboard layout not found!") 83 | 84 | 85 | class LinuxBase: 86 | browser_window: Window 87 | hwnd: int 88 | pid: int 89 | scale_factor: float = 1.0 90 | toolbar_height: int = 0 91 | render_widget_height: int = 0 92 | shifted_chars = get_kb_layout_shifted_chars() 93 | 94 | def __init__(self, pid: int, scale_factor: float) -> None: 95 | self.pid = pid 96 | self.scale_factor = scale_factor 97 | self._loop = asyncio.get_event_loop() 98 | 99 | display_env = os.getenv("DISPLAY") 100 | self.display = display.Display(display_env) 101 | self.tab_pid = self.get_window() 102 | 103 | self.browser_window = self.display.create_resource_object("window", self.tab_pid) 104 | 105 | def get_window(self) -> Any: 106 | name_atom = self.display.get_atom("WM_NAME", only_if_exists=True) 107 | pid_atom = self.display.get_atom("_NET_WM_PID", only_if_exists=True) 108 | res_windows: List[Window] = [] 109 | 110 | # Getting all WindowIds by PID by recursively searching through all windows under the root window query tree 111 | def search_windows_by_pid(query_tree, pid: int): 112 | for window in query_tree.children: 113 | window_pid = window.get_property(pid_atom, 0, 0, pow(2, 32) - 1) 114 | if window_pid and window_pid.value[0] == pid: 115 | res_windows.append(window) 116 | if window.query_tree().children: 117 | search_windows_by_pid(window.query_tree(), pid) 118 | 119 | search_windows_by_pid(self.display.screen().root.query_tree(), self.pid) 120 | if not res_windows: 121 | raise WindowClosedException(f"No windows found for PID: {self.pid}") 122 | 123 | for window in res_windows: 124 | # Getting necessary window properties 125 | title = window.get_property(name_atom, 0, 0, pow(2, 32) - 1).value 126 | min_height = window.get_wm_normal_hints().min_height 127 | # parent_offset_coords = window.translate_coords(window.query_tree().parent, 0, 0) 128 | # window_x, window_y = parent_offset_coords.x, parent_offset_coords.y 129 | 130 | # Filter out non-browser windows, for example the Taskbar or Info Bars 131 | if (b"google-chrome" in title) or (title == b"chrome") or (min_height == 0): # or (window_x == window_y) or not all((window_x, window_y)) 132 | continue 133 | 134 | self.browser_window = window 135 | return self.browser_window 136 | 137 | raise WindowClosedException(f"No windows found for PID: {self.pid}") 138 | 139 | def ensure_window(self) -> None: 140 | try: 141 | # No Easy Visibility Check, well just check a random window attribute... 142 | if not self.browser_window.get_wm_normal_hints().min_height: 143 | self.get_window() 144 | except BadWindow: 145 | self.get_window() 146 | 147 | async def async_get_window(self) -> Any: 148 | name_atom = self.display.get_atom("WM_NAME", only_if_exists=True) 149 | pid_atom = self.display.get_atom("_NET_WM_PID", only_if_exists=True) 150 | res_windows: List[Window] = [] 151 | 152 | # Getting all WindowIds by PID by recursively searching through all windows under the root window query tree 153 | def search_windows_by_pid(query_tree, pid: int): 154 | for window in query_tree.children: 155 | window_pid = window.get_property(pid_atom, 0, 0, pow(2, 32) - 1) 156 | if window_pid and window_pid.value[0] == pid: 157 | res_windows.append(window) 158 | if window.query_tree().children: 159 | search_windows_by_pid(window.query_tree(), pid) 160 | 161 | await self._loop.run_in_executor(None, lambda: search_windows_by_pid(self.display.screen().root.query_tree(), self.pid)) 162 | if not res_windows: 163 | raise WindowClosedException(f"No windows found for PID: {self.pid}") 164 | 165 | for window in res_windows: 166 | # Getting necessary window properties 167 | title = window.get_property(name_atom, 0, 0, pow(2, 32) - 1).value 168 | min_height = window.get_wm_normal_hints().min_height 169 | # parent_offset_coords = window.translate_coords(window.query_tree().parent, 0, 0) 170 | # window_x, window_y = parent_offset_coords.x, parent_offset_coords.y 171 | 172 | # Filter out non-browser windows, for example the Taskbar or Info Bars 173 | if (b"google-chrome" in title) or (title == b"chrome") or (min_height == 0): # or (window_x == window_y) or not all((window_x, window_y)) 174 | continue 175 | 176 | self.browser_window = window 177 | return self.browser_window 178 | 179 | raise WindowClosedException(f"No windows found for PID: {self.pid}") 180 | 181 | def _offset_toolbar_height(self) -> Tuple[int, int]: 182 | # Get Window Location 183 | root_offset_coords = self.browser_window.translate_coords(self.browser_window.query_tree().root, 0, 0) 184 | parent_offset_coords = self.browser_window.translate_coords(self.browser_window.query_tree().parent, 0, 0) 185 | window_x = abs(root_offset_coords.x) + abs(parent_offset_coords.x) 186 | window_y = abs(root_offset_coords.y) + abs(parent_offset_coords.y) 187 | 188 | # Get Chrome Toolbar Height (Note: Fetching Chromes "Program Specified Minimum Size" - 1 for Chrome Toolbar Height + 1 for the Minimum Tab Size) 189 | chrome_toolbar_height = self.browser_window.get_wm_normal_hints().min_height - 1 190 | 191 | # Get Linux (Outer) Window Toolbar Height 192 | frame_extends_atom = self.display.get_atom("_NET_FRAME_EXTENTS", only_if_exists=True) 193 | if frame_extends_atom == X.NONE: 194 | raise ValueError('No Atom interned with the Name "_NET_FRAME_EXTENTS".') 195 | 196 | net_frame_extends = self.browser_window.get_property(frame_extends_atom, 0, 0, pow(2, 32) - 1) 197 | if net_frame_extends: 198 | window_toolbar_height = net_frame_extends.value[2] 199 | window_toolbar_width = net_frame_extends.value[3] 200 | else: 201 | window_toolbar_height = 0 202 | window_toolbar_width = 0 203 | 204 | offset_width: int = window_x - window_toolbar_width 205 | offset_height: int = window_y - window_toolbar_height + chrome_toolbar_height 206 | return offset_width, offset_height 207 | 208 | @staticmethod 209 | def _translate_button(button: Literal["left", "right", "middle", "scroll_up", "scroll_down"]) -> int: 210 | if button == "left": 211 | return 1 212 | elif button == "middle": 213 | return 2 214 | elif button == "right": 215 | return 3 216 | elif button == "scroll_up": 217 | return 4 218 | elif button == "scroll_down": 219 | return 5 220 | 221 | def down(self, button: Literal["left", "right", "middle"], x: int, y: int) -> None: 222 | self.ensure_window() 223 | self.move(x=x, y=y) 224 | fake_input(self.display, X.ButtonPress, self._translate_button(button)) 225 | self.display.sync() 226 | 227 | def up(self, button: Literal["left", "right", "middle"], x: int, y: int) -> None: 228 | self.ensure_window() 229 | self.move(x=x, y=y) 230 | fake_input(self.display, X.ButtonRelease, self._translate_button(button)) 231 | self.display.sync() 232 | 233 | def move(self, x: int, y: int) -> None: 234 | self.ensure_window() 235 | offset_width, offset_height = self._offset_toolbar_height() 236 | x = int(x * self.scale_factor) + offset_width 237 | y = int(y * self.scale_factor) + offset_height 238 | 239 | fake_input(self.display, X.MotionNotify, x=x, y=y) 240 | self.display.sync() 241 | 242 | def scroll(self, direction: Literal["up", "down", "left", "right"], amount: int) -> None: 243 | self.ensure_window() 244 | if direction in ("left", "right"): 245 | raise NotImplementedError("Scrolling horizontally is not supported on Linux.") 246 | 247 | scroll_direction: Literal["scroll_up", "scroll_down"] = "scroll_up" if direction == "up" else "scroll_down" 248 | 249 | for _ in range(amount): 250 | fake_input(self.display, X.ButtonPress, self._translate_button(scroll_direction)) 251 | self.display.sync() 252 | fake_input(self.display, X.ButtonRelease, self._translate_button(scroll_direction)) 253 | self.display.sync() 254 | 255 | def send_keystrokes(self, text: str) -> None: 256 | self.ensure_window() 257 | selective_regex = re.compile(r"{[^{}]*}|.") # Only for redundancy of windows implementations 258 | shift_keycode = self.display.keysym_to_keycode(0xFFE1) # Shift Key (0xFFE1) 259 | 260 | for key in selective_regex.findall(text): 261 | shifted_key = key.isupper() or key in self.shifted_chars 262 | if key in symbol_dict: 263 | key = symbol_dict[key] 264 | 265 | keysym = string_to_keysym(key) 266 | keycode = self.display.keysym_to_keycode(keysym) 267 | self.browser_window.set_input_focus(X.RevertToNone, X.CurrentTime) 268 | 269 | if shifted_key: 270 | fake_input(self.display, X.KeyPress, shift_keycode) 271 | # time.sleep(0.1) 272 | 273 | fake_input(self.display, X.KeyPress, keycode) 274 | self.display.sync() 275 | time.sleep(0.01) # Note: Might want to increase this in the future, to make it more human-like, but pywinauto uses the same timeouts so for now its fine. 276 | 277 | fake_input(self.display, X.KeyRelease, keycode) 278 | 279 | if shifted_key: 280 | # time.sleep(0.1) 281 | fake_input(self.display, X.KeyRelease, shift_keycode) 282 | self.display.sync() 283 | -------------------------------------------------------------------------------- /cdp_patches/input/os_base/windows.py: -------------------------------------------------------------------------------- 1 | import asyncio 2 | import ctypes 3 | import re 4 | import warnings 5 | from typing import List, Literal, Union 6 | 7 | from pywinauto import application, timings 8 | from pywinauto.application import WindowSpecification 9 | from pywinauto.base_wrapper import ElementNotVisible 10 | from pywinauto.controls.hwndwrapper import HwndWrapper, InvalidWindowHandle 11 | 12 | from cdp_patches.input.exceptions import WindowClosedException 13 | 14 | timings.Timings.fast() 15 | timings.TimeConfig._timings["sendmessagetimeout_timeout"] = 0 16 | timings.TimeConfig._timings["after_click_wait"] = 0 17 | timings.TimeConfig._timings["after_clickinput_wait"] = 0 18 | timings.TimeConfig._timings["after_sendkeys_key_wait"] = 0 19 | # don't block asyncio 20 | timings.TimeConfig._timings["scroll_step_wait"] = 0.01 21 | timings.TimeConfig._timings["window_find_timeout"] = 0.01 22 | timings.TimeConfig._timings["exists_timeout"] = 0.01 23 | 24 | warnings.filterwarnings("ignore", category=UserWarning, message="32-bit application should be automated using 32-bit Python (you use 64-bit Python)") 25 | 26 | 27 | def get_top_window(app: application.Application, windows=List[Union[WindowSpecification, HwndWrapper]]) -> WindowSpecification: 28 | if windows is None: 29 | windows = app 30 | # win32_app.top_window(), but without timeout 31 | criteria = {"backend": app.backend.name} 32 | if windows[0].handle: 33 | criteria["handle"] = windows[0].handle 34 | else: 35 | criteria["name"] = windows[0].name 36 | return WindowSpecification(criteria, allow_magic_lookup=app.allow_magic_lookup) 37 | 38 | 39 | class WindowsBase: 40 | browser_window: Union[WindowSpecification, HwndWrapper] 41 | hwnd: int 42 | pid: int 43 | scale_factor: float = 1.0 44 | toolbar_height: int = 0 45 | win32_app: application.Application = None 46 | 47 | def __init__(self, pid: int, scale_factor: float) -> None: 48 | self.pid = pid 49 | self.scale_factor = scale_factor 50 | self._loop = asyncio.get_event_loop() 51 | 52 | def include_windows_scale_factor(self): 53 | windows_scale_factor = ctypes.windll.shcore.GetScaleFactorForDevice(0) / 100 54 | self.scale_factor *= windows_scale_factor 55 | 56 | def get_window( 57 | self, 58 | timeout: float = 1, 59 | ) -> WindowSpecification: 60 | if self.win32_app is None: 61 | # save time 62 | self.win32_app = application.Application(backend="win32") 63 | self.win32_app.connect(process=self.pid, timeout=timeout) 64 | try: 65 | windows = self.win32_app.windows() 66 | except InvalidWindowHandle: 67 | raise WindowClosedException(pid=self.pid) 68 | if not windows: 69 | raise WindowClosedException(f"No windows found for PID: {self.pid}") 70 | for window in windows: 71 | if window.element_info.class_name == "Chrome_WidgetWin_1" and window.is_visible(): 72 | self.browser_window = window 73 | break 74 | else: 75 | self.browser_window = get_top_window(self.win32_app, windows) 76 | 77 | for child in self.browser_window.iter_children(): 78 | if child.element_info.class_name == "Chrome_RenderWidgetHostHWND": 79 | self.browser_window = child 80 | 81 | self.hwnd = self.browser_window.handle 82 | # Perform Window Checks 83 | try: 84 | self.browser_window.verify_actionable() 85 | except ElementNotVisible: 86 | raise WindowClosedException(pid=self.pid) 87 | if not self.browser_window.is_normal(): 88 | raise WindowClosedException(pid=self.pid) 89 | 90 | return self.browser_window 91 | 92 | def ensure_window(self) -> None: 93 | try: 94 | if not self.browser_window.is_visible(): 95 | self.get_window() 96 | except InvalidWindowHandle: 97 | self.get_window() 98 | 99 | async def async_get_window(self, timeout: float = 1) -> WindowSpecification: 100 | if self.win32_app is None: 101 | # save time 102 | self.win32_app = application.Application(backend="win32") 103 | await self._loop.run_in_executor(None, lambda: self.win32_app.connect(process=self.pid, timeout=timeout)) 104 | 105 | windows = self.win32_app.windows() 106 | if not windows: 107 | raise WindowClosedException(f"No windows found for PID: {self.pid}") 108 | for window in windows: 109 | if window.element_info.class_name == "Chrome_WidgetWin_1" and window.is_visible(): 110 | self.browser_window = window 111 | break 112 | else: 113 | self.browser_window = get_top_window(self.win32_app, windows) 114 | 115 | for child in self.browser_window.iter_children(): 116 | if child.element_info.class_name == "Chrome_RenderWidgetHostHWND": 117 | self.browser_window = child 118 | 119 | self.hwnd = self.browser_window.handle 120 | # Perform Window Checks 121 | try: 122 | self.browser_window.verify_actionable() 123 | except ElementNotVisible: 124 | raise WindowClosedException(pid=self.pid) 125 | if not self.browser_window.is_normal(): 126 | raise WindowClosedException(pid=self.pid) 127 | 128 | return self.browser_window 129 | 130 | def down(self, button: Literal["left", "right", "middle"], x: int, y: int) -> None: 131 | self.ensure_window() 132 | self.browser_window.press_mouse(button=button, coords=(int(x * self.scale_factor), int(y * self.scale_factor))) 133 | 134 | def up(self, button: Literal["left", "right", "middle"], x: int, y: int) -> None: 135 | self.ensure_window() 136 | self.browser_window.release_mouse(button=button, coords=(int(x * self.scale_factor), int(y * self.scale_factor))) 137 | 138 | def move(self, x: int, y: int) -> None: 139 | self.ensure_window() 140 | self.browser_window.move_mouse(coords=(int(x * self.scale_factor), int(y * self.scale_factor)), pressed="left") 141 | 142 | def scroll(self, direction: Literal["up", "down", "left", "right"], amount: int) -> None: 143 | self.ensure_window() 144 | self.browser_window.scroll(direction=direction, amount="line", count=int(amount * self.scale_factor)) 145 | 146 | def send_keystrokes(self, text: str) -> None: 147 | # Unmodifying Pywinauto's modifiers & adding down/up modifiers 148 | modified_text = "" 149 | selective_regex = re.compile(r"{[^{}]*}|.") 150 | 151 | for key in selective_regex.findall(text): 152 | if key in ["+", "^", "%", "~"]: 153 | key = "{" + key + "}" 154 | 155 | modified_text += key 156 | 157 | self.ensure_window() 158 | self.browser_window.send_keystrokes(modified_text) 159 | -------------------------------------------------------------------------------- /cdp_patches/input/sync_input.py: -------------------------------------------------------------------------------- 1 | import platform 2 | import random 3 | import re 4 | import sys 5 | import threading 6 | import time 7 | from typing import Literal, Optional, Union 8 | 9 | if sys.version_info.minor >= 10: 10 | from typing import TypeAlias 11 | else: 12 | TypeAlias = "TypeAlias" # type: ignore[assignment] 13 | 14 | from cdp_patches import is_windows 15 | from cdp_patches.input.exceptions import WindowClosedException 16 | 17 | if is_windows: 18 | from pywinauto.application import ProcessNotFoundError 19 | from pywinauto.base_wrapper import ElementNotEnabled 20 | 21 | from cdp_patches.input.os_base.windows import WindowsBase # type: ignore[assignment] 22 | 23 | LinuxBase: TypeAlias = WindowsBase # type: ignore[no-redef] 24 | InputBase = WindowsBase # type: ignore 25 | WindowErrors = (ValueError, ElementNotEnabled, ProcessNotFoundError, WindowClosedException) # type: ignore[assignment] 26 | else: 27 | from cdp_patches.input.os_base.linux import LinuxBase # type: ignore[assignment] 28 | 29 | WindowsBase: TypeAlias = LinuxBase # type: ignore[no-redef] 30 | InputBase = LinuxBase # type: ignore 31 | WindowErrors = (AssertionError, ValueError, WindowClosedException) # type: ignore[assignment] 32 | 33 | from .browsers import DriverlessSyncChrome, SeleniumChrome, get_sync_browser_pid, get_sync_scale_factor, sync_browsers 34 | from .mouse_trajectory import HumanizeMouseTrajectory 35 | 36 | 37 | class SyncInput: 38 | emulate_behaviour: Optional[bool] = True 39 | pid: Optional[int] 40 | # _base: Union[WindowsBase, LinuxBase] 41 | _base: InputBase 42 | window_timeout: float = 30 43 | _scale_factor: float = 1.0 44 | sleep_timeout: float = 0.01 45 | typing_speed: int = 50 46 | last_x: int = 0 47 | last_y: int = 0 48 | selective_modifiers_regex = re.compile(r"{[^{}]*}|.") 49 | 50 | def __init__( 51 | self, pid: Optional[int] = None, browser: Optional[sync_browsers] = None, scale_factor: Optional[float] = 1.0, emulate_behaviour: Optional[bool] = True, window_timeout: Optional[float] = 30.0 52 | ) -> None: 53 | if platform.system() not in ("Windows", "Linux"): 54 | raise SystemError("Unknown system (You´re probably using MacOS, which is currently not supported).") 55 | 56 | self._scale_factor = scale_factor or self._scale_factor 57 | self.window_timeout = window_timeout or self.window_timeout 58 | self.emulate_behaviour = emulate_behaviour or self.emulate_behaviour 59 | self._move_lock = threading.Lock() 60 | 61 | if browser: 62 | self.pid = get_sync_browser_pid(browser) 63 | self._scale_factor = float(get_sync_scale_factor(browser)) 64 | elif pid: 65 | self.pid = pid 66 | else: 67 | raise ValueError("You must provide a pid or a browser") 68 | 69 | self._base = InputBase(self.pid, self._scale_factor) # type: ignore 70 | self._wait_for_window() 71 | 72 | # Include Windows Scale Factor for every browser except DriverlessSyncChrome 73 | if is_windows and not isinstance(browser, (DriverlessSyncChrome, SeleniumChrome)): 74 | self._base.include_windows_scale_factor() 75 | 76 | @property 77 | def base(self) -> Union[WindowsBase, LinuxBase]: 78 | return self._base 79 | 80 | @property 81 | def scale_factor(self) -> float: 82 | return self._scale_factor 83 | 84 | @scale_factor.setter 85 | def scale_factor(self, scale_value: float) -> None: 86 | self._scale_factor = scale_value 87 | if self._base: 88 | self._base.scale_factor = scale_value 89 | 90 | def _wait_for_window(self) -> None: 91 | max_wait = time.perf_counter() + self.window_timeout 92 | while time.perf_counter() < max_wait: 93 | try: 94 | if self._base.get_window(): 95 | return 96 | except WindowErrors: 97 | pass 98 | self._sleep_timeout(0.1) 99 | 100 | raise TimeoutError(f"Chrome Window (PID: {self.pid}) not found in {self.window_timeout} seconds.") 101 | 102 | def _sleep_timeout(self, timeout: Optional[float] = None) -> None: 103 | timeout = timeout or self.sleep_timeout 104 | # if not random.randint(0, 10): 105 | # timeout_random = timeout / 10 106 | # timeout = random.uniform(timeout - timeout_random, timeout + timeout_random) 107 | 108 | time.sleep(timeout) 109 | # Perfect Precise Sleep 110 | # start = time.perf_counter() 111 | # while time.perf_counter() - start < timeout: 112 | # pass 113 | 114 | def click(self, button: Literal["left", "right", "middle"], x: Union[int, float], y: Union[int, float], emulate_behaviour: Optional[bool] = True, timeout: Optional[float] = 0.07) -> None: 115 | x, y = int(x), int(y) 116 | 117 | self.down(button=button, x=x, y=y, emulate_behaviour=emulate_behaviour, timeout=timeout) 118 | if self.emulate_behaviour and emulate_behaviour: 119 | self._sleep_timeout(timeout=timeout) 120 | self.up(button=button, x=x, y=y) 121 | self.last_x, self.last_y = x, y 122 | 123 | def double_click(self, button: Literal["left", "right", "middle"], x: Union[int, float], y: Union[int, float], emulate_behaviour: Optional[bool] = True, timeout: Optional[float] = None) -> None: 124 | x, y = int(x), int(y) 125 | 126 | self.click(button=button, x=x, y=y, timeout=timeout, emulate_behaviour=emulate_behaviour) 127 | if emulate_behaviour and self.emulate_behaviour: 128 | self._sleep_timeout(random.uniform(0.14, 0.21)) 129 | # self._sleep_timeout(timeout=timeout) 130 | self.click(button=button, x=x, y=y, emulate_behaviour=False, timeout=timeout) 131 | 132 | self.last_x, self.last_y = x, y 133 | 134 | def down(self, button: Literal["left", "right", "middle"], x: Union[int, float], y: Union[int, float], emulate_behaviour: Optional[bool] = True, timeout: Optional[float] = None) -> None: 135 | x, y = int(x), int(y) 136 | 137 | if self.emulate_behaviour and emulate_behaviour: 138 | self.move(x=x, y=y, emulate_behaviour=emulate_behaviour, timeout=timeout) 139 | self._base.down(button=button, x=x, y=y) 140 | self.last_x, self.last_y = x, y 141 | 142 | def up(self, button: Literal["left", "right", "middle"], x: Union[int, float], y: Union[int, float]) -> None: 143 | x, y = int(x), int(y) 144 | 145 | self._base.up(button=button, x=x, y=y) 146 | self.last_x, self.last_y = x, y 147 | 148 | def move(self, x: Union[int, float], y: Union[int, float], emulate_behaviour: Optional[bool] = True, timeout: Optional[float] = None) -> None: 149 | with self._move_lock: 150 | x, y = int(x), int(y) 151 | 152 | if self.emulate_behaviour and emulate_behaviour: 153 | humanized_points = HumanizeMouseTrajectory((self.last_x, self.last_y), (x, y)) 154 | 155 | # Move Mouse to new random locations 156 | for i, (human_x, human_y) in enumerate(humanized_points.points): 157 | self._base.move(x=int(human_x), y=int(human_y)) 158 | self._sleep_timeout(timeout=timeout) 159 | 160 | self._base.move(x=x, y=y) 161 | self.last_x, self.last_y = x, y 162 | 163 | def scroll(self, direction: Literal["up", "down", "left", "right"], amount: int) -> None: 164 | self._base.scroll(direction=direction, amount=amount) 165 | 166 | def type(self, text: str, fill: Optional[bool] = False, timeout: Optional[float] = None) -> None: 167 | if self.emulate_behaviour and not fill: 168 | for i, char in enumerate(self.selective_modifiers_regex.findall(text)): 169 | # If new word is started wait some more time 170 | if i != 0 and text[i - 1] == " ": 171 | self._sleep_timeout(timeout=timeout) 172 | 173 | self._base.send_keystrokes(char) 174 | self._sleep_timeout((random.random() * 10) / self.typing_speed) 175 | else: 176 | self._base.send_keystrokes(text) 177 | -------------------------------------------------------------------------------- /docs/CONTRIBUTING.md: -------------------------------------------------------------------------------- 1 | # How to develop on this project 2 | 3 | Botright welcomes contributions from the community. 4 | 5 | **You need PYTHON3!** 6 | 7 | This instructions are for linux base systems. (Linux, MacOS, BSD, etc.) 8 | ## Setting up your own fork of this repo. 9 | 10 | - On github interface click on `Fork` button. 11 | - Clone your fork of this repo. `git clone git@github.com:YOUR_GIT_USERNAME/botright.git` 12 | - Enter the directory `cd botright` 13 | - Add upstream repo `git remote add upstream https://github.com/Vinyzu/botright` 14 | 15 | ## Setting up your own virtual environment 16 | 17 | Run `make virtualenv` to create a virtual environment. 18 | then activate it with `source .venv/bin/activate`. 19 | 20 | ## Install the project in develop mode 21 | 22 | Run `make install` to install the project in develop mode. 23 | 24 | ## Run the tests to ensure everything is working 25 | 26 | Run `make test` to run the tests. 27 | 28 | ## Create a new branch to work on your contribution 29 | 30 | Run `git checkout -b my_contribution` 31 | 32 | ## Make your changes 33 | 34 | Edit the files using your preferred editor. (we recommend VIM or VSCode) 35 | 36 | ## Format the code 37 | 38 | Run `make fmt` to format the code. 39 | 40 | ## Run the linter 41 | 42 | Run `make lint` to run the linter. 43 | 44 | ## Test your changes 45 | 46 | Run `make test` to run the tests. 47 | 48 | Ensure code coverage report shows `100%` coverage, add tests to your PR. 49 | 50 | ## Build the docs locally 51 | 52 | Run `make docs` to build the docs. 53 | 54 | Ensure your new changes are documented. 55 | 56 | ## Commit your changes 57 | 58 | This project uses [conventional git commit messages](https://www.conventionalcommits.org/en/v1.0.0/). 59 | 60 | Example: `fix(package): update setup.py arguments 🎉` (emojis are fine too) 61 | 62 | ## Push your changes to your fork 63 | 64 | Run `git push origin my_contribution` 65 | 66 | ## Submit a pull request 67 | 68 | On github interface, click on `Pull Request` button. 69 | 70 | Wait CI to run and one of the developers will review your PR. 71 | ## Makefile utilities 72 | 73 | This project comes with a `Makefile` that contains a number of useful utility. 74 | 75 | ```bash 76 | ❯ make 77 | Usage: make 78 | 79 | Targets: 80 | help: ## Show the help. 81 | install: ## Install the project in dev mode. 82 | fmt: ## Format code using black & isort. 83 | lint: ## Run pep8, black, mypy linters. 84 | test: lint ## Run tests and generate coverage report. 85 | watch: ## Run tests on every change. 86 | clean: ## Clean unused files. 87 | virtualenv: ## Create a virtual environment. 88 | release: ## Create a new tag for release. 89 | docs: ## Build the documentation. 90 | switch-to-poetry: ## Switch to poetry package manager. 91 | init: ## Initialize the project based on an application template. 92 | ``` 93 | 94 | ## Making a new release 95 | 96 | This project uses [semantic versioning](https://semver.org/) and tags releases with `X.Y.Z` 97 | Every time a new tag is created and pushed to the remote repo, github actions will 98 | automatically create a new release on github and trigger a release on PyPI. 99 | 100 | For this to work you need to setup a secret called `PIPY_API_TOKEN` on the project settings>secrets, 101 | this token can be generated on [pypi.org](https://pypi.org/account/). 102 | 103 | To trigger a new release all you need to do is. 104 | 105 | 1. If you have changes to add to the repo 106 | * Make your changes following the steps described above. 107 | * Commit your changes following the [conventional git commit messages](https://www.conventionalcommits.org/en/v1.0.0/). 108 | 2. Run the tests to ensure everything is working. 109 | 4. Run `make release` to create a new tag and push it to the remote repo. 110 | 111 | the `make release` will ask you the version number to create the tag, ex: type `0.1.1` when you are asked. 112 | 113 | > **CAUTION**: The make release will change local changelog files and commit all the unstaged changes you have. -------------------------------------------------------------------------------- /docs/HISTORY.md: -------------------------------------------------------------------------------- 1 | Changelog 2 | ========= 3 | 4 | 1.1 (2024-04-24) 5 | ------------------ 6 | - Official Support Scrolling. [Vinyzu] 7 | - Change Exception to Warning when importing CDP-Patches on MacOS. [Vinyzu] 8 | - Fix Documentation. [Vinyzu] 9 | 10 | 1.0(2024-03-23) 11 | ------------------ 12 | - Initial Release. [Vinyzu] -------------------------------------------------------------------------------- /docs/gitbook/README.md: -------------------------------------------------------------------------------- 1 | --- 2 | description: Welcome to CDP-Patches! 3 | --- 4 | 5 | # Installation 6 | 7 | 8 | 9 | ## Install it from PyPI [![PyPI version](https://img.shields.io/pypi/v/cdp-patches.svg)](https://pypi.org/project/cdp-patches/) 10 | 11 | ```bash 12 | pip install cdp-patches 13 | ``` 14 | 15 |
16 | 17 | Or for Full Linting 18 | 19 | #### (Includes: playwright, botright, selenium, selenium\_driverless) 20 | 21 | ```bash 22 | pip install cdp-patches[automation_linting] 23 | ``` 24 | 25 |
26 | 27 | *** 28 | 29 | ## Leak Patches 30 | 31 |
32 | 33 | Input Package 34 | 35 | ## First Script 36 | 37 | ### Sync Usage 38 | 39 | ```python 40 | from cdp_patches.input import SyncInput 41 | 42 | sync_input = SyncInput(pid=pid) 43 | # Or 44 | sync_input = SyncInput(browser=browser) 45 | 46 | # Dispatch Inputs 47 | sync_input.click("left", 100, 100) # Left click at (100, 100) 48 | sync_input.double_click("left", 100, 100) # Left double-click at (100, 100) 49 | sync_input.down("left", 100, 100) # Left mouse button down at (100, 100) 50 | sync_input.up("left", 100, 100) # Left mouse button up at (100, 100) 51 | sync_input.move(100, 100) # Move mouse to (100, 100) 52 | sync_input.scroll("down", 10) # Scroll down by 10 lines 53 | sync_input.type("Hello World!") # Type "Hello WorldS 54 | ``` 55 | 56 | [sync-usage.md](input/sync-usage.md "mention") 57 | 58 | *** 59 | 60 | ### Async Usage 61 | 62 | ```python 63 | import asyncio 64 | 65 | from cdp_patches.input import AsyncInput 66 | 67 | async def main(): 68 | async_input = await AsyncInput(pid=pid) 69 | # Or 70 | async_input = await AsyncInput(browser=browser) 71 | 72 | # Dispatch Inputs 73 | await async_input.click("left", 100, 100) # Left click at (100, 100) 74 | await async_input.double_click("left", 100, 100) # Left double-click at (100, 100) 75 | await async_input.down("left", 100, 100) # Left mouse button down at (100, 100) 76 | await async_input.up("left", 100, 100) # Left mouse button up at (100, 100) 77 | await async_input.move(100, 100) # Move mouse to (100, 100) 78 | await async_input.scroll("down", 10) # Scroll down by 10 lines 79 | await async_input.type("Hello World!") # Type "Hello World!" 80 | 81 | if __name__ == '__main__': 82 | asyncio.run(main()) 83 | ``` 84 | 85 | [async-usage.md](input/async-usage.md "mention") 86 | 87 | *** 88 | 89 | ### Usage with Selenium 90 | 91 | [selenium-usage.md](input/selenium-usage.md "mention") 92 | 93 | ### Usage with Playwright 94 | 95 | [playwright-usage.md](input/playwright-usage.md "mention") 96 | 97 |
98 | 99 | -------------------------------------------------------------------------------- /docs/gitbook/SUMMARY.md: -------------------------------------------------------------------------------- 1 | # Table of contents 2 | 3 | * [Installation](README.md) 4 | * [Input](input/README.md) 5 | * [Sync Usage](input/sync-usage.md) 6 | * [Async Usage](input/async-usage.md) 7 | * [Selenium Usage](input/selenium-usage.md) 8 | * [Playwright Usage](input/playwright-usage.md) 9 | -------------------------------------------------------------------------------- /docs/gitbook/input/README.md: -------------------------------------------------------------------------------- 1 | # Input 2 | 3 | ## Concept: Input Domain Leaks 4 | 5 | Bypass CDP-Leaks in [Input](https://chromedevtools.github.io/devtools-protocol/tot/Input/) domains.\ 6 | For an interaction event _`e`_, the page coordinates won't ever equal the screen coordinates, unless Chrome is in full-screen. \ 7 | However, all CDP input commands just set it the same by default (see [crbug#1477537](https://bugs.chromium.org/p/chromium/issues/detail?id=1477537)).\ 8 | 9 | 10 | ```javascript 11 | var is_bot = (e.pageY == e.screenY && e.pageX == e.screenX) 12 | if (is_bot && 1 >= outerHeight - innerHeight){ // fullscreen 13 | is_bot = false 14 | } 15 | ``` 16 | 17 | 18 | 19 | *** 20 | 21 | {% hint style="warning" %} 22 | #### Because Chrome does not recognize Input Events to specific tabs, these methods can only be used on the active tab. 23 | 24 | #### Chrome Tabs do have their own process with a process id (PID), but these can not be controlled using Input Events as they´re just engines. 25 | {% endhint %} 26 | 27 | {% hint style="warning" %} 28 | #### Pressing SHIFT or CAPSLOCK manually on Windows affects `input.type(text)` as well. 29 | {% endhint %} 30 | 31 | *** 32 | 33 | **Owner**: [Vinyzu](https://github.com/Vinyzu/)\ 34 | **Co-Maintainer**: [Kaliiiiiiiiii](https://github.com/kaliiiiiiiiii/) 35 | 36 | 37 | 38 | -------------------------------------------------------------------------------- /docs/gitbook/input/async-usage.md: -------------------------------------------------------------------------------- 1 | # Async Usage 2 | 3 | 4 | 5 | ## AsyncInput 6 | 7 | > ### `await cdp_patches.input.AsyncInput()` 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 |
KwargsTypeUsageDefaults
pidint 23 |

The Main Chrome Browser Window PID to connect to. Can also be found in Chromes Task-Manager (Shift+Esc).

24 |

25 |
None
browserasync_browsersAn Async Browser Instance. Can be any of:
(sd = selenium_driverless)
(pw = playwright)
sd.webdriver.Chrome,
pw.async_api.Browser, pw.async_api.BrowserContext
None
scale_factorfloatThe Scaling Factor of the Browser. If a browser Instance is passed, this value gets determined automatically.1.0
emulate_behaviourboolWhether to emulate human behaviour.True
window_timeoutfloatTimeout to wait for a window to be recognized. In Seconds.30
54 | 55 | *** 56 | 57 | ### AsyncInput Properties 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | 94 | 95 | 96 | 97 | 98 | 99 | 100 | 101 | 102 | 103 | 104 | 105 | 106 | 107 | 108 | 109 | 110 | 111 | 112 | 113 | 114 | 115 |
PropertyTypeUsageDefaults
emulate_behaviourboolWhether to emulate human behaviour.True
window_timeoutint 79 |

The Time of how long to search for the Window in seconds.

80 |

81 |
30
baseWindowsBase |
LinuxBase
The Base Interaction Layer. Can be useful in some special cases in which emulate_behaviour isnt sufficient. Only Readable.None
scale_factorfloatThe Scaling Factor of the Browser. If a browserInstance is passed, this value gets determined automatically.1.0
sleep_timeoutfloatHow long to sleep after certain actions, for example in between a double-click. In Seconds.0.01
window_timeoutfloatTimeout to wait for a window to be recognized. In Seconds.30
typing_speedintHow fast to type in WPM.50
116 | 117 | *** 118 | 119 | ### AsyncInput Methods 120 | 121 |
# Type Abbreviations
122 | Pos = Union[int, float]
123 | Button = Literal["left", "right", "middle"]
124 | EmulateBehaviour: Optional[bool] = True
125 | Timeout: Optional[float] = Non
126 | 
127 | # Click at the given coordinates with the given button
128 | await async_input.click(button: Button, x: Pos, y: Pos, emulate_behaviour: EmulateBehaviour, timeout: Timeout)
129 | 
130 | # Double-Click at the given coordinates with the given button
131 | await async_input.double_click(button: Button, x: Pos, y: Pos, emulate_behaviour: EmulateBehaviour, timeout: Timeout)
132 | 
133 | # Mouse-Down at the given coordinates with the given button
134 | await async_input.down(button: Button, x: Pos, y: Pos, emulate_behaviour: EmulateBehaviour, timeout: Timeout)
135 | 
136 | # Mouse-Up at the given coordinates with the given button
137 | await async_input.up(button: Button, x: Pos, y: Pos)
138 | 
139 | # Mouse-Move to the given coordinates
140 | await async_input.move(x: Pos, y: Pos, emulate_behaviour: EmulateBehaviour, timeout: Timeout)
141 | 
142 | # Scroll the page in the given direction by the given amount
143 | await async_input.scroll(direction: Literal["up", "down", "left", "right"], amount: int)
144 | 
145 | # Type the given text and optionally fill the input field (Like pasting)
146 | await async_input.type(text: str, fill: Optional[bool] = False, timeout: Timeout)
147 | 
148 | 149 | -------------------------------------------------------------------------------- /docs/gitbook/input/playwright-usage.md: -------------------------------------------------------------------------------- 1 | # Playwright Usage 2 | 3 | 4 | 5 | ## Sync Usage (Sync Playwright) 6 | 7 | ```python 8 | from playwright.sync_api import sync_playwright, Locator 9 | from cdp_patches.input import SyncInput 10 | 11 | # Locator Position Helper 12 | def get_locator_pos(locator: Locator): 13 | bounding_box = locator.bounding_box() 14 | assert bounding_box 15 | 16 | x, y, width, height = bounding_box.get("x"), bounding_box.get("y"), bounding_box.get("width"), bounding_box.get("height") 17 | assert x and y and width and height 18 | 19 | x, y = x + width // 2, y + height // 2 20 | return x, y 21 | 22 | with sync_playwright() as playwright: 23 | browser = playwright.chromium.launch() 24 | page = browser.new_page() 25 | sync_input = SyncInput(browser=browser) # Also works with Contexts 26 | 27 | # Example: Click Button 28 | # Find Button Coords 29 | locator = page.locator("button") 30 | x, y = get_locator_pos(locator) 31 | # Click Coords => Click Button 32 | sync_input.click("left", x, y) 33 | ``` 34 | 35 | ## Async Usage (Async Playwright / Botright) 36 | 37 | ```python 38 | import asyncio 39 | 40 | from playwright.async_api import async_playwright, Locator 41 | from cdp_patches.input import AsyncInput 42 | 43 | 44 | # Locator Position Helper 45 | async def get_locator_pos(locator: Locator): 46 | bounding_box = await locator.bounding_box() 47 | assert bounding_box 48 | 49 | 50 | x, y, width, height = bounding_box.get("x"), bounding_box.get("y"), bounding_box.get("width"), bounding_box.get("height") 51 | 52 | assert x and y and width and height 53 | 54 | x, y = x + width // 2, y + height // 2 55 | return x, y 56 | 57 | 58 | async def main(): 59 | async with async_playwright() as playwright: 60 | browser = await playwright.chromium.launch() 61 | page = await browser.new_page() 62 | async_input = await AsyncInput(browser=browser) # Also works with Contexts 63 | 64 | # Example: Click Button 65 | # Find Button Coords 66 | locator = page.locator("button") 67 | x, y = await get_locator_pos(locator) 68 | # Click Coords => Click Button 69 | await async_input.click("left", x, y) 70 | 71 | 72 | asyncio.run(main()) 73 | ``` 74 | -------------------------------------------------------------------------------- /docs/gitbook/input/selenium-usage.md: -------------------------------------------------------------------------------- 1 | # Selenium Usage 2 | 3 | 4 | 5 | ## Sync Usage (Selenium) 6 | 7 | ```python 8 | from selenium import webdriver 9 | from selenium.webdriver.remote.webelement import WebElement 10 | from selenium.webdriver.common.by import By 11 | from cdp_patches.input import SyncInput 12 | 13 | # Locator Position Helper 14 | def get_locator_pos(locator: WebElement): 15 | location = locator.location 16 | size = locator.size 17 | assert location, size 18 | 19 | x, y, width, height = location.get("x"), location.get("y"), size.get("width"), size.get("height") 20 | assert x and y and width and height 21 | 22 | x, y = x + width // 2, y + height // 2 23 | return x, y 24 | 25 | options = webdriver.ChromeOptions() 26 | # disable logs & automation 27 | options.add_experimental_option("excludeSwitches", ["enable-logging", "enable-automation"]) 28 | options.add_experimental_option("useAutomationExtension", False) 29 | options.add_argument("--log-level=3") 30 | 31 | with webdriver.Chrome(...) as driver: 32 | sync_input = SyncInput(browser=driver) 33 | 34 | # Example: Click Button 35 | # Find Button Coords 36 | locator = driver.find_element(By.XPATH, "//button") 37 | x, y = get_locator_pos(locator) 38 | # Click Coords => Click Button 39 | sync_input.click("left", x, y) 40 | ``` 41 | 42 | *** 43 | 44 | ## Async Usage (Async Selenium-Driverless) 45 | 46 | ```python 47 | import asyncio 48 | from selenium_driverless import webdriver 49 | from selenium_driverless.types.by import By 50 | from cdp_patches.input import AsyncInput 51 | 52 | async def main(): 53 | async with webdriver.Chrome(...) as driver: 54 | async_input = await AsyncInput(browser=driver) 55 | 56 | # Example: Click Button 57 | # Find Button Coords 58 | locator = await driver.find_element(By.XPATH, "//button") 59 | x, y = await locator.mid_location() 60 | # Click Coords => Click Button 61 | await async_input.click("left", x, y) 62 | 63 | asyncio.run(main()) 64 | ``` 65 | -------------------------------------------------------------------------------- /docs/gitbook/input/sync-usage.md: -------------------------------------------------------------------------------- 1 | # Sync Usage 2 | 3 | 4 | 5 | ## SyncInput 6 | 7 | > ### `cdp_patches.input.SyncInput()` 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 |
KwargsTypeUsageDefaults
pidint 23 |

The Main Chrome Browser Window PID to connect to. Can also be found in Chromes Task-Manager (Shift+Esc).

24 |

25 |
None
browsersync_browsersA Sync Browser Instance. Can be any of:
(sd = selenium_driverless)
(pw = playwright)
selenium.webdriver.Chrome, sd.sync.webdriver.Chrome,
pw.sync_api.Browser, pw.sync_api.BrowserContext
None
scale_factorfloatThe Scaling Factor of the Browser. If a browser Instance is passed, this value gets determined automatically.1.0
emulate_behaviourboolWhether to emulate human behaviour.True
window_timeoutfloatTimeout to wait for a window to be recognized. In Seconds.30
54 | 55 | *** 56 | 57 | ### SyncInput Properties 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | 94 | 95 | 96 | 97 | 98 | 99 | 100 | 101 | 102 | 103 | 104 | 105 | 106 | 107 | 108 | 109 | 110 | 111 | 112 | 113 | 114 |
PropertyTypeUsageDefaults
emulate_behaviourboolWhether to emulate human behaviour.True
window_timeoutint 79 |

The Time of how long to search for the Window in seconds.

80 |

81 |
30
baseWindowsBase |
LinuxBase
The Base Interaction Layer. Can be useful in some special cases in which emulate_behaviour isnt sufficient. Only Readable.None
scale_factorfloatThe Scaling Factor of the Browser. If a browserInstance is passed, this value gets determined automatically.1.0
sleep_timeoutfloatHow long to sleep after certain actions, for example in between a double-click. In Seconds.0.01
window_timeoutfloatTimeout to wait for a window to be recognized. In Seconds.30
typing_speedintHow fast to type in WPM.50
115 | 116 | *** 117 | 118 | ### SyncInput Methods 119 | 120 | {% code fullWidth="true" %} 121 | ```python 122 | # Type Abbreviations 123 | Pos = Union[int, float] 124 | Button = Literal["left", "right", "middle"] 125 | EmulateBehaviour: Optional[bool] = True 126 | Timeout: Optional[float] = Non 127 | 128 | # Click at the given coordinates with the given button 129 | sync_input.click(button: Button, x: Pos, y: Pos, emulate_behaviour: EmulateBehaviour, timeout: Timeout) 130 | 131 | # Double-Click at the given coordinates with the given button 132 | sync_input.double_click(button: Button, x: Pos, y: Pos, emulate_behaviour: EmulateBehaviour, timeout: Timeout) 133 | 134 | # Mouse-Down at the given coordinates with the given button 135 | sync_input.down(button: Button, x: Pos, y: Pos, emulate_behaviour: EmulateBehaviour, timeout: Timeout) 136 | 137 | # Mouse-Up at the given coordinates with the given button 138 | sync_input.up(button: Button, x: Pos, y: Pos) 139 | 140 | # Mouse-Move to the given coordinates 141 | sync_input.move(x: Pos, y: Pos, emulate_behaviour: EmulateBehaviour, timeout: Timeout) 142 | 143 | # Scroll the page in the given direction by the given amount 144 | sync_input.scroll(direction: Literal["up", "down", "left", "right"], amount: int) 145 | 146 | # Type the given text and optionally fill the input field (Like pasting) 147 | sync_input.type(text: str, fill: Optional[bool] = False, timeout: Timeout) 148 | ``` 149 | {% endcode %} 150 | 151 | -------------------------------------------------------------------------------- /py.typed: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kaliiiiiiiiii-Vinyzu/CDP-Patches/602b680abf50b8190a5955ec7feb8c2045002e8d/py.typed -------------------------------------------------------------------------------- /pyproject.toml: -------------------------------------------------------------------------------- 1 | [build-system] 2 | requires = ["setuptools>=68.0", "wheel"] 3 | build-backend = "setuptools.build_meta" 4 | 5 | [tool.pytest.ini_options] 6 | testpaths = [ 7 | "tests", 8 | ] 9 | filterwarnings = [ 10 | "ignore::DeprecationWarning", 11 | ] 12 | 13 | [tool.mypy] 14 | mypy_path = "botright" 15 | check_untyped_defs = true 16 | disallow_any_generics = true 17 | ignore_missing_imports = true 18 | no_implicit_optional = true 19 | show_error_codes = true 20 | strict_equality = true 21 | warn_redundant_casts = true 22 | warn_return_any = true 23 | warn_unreachable = true 24 | warn_unused_configs = true 25 | no_implicit_reexport = true 26 | disable_error_code = "method-assign" 27 | 28 | [tool.black] 29 | line-length = 200 30 | 31 | [tool.isort] 32 | py_version = 310 33 | line_length = 200 34 | multi_line_output = 7 -------------------------------------------------------------------------------- /requirements-test.txt: -------------------------------------------------------------------------------- 1 | # This requirements are for development and testing only, not for production. 2 | flake8==7.1.0 3 | pytest==8.2.2 4 | pytest_asyncio==0.23.7 5 | mypy==1.10.1 6 | types-setuptools==69.5.* 7 | black==24.4.2 8 | isort==5.13.2 9 | playwright==1.44.0 10 | selenium==4.22.0 11 | selenium_driverless==1.9.3.1 12 | twisted==24.3.0 13 | types-requests==2.31.* 14 | webdriver_manager==4.0.1 -------------------------------------------------------------------------------- /requirements.txt: -------------------------------------------------------------------------------- 1 | numpy==1.26.4 2 | pywinauto==0.6.8 3 | requests==2.31.0 4 | websockets==12.0 5 | python-xlib==0.33 -------------------------------------------------------------------------------- /setup.cfg: -------------------------------------------------------------------------------- 1 | [metadata] 2 | name = cdp_patches 3 | version = attr: cdp_patches.VERSION 4 | description = Patching CDP (Chrome DevTools Protocol) leaks on OS level. Easy to use with Playwright, Selenium, and other web automation tools. 5 | long_description = file: README.md 6 | long_description_content_type = text/markdown 7 | author = Vinyzu, Kaliiiiiiiiii 8 | url = https://github.com/Kaliiiiiiiiii-Vinyzu/CDP-Patches/ 9 | license = GNU General Public License v3.0 10 | license_file = LICENSE 11 | keywords = botright, playwright, browser, automation, fingerprints, fingerprinting, dataset, data, selenium, chrome, patching, web-automation 12 | classifiers = 13 | Topic :: Scientific/Engineering 14 | Topic :: Scientific/Engineering :: Artificial Intelligence 15 | Topic :: Software Development 16 | Topic :: Software Development :: Libraries 17 | Topic :: Software Development :: Libraries :: Python Modules 18 | Topic :: Internet :: WWW/HTTP :: Browsers 19 | License :: OSI Approved :: Apache Software License 20 | Programming Language :: Python :: 3 21 | 22 | [options] 23 | zip_safe = no 24 | python_requires = >=3.8 25 | packages = find: 26 | install_requires = 27 | numpy 28 | websockets 29 | requests 30 | pywinauto; platform_system=='Windows' 31 | python-xlib; platform_system=='Linux' 32 | 33 | 34 | [options.package_data] 35 | * = requirements.txt 36 | 37 | [options.packages.find] 38 | include = cdp_patches, cdp_patches.*, LICENSE 39 | exclude = tests, .github 40 | 41 | [options.extras_require] 42 | automation_linting = 43 | playwright 44 | botright 45 | selenium 46 | selenium_driverless 47 | webdriver-manager 48 | testing = 49 | pytest 50 | mypy 51 | flake8 52 | tox 53 | types-requests 54 | playwright 55 | botright 56 | selenium 57 | selenium_driverless 58 | webdriver-manager 59 | -------------------------------------------------------------------------------- /tests/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kaliiiiiiiiii-Vinyzu/CDP-Patches/602b680abf50b8190a5955ec7feb8c2045002e8d/tests/__init__.py -------------------------------------------------------------------------------- /tests/assets/input/button.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Button test 5 | 6 | 7 | 8 | 9 | 31 | 32 | -------------------------------------------------------------------------------- /tests/assets/input/keyboard.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Keyboard test 5 | 6 | 7 | 8 | 41 | 42 | -------------------------------------------------------------------------------- /tests/assets/input/mouse-helper.js: -------------------------------------------------------------------------------- 1 | // This injects a box into the page that moves with the mouse; 2 | // Useful for debugging 3 | (function(){ 4 | const box = document.createElement('div'); 5 | box.classList.add('mouse-helper'); 6 | const styleElement = document.createElement('style'); 7 | styleElement.innerHTML = ` 8 | .mouse-helper { 9 | pointer-events: none; 10 | position: absolute; 11 | top: 0; 12 | left: 0; 13 | width: 20px; 14 | height: 20px; 15 | background: rgba(0,0,0,.4); 16 | border: 1px solid white; 17 | border-radius: 10px; 18 | margin-left: -10px; 19 | margin-top: -10px; 20 | transition: background .2s, border-radius .2s, border-color .2s; 21 | } 22 | .mouse-helper.button-1 { 23 | transition: none; 24 | background: rgba(0,0,0,0.9); 25 | } 26 | .mouse-helper.button-2 { 27 | transition: none; 28 | border-color: rgba(0,0,255,0.9); 29 | } 30 | .mouse-helper.button-3 { 31 | transition: none; 32 | border-radius: 4px; 33 | } 34 | .mouse-helper.button-4 { 35 | transition: none; 36 | border-color: rgba(255,0,0,0.9); 37 | } 38 | .mouse-helper.button-5 { 39 | transition: none; 40 | border-color: rgba(0,255,0,0.9); 41 | } 42 | `; 43 | document.head.appendChild(styleElement); 44 | document.body.appendChild(box); 45 | document.addEventListener('mousemove', event => { 46 | box.style.left = event.pageX + 'px'; 47 | box.style.top = event.pageY + 'px'; 48 | updateButtons(event.buttons); 49 | }, true); 50 | document.addEventListener('mousedown', event => { 51 | updateButtons(event.buttons); 52 | box.classList.add('button-' + event.which); 53 | }, true); 54 | document.addEventListener('mouseup', event => { 55 | updateButtons(event.buttons); 56 | box.classList.remove('button-' + event.which); 57 | }, true); 58 | function updateButtons(buttons) { 59 | for (let i = 0; i < 5; i++) 60 | box.classList.toggle('button-' + i, buttons & (1 << i)); 61 | } 62 | })(); -------------------------------------------------------------------------------- /tests/assets/input/scrollable.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Scrollable test 5 | 6 | 7 | 8 | 22 | 23 | -------------------------------------------------------------------------------- /tests/assets/input/textarea.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Textarea test 5 | 6 | 7 | 8 | 9 |
10 |
Plain div
11 | 12 | 19 | 20 | -------------------------------------------------------------------------------- /tests/assets/offscreenbuttons.html: -------------------------------------------------------------------------------- 1 | 37 |
38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 |
50 | -------------------------------------------------------------------------------- /tests/conftest.py: -------------------------------------------------------------------------------- 1 | from typing import AsyncGenerator, Generator, List 2 | 3 | import pytest 4 | import pytest_asyncio 5 | from playwright.async_api import Page as AsyncPage 6 | from playwright.async_api import async_playwright 7 | from playwright.sync_api import Page as SyncPage 8 | from playwright.sync_api import sync_playwright 9 | from selenium import webdriver as selenium_webdriver 10 | from selenium.webdriver.chrome.service import Service as SeleniumChromeService 11 | from selenium_driverless import webdriver as async_webdriver 12 | from selenium_driverless.sync import webdriver as sync_webdriver 13 | from webdriver_manager.chrome import ChromeDriverManager 14 | 15 | from cdp_patches.input import AsyncInput, SyncInput 16 | 17 | from .server import Server, test_server 18 | 19 | flags: List[str] = [ 20 | "--incognito", 21 | "--accept-lang=en-US", 22 | "--lang=en-US", 23 | "--no-pings", 24 | "--mute-audio", 25 | "--no-first-run", 26 | "--no-default-browser-check", 27 | "--disable-cloud-import", 28 | "--disable-gesture-typing", 29 | "--disable-offer-store-unmasked-wallet-cards", 30 | "--disable-offer-upload-credit-cards", 31 | "--disable-print-preview", 32 | "--disable-voice-input", 33 | "--disable-wake-on-wifi", 34 | "--disable-cookie-encryption", 35 | "--ignore-gpu-blocklist", 36 | "--enable-async-dns", 37 | "--enable-simple-cache-backend", 38 | "--enable-tcp-fast-open", 39 | "--prerender-from-omnibox=disabled", 40 | "--enable-web-bluetooth", 41 | "--disable-features=AudioServiceOutOfProcess,IsolateOrigins,site-per-process,TranslateUI,BlinkGenPropertyTrees", 42 | "--aggressive-cache-discard", 43 | "--disable-extensions", 44 | "--disable-ipc-flooding-protection", 45 | "--disable-blink-features=AutomationControlled", 46 | "--test-type", 47 | "--enable-features=NetworkService,NetworkServiceInProcess,TrustTokens,TrustTokensAlwaysAllowIssuance", 48 | "--disable-component-extensions-with-background-pages", 49 | "--disable-default-apps", 50 | "--disable-breakpad", 51 | "--disable-component-update", 52 | "--disable-domain-reliability", 53 | "--disable-sync", 54 | "--disable-client-side-phishing-detection", 55 | "--disable-hang-monitor", 56 | "--disable-popup-blocking", 57 | "--disable-prompt-on-repost", 58 | "--metrics-recording-only", 59 | "--safebrowsing-disable-auto-update", 60 | "--password-store=basic", 61 | "--autoplay-policy=no-user-gesture-required", 62 | "--use-mock-keychain", 63 | "--force-webrtc-ip-handling-policy=disable_non_proxied_udp", 64 | "--webrtc-ip-handling-policy=disable_non_proxied_udp", 65 | "--disable-session-crashed-bubble", 66 | "--disable-crash-reporter", 67 | "--disable-dev-shm-usage", 68 | "--force-color-profile=srgb", 69 | "--disable-translate", 70 | "--disable-background-networking", 71 | "--disable-background-timer-throttling", 72 | "--disable-backgrounding-occluded-windows", 73 | "--disable-infobars", 74 | "--hide-scrollbars", 75 | "--disable-renderer-backgrounding", 76 | "--font-render-hinting=none", 77 | "--disable-logging", 78 | "--enable-surface-synchronization", 79 | "--run-all-compositor-stages-before-draw", 80 | "--disable-threaded-animation", 81 | "--disable-threaded-scrolling", 82 | "--disable-checker-imaging", 83 | "--disable-new-content-rendering-timeout", 84 | "--disable-image-animation-resync", 85 | "--disable-partial-raster", 86 | "--blink-settings=primaryHoverType=2,availableHoverTypes=2," "primaryPointerType=4,availablePointerTypes=4", 87 | "--disable-layer-tree-host-memory-pressure", 88 | ] 89 | 90 | 91 | @pytest_asyncio.fixture(autouse=True, scope="session") 92 | def run_around_tests(): 93 | test_server.start() 94 | yield 95 | test_server.stop() 96 | 97 | 98 | @pytest_asyncio.fixture 99 | def server() -> Generator[Server, None, None]: 100 | yield test_server.server 101 | 102 | 103 | @pytest.fixture 104 | def sync_page() -> Generator[SyncPage, None, None]: 105 | with sync_playwright() as p: 106 | browser = p.chromium.launch(headless=False, args=flags) 107 | context = browser.new_context(locale="en-US") 108 | page = context.new_page() 109 | page.sync_input = SyncInput(browser=context) # type: ignore[attr-defined] 110 | 111 | yield page 112 | 113 | 114 | @pytest_asyncio.fixture 115 | async def async_page() -> AsyncGenerator[AsyncPage, None]: 116 | async with async_playwright() as p: 117 | browser = await p.chromium.launch(headless=False, args=flags) 118 | context = await browser.new_context(locale="en-US") 119 | page = await context.new_page() 120 | page.async_input = await AsyncInput(browser=context) # type: ignore[attr-defined] 121 | yield page 122 | 123 | 124 | @pytest.fixture 125 | def sync_driver() -> Generator[sync_webdriver.Chrome, None, None]: 126 | options = sync_webdriver.ChromeOptions() 127 | for flag in flags: 128 | options.add_argument(flag) 129 | 130 | with sync_webdriver.Chrome(options) as driver: 131 | driver.sync_input = SyncInput(browser=driver) 132 | yield driver 133 | 134 | 135 | @pytest.fixture 136 | def selenium_driver() -> Generator[selenium_webdriver.Chrome, None, None]: 137 | options = selenium_webdriver.ChromeOptions() 138 | for flag in flags: 139 | options.add_argument(flag) 140 | 141 | # disable logs & automation 142 | options.add_experimental_option("excludeSwitches", ["enable-logging", "enable-automation"]) 143 | options.add_experimental_option("useAutomationExtension", False) 144 | options.add_argument("--log-level=3") 145 | 146 | # start url at about:blank 147 | options.add_argument("about:blank") 148 | 149 | with selenium_webdriver.Chrome(options, service=SeleniumChromeService(ChromeDriverManager().install())) as driver: 150 | driver.sync_input = SyncInput(browser=driver) 151 | yield driver 152 | 153 | 154 | @pytest_asyncio.fixture 155 | async def async_driver() -> AsyncGenerator[async_webdriver.Chrome, None]: 156 | options = async_webdriver.ChromeOptions() 157 | for flag in flags: 158 | options.add_argument(flag) 159 | 160 | async with async_webdriver.Chrome(options) as driver: 161 | driver.async_input = await AsyncInput(browser=driver) 162 | yield driver 163 | -------------------------------------------------------------------------------- /tests/server.py: -------------------------------------------------------------------------------- 1 | import abc 2 | import asyncio 3 | import contextlib 4 | import gzip 5 | import mimetypes 6 | import socket 7 | import threading 8 | from contextlib import closing 9 | from http import HTTPStatus 10 | from typing import Any, Callable, Dict, Generator, Generic, Optional, Set, Tuple, TypeVar, cast 11 | from urllib.parse import urlparse 12 | 13 | from playwright._impl._path_utils import get_file_dirname 14 | from twisted.internet import reactor as _twisted_reactor 15 | from twisted.internet.selectreactor import SelectReactor 16 | from twisted.web import http 17 | 18 | _dirname = get_file_dirname() 19 | reactor = cast(SelectReactor, _twisted_reactor) 20 | 21 | 22 | def find_free_port() -> int: 23 | with closing(socket.socket(socket.AF_INET, socket.SOCK_STREAM)) as s: 24 | s.bind(("", 0)) 25 | s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) 26 | return int(s.getsockname()[1]) 27 | 28 | 29 | T = TypeVar("T") 30 | 31 | 32 | class ExpectResponse(Generic[T]): 33 | def __init__(self) -> None: 34 | self._value: T 35 | 36 | @property 37 | def value(self) -> T: 38 | if not hasattr(self, "_value"): 39 | raise ValueError("no received value") 40 | return self._value 41 | 42 | 43 | class TestServerRequest(http.Request): 44 | __test__ = False 45 | channel: "TestServerHTTPChannel" 46 | post_body: Optional[bytes] = None 47 | 48 | def process(self) -> None: 49 | server = self.channel.factory.server_instance 50 | if self.content: 51 | self.post_body = self.content.read() 52 | self.content.seek(0, 0) 53 | else: 54 | self.post_body = None 55 | uri = urlparse(self.uri.decode()) 56 | path = uri.path 57 | 58 | request_subscriber = server.request_subscribers.get(path) 59 | if request_subscriber: 60 | request_subscriber._loop.call_soon_threadsafe(request_subscriber.set_result, self) 61 | server.request_subscribers.pop(path) 62 | 63 | if server.auth.get(path): 64 | authorization_header = self.requestHeaders.getRawHeaders("authorization") 65 | creds_correct = False 66 | if authorization_header: 67 | creds_correct = server.auth.get(path) == ( 68 | self.getUser().decode(), 69 | self.getPassword().decode(), 70 | ) 71 | if not creds_correct: 72 | self.setHeader(b"www-authenticate", 'Basic realm="Secure Area"') 73 | self.setResponseCode(HTTPStatus.UNAUTHORIZED) 74 | self.finish() 75 | return 76 | if server.csp.get(path): 77 | self.setHeader(b"Content-Security-Policy", server.csp[path]) 78 | if server.routes.get(path): 79 | server.routes[path](self) 80 | return 81 | file_content = None 82 | try: 83 | file_content = (server.static_path / path[1:]).read_bytes() 84 | content_type = mimetypes.guess_type(path)[0] 85 | if content_type and content_type.startswith("text/"): 86 | content_type += "; charset=utf-8" 87 | self.setHeader(b"Content-Type", content_type) 88 | self.setHeader(b"Cache-Control", "no-cache, no-store") 89 | if path in server.gzip_routes: 90 | self.setHeader("Content-Encoding", "gzip") 91 | self.write(gzip.compress(file_content)) 92 | else: 93 | self.setHeader(b"Content-Length", str(len(file_content))) 94 | self.write(file_content) 95 | self.setResponseCode(HTTPStatus.OK) 96 | except (FileNotFoundError, IsADirectoryError, PermissionError): 97 | self.setResponseCode(HTTPStatus.NOT_FOUND) 98 | self.finish() 99 | 100 | 101 | class TestServerHTTPChannel(http.HTTPChannel): 102 | factory: "TestServerFactory" 103 | requestFactory = TestServerRequest 104 | 105 | 106 | class TestServerFactory(http.HTTPFactory): 107 | server_instance: "Server" 108 | protocol = TestServerHTTPChannel 109 | 110 | 111 | class Server: 112 | protocol = "http" 113 | 114 | def __init__(self) -> None: 115 | self.PORT = find_free_port() 116 | self.EMPTY_PAGE = f"{self.protocol}://localhost:{self.PORT}/empty.html" 117 | self.PREFIX = f"{self.protocol}://localhost:{self.PORT}" 118 | self.CROSS_PROCESS_PREFIX = f"{self.protocol}://127.0.0.1:{self.PORT}" 119 | # On Windows, this list can be empty, reporting text/plain for scripts. 120 | mimetypes.add_type("text/html", ".html") 121 | mimetypes.add_type("text/css", ".css") 122 | mimetypes.add_type("application/javascript", ".js") 123 | mimetypes.add_type("image/png", ".png") 124 | mimetypes.add_type("font/woff2", ".woff2") 125 | 126 | def __repr__(self) -> str: 127 | return self.PREFIX 128 | 129 | @abc.abstractmethod 130 | def listen(self, factory: TestServerFactory) -> None: 131 | pass 132 | 133 | def start(self) -> None: 134 | request_subscribers: Dict[str, asyncio.Future[TestServerRequest]] = {} 135 | auth: Dict[str, Tuple[str, str]] = {} 136 | csp: Dict[str, str] = {} 137 | routes: Dict[str, Callable[[TestServerRequest], Any]] = {} 138 | gzip_routes: Set[str] = set() 139 | self.request_subscribers = request_subscribers 140 | self.auth = auth 141 | self.csp = csp 142 | self.routes = routes 143 | self.gzip_routes = gzip_routes 144 | self.static_path = _dirname / "assets" 145 | factory = TestServerFactory() 146 | factory.server_instance = self 147 | self.listen(factory) 148 | 149 | async def wait_for_request(self, path: str) -> TestServerRequest: 150 | if path in self.request_subscribers: 151 | return await self.request_subscribers[path] 152 | future: asyncio.Future["TestServerRequest"] = asyncio.Future() 153 | self.request_subscribers[path] = future 154 | return await future 155 | 156 | @contextlib.contextmanager 157 | def expect_request(self, path: str) -> Generator[ExpectResponse[TestServerRequest], None, None]: 158 | future = asyncio.create_task(self.wait_for_request(path)) 159 | 160 | cb_wrapper: ExpectResponse[TestServerRequest] = ExpectResponse() 161 | 162 | def done_cb(task: asyncio.Task[TestServerRequest]) -> None: 163 | cb_wrapper._value = future.result() 164 | 165 | future.add_done_callback(done_cb) 166 | yield cb_wrapper 167 | 168 | def set_auth(self, path: str, username: str, password: str) -> None: 169 | self.auth[path] = (username, password) 170 | 171 | def set_csp(self, path: str, value: str) -> None: 172 | self.csp[path] = value 173 | 174 | def reset(self) -> None: 175 | if self.request_subscribers: 176 | self.request_subscribers.clear() 177 | self.auth.clear() 178 | self.csp.clear() 179 | self.gzip_routes.clear() 180 | self.routes.clear() 181 | 182 | def set_route(self, path: str, callback: Callable[[TestServerRequest], Any]) -> None: 183 | self.routes[path] = callback 184 | 185 | def enable_gzip(self, path: str) -> None: 186 | self.gzip_routes.add(path) 187 | 188 | def set_redirect(self, from_: str, to: str) -> None: 189 | def handle_redirect(request: http.Request) -> None: 190 | request.setResponseCode(HTTPStatus.FOUND) 191 | request.setHeader("location", to) 192 | request.finish() 193 | 194 | self.set_route(from_, handle_redirect) 195 | 196 | 197 | class HTTPServer(Server): 198 | def listen(self, factory: http.HTTPFactory) -> None: 199 | reactor.listenTCP(self.PORT, factory, interface="127.0.0.1") 200 | try: 201 | reactor.listenTCP(self.PORT, factory, interface="::1") 202 | except Exception: 203 | pass 204 | 205 | 206 | class TestServer: 207 | def __init__(self) -> None: 208 | self.server = HTTPServer() 209 | 210 | def start(self) -> None: 211 | self.server.start() 212 | self.thread = threading.Thread(target=lambda: reactor.run(installSignalHandlers=False)) 213 | self.thread.start() 214 | 215 | def stop(self) -> None: 216 | reactor.stop() 217 | self.thread.join() 218 | 219 | def reset(self) -> None: 220 | self.server.reset() 221 | 222 | 223 | test_server = TestServer() 224 | -------------------------------------------------------------------------------- /tests/test_async_driverless.py: -------------------------------------------------------------------------------- 1 | import asyncio 2 | 3 | import pytest 4 | from selenium_driverless.types.by import By 5 | from selenium_driverless.types.webelement import WebElement 6 | from selenium_driverless.webdriver import Chrome 7 | 8 | from cdp_patches.input.exceptions import WindowClosedException 9 | from tests.server import Server 10 | 11 | # from input import KeyboardCodes 12 | 13 | 14 | async def get_locator_pos(locator: WebElement): 15 | location = await locator.location 16 | size = await locator.size 17 | assert location, size 18 | 19 | x, y, width, height = location.get("x"), location.get("y"), size.get("width"), size.get("height") 20 | assert x and y and width and height 21 | 22 | x, y = x + width // 2, y + height // 2 23 | return x, y 24 | 25 | 26 | @pytest.mark.skip("Currently bugged by Driverless. Skipping until Update.") 27 | @pytest.mark.asyncio 28 | async def test_input_leak(async_driver: Chrome, server: Server) -> None: 29 | await async_driver.get(server.PREFIX + "/input/button.html") 30 | await async_driver.sleep(1) 31 | await async_driver.execute_script( 32 | """ 33 | const click_elem = document.querySelector("button") 34 | window.is_leaking = new Promise((resolve, reject) => {callback = resolve}); 35 | const on_click = async function(e){ 36 | var is_bot = (e.pageY == e.screenY && e.pageX == e.screenX) 37 | if (is_bot && 1 >= outerHeight - innerHeight){ // fullscreen 38 | is_bot = false 39 | } 40 | callback(is_bot) 41 | } 42 | click_elem.removeEventListener("mousedown", self) 43 | click_elem.addEventListener("mousedown", on_click) 44 | """ 45 | ) 46 | sync_locator = await async_driver.find_element(By.XPATH, "//button") 47 | x, y = await get_locator_pos(sync_locator) 48 | await async_driver.async_input.click("left", x, y) # type: ignore[attr-defined] 49 | 50 | is_leaking = await async_driver.eval_async("return await window.is_leaking", timeout=300) 51 | assert not is_leaking 52 | 53 | 54 | @pytest.mark.asyncio 55 | async def test_click_the_button(async_driver: Chrome, server: Server) -> None: 56 | await async_driver.get(server.PREFIX + "/input/button.html") 57 | await async_driver.sleep(1) 58 | sync_locator = await async_driver.find_element(By.XPATH, "//button") 59 | x, y = await get_locator_pos(sync_locator) 60 | await async_driver.async_input.click("left", x, y) # type: ignore[attr-defined] 61 | assert await async_driver.execute_script("return result") == "Clicked" 62 | 63 | 64 | @pytest.mark.asyncio 65 | async def test_double_click_the_button(async_driver: Chrome, server: Server) -> None: 66 | await async_driver.get(server.PREFIX + "/input/button.html") 67 | await async_driver.sleep(1) 68 | await async_driver.execute_script( 69 | """window.double = false; 70 | button = document.querySelector('button'); 71 | button.addEventListener('dblclick', event => window.double = true);""" 72 | ) 73 | 74 | sync_locator = await async_driver.find_element(By.XPATH, "//button") 75 | x, y = await get_locator_pos(sync_locator) 76 | await async_driver.async_input.double_click("left", x, y) # type: ignore[attr-defined] 77 | assert await async_driver.execute_script("return window.double") 78 | assert await async_driver.execute_script("return result") == "Clicked" 79 | 80 | 81 | @pytest.mark.asyncio 82 | async def test_locators_hover(async_driver: Chrome, server: Server) -> None: 83 | # x = """window.addEventListener("DOMContentLoaded",()=>{function highest_z_idx(){const allElements=document.querySelectorAll("*");let highestZIndex=0;allElements.forEach((element)=>{const 84 | # zIndex=parseInt(getComputedStyle(element).zIndex,10);if(zIndex&&zIndex>highestZIndex){highestZIndex=zIndex}});return highestZIndex}function round_2(num){return Math.round(( 85 | # num+Number.EPSILON)*100)/100}z_idx=highest_z_idx();const canvas=document.createElement("canvas");canvas.style.position="fixed";canvas.style.top="0";canvas.style.left="0";canvas.style.zIndex= 86 | # String(z_idx+1);canvas.width=window.innerWidth;canvas.height=window.innerHeight;canvas.style.pointerEvents="none";const clearButton=document.createElement("button");clearButton.textContent= 87 | # "Clear";clearButton.style.position="fixed";clearButton.style.top="10px";clearButton.style.left="10px";clearButton.id="clear";clearButton.style.zIndex=String(z_idx+2);clearButton.style.opacity= 88 | # "0.7";const tab=document.createElement("div");tab.style.position="fixed";tab.style.top="10px";tab.style.right="10px";tab.style.padding="5px 10px";tab.style.borderRadius="5px"; 89 | # tab.style.pointerEvents="none";tab.style.fontFamily="Arial, sans-serif";tab.style.fontSize="14px";tab.style.fontWeight="bold";tab.style.zIndex=String(z_idx+3);tab.style.opacity="0.8"; 90 | # tab.textContent="average ClickDeltaTime: 0.00ms +/-0.00,Average Frequency: 0.00 Hz, count:0, x:0, y:0";const graphCanvas=document.createElement("canvas");graphCanvas.width=window.innerWidth; 91 | # graphCanvas.height=200;graphCanvas.style.position="fixed";graphCanvas.style.bottom="0";graphCanvas.style.left="0";graphCanvas.style.zIndex=String(z_idx+4);graphCanvas.style.pointerEvents= 92 | # "None";const ctx=canvas.getContext("2d");let lastEventTime=0;let timeSinceClear=0;let timeDeltaData=[];let mousedownTime=0;let clickdeltaTimes=[];let averageClickDeltaTime=0; 93 | # click_delta_max_diff=0;function plot_point(x,y,color="red",radius="2",opacity=0.5){ctx.fillStyle=color;ctx.globalAlpha=opacity;ctx.beginPath();ctx.arc(x,y,radius,0,Math.PI*2);ctx.fill(); 94 | # ctx.globalAlpha=1;}function clear(){ctx.clearRect(0,0,canvas.width,canvas.height);lastEventTime=0;timeSinceClear=0;timeDeltaData=[];let mousedownTime=0;let clickdeltaTimes=[]; 95 | # let averageClickDeltaTime=0;click_delta_max_diff=0;tab.textContent="average ClickDeltaTime: 0.00ms +/-0.00, Average Frequency: 0.00 Hz, count:0, x:0, y:0"};function updateCanvasDimensions() 96 | # {canvas.width=window.innerWidth;canvas.height=window.innerHeight;graphCanvas.width=window.innerWidth;drawTimeDeltaGraph()}function drawTimeDeltaGraph(){const graphCtx= 97 | # graphCanvas.getContext("2d");graphCtx.clearRect(0,0,graphCanvas.width,graphCanvas.height);graphCtx.globalAlpha=0.3;graphCtx.fillStyle="white";graphCtx.fillRect(0,0,graphCanvas.width, 98 | # graphCanvas.height);graphCtx.globalAlpha=1;graphCtx.fillStyle="black";if(timeDeltaData.length>graphCanvas.width){timeDeltaData.splice(0,timeDeltaData.length-graphCanvas.width)} 99 | # const maxTimeDelta=Math.max(...timeDeltaData);const scaleFactor=graphCanvas.height/maxTimeDelta;const gridSpacing=20;graphCtx.strokeStyle="black";graphCtx.beginPath();for(let y=0;y<= 100 | # graphCanvas.height;y+=gridSpacing){graphCtx.moveTo(0,y);graphCtx.lineTo(graphCanvas.width,y);const timeValue=(maxTimeDelta*(graphCanvas.height-y)/graphCanvas.height).toFixed(2); 101 | # if(isFinite(timeValue)){graphCtx.fillText(timeValue+" ms",graphCanvas.width-50,y+12)}}graphCtx.stroke();graphCtx.beginPath();graphCtx.strokeStyle="black";graphCtx.moveTo(0,0); 102 | # graphCtx.lineTo(graphCanvas.width,0);graphCtx.stroke();graphCtx.beginPath();graphCtx.strokeStyle="black";graphCtx.moveTo(0,graphCanvas.height);graphCtx.lineTo(graphCanvas.width, 103 | # graphCanvas.height);graphCtx.stroke();graphCtx.beginPath();graphCtx.strokeStyle="green";graphCtx.moveTo(0,graphCanvas.height-timeDeltaData[0]*scaleFactor);for(let i=1;i< 104 | # timeDeltaData.length;i+=1){graphCtx.lineTo(i,graphCanvas.height-timeDeltaData[i]*scaleFactor)}graphCtx.stroke();graphCtx.fillStyle="black";graphCtx.font="12px Arial"; 105 | # graphCtx.fillText("0 ms",2,graphCanvas.height-2);graphCtx.fillText(`${timeDeltaData.length-1} ms`,graphCanvas.width-30,graphCanvas.height-2);graphCtx.fillText(`${maxTimeDelta.toFixed(2)} ms` 106 | # ,2,10)}function move_handler(event){const currentTime=Date.now();const delta=currentTime-lastEventTime;const x=event.x;const y=event.y;if(delta<=100){if(lastEventTime!==0){timeSinceClear+= 107 | # delta;timeDeltaData.push(delta);const averageDelta=timeDeltaData.length===0?0:timeDeltaData.reduce((sum,value)=>sum+value)/timeDeltaData.length;const frequency=averageDelta===0?0:1000/ 108 | # averageDelta;tab.textContent=`average ClickDeltaTime: ${ averageClickDeltaTime }ms +/-${ click_delta_max_diff }, Average Frequency: ${frequency.toFixed(2)} Hz, count:${timeDeltaData.length } 109 | # ,x:${ x }, y:${ y }`;drawTimeDeltaGraph()}}lastEventTime=currentTime;plot_point(x,y)}function click_handler(e){plot_point(e.x,e.y,"green",5)};function mouseup_handler(){const mouseupTime= 110 | # Date.now();const deltaTime=mouseupTime-mousedownTime;clickdeltaTimes.push(deltaTime);delta_average=clickdeltaTimes.reduce((sum,time)=>sum+time, 111 | # 0)/clickdeltaTimes.length;click_delta_max_diff=round_2(((Math.max(...clickdeltaTimes)-delta_average)+(delta_average-Math.min(...clickdeltaTimes)))/2);averageClickDeltaTime= 112 | # round_2(delta_average)}document.body.appendChild(canvas);document.body.appendChild(graphCanvas);document.addEventListener("mousemove",move_handler);document.addEventListener("click", 113 | # click_handler);document.addEventListener("mousedown",(e)=>{mousedownTime=Date.now()});document.body.appendChild(tab);window.addEventListener("resize",updateCanvasDimensions); 114 | # document.body.appendChild(clearButton);clearButton.addEventListener("click",clear);updateCanvasDimensions();document.addEventListener("mouseup",mouseup_handler);})""" 115 | # sync_page.add_init_script(x) 116 | 117 | await async_driver.get(server.PREFIX + "/input/scrollable.html") 118 | await async_driver.sleep(1) 119 | await async_driver.async_input.move(500, 100) # type: ignore[attr-defined] 120 | 121 | sync_locator = await async_driver.find_element(By.ID, "button-12") 122 | x, y = await get_locator_pos(sync_locator) 123 | await async_driver.async_input.move(x, y) # type: ignore[attr-defined] 124 | 125 | await asyncio.sleep(0.5) 126 | assert await async_driver.execute_script("return document.querySelector('button:hover').id") == "button-12" 127 | 128 | 129 | @pytest.mark.asyncio 130 | async def test_fill_input(async_driver: Chrome, server: Server) -> None: 131 | await async_driver.get(server.PREFIX + "/input/textarea.html") 132 | await async_driver.sleep(1) 133 | sync_locator = await async_driver.find_element(By.XPATH, "//input") 134 | assert sync_locator 135 | 136 | x, y = await get_locator_pos(sync_locator) 137 | await async_driver.async_input.click("left", x, y) # type: ignore[attr-defined] 138 | await async_driver.async_input.type("some value", fill=True) # type: ignore[attr-defined] 139 | assert await async_driver.execute_script("return result") == "some value" 140 | 141 | 142 | @pytest.mark.asyncio 143 | async def test_keyboard_type_into_a_textarea(async_driver: Chrome) -> None: 144 | await async_driver.execute_script( 145 | """ 146 | const textarea = document.createElement('textarea'); 147 | document.body.appendChild(textarea); 148 | textarea.focus(); 149 | """ 150 | ) 151 | await async_driver.sleep(1) 152 | text = "Hello world. I +am the %text that was typed!" 153 | 154 | sync_locator = await async_driver.find_element(By.XPATH, "//textarea") 155 | assert sync_locator 156 | 157 | x, y = await get_locator_pos(sync_locator) 158 | await async_driver.async_input.click("left", x, y) # type: ignore[attr-defined] 159 | 160 | await async_driver.async_input.type(text) # type: ignore[attr-defined] 161 | assert await async_driver.execute_script('return document.querySelector("textarea").value') == text 162 | 163 | 164 | @pytest.mark.asyncio 165 | async def test_quit_exception(async_driver: Chrome) -> None: 166 | await async_driver.quit() 167 | await asyncio.sleep(5) 168 | 169 | with pytest.raises(WindowClosedException): 170 | await async_driver.async_input.down("left", 100, 100, emulate_behaviour=False) 171 | with pytest.raises(WindowClosedException): 172 | await async_driver.async_input.up("left", 110, 110) 173 | with pytest.raises(WindowClosedException): 174 | await async_driver.async_input.move(50, 50, emulate_behaviour=False) 175 | with pytest.raises(WindowClosedException): 176 | await async_driver.async_input.scroll("up", 10) 177 | with pytest.raises(WindowClosedException): 178 | await async_driver.async_input.type("test") 179 | -------------------------------------------------------------------------------- /tests/test_async_playwright.py: -------------------------------------------------------------------------------- 1 | import asyncio 2 | 3 | import pytest 4 | from playwright.async_api import Locator, Page 5 | 6 | from cdp_patches.input.exceptions import WindowClosedException 7 | from tests.server import Server 8 | 9 | # from input import KeyboardCodes 10 | 11 | 12 | async def get_locator_pos(locator: Locator): 13 | bounding_box = await locator.bounding_box() 14 | assert bounding_box 15 | 16 | x, y, width, height = bounding_box.get("x"), bounding_box.get("y"), bounding_box.get("width"), bounding_box.get("height") 17 | assert x and y and width and height 18 | 19 | x, y = x + width // 2, y + height // 2 20 | return x, y 21 | 22 | 23 | @pytest.mark.asyncio 24 | async def test_input_leak(async_page: Page, server: Server) -> None: 25 | await async_page.goto(server.PREFIX + "/input/button.html") 26 | await async_page.evaluate( 27 | """ 28 | const click_elem = document.querySelector("button") 29 | window.is_leaking = new Promise((resolve, reject) => {callback = resolve}); 30 | const on_click = async function(e){ 31 | var is_bot = (e.pageY == e.screenY && e.pageX == e.screenX) 32 | if (is_bot && 1 >= outerHeight - innerHeight){ // fullscreen 33 | is_bot = false 34 | } 35 | callback(is_bot) 36 | } 37 | click_elem.removeEventListener("mousedown", self) 38 | click_elem.addEventListener("mousedown", on_click) 39 | """ 40 | ) 41 | async_locator = async_page.locator("button") 42 | x, y = await get_locator_pos(async_locator) 43 | await async_page.async_input.click("left", x, y) # type: ignore[attr-defined] 44 | 45 | is_leaking = await async_page.evaluate("() => window.is_leaking") 46 | assert not is_leaking 47 | 48 | 49 | @pytest.mark.asyncio 50 | async def test_click_the_button(async_page: Page, server: Server) -> None: 51 | await async_page.goto(server.PREFIX + "/input/button.html") 52 | locator = async_page.locator("button") 53 | x, y = await get_locator_pos(locator) 54 | await async_page.async_input.click("left", x, y) # type: ignore[attr-defined] 55 | assert await async_page.evaluate("result") == "Clicked" 56 | 57 | 58 | @pytest.mark.asyncio 59 | async def test_double_click_the_button(async_page: Page, server: Server) -> None: 60 | await async_page.goto(server.PREFIX + "/input/button.html") 61 | await async_page.evaluate( 62 | """() => { 63 | window.double = false; 64 | button = document.querySelector('button'); 65 | button.addEventListener('dblclick', event => window.double = true); 66 | }""" 67 | ) 68 | 69 | locator = async_page.locator("button") 70 | x, y = await get_locator_pos(locator) 71 | await async_page.async_input.double_click("left", x, y) # type: ignore[attr-defined] 72 | assert await async_page.evaluate("double") 73 | assert await async_page.evaluate("result") == "Clicked" 74 | 75 | 76 | @pytest.mark.asyncio 77 | async def test_locators_hover(async_page: Page, server: Server) -> None: 78 | await async_page.goto(server.PREFIX + "/input/scrollable.html") 79 | await async_page.async_input.move(500, 100) # type: ignore[attr-defined] 80 | 81 | button = async_page.locator("#button-12") 82 | x, y = await get_locator_pos(button) 83 | await async_page.async_input.move(x, y) # type: ignore[attr-defined] 84 | 85 | await asyncio.sleep(0.5) 86 | assert await async_page.evaluate("document.querySelector('button:hover').id") == "button-12" 87 | 88 | 89 | @pytest.mark.skip(reason="Scroll Tests currently arent implemented properly.") 90 | @pytest.mark.asyncio 91 | async def test_scroll(async_page: Page, server: Server) -> None: 92 | await async_page.goto(server.PREFIX + "/offscreenbuttons.html") 93 | for i in range(11): 94 | button = await async_page.query_selector(f"#btn{i}") 95 | assert button 96 | before = await button.evaluate( 97 | """button => { 98 | return button.getBoundingClientRect().right - window.innerWidth 99 | }""" 100 | ) 101 | 102 | assert before == 10 * i 103 | # await button.scroll_into_view_if_needed() 104 | await async_page.async_input.scroll("right", i) # type: ignore[attr-defined] 105 | 106 | after = await button.evaluate( 107 | """button => { 108 | return button.getBoundingClientRect().right - window.innerWidth 109 | }""" 110 | ) 111 | 112 | assert after <= 0 113 | await async_page.evaluate("() => window.scrollTo(0, 0)") 114 | 115 | 116 | @pytest.mark.asyncio 117 | async def test_fill_input(async_page: Page, server: Server) -> None: 118 | await async_page.goto(server.PREFIX + "/input/textarea.html") 119 | handle = async_page.locator("input") 120 | assert handle 121 | 122 | x, y = await get_locator_pos(handle) 123 | await async_page.async_input.click("left", x, y) # type: ignore[attr-defined] 124 | await async_page.async_input.type("some value", fill=True) # type: ignore[attr-defined] 125 | assert await async_page.evaluate("result") == "some value" 126 | 127 | 128 | @pytest.mark.asyncio 129 | async def test_keyboard_type_into_a_textarea(async_page: Page) -> None: 130 | await async_page.evaluate( 131 | """ 132 | const textarea = document.createElement('textarea'); 133 | document.body.appendChild(textarea); 134 | textarea.focus(); 135 | """ 136 | ) 137 | text = "Hello world. I +am the %text that was typed!" 138 | 139 | handle = async_page.locator("textarea") 140 | assert handle 141 | 142 | x, y = await get_locator_pos(handle) 143 | await async_page.async_input.click("left", x, y) # type: ignore[attr-defined] 144 | 145 | await async_page.async_input.type(text) # type: ignore[attr-defined] 146 | assert await async_page.evaluate('document.querySelector("textarea").value') == text 147 | 148 | 149 | # @pytest.mark.asyncio 150 | # async def test_should_report_shiftkey(async_page: Page, server: Server) -> None: 151 | # await async_page.goto(server.PREFIX + "/input/keyboard.html") 152 | # code_for_key = {KeyboardCodes.VK_SHIFT: ["Shift", "16"], KeyboardCodes.VK_CONTROL: ["Control", "17"]} 153 | # # , KeyboardCodes.VK_MENU: ["Alt", "18"] 154 | # 155 | # handle = async_page.locator("textarea") 156 | # assert handle 157 | # 158 | # x, y = await get_locator_pos(handle) 159 | # await async_page.async_input.click("left", x, y) # type: ignore[attr-defined] 160 | # 161 | # for modifier_key, js_key in code_for_key.items(): 162 | # await async_page.async_input.press_keys(modifier_key) # type: ignore[attr-defined] 163 | # # async_page.async_input._base.browser_window.send_keystrokes("{VK_SHIFT down} bruh") # type: ignore[attr-defined] 164 | # 165 | # assert ( 166 | # await async_page.evaluate("() => getResult()") 167 | # == "Keydown: " 168 | # + js_key[0] 169 | # + " " 170 | # + js_key[0] 171 | # + "Left " 172 | # + js_key[1] 173 | # + " [" 174 | # + js_key[0] 175 | # + "]" 176 | # ) 177 | # 178 | # await async_page.async_input.press_keys("!") # type: ignore[attr-defined] 179 | # # Shift+! will generate a keypress 180 | # if js_key[0] == "Shift": 181 | # assert ( 182 | # await async_page.evaluate("() => getResult()") 183 | # == "Keydown: ! Digit1 49 [" 184 | # + js_key[0] 185 | # + "]\nKeypress: ! Digit1 33 33 [" 186 | # + js_key[0] 187 | # + "]" 188 | # ) 189 | # else: 190 | # assert ( 191 | # await async_page.evaluate("() => getResult()") 192 | # == "Keydown: ! Digit1 49 [" + js_key[0] + "]" 193 | # ) 194 | # 195 | # await async_page.async_input.release_keys("!") # type: ignore[attr-defined] 196 | # assert ( 197 | # await async_page.evaluate("() => getResult()") 198 | # == "Keyup: ! Digit1 49 [" + js_key[0] + "]" 199 | # ) 200 | # await async_page.async_input.release_keys(modifier_key) # type: ignore[attr-defined] 201 | # assert ( 202 | # await async_page.evaluate("() => getResult()") 203 | # == "Keyup: " 204 | # + js_key[0] 205 | # + " " 206 | # + js_key[0] 207 | # + "Left " 208 | # + js_key[1] 209 | # + " []" 210 | # ) 211 | 212 | 213 | @pytest.mark.asyncio 214 | async def test_quit_exception(async_page: Page) -> None: 215 | await async_page.close() 216 | await asyncio.sleep(5) 217 | 218 | with pytest.raises(WindowClosedException): 219 | await async_page.async_input.down("left", 100, 100, emulate_behaviour=False) # type: ignore[attr-defined] 220 | with pytest.raises(WindowClosedException): 221 | await async_page.async_input.up("left", 110, 110) # type: ignore[attr-defined] 222 | with pytest.raises(WindowClosedException): 223 | await async_page.async_input.move(50, 50, emulate_behaviour=False) # type: ignore[attr-defined] 224 | with pytest.raises(WindowClosedException): 225 | await async_page.async_input.scroll("up", 10) # type: ignore[attr-defined] 226 | with pytest.raises(WindowClosedException): 227 | await async_page.async_input.type("test") # type: ignore[attr-defined] 228 | -------------------------------------------------------------------------------- /tests/test_selenium.py: -------------------------------------------------------------------------------- 1 | import time 2 | 3 | import pytest 4 | from selenium.webdriver import Chrome 5 | from selenium.webdriver.common.by import By 6 | from selenium.webdriver.remote.webelement import WebElement 7 | 8 | from cdp_patches.input.exceptions import WindowClosedException 9 | from tests.server import Server 10 | 11 | 12 | def get_locator_pos(locator: WebElement): 13 | location = locator.location 14 | size = locator.size 15 | assert location, size 16 | 17 | x, y, width, height = location.get("x"), location.get("y"), size.get("width"), size.get("height") 18 | assert x and y and width and height 19 | 20 | x, y = x + width // 2, y + height // 2 21 | return x, y 22 | 23 | 24 | def test_input_leak(selenium_driver: Chrome, server: Server) -> None: 25 | selenium_driver.get(server.PREFIX + "/input/button.html") 26 | selenium_driver.execute_script( 27 | """ 28 | const click_elem = document.querySelector("button") 29 | window.is_leaking = new Promise((resolve, reject) => {callback = resolve}); 30 | const on_click = async function(e){ 31 | var is_bot = (e.pageY == e.screenY && e.pageX == e.screenX) 32 | if (is_bot && 1 >= outerHeight - innerHeight){ // fullscreen 33 | is_bot = false 34 | } 35 | callback(is_bot) 36 | } 37 | click_elem.removeEventListener("mousedown", self) 38 | click_elem.addEventListener("mousedown", on_click) 39 | """ 40 | ) 41 | sync_locator = selenium_driver.find_element(By.XPATH, "//button") 42 | x, y = get_locator_pos(sync_locator) 43 | selenium_driver.sync_input.click("left", x, y) # type: ignore[attr-defined] 44 | 45 | time.sleep(2) 46 | is_leaking = selenium_driver.execute_async_script("window.is_leaking.then(arguments[arguments.length - 1])") 47 | assert not is_leaking 48 | 49 | 50 | def test_click_the_button(selenium_driver: Chrome, server: Server) -> None: 51 | selenium_driver.get(server.PREFIX + "/input/button.html") 52 | sync_locator = selenium_driver.find_element(By.XPATH, "//button") 53 | x, y = get_locator_pos(sync_locator) 54 | selenium_driver.sync_input.click("left", x, y) # type: ignore[attr-defined] 55 | assert selenium_driver.execute_script("return result") == "Clicked" 56 | 57 | 58 | def test_double_click_the_button(selenium_driver: Chrome, server: Server) -> None: 59 | selenium_driver.get(server.PREFIX + "/input/button.html") 60 | selenium_driver.execute_script( 61 | """window.double = false; 62 | button = document.querySelector('button'); 63 | button.addEventListener('dblclick', event => window.double = true);""" 64 | ) 65 | 66 | sync_locator = selenium_driver.find_element(By.XPATH, "//button") 67 | x, y = get_locator_pos(sync_locator) 68 | selenium_driver.sync_input.double_click("left", x, y) # type: ignore[attr-defined] 69 | assert selenium_driver.execute_script("return window.double") 70 | assert selenium_driver.execute_script("return result") == "Clicked" 71 | 72 | 73 | def test_locators_hover(selenium_driver: Chrome, server: Server) -> None: 74 | # x = """window.addEventListener("DOMContentLoaded",()=>{function highest_z_idx(){const allElements=document.querySelectorAll("*");let highestZIndex=0;allElements.forEach((element)=>{const 75 | # zIndex=parseInt(getComputedStyle(element).zIndex,10);if(zIndex&&zIndex>highestZIndex){highestZIndex=zIndex}});return highestZIndex}function round_2(num){return Math.round(( 76 | # num+Number.EPSILON)*100)/100}z_idx=highest_z_idx();const canvas=document.createElement("canvas");canvas.style.position="fixed";canvas.style.top="0";canvas.style.left="0";canvas.style.zIndex= 77 | # String(z_idx+1);canvas.width=window.innerWidth;canvas.height=window.innerHeight;canvas.style.pointerEvents="none";const clearButton=document.createElement("button");clearButton.textContent= 78 | # "Clear";clearButton.style.position="fixed";clearButton.style.top="10px";clearButton.style.left="10px";clearButton.id="clear";clearButton.style.zIndex=String(z_idx+2);clearButton.style.opacity= 79 | # "0.7";const tab=document.createElement("div");tab.style.position="fixed";tab.style.top="10px";tab.style.right="10px";tab.style.padding="5px 10px";tab.style.borderRadius="5px"; 80 | # tab.style.pointerEvents="none";tab.style.fontFamily="Arial, sans-serif";tab.style.fontSize="14px";tab.style.fontWeight="bold";tab.style.zIndex=String(z_idx+3);tab.style.opacity="0.8"; 81 | # tab.textContent="average ClickDeltaTime: 0.00ms +/-0.00,Average Frequency: 0.00 Hz, count:0, x:0, y:0";const graphCanvas=document.createElement("canvas");graphCanvas.width=window.innerWidth; 82 | # graphCanvas.height=200;graphCanvas.style.position="fixed";graphCanvas.style.bottom="0";graphCanvas.style.left="0";graphCanvas.style.zIndex=String(z_idx+4);graphCanvas.style.pointerEvents= 83 | # "None";const ctx=canvas.getContext("2d");let lastEventTime=0;let timeSinceClear=0;let timeDeltaData=[];let mousedownTime=0;let clickdeltaTimes=[];let averageClickDeltaTime=0; 84 | # click_delta_max_diff=0;function plot_point(x,y,color="red",radius="2",opacity=0.5){ctx.fillStyle=color;ctx.globalAlpha=opacity;ctx.beginPath();ctx.arc(x,y,radius,0,Math.PI*2);ctx.fill(); 85 | # ctx.globalAlpha=1;}function clear(){ctx.clearRect(0,0,canvas.width,canvas.height);lastEventTime=0;timeSinceClear=0;timeDeltaData=[];let mousedownTime=0;let clickdeltaTimes=[]; 86 | # let averageClickDeltaTime=0;click_delta_max_diff=0;tab.textContent="average ClickDeltaTime: 0.00ms +/-0.00, Average Frequency: 0.00 Hz, count:0, x:0, y:0"};function updateCanvasDimensions() 87 | # {canvas.width=window.innerWidth;canvas.height=window.innerHeight;graphCanvas.width=window.innerWidth;drawTimeDeltaGraph()}function drawTimeDeltaGraph(){const graphCtx= 88 | # graphCanvas.getContext("2d");graphCtx.clearRect(0,0,graphCanvas.width,graphCanvas.height);graphCtx.globalAlpha=0.3;graphCtx.fillStyle="white";graphCtx.fillRect(0,0,graphCanvas.width, 89 | # graphCanvas.height);graphCtx.globalAlpha=1;graphCtx.fillStyle="black";if(timeDeltaData.length>graphCanvas.width){timeDeltaData.splice(0,timeDeltaData.length-graphCanvas.width)} 90 | # const maxTimeDelta=Math.max(...timeDeltaData);const scaleFactor=graphCanvas.height/maxTimeDelta;const gridSpacing=20;graphCtx.strokeStyle="black";graphCtx.beginPath();for(let y=0;y<= 91 | # graphCanvas.height;y+=gridSpacing){graphCtx.moveTo(0,y);graphCtx.lineTo(graphCanvas.width,y);const timeValue=(maxTimeDelta*(graphCanvas.height-y)/graphCanvas.height).toFixed(2); 92 | # if(isFinite(timeValue)){graphCtx.fillText(timeValue+" ms",graphCanvas.width-50,y+12)}}graphCtx.stroke();graphCtx.beginPath();graphCtx.strokeStyle="black";graphCtx.moveTo(0,0); 93 | # graphCtx.lineTo(graphCanvas.width,0);graphCtx.stroke();graphCtx.beginPath();graphCtx.strokeStyle="black";graphCtx.moveTo(0,graphCanvas.height);graphCtx.lineTo(graphCanvas.width, 94 | # graphCanvas.height);graphCtx.stroke();graphCtx.beginPath();graphCtx.strokeStyle="green";graphCtx.moveTo(0,graphCanvas.height-timeDeltaData[0]*scaleFactor);for(let i=1;i< 95 | # timeDeltaData.length;i+=1){graphCtx.lineTo(i,graphCanvas.height-timeDeltaData[i]*scaleFactor)}graphCtx.stroke();graphCtx.fillStyle="black";graphCtx.font="12px Arial"; 96 | # graphCtx.fillText("0 ms",2,graphCanvas.height-2);graphCtx.fillText(`${timeDeltaData.length-1} ms`,graphCanvas.width-30,graphCanvas.height-2);graphCtx.fillText(`${maxTimeDelta.toFixed(2)} ms` 97 | # ,2,10)}function move_handler(event){const currentTime=Date.now();const delta=currentTime-lastEventTime;const x=event.x;const y=event.y;if(delta<=100){if(lastEventTime!==0){timeSinceClear+= 98 | # delta;timeDeltaData.push(delta);const averageDelta=timeDeltaData.length===0?0:timeDeltaData.reduce((sum,value)=>sum+value)/timeDeltaData.length;const frequency=averageDelta===0?0:1000/ 99 | # averageDelta;tab.textContent=`average ClickDeltaTime: ${ averageClickDeltaTime }ms +/-${ click_delta_max_diff }, Average Frequency: ${frequency.toFixed(2)} Hz, count:${timeDeltaData.length } 100 | # ,x:${ x }, y:${ y }`;drawTimeDeltaGraph()}}lastEventTime=currentTime;plot_point(x,y)}function click_handler(e){plot_point(e.x,e.y,"green",5)};function mouseup_handler(){const mouseupTime= 101 | # Date.now();const deltaTime=mouseupTime-mousedownTime;clickdeltaTimes.push(deltaTime);delta_average=clickdeltaTimes.reduce((sum,time)=>sum+time, 102 | # 0)/clickdeltaTimes.length;click_delta_max_diff=round_2(((Math.max(...clickdeltaTimes)-delta_average)+(delta_average-Math.min(...clickdeltaTimes)))/2);averageClickDeltaTime= 103 | # round_2(delta_average)}document.body.appendChild(canvas);document.body.appendChild(graphCanvas);document.addEventListener("mousemove",move_handler);document.addEventListener("click", 104 | # click_handler);document.addEventListener("mousedown",(e)=>{mousedownTime=Date.now()});document.body.appendChild(tab);window.addEventListener("resize",updateCanvasDimensions); 105 | # document.body.appendChild(clearButton);clearButton.addEventListener("click",clear);updateCanvasDimensions();document.addEventListener("mouseup",mouseup_handler);})""" 106 | # sync_page.add_init_script(x) 107 | 108 | selenium_driver.get(server.PREFIX + "/input/scrollable.html") 109 | selenium_driver.sync_input.move(500, 100) # type: ignore[attr-defined] 110 | 111 | sync_locator = selenium_driver.find_element(By.ID, "button-12") 112 | x, y = get_locator_pos(sync_locator) 113 | selenium_driver.sync_input.move(x, y) # type: ignore[attr-defined] 114 | 115 | time.sleep(0.5) 116 | assert selenium_driver.execute_script("return document.querySelector('button:hover').id") == "button-12" 117 | 118 | 119 | def test_fill_input(selenium_driver: Chrome, server: Server) -> None: 120 | selenium_driver.get(server.PREFIX + "/input/textarea.html") 121 | sync_locator = selenium_driver.find_element(By.XPATH, "//input") 122 | assert sync_locator 123 | 124 | x, y = get_locator_pos(sync_locator) 125 | selenium_driver.sync_input.click("left", x, y) # type: ignore[attr-defined] 126 | selenium_driver.sync_input.type("some value", fill=True) # type: ignore[attr-defined] 127 | assert selenium_driver.execute_script("return result") == "some value" 128 | 129 | 130 | def test_keyboard_type_into_a_textarea(selenium_driver: Chrome) -> None: 131 | selenium_driver.execute_script( 132 | """ 133 | const textarea = document.createElement('textarea'); 134 | document.body.appendChild(textarea); 135 | textarea.focus(); 136 | """ 137 | ) 138 | text = "Hello world. I +am the %text that was typed!" 139 | 140 | sync_locator = selenium_driver.find_element(By.XPATH, "//textarea") 141 | assert sync_locator 142 | 143 | x, y = get_locator_pos(sync_locator) 144 | selenium_driver.sync_input.click("left", x, y) # type: ignore[attr-defined] 145 | 146 | selenium_driver.sync_input.type(text) # type: ignore[attr-defined] 147 | assert selenium_driver.execute_script('return document.querySelector("textarea").value') == text 148 | 149 | 150 | def test_quit_exception(selenium_driver: Chrome) -> None: 151 | selenium_driver.quit() 152 | time.sleep(5) 153 | 154 | with pytest.raises(WindowClosedException): 155 | selenium_driver.sync_input.down("left", 100, 100, emulate_behaviour=False) # type: ignore[attr-defined] 156 | with pytest.raises(WindowClosedException): 157 | selenium_driver.sync_input.up("left", 110, 110) # type: ignore[attr-defined] 158 | with pytest.raises(WindowClosedException): 159 | selenium_driver.sync_input.move(50, 50, emulate_behaviour=False) # type: ignore[attr-defined] 160 | with pytest.raises(WindowClosedException): 161 | selenium_driver.sync_input.scroll("up", 10) # type: ignore[attr-defined] 162 | with pytest.raises(WindowClosedException): 163 | selenium_driver.sync_input.type("test") # type: ignore[attr-defined] 164 | -------------------------------------------------------------------------------- /tests/test_sync_driverless.py: -------------------------------------------------------------------------------- 1 | import time 2 | 3 | import pytest 4 | from selenium_driverless.sync.webdriver import Chrome 5 | from selenium_driverless.types.by import By 6 | from selenium_driverless.types.webelement import WebElement 7 | 8 | from cdp_patches.input.exceptions import WindowClosedException 9 | from tests.server import Server 10 | 11 | # from input import KeyboardCodes 12 | 13 | 14 | def get_locator_pos(locator: WebElement): 15 | location = locator.location 16 | size = locator.size 17 | assert location, size 18 | 19 | x, y, width, height = location.get("x"), location.get("y"), size.get("width"), size.get("height") 20 | assert x and y and width and height 21 | 22 | x, y = x + width // 2, y + height // 2 23 | return x, y 24 | 25 | 26 | def test_input_leak(sync_driver: Chrome, server: Server) -> None: 27 | sync_driver.get(server.PREFIX + "/input/button.html") 28 | sync_driver.sleep(1) 29 | sync_driver.execute_script( 30 | """ 31 | const click_elem = document.querySelector("button") 32 | window.is_leaking = new Promise((resolve, reject) => {callback = resolve}); 33 | const on_click = async function(e){ 34 | var is_bot = (e.pageY == e.screenY && e.pageX == e.screenX) 35 | if (is_bot && 1 >= outerHeight - innerHeight){ // fullscreen 36 | is_bot = false 37 | } 38 | callback(is_bot) 39 | } 40 | click_elem.removeEventListener("mousedown", self) 41 | click_elem.addEventListener("mousedown", on_click) 42 | """ 43 | ) 44 | sync_locator = sync_driver.find_element(By.XPATH, "//button") 45 | x, y = get_locator_pos(sync_locator) 46 | sync_driver.sync_input.click("left", x, y) # type: ignore[attr-defined] 47 | 48 | is_leaking = sync_driver.eval_async("return await window.is_leaking") 49 | assert not is_leaking 50 | 51 | 52 | def test_click_the_button(sync_driver: Chrome, server: Server) -> None: 53 | sync_driver.get(server.PREFIX + "/input/button.html") 54 | sync_driver.sleep(1) 55 | sync_locator = sync_driver.find_element(By.XPATH, "//button") 56 | x, y = get_locator_pos(sync_locator) 57 | sync_driver.sync_input.click("left", x, y) # type: ignore[attr-defined] 58 | assert sync_driver.execute_script("return result") == "Clicked" 59 | 60 | 61 | def test_double_click_the_button(sync_driver: Chrome, server: Server) -> None: 62 | sync_driver.get(server.PREFIX + "/input/button.html") 63 | sync_driver.sleep(1) 64 | sync_driver.execute_script( 65 | """window.double = false; 66 | button = document.querySelector('button'); 67 | button.addEventListener('dblclick', event => window.double = true);""" 68 | ) 69 | 70 | sync_locator = sync_driver.find_element(By.XPATH, "//button") 71 | x, y = get_locator_pos(sync_locator) 72 | sync_driver.sync_input.double_click("left", x, y) # type: ignore[attr-defined] 73 | assert sync_driver.execute_script("return window.double") 74 | assert sync_driver.execute_script("return result") == "Clicked" 75 | 76 | 77 | def test_locators_hover(sync_driver: Chrome, server: Server) -> None: 78 | # x = """window.addEventListener("DOMContentLoaded",()=>{function highest_z_idx(){const allElements=document.querySelectorAll("*");let highestZIndex=0;allElements.forEach((element)=>{const 79 | # zIndex=parseInt(getComputedStyle(element).zIndex,10);if(zIndex&&zIndex>highestZIndex){highestZIndex=zIndex}});return highestZIndex}function round_2(num){return Math.round(( 80 | # num+Number.EPSILON)*100)/100}z_idx=highest_z_idx();const canvas=document.createElement("canvas");canvas.style.position="fixed";canvas.style.top="0";canvas.style.left="0";canvas.style.zIndex= 81 | # String(z_idx+1);canvas.width=window.innerWidth;canvas.height=window.innerHeight;canvas.style.pointerEvents="none";const clearButton=document.createElement("button");clearButton.textContent= 82 | # "Clear";clearButton.style.position="fixed";clearButton.style.top="10px";clearButton.style.left="10px";clearButton.id="clear";clearButton.style.zIndex=String(z_idx+2);clearButton.style.opacity= 83 | # "0.7";const tab=document.createElement("div");tab.style.position="fixed";tab.style.top="10px";tab.style.right="10px";tab.style.padding="5px 10px";tab.style.borderRadius="5px"; 84 | # tab.style.pointerEvents="none";tab.style.fontFamily="Arial, sans-serif";tab.style.fontSize="14px";tab.style.fontWeight="bold";tab.style.zIndex=String(z_idx+3);tab.style.opacity="0.8"; 85 | # tab.textContent="average ClickDeltaTime: 0.00ms +/-0.00,Average Frequency: 0.00 Hz, count:0, x:0, y:0";const graphCanvas=document.createElement("canvas");graphCanvas.width=window.innerWidth; 86 | # graphCanvas.height=200;graphCanvas.style.position="fixed";graphCanvas.style.bottom="0";graphCanvas.style.left="0";graphCanvas.style.zIndex=String(z_idx+4);graphCanvas.style.pointerEvents= 87 | # "None";const ctx=canvas.getContext("2d");let lastEventTime=0;let timeSinceClear=0;let timeDeltaData=[];let mousedownTime=0;let clickdeltaTimes=[];let averageClickDeltaTime=0; 88 | # click_delta_max_diff=0;function plot_point(x,y,color="red",radius="2",opacity=0.5){ctx.fillStyle=color;ctx.globalAlpha=opacity;ctx.beginPath();ctx.arc(x,y,radius,0,Math.PI*2);ctx.fill(); 89 | # ctx.globalAlpha=1;}function clear(){ctx.clearRect(0,0,canvas.width,canvas.height);lastEventTime=0;timeSinceClear=0;timeDeltaData=[];let mousedownTime=0;let clickdeltaTimes=[]; 90 | # let averageClickDeltaTime=0;click_delta_max_diff=0;tab.textContent="average ClickDeltaTime: 0.00ms +/-0.00, Average Frequency: 0.00 Hz, count:0, x:0, y:0"};function updateCanvasDimensions() 91 | # {canvas.width=window.innerWidth;canvas.height=window.innerHeight;graphCanvas.width=window.innerWidth;drawTimeDeltaGraph()}function drawTimeDeltaGraph(){const graphCtx= 92 | # graphCanvas.getContext("2d");graphCtx.clearRect(0,0,graphCanvas.width,graphCanvas.height);graphCtx.globalAlpha=0.3;graphCtx.fillStyle="white";graphCtx.fillRect(0,0,graphCanvas.width, 93 | # graphCanvas.height);graphCtx.globalAlpha=1;graphCtx.fillStyle="black";if(timeDeltaData.length>graphCanvas.width){timeDeltaData.splice(0,timeDeltaData.length-graphCanvas.width)} 94 | # const maxTimeDelta=Math.max(...timeDeltaData);const scaleFactor=graphCanvas.height/maxTimeDelta;const gridSpacing=20;graphCtx.strokeStyle="black";graphCtx.beginPath();for(let y=0;y<= 95 | # graphCanvas.height;y+=gridSpacing){graphCtx.moveTo(0,y);graphCtx.lineTo(graphCanvas.width,y);const timeValue=(maxTimeDelta*(graphCanvas.height-y)/graphCanvas.height).toFixed(2); 96 | # if(isFinite(timeValue)){graphCtx.fillText(timeValue+" ms",graphCanvas.width-50,y+12)}}graphCtx.stroke();graphCtx.beginPath();graphCtx.strokeStyle="black";graphCtx.moveTo(0,0); 97 | # graphCtx.lineTo(graphCanvas.width,0);graphCtx.stroke();graphCtx.beginPath();graphCtx.strokeStyle="black";graphCtx.moveTo(0,graphCanvas.height);graphCtx.lineTo(graphCanvas.width, 98 | # graphCanvas.height);graphCtx.stroke();graphCtx.beginPath();graphCtx.strokeStyle="green";graphCtx.moveTo(0,graphCanvas.height-timeDeltaData[0]*scaleFactor);for(let i=1;i< 99 | # timeDeltaData.length;i+=1){graphCtx.lineTo(i,graphCanvas.height-timeDeltaData[i]*scaleFactor)}graphCtx.stroke();graphCtx.fillStyle="black";graphCtx.font="12px Arial"; 100 | # graphCtx.fillText("0 ms",2,graphCanvas.height-2);graphCtx.fillText(`${timeDeltaData.length-1} ms`,graphCanvas.width-30,graphCanvas.height-2);graphCtx.fillText(`${maxTimeDelta.toFixed(2)} ms` 101 | # ,2,10)}function move_handler(event){const currentTime=Date.now();const delta=currentTime-lastEventTime;const x=event.x;const y=event.y;if(delta<=100){if(lastEventTime!==0){timeSinceClear+= 102 | # delta;timeDeltaData.push(delta);const averageDelta=timeDeltaData.length===0?0:timeDeltaData.reduce((sum,value)=>sum+value)/timeDeltaData.length;const frequency=averageDelta===0?0:1000/ 103 | # averageDelta;tab.textContent=`average ClickDeltaTime: ${ averageClickDeltaTime }ms +/-${ click_delta_max_diff }, Average Frequency: ${frequency.toFixed(2)} Hz, count:${timeDeltaData.length } 104 | # ,x:${ x }, y:${ y }`;drawTimeDeltaGraph()}}lastEventTime=currentTime;plot_point(x,y)}function click_handler(e){plot_point(e.x,e.y,"green",5)};function mouseup_handler(){const mouseupTime= 105 | # Date.now();const deltaTime=mouseupTime-mousedownTime;clickdeltaTimes.push(deltaTime);delta_average=clickdeltaTimes.reduce((sum,time)=>sum+time, 106 | # 0)/clickdeltaTimes.length;click_delta_max_diff=round_2(((Math.max(...clickdeltaTimes)-delta_average)+(delta_average-Math.min(...clickdeltaTimes)))/2);averageClickDeltaTime= 107 | # round_2(delta_average)}document.body.appendChild(canvas);document.body.appendChild(graphCanvas);document.addEventListener("mousemove",move_handler);document.addEventListener("click", 108 | # click_handler);document.addEventListener("mousedown",(e)=>{mousedownTime=Date.now()});document.body.appendChild(tab);window.addEventListener("resize",updateCanvasDimensions); 109 | # document.body.appendChild(clearButton);clearButton.addEventListener("click",clear);updateCanvasDimensions();document.addEventListener("mouseup",mouseup_handler);})""" 110 | # sync_page.add_init_script(x) 111 | 112 | sync_driver.get(server.PREFIX + "/input/scrollable.html") 113 | sync_driver.sleep(1) 114 | sync_driver.sync_input.move(500, 100) # type: ignore[attr-defined] 115 | 116 | sync_locator = sync_driver.find_element(By.ID, "button-12") 117 | x, y = get_locator_pos(sync_locator) 118 | sync_driver.sync_input.move(x, y) # type: ignore[attr-defined] 119 | 120 | time.sleep(0.5) 121 | assert sync_driver.execute_script("return document.querySelector('button:hover').id") == "button-12" 122 | 123 | 124 | def test_fill_input(sync_driver: Chrome, server: Server) -> None: 125 | sync_driver.get(server.PREFIX + "/input/textarea.html") 126 | sync_driver.sleep(1) 127 | sync_locator = sync_driver.find_element(By.XPATH, "//input") 128 | assert sync_locator 129 | 130 | x, y = get_locator_pos(sync_locator) 131 | sync_driver.sync_input.click("left", x, y) # type: ignore[attr-defined] 132 | sync_driver.sync_input.type("some value", fill=True) # type: ignore[attr-defined] 133 | assert sync_driver.execute_script("return result") == "some value" 134 | 135 | 136 | def test_keyboard_type_into_a_textarea(sync_driver: Chrome) -> None: 137 | sync_driver.execute_script( 138 | """ 139 | const textarea = document.createElement('textarea'); 140 | document.body.appendChild(textarea); 141 | textarea.focus(); 142 | """ 143 | ) 144 | sync_driver.sleep(1) 145 | text = "Hello world. I +am the %text that was typed!" 146 | 147 | sync_locator = sync_driver.find_element(By.XPATH, "//textarea") 148 | assert sync_locator 149 | 150 | x, y = get_locator_pos(sync_locator) 151 | sync_driver.sync_input.click("left", x, y) # type: ignore[attr-defined] 152 | 153 | sync_driver.sync_input.type(text) # type: ignore[attr-defined] 154 | assert sync_driver.execute_script('return document.querySelector("textarea").value') == text 155 | 156 | 157 | def test_quit_exception(sync_driver: Chrome) -> None: 158 | sync_driver.quit() 159 | time.sleep(5) 160 | 161 | with pytest.raises(WindowClosedException): 162 | sync_driver.sync_input.down("left", 100, 100, emulate_behaviour=False) 163 | with pytest.raises(WindowClosedException): 164 | sync_driver.sync_input.up("left", 110, 110) 165 | with pytest.raises(WindowClosedException): 166 | sync_driver.sync_input.move(50, 50, emulate_behaviour=False) 167 | with pytest.raises(WindowClosedException): 168 | sync_driver.sync_input.scroll("up", 10) 169 | with pytest.raises(WindowClosedException): 170 | sync_driver.sync_input.type("test") 171 | -------------------------------------------------------------------------------- /tests/test_sync_playwright.py: -------------------------------------------------------------------------------- 1 | import time 2 | 3 | import pytest 4 | from playwright.sync_api import Locator, Page 5 | 6 | from cdp_patches.input.exceptions import WindowClosedException 7 | from tests.server import Server 8 | 9 | # from input import KeyboardCodes 10 | 11 | 12 | def get_locator_pos(locator: Locator): 13 | bounding_box = locator.bounding_box() 14 | assert bounding_box 15 | 16 | x, y, width, height = bounding_box.get("x"), bounding_box.get("y"), bounding_box.get("width"), bounding_box.get("height") 17 | assert x and y and width and height 18 | 19 | x, y = x + width // 2, y + height // 2 20 | return x, y 21 | 22 | 23 | def test_input_leak(sync_page: Page, server: Server) -> None: 24 | sync_page.goto(server.PREFIX + "/input/button.html") 25 | sync_page.evaluate( 26 | """ 27 | const click_elem = document.querySelector("button") 28 | window.is_leaking = new Promise((resolve, reject) => {callback = resolve}); 29 | const on_click = async function(e){ 30 | var is_bot = (e.pageY == e.screenY && e.pageX == e.screenX) 31 | if (is_bot && 1 >= outerHeight - innerHeight){ // fullscreen 32 | is_bot = false 33 | } 34 | callback(is_bot) 35 | } 36 | click_elem.removeEventListener("mousedown", self) 37 | click_elem.addEventListener("mousedown", on_click) 38 | """ 39 | ) 40 | sync_locator = sync_page.locator("button") 41 | x, y = get_locator_pos(sync_locator) 42 | sync_page.sync_input.click("left", x, y) # type: ignore[attr-defined] 43 | 44 | is_leaking = sync_page.evaluate("() => window.is_leaking") 45 | assert not is_leaking 46 | 47 | 48 | def test_click_the_button(sync_page: Page, server: Server) -> None: 49 | sync_page.goto(server.PREFIX + "/input/button.html") 50 | locator = sync_page.locator("button") 51 | x, y = get_locator_pos(locator) 52 | sync_page.sync_input.click("left", x, y) # type: ignore[attr-defined] 53 | assert sync_page.evaluate("result") == "Clicked" 54 | 55 | 56 | def test_double_click_the_button(sync_page: Page, server: Server) -> None: 57 | sync_page.goto(server.PREFIX + "/input/button.html") 58 | sync_page.evaluate( 59 | """() => { 60 | window.double = false; 61 | button = document.querySelector('button'); 62 | button.addEventListener('dblclick', event => window.double = true); 63 | }""" 64 | ) 65 | 66 | locator = sync_page.locator("button") 67 | x, y = get_locator_pos(locator) 68 | sync_page.sync_input.double_click("left", x, y) # type: ignore[attr-defined] 69 | assert sync_page.evaluate("double") 70 | assert sync_page.evaluate("result") == "Clicked" 71 | 72 | 73 | def test_locators_hover(sync_page: Page, server: Server) -> None: 74 | sync_page.goto(server.PREFIX + "/input/scrollable.html") 75 | sync_page.sync_input.move(500, 100) # type: ignore[attr-defined] 76 | 77 | button = sync_page.locator("#button-12") 78 | x, y = get_locator_pos(button) 79 | sync_page.sync_input.move(x, y) # type: ignore[attr-defined] 80 | 81 | time.sleep(0.5) 82 | assert sync_page.evaluate("document.querySelector('button:hover').id") == "button-12" 83 | 84 | 85 | @pytest.mark.skip(reason="Scroll Tests currently arent implemented properly.") 86 | def test_scroll(sync_page: Page, server: Server) -> None: 87 | sync_page.goto(server.PREFIX + "/offscreenbuttons.html") 88 | for i in range(11): 89 | button = sync_page.query_selector(f"#btn{i}") 90 | assert button 91 | before = button.evaluate( 92 | """button => { 93 | return button.getBoundingClientRect().right - window.innerWidth 94 | }""" 95 | ) 96 | 97 | assert before == 10 * i 98 | # button.scroll_into_view_if_needed() 99 | sync_page.sync_input.scroll("right", i) # type: ignore[attr-defined] 100 | 101 | after = button.evaluate( 102 | """button => { 103 | return button.getBoundingClientRect().right - window.innerWidth 104 | }""" 105 | ) 106 | 107 | assert after <= 0 108 | sync_page.evaluate("() => window.scrollTo(0, 0)") 109 | 110 | 111 | def test_fill_input(sync_page: Page, server: Server) -> None: 112 | sync_page.goto(server.PREFIX + "/input/textarea.html") 113 | handle = sync_page.locator("input") 114 | assert handle 115 | 116 | x, y = get_locator_pos(handle) 117 | sync_page.sync_input.click("left", x, y) # type: ignore[attr-defined] 118 | sync_page.sync_input.type("some value", fill=True) # type: ignore[attr-defined] 119 | assert sync_page.evaluate("result") == "some value" 120 | 121 | 122 | def test_keyboard_type_into_a_textarea(sync_page: Page) -> None: 123 | sync_page.evaluate( 124 | """ 125 | const textarea = document.createElement('textarea'); 126 | document.body.appendChild(textarea); 127 | textarea.focus(); 128 | """ 129 | ) 130 | text = "Hello world. I +am the %text that was typed!" 131 | 132 | handle = sync_page.locator("textarea") 133 | assert handle 134 | 135 | x, y = get_locator_pos(handle) 136 | sync_page.sync_input.click("left", x, y) # type: ignore[attr-defined] 137 | 138 | sync_page.sync_input.type(text) # type: ignore[attr-defined] 139 | assert sync_page.evaluate('document.querySelector("textarea").value') == text 140 | 141 | 142 | # def test_should_report_shiftkey(sync_page: Page, server: Server) -> None: 143 | # sync_page.goto(server.PREFIX + "/input/keyboard.html") 144 | # code_for_key = {KeyboardCodes.VK_SHIFT: ["Shift", "16"], KeyboardCodes.VK_CONTROL: ["Control", "17"]} 145 | # # , KeyboardCodes.VK_MENU: ["Alt", "18"] 146 | # 147 | # handle = sync_page.locator("textarea") 148 | # assert handle 149 | # 150 | # x, y = get_locator_pos(handle) 151 | # sync_page.sync_input.click("left", x, y) # type: ignore[attr-defined] 152 | # 153 | # for modifier_key, js_key in code_for_key.items(): 154 | # sync_page.sync_input.press_keys(modifier_key) # type: ignore[attr-defined] 155 | # # sync_page.sync_input._base.browser_window.send_keystrokes("{VK_SHIFT down} bruh") # type: ignore[attr-defined] 156 | # 157 | # assert ( 158 | # sync_page.evaluate("() => getResult()") 159 | # == "Keydown: " 160 | # + js_key[0] 161 | # + " " 162 | # + js_key[0] 163 | # + "Left " 164 | # + js_key[1] 165 | # + " [" 166 | # + js_key[0] 167 | # + "]" 168 | # ) 169 | # 170 | # sync_page.sync_input.press_keys("!") # type: ignore[attr-defined] 171 | # # Shift+! will generate a keypress 172 | # if js_key[0] == "Shift": 173 | # assert ( 174 | # sync_page.evaluate("() => getResult()") 175 | # == "Keydown: ! Digit1 49 [" 176 | # + js_key[0] 177 | # + "]\nKeypress: ! Digit1 33 33 [" 178 | # + js_key[0] 179 | # + "]" 180 | # ) 181 | # else: 182 | # assert ( 183 | # sync_page.evaluate("() => getResult()") 184 | # == "Keydown: ! Digit1 49 [" + js_key[0] + "]" 185 | # ) 186 | # 187 | # sync_page.sync_input.release_keys("!") # type: ignore[attr-defined] 188 | # assert ( 189 | # sync_page.evaluate("() => getResult()") 190 | # == "Keyup: ! Digit1 49 [" + js_key[0] + "]" 191 | # ) 192 | # sync_page.sync_input.release_keys(modifier_key) # type: ignore[attr-defined] 193 | # assert ( 194 | # sync_page.evaluate("() => getResult()") 195 | # == "Keyup: " 196 | # + js_key[0] 197 | # + " " 198 | # + js_key[0] 199 | # + "Left " 200 | # + js_key[1] 201 | # + " []" 202 | # ) 203 | 204 | 205 | def test_quit_exception(sync_page: Page) -> None: 206 | sync_page.close() 207 | time.sleep(5) 208 | 209 | with pytest.raises(WindowClosedException): 210 | sync_page.sync_input.down("left", 100, 100, emulate_behaviour=False) # type: ignore[attr-defined] 211 | with pytest.raises(WindowClosedException): 212 | sync_page.sync_input.up("left", 110, 110) # type: ignore[attr-defined] 213 | with pytest.raises(WindowClosedException): 214 | sync_page.sync_input.move(50, 50, emulate_behaviour=False) # type: ignore[attr-defined] 215 | with pytest.raises(WindowClosedException): 216 | sync_page.sync_input.scroll("up", 10) # type: ignore[attr-defined] 217 | with pytest.raises(WindowClosedException): 218 | sync_page.sync_input.type("test") # type: ignore[attr-defined] 219 | --------------------------------------------------------------------------------