├── .github └── dependabot.yml ├── .gitignore ├── .isort.cfg ├── .taskcluster.yml ├── CODE_OF_CONDUCT.md ├── LICENSE ├── MANIFEST.in ├── README.md ├── coverage_crawler ├── __init__.py ├── crawler.py ├── diff.py ├── filterpaths.py ├── generatehtml.py └── latest_cov_build.py ├── download_artifacts.py ├── requirements.txt ├── run_crawler.py ├── setup.cfg ├── setup.py ├── style.css ├── test-requirements.txt ├── tests ├── __init__.py ├── crawler │ ├── __init__.py │ └── test_crawler_unittest.py ├── example_website │ ├── __init__.py │ ├── templates │ │ └── index.html │ └── website_app.py ├── test_coverage_crawler.py └── test_diff.py └── websites.txt /.github/dependabot.yml: -------------------------------------------------------------------------------- 1 | version: 2 2 | updates: 3 | - package-ecosystem: pip 4 | directory: "/" 5 | schedule: 6 | interval: monthly 7 | open-pull-requests-limit: 99 8 | -------------------------------------------------------------------------------- /.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 | env/ 12 | build/ 13 | develop-eggs/ 14 | dist/ 15 | downloads/ 16 | eggs/ 17 | .eggs/ 18 | lib/ 19 | lib64/ 20 | parts/ 21 | sdist/ 22 | var/ 23 | wheels/ 24 | *.egg-info/ 25 | .installed.cfg 26 | *.egg 27 | 28 | # PyInstaller 29 | # Usually these files are written by a python script from a template 30 | # before PyInstaller builds the exe, so as to inject date/other infos into it. 31 | *.manifest 32 | *.spec 33 | 34 | # Installer logs 35 | pip-log.txt 36 | pip-delete-this-directory.txt 37 | 38 | # Unit test / coverage reports 39 | htmlcov/ 40 | .tox/ 41 | .coverage 42 | .coverage.* 43 | .cache 44 | nosetests.xml 45 | coverage.xml 46 | *.cover 47 | .hypothesis/ 48 | 49 | # Translations 50 | *.mo 51 | *.pot 52 | 53 | # Django stuff: 54 | *.log 55 | local_settings.py 56 | 57 | # Flask stuff: 58 | instance/ 59 | .webassets-cache 60 | 61 | # Scrapy stuff: 62 | .scrapy 63 | 64 | # Sphinx documentation 65 | docs/_build/ 66 | 67 | # PyBuilder 68 | target/ 69 | 70 | # Jupyter Notebook 71 | .ipynb_checkpoints 72 | 73 | # pyenv 74 | .python-version 75 | 76 | # celery beat schedule file 77 | celerybeat-schedule 78 | 79 | # SageMath parsed files 80 | *.sage.py 81 | 82 | # dotenv 83 | .env 84 | 85 | # virtualenv 86 | .venv 87 | venv/ 88 | ENV/ 89 | 90 | # Spyder project settings 91 | .spyderproject 92 | .spyproject 93 | 94 | # Rope project settings 95 | .ropeproject 96 | 97 | # mkdocs documentation 98 | /site 99 | 100 | # mypy 101 | .mypy_cache/ 102 | 103 | data/ 104 | tools/ 105 | ccov-artifacts/ 106 | .pytest_cache/ 107 | lcov/ 108 | lcov-bin/ 109 | mozilla-central/ 110 | report/ 111 | output.json 112 | tests_report.json 113 | output.info 114 | *.zip 115 | -------------------------------------------------------------------------------- /.isort.cfg: -------------------------------------------------------------------------------- 1 | [settings] 2 | force_single_line=True 3 | -------------------------------------------------------------------------------- /.taskcluster.yml: -------------------------------------------------------------------------------- 1 | version: 1 2 | policy: 3 | pullRequests: public 4 | tasks: 5 | $let: 6 | user: ${event.sender.login} 7 | 8 | head_branch: 9 | $if: 'tasks_for == "github-pull-request"' 10 | then: ${event.pull_request.head.ref} 11 | else: 12 | $if: 'tasks_for == "github-push"' 13 | then: ${event.ref} 14 | else: ${event.release.target_commitish} 15 | 16 | head_rev: 17 | $if: 'tasks_for == "github-pull-request"' 18 | then: ${event.pull_request.head.sha} 19 | else: 20 | $if: 'tasks_for == "github-push"' 21 | then: ${event.after} 22 | else: ${event.release.tag_name} 23 | 24 | repository: 25 | $if: 'tasks_for == "github-pull-request"' 26 | then: ${event.pull_request.head.repo.html_url} 27 | else: ${event.repository.html_url} 28 | in: 29 | - taskId: {$eval: as_slugid("tests_task")} 30 | created: {$fromNow: ''} 31 | deadline: {$fromNow: '1 hour'} 32 | provisionerId: proj-relman 33 | workerType: ci 34 | payload: 35 | maxRunTime: 3600 36 | image: python:3.7 37 | command: 38 | - "/bin/bash" 39 | - "-lcx" 40 | - "git clone --quiet ${repository} && 41 | cd coverage-crawler && 42 | git -c advice.detachedHead=false checkout ${head_rev} && 43 | pip install --quiet -r requirements.txt && 44 | pip install --quiet -r test-requirements.txt && 45 | flake8 . && 46 | python -m pytest tests/test_*.py && 47 | python setup.py sdist bdist_wheel && 48 | pip install dist/coverage_crawler-1.0.0.tar.gz" 49 | metadata: 50 | name: coverage-crawler tests 51 | description: coverage-crawler tests 52 | owner: mcastelluccio@mozilla.com 53 | source: ${repository}/raw/${head_rev}/.taskcluster.yml 54 | -------------------------------------------------------------------------------- /CODE_OF_CONDUCT.md: -------------------------------------------------------------------------------- 1 | # Community Participation Guidelines 2 | 3 | This repository is governed by Mozilla's code of conduct and etiquette guidelines. 4 | For more details, please read the 5 | [Mozilla Community Participation Guidelines](https://www.mozilla.org/about/governance/policies/participation/). 6 | 7 | ## How to Report 8 | For more information on how to report violations of the Community Participation Guidelines, please read our '[How to Report](https://www.mozilla.org/about/governance/policies/participation/reporting/)' page. 9 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Mozilla Public License Version 2.0 2 | ================================== 3 | 4 | 1. Definitions 5 | -------------- 6 | 7 | 1.1. "Contributor" 8 | means each individual or legal entity that creates, contributes to 9 | the creation of, or owns Covered Software. 10 | 11 | 1.2. "Contributor Version" 12 | means the combination of the Contributions of others (if any) used 13 | by a Contributor and that particular Contributor's Contribution. 14 | 15 | 1.3. "Contribution" 16 | means Covered Software of a particular Contributor. 17 | 18 | 1.4. "Covered Software" 19 | means Source Code Form to which the initial Contributor has attached 20 | the notice in Exhibit A, the Executable Form of such Source Code 21 | Form, and Modifications of such Source Code Form, in each case 22 | including portions thereof. 23 | 24 | 1.5. "Incompatible With Secondary Licenses" 25 | means 26 | 27 | (a) that the initial Contributor has attached the notice described 28 | in Exhibit B to the Covered Software; or 29 | 30 | (b) that the Covered Software was made available under the terms of 31 | version 1.1 or earlier of the License, but not also under the 32 | terms of a Secondary License. 33 | 34 | 1.6. "Executable Form" 35 | means any form of the work other than Source Code Form. 36 | 37 | 1.7. "Larger Work" 38 | means a work that combines Covered Software with other material, in 39 | a separate file or files, that is not Covered Software. 40 | 41 | 1.8. "License" 42 | means this document. 43 | 44 | 1.9. "Licensable" 45 | means having the right to grant, to the maximum extent possible, 46 | whether at the time of the initial grant or subsequently, any and 47 | all of the rights conveyed by this License. 48 | 49 | 1.10. "Modifications" 50 | means any of the following: 51 | 52 | (a) any file in Source Code Form that results from an addition to, 53 | deletion from, or modification of the contents of Covered 54 | Software; or 55 | 56 | (b) any new file in Source Code Form that contains any Covered 57 | Software. 58 | 59 | 1.11. "Patent Claims" of a Contributor 60 | means any patent claim(s), including without limitation, method, 61 | process, and apparatus claims, in any patent Licensable by such 62 | Contributor that would be infringed, but for the grant of the 63 | License, by the making, using, selling, offering for sale, having 64 | made, import, or transfer of either its Contributions or its 65 | Contributor Version. 66 | 67 | 1.12. "Secondary License" 68 | means either the GNU General Public License, Version 2.0, the GNU 69 | Lesser General Public License, Version 2.1, the GNU Affero General 70 | Public License, Version 3.0, or any later versions of those 71 | licenses. 72 | 73 | 1.13. "Source Code Form" 74 | means the form of the work preferred for making modifications. 75 | 76 | 1.14. "You" (or "Your") 77 | means an individual or a legal entity exercising rights under this 78 | License. For legal entities, "You" includes any entity that 79 | controls, is controlled by, or is under common control with You. For 80 | purposes of this definition, "control" means (a) the power, direct 81 | or indirect, to cause the direction or management of such entity, 82 | whether by contract or otherwise, or (b) ownership of more than 83 | fifty percent (50%) of the outstanding shares or beneficial 84 | ownership of such entity. 85 | 86 | 2. License Grants and Conditions 87 | -------------------------------- 88 | 89 | 2.1. Grants 90 | 91 | Each Contributor hereby grants You a world-wide, royalty-free, 92 | non-exclusive license: 93 | 94 | (a) under intellectual property rights (other than patent or trademark) 95 | Licensable by such Contributor to use, reproduce, make available, 96 | modify, display, perform, distribute, and otherwise exploit its 97 | Contributions, either on an unmodified basis, with Modifications, or 98 | as part of a Larger Work; and 99 | 100 | (b) under Patent Claims of such Contributor to make, use, sell, offer 101 | for sale, have made, import, and otherwise transfer either its 102 | Contributions or its Contributor Version. 103 | 104 | 2.2. Effective Date 105 | 106 | The licenses granted in Section 2.1 with respect to any Contribution 107 | become effective for each Contribution on the date the Contributor first 108 | distributes such Contribution. 109 | 110 | 2.3. Limitations on Grant Scope 111 | 112 | The licenses granted in this Section 2 are the only rights granted under 113 | this License. No additional rights or licenses will be implied from the 114 | distribution or licensing of Covered Software under this License. 115 | Notwithstanding Section 2.1(b) above, no patent license is granted by a 116 | Contributor: 117 | 118 | (a) for any code that a Contributor has removed from Covered Software; 119 | or 120 | 121 | (b) for infringements caused by: (i) Your and any other third party's 122 | modifications of Covered Software, or (ii) the combination of its 123 | Contributions with other software (except as part of its Contributor 124 | Version); or 125 | 126 | (c) under Patent Claims infringed by Covered Software in the absence of 127 | its Contributions. 128 | 129 | This License does not grant any rights in the trademarks, service marks, 130 | or logos of any Contributor (except as may be necessary to comply with 131 | the notice requirements in Section 3.4). 132 | 133 | 2.4. Subsequent Licenses 134 | 135 | No Contributor makes additional grants as a result of Your choice to 136 | distribute the Covered Software under a subsequent version of this 137 | License (see Section 10.2) or under the terms of a Secondary License (if 138 | permitted under the terms of Section 3.3). 139 | 140 | 2.5. Representation 141 | 142 | Each Contributor represents that the Contributor believes its 143 | Contributions are its original creation(s) or it has sufficient rights 144 | to grant the rights to its Contributions conveyed by this License. 145 | 146 | 2.6. Fair Use 147 | 148 | This License is not intended to limit any rights You have under 149 | applicable copyright doctrines of fair use, fair dealing, or other 150 | equivalents. 151 | 152 | 2.7. Conditions 153 | 154 | Sections 3.1, 3.2, 3.3, and 3.4 are conditions of the licenses granted 155 | in Section 2.1. 156 | 157 | 3. Responsibilities 158 | ------------------- 159 | 160 | 3.1. Distribution of Source Form 161 | 162 | All distribution of Covered Software in Source Code Form, including any 163 | Modifications that You create or to which You contribute, must be under 164 | the terms of this License. You must inform recipients that the Source 165 | Code Form of the Covered Software is governed by the terms of this 166 | License, and how they can obtain a copy of this License. You may not 167 | attempt to alter or restrict the recipients' rights in the Source Code 168 | Form. 169 | 170 | 3.2. Distribution of Executable Form 171 | 172 | If You distribute Covered Software in Executable Form then: 173 | 174 | (a) such Covered Software must also be made available in Source Code 175 | Form, as described in Section 3.1, and You must inform recipients of 176 | the Executable Form how they can obtain a copy of such Source Code 177 | Form by reasonable means in a timely manner, at a charge no more 178 | than the cost of distribution to the recipient; and 179 | 180 | (b) You may distribute such Executable Form under the terms of this 181 | License, or sublicense it under different terms, provided that the 182 | license for the Executable Form does not attempt to limit or alter 183 | the recipients' rights in the Source Code Form under this License. 184 | 185 | 3.3. Distribution of a Larger Work 186 | 187 | You may create and distribute a Larger Work under terms of Your choice, 188 | provided that You also comply with the requirements of this License for 189 | the Covered Software. If the Larger Work is a combination of Covered 190 | Software with a work governed by one or more Secondary Licenses, and the 191 | Covered Software is not Incompatible With Secondary Licenses, this 192 | License permits You to additionally distribute such Covered Software 193 | under the terms of such Secondary License(s), so that the recipient of 194 | the Larger Work may, at their option, further distribute the Covered 195 | Software under the terms of either this License or such Secondary 196 | License(s). 197 | 198 | 3.4. Notices 199 | 200 | You may not remove or alter the substance of any license notices 201 | (including copyright notices, patent notices, disclaimers of warranty, 202 | or limitations of liability) contained within the Source Code Form of 203 | the Covered Software, except that You may alter any license notices to 204 | the extent required to remedy known factual inaccuracies. 205 | 206 | 3.5. Application of Additional Terms 207 | 208 | You may choose to offer, and to charge a fee for, warranty, support, 209 | indemnity or liability obligations to one or more recipients of Covered 210 | Software. However, You may do so only on Your own behalf, and not on 211 | behalf of any Contributor. You must make it absolutely clear that any 212 | such warranty, support, indemnity, or liability obligation is offered by 213 | You alone, and You hereby agree to indemnify every Contributor for any 214 | liability incurred by such Contributor as a result of warranty, support, 215 | indemnity or liability terms You offer. You may include additional 216 | disclaimers of warranty and limitations of liability specific to any 217 | jurisdiction. 218 | 219 | 4. Inability to Comply Due to Statute or Regulation 220 | --------------------------------------------------- 221 | 222 | If it is impossible for You to comply with any of the terms of this 223 | License with respect to some or all of the Covered Software due to 224 | statute, judicial order, or regulation then You must: (a) comply with 225 | the terms of this License to the maximum extent possible; and (b) 226 | describe the limitations and the code they affect. Such description must 227 | be placed in a text file included with all distributions of the Covered 228 | Software under this License. Except to the extent prohibited by statute 229 | or regulation, such description must be sufficiently detailed for a 230 | recipient of ordinary skill to be able to understand it. 231 | 232 | 5. Termination 233 | -------------- 234 | 235 | 5.1. The rights granted under this License will terminate automatically 236 | if You fail to comply with any of its terms. However, if You become 237 | compliant, then the rights granted under this License from a particular 238 | Contributor are reinstated (a) provisionally, unless and until such 239 | Contributor explicitly and finally terminates Your grants, and (b) on an 240 | ongoing basis, if such Contributor fails to notify You of the 241 | non-compliance by some reasonable means prior to 60 days after You have 242 | come back into compliance. Moreover, Your grants from a particular 243 | Contributor are reinstated on an ongoing basis if such Contributor 244 | notifies You of the non-compliance by some reasonable means, this is the 245 | first time You have received notice of non-compliance with this License 246 | from such Contributor, and You become compliant prior to 30 days after 247 | Your receipt of the notice. 248 | 249 | 5.2. If You initiate litigation against any entity by asserting a patent 250 | infringement claim (excluding declaratory judgment actions, 251 | counter-claims, and cross-claims) alleging that a Contributor Version 252 | directly or indirectly infringes any patent, then the rights granted to 253 | You by any and all Contributors for the Covered Software under Section 254 | 2.1 of this License shall terminate. 255 | 256 | 5.3. In the event of termination under Sections 5.1 or 5.2 above, all 257 | end user license agreements (excluding distributors and resellers) which 258 | have been validly granted by You or Your distributors under this License 259 | prior to termination shall survive termination. 260 | 261 | ************************************************************************ 262 | * * 263 | * 6. Disclaimer of Warranty * 264 | * ------------------------- * 265 | * * 266 | * Covered Software is provided under this License on an "as is" * 267 | * basis, without warranty of any kind, either expressed, implied, or * 268 | * statutory, including, without limitation, warranties that the * 269 | * Covered Software is free of defects, merchantable, fit for a * 270 | * particular purpose or non-infringing. The entire risk as to the * 271 | * quality and performance of the Covered Software is with You. * 272 | * Should any Covered Software prove defective in any respect, You * 273 | * (not any Contributor) assume the cost of any necessary servicing, * 274 | * repair, or correction. This disclaimer of warranty constitutes an * 275 | * essential part of this License. No use of any Covered Software is * 276 | * authorized under this License except under this disclaimer. * 277 | * * 278 | ************************************************************************ 279 | 280 | ************************************************************************ 281 | * * 282 | * 7. Limitation of Liability * 283 | * -------------------------- * 284 | * * 285 | * Under no circumstances and under no legal theory, whether tort * 286 | * (including negligence), contract, or otherwise, shall any * 287 | * Contributor, or anyone who distributes Covered Software as * 288 | * permitted above, be liable to You for any direct, indirect, * 289 | * special, incidental, or consequential damages of any character * 290 | * including, without limitation, damages for lost profits, loss of * 291 | * goodwill, work stoppage, computer failure or malfunction, or any * 292 | * and all other commercial damages or losses, even if such party * 293 | * shall have been informed of the possibility of such damages. This * 294 | * limitation of liability shall not apply to liability for death or * 295 | * personal injury resulting from such party's negligence to the * 296 | * extent applicable law prohibits such limitation. Some * 297 | * jurisdictions do not allow the exclusion or limitation of * 298 | * incidental or consequential damages, so this exclusion and * 299 | * limitation may not apply to You. * 300 | * * 301 | ************************************************************************ 302 | 303 | 8. Litigation 304 | ------------- 305 | 306 | Any litigation relating to this License may be brought only in the 307 | courts of a jurisdiction where the defendant maintains its principal 308 | place of business and such litigation shall be governed by laws of that 309 | jurisdiction, without reference to its conflict-of-law provisions. 310 | Nothing in this Section shall prevent a party's ability to bring 311 | cross-claims or counter-claims. 312 | 313 | 9. Miscellaneous 314 | ---------------- 315 | 316 | This License represents the complete agreement concerning the subject 317 | matter hereof. If any provision of this License is held to be 318 | unenforceable, such provision shall be reformed only to the extent 319 | necessary to make it enforceable. Any law or regulation which provides 320 | that the language of a contract shall be construed against the drafter 321 | shall not be used to construe this License against a Contributor. 322 | 323 | 10. Versions of the License 324 | --------------------------- 325 | 326 | 10.1. New Versions 327 | 328 | Mozilla Foundation is the license steward. Except as provided in Section 329 | 10.3, no one other than the license steward has the right to modify or 330 | publish new versions of this License. Each version will be given a 331 | distinguishing version number. 332 | 333 | 10.2. Effect of New Versions 334 | 335 | You may distribute the Covered Software under the terms of the version 336 | of the License under which You originally received the Covered Software, 337 | or under the terms of any subsequent version published by the license 338 | steward. 339 | 340 | 10.3. Modified Versions 341 | 342 | If you create software not governed by this License, and you want to 343 | create a new license for such software, you may create and use a 344 | modified version of this License if you rename the license and remove 345 | any references to the name of the license steward (except to note that 346 | such modified license differs from this License). 347 | 348 | 10.4. Distributing Source Code Form that is Incompatible With Secondary 349 | Licenses 350 | 351 | If You choose to distribute Source Code Form that is Incompatible With 352 | Secondary Licenses under the terms of this version of the License, the 353 | notice described in Exhibit B of this License must be attached. 354 | 355 | Exhibit A - Source Code Form License Notice 356 | ------------------------------------------- 357 | 358 | This Source Code Form is subject to the terms of the Mozilla Public 359 | License, v. 2.0. If a copy of the MPL was not distributed with this 360 | file, You can obtain one at http://mozilla.org/MPL/2.0/. 361 | 362 | If it is not possible or desirable to put the notice in a particular 363 | file, then You may include the notice in a location (such as a LICENSE 364 | file in a relevant directory) where a recipient would be likely to look 365 | for such a notice. 366 | 367 | You may add additional accurate notices of copyright ownership. 368 | 369 | Exhibit B - "Incompatible With Secondary Licenses" Notice 370 | --------------------------------------------------------- 371 | 372 | This Source Code Form is "Incompatible With Secondary Licenses", as 373 | defined by the Mozilla Public License, v. 2.0. 374 | -------------------------------------------------------------------------------- /MANIFEST.in: -------------------------------------------------------------------------------- 1 | include requirements.txt 2 | include style.css 3 | include websites.txt 4 | 5 | recursive-exclude * __pycache__ 6 | recursive-exclude * *.py[co] 7 | recursive-exclude tests * 8 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # coverage-crawler 2 | [](https://community-tc.services.mozilla.com/api/github/v1/repository/mozilla/coverage-crawler/master/latest) 3 | 4 | A crawler to find websites that exercise code in Firefox that is not covered by unit tests 5 | 6 | ## Software requirements 7 | - Python 3.6+ 8 | - [Mercurial](https://www.mercurial-scm.org/) 9 | 10 | ## Usage as a script 11 | 12 | - Install requirements with `pip install -r requirements.txt`; 13 | - Install development requirements with `pip install -r test-requirements.txt`; 14 | - Run the `download_artifacts.py` script with the desired revision passed as argument to download the latest Firefox coverage build; 15 | - Run the `run_crawler.py` script. 16 | 17 | ## Usage as a module 18 | 19 | - Add this project's repository to your requirements file as a Git dependency; 20 | - Import `coverage_crawler`; 21 | - Use function `download_artifacts` from `latest_cov_build.py` with the desired revision passed as argument to download the latest Firefox coverage build and other artifacts; 22 | - Run function `run` from `crawler.py` with the desired website passed as an argument. 23 | -------------------------------------------------------------------------------- /coverage_crawler/__init__.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | -------------------------------------------------------------------------------- /coverage_crawler/crawler.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | 3 | import json 4 | import os 5 | import random 6 | import subprocess 7 | import sys 8 | import tempfile 9 | import time 10 | import traceback 11 | import uuid 12 | 13 | from selenium import webdriver 14 | from selenium.common.exceptions import ElementNotInteractableException 15 | from selenium.common.exceptions import InvalidSelectorException 16 | from selenium.common.exceptions import NoAlertPresentException 17 | from selenium.common.exceptions import NoSuchWindowException 18 | from selenium.common.exceptions import StaleElementReferenceException 19 | from selenium.common.exceptions import TimeoutException 20 | from selenium.common.exceptions import WebDriverException 21 | from selenium.webdriver.common.by import By 22 | from selenium.webdriver.common.keys import Keys 23 | 24 | from coverage_crawler import diff 25 | from coverage_crawler import filterpaths 26 | from coverage_crawler import generatehtml 27 | 28 | already_clicked_elems = set() 29 | 30 | 31 | def set_timeouts(driver): 32 | driver.set_script_timeout(30) 33 | driver.set_page_load_timeout(30) 34 | driver.implicitly_wait(30) 35 | 36 | 37 | def wait_loaded(driver): 38 | try: 39 | driver.execute_async_script(""" 40 | let done = arguments[0]; 41 | 42 | window.onload = done; 43 | if (document.readyState === "complete") { 44 | done(); 45 | } 46 | """) 47 | except: # noqa: E722 48 | traceback.print_exc(file=sys.stderr) 49 | print('Continuing...') 50 | 51 | # We hope the page is fully loaded in 7 seconds. 52 | time.sleep(7) 53 | 54 | try: 55 | driver.execute_async_script(""" 56 | window.requestIdleCallback(arguments[0], { 57 | timeout: 60000 58 | }); 59 | """) 60 | except: # noqa: E722 61 | traceback.print_exc(file=sys.stderr) 62 | print('Continuing...') 63 | 64 | 65 | def close_all_windows_except_first(driver): 66 | windows = driver.window_handles 67 | 68 | for window in windows[1:]: 69 | driver.switch_to.window(window) 70 | driver.close() 71 | 72 | while True: 73 | try: 74 | alert = driver.switch_to.alert() 75 | alert.dismiss() 76 | except (NoAlertPresentException, NoSuchWindowException): 77 | break 78 | 79 | driver.switch_to.window(windows[0]) 80 | 81 | 82 | def find_children(driver): 83 | body = driver.find_elements(By.TAG_NAME, 'body') 84 | assert len(body) == 1 85 | body = body[0] 86 | 87 | body.send_keys(Keys.CONTROL, 0) 88 | 89 | buttons = body.find_elements(By.TAG_NAME, 'button') 90 | links = body.find_elements(By.TAG_NAME, 'a') 91 | inputs = body.find_elements(By.TAG_NAME, 'input') 92 | selects = body.find_elements(By.TAG_NAME, 'select') 93 | children = buttons + links + inputs + selects 94 | 95 | random.shuffle(children) 96 | 97 | return children 98 | 99 | 100 | def do_something(driver): 101 | not_clickable_elems = set() 102 | 103 | children = find_children(driver) 104 | 105 | while True: 106 | elem = None 107 | 108 | try: 109 | # If we have clickable elements on which we haven't clicked yet, use them; otherwise, use all elements 110 | if set(children) - already_clicked_elems > not_clickable_elems: 111 | children = list(set(children) - already_clicked_elems) 112 | 113 | for child in children: 114 | # If the element is not displayed or is disabled, the user can't interact with it. Skip 115 | # non-displayed/disabled elements, since we're trying to mimic a real user. 116 | if not child.is_displayed() or not child.is_enabled() or child in not_clickable_elems: 117 | continue 118 | 119 | elem = child 120 | break 121 | 122 | if elem is None: 123 | return None 124 | 125 | driver.execute_script('return arguments[0].scrollIntoView();', elem) 126 | time.sleep(1) 127 | 128 | if elem.tag_name in ['button', 'a']: 129 | elem.click() 130 | elif elem.tag_name == 'input': 131 | input_type = elem.get_attribute('type') 132 | if input_type == 'url': 133 | elem.send_keys('http://www.mozilla.org/') 134 | elif input_type == 'text': 135 | elem.send_keys('marco') 136 | elif input_type == 'email': 137 | elem.send_keys('prova@email.it') 138 | elif input_type == 'password': 139 | elem.send_keys('aMildlyComplexPasswordIn2017') 140 | elif input_type == 'checkbox': 141 | elem.click() 142 | elif input_type == 'number': 143 | elem.send_keys('3') 144 | elif input_type in ['submit', 'reset', 'button']: 145 | elem.click() 146 | elif input_type == 'color': 147 | driver.execute_script("arguments[0].value = '#ff0000'", elem) 148 | elif input_type == 'search': 149 | elem.clear() 150 | elem.send_keys('quick search') 151 | elif input_type == 'radio': 152 | elem.click() 153 | elif input_type == 'tel': 154 | elem.send_keys('1234567890') 155 | elif input_type == 'date': 156 | elem.send_keys('20000101') 157 | else: 158 | raise Exception('Unsupported input type: %s' % input_type) 159 | elif elem.tag_name == 'select': 160 | for option in elem.find_elements(By.TAG_NAME, 'option'): 161 | if option.text != '': 162 | option.click() 163 | break 164 | 165 | already_clicked_elems.add(elem) 166 | 167 | close_all_windows_except_first(driver) 168 | 169 | # Get all the attributes of the child. 170 | return get_all_attributes(driver, child) 171 | 172 | except StaleElementReferenceException: 173 | traceback.print_exc(file=sys.stderr) 174 | close_all_windows_except_first(driver) 175 | children = find_children(driver) 176 | 177 | except (ElementNotInteractableException, InvalidSelectorException, WebDriverException): 178 | # Ignore frequent exceptions. 179 | traceback.print_exc(file=sys.stderr) 180 | close_all_windows_except_first(driver) 181 | not_clickable_elems.add(elem) 182 | 183 | 184 | def get_all_attributes(driver, child): 185 | child_attributes = driver.execute_script(""" 186 | let elem_attribute = {}; 187 | 188 | for (let i = 0; i < arguments[0].attributes.length; i++) { 189 | elem_attribute[arguments[0].attributes[i].name] = arguments[0].attributes[i].value; 190 | } 191 | return elem_attribute; 192 | """, child) 193 | 194 | return child_attributes 195 | 196 | 197 | def run_in_driver(website, driver): 198 | print('Running {}'.format(website)) 199 | 200 | try: 201 | driver.get(website) 202 | except TimeoutException: 203 | # Ignore timeouts, as they are too frequent. 204 | traceback.print_exc(file=sys.stderr) 205 | print('Continuing...') 206 | 207 | saved_sequence = [] 208 | for i in range(0, 20): 209 | print('Iteration {}'.format(i)) 210 | try: 211 | elem_attributes = do_something(driver) 212 | if elem_attributes is None: 213 | print('Cannot find any element to interact with on {}'.format(website)) 214 | break 215 | saved_sequence.append(elem_attributes) 216 | 217 | print(' - Using {}'.format(elem_attributes)) 218 | except TimeoutException: 219 | # Ignore frequent Timeout exceptions. 220 | traceback.print_exc(file=sys.stderr) 221 | print('Continuing...') 222 | 223 | return saved_sequence 224 | 225 | 226 | def run(websites): 227 | # Environmental vars set to overwrite default location of .gcda files 228 | if sys.platform.startswith('linux') or sys.platform.startswith('darwin'): 229 | prefix = '/builds/worker/workspace/build/src/' 230 | strip_count = prefix.count('/') 231 | elif sys.platform.startswith('cygwin') or sys.platform.startswith('win32'): 232 | prefix = 'z:/build/build/src/' 233 | strip_count = prefix.count('/') + 1 234 | 235 | # Remove a prefix from the path where .gcda files are stored 236 | os.environ['GCOV_PREFIX_STRIP'] = str(strip_count) 237 | os.environ['PATH'] += os.pathsep + os.path.abspath('tools') 238 | os.environ['MOZ_HEADLESS'] = '1' 239 | already_clicked_elems.clear() 240 | # Create temporary directories with context manager 241 | with tempfile.TemporaryDirectory() as gcov_dir, tempfile.TemporaryDirectory() as jsvm_dir: 242 | os.environ['GCOV_PREFIX'] = gcov_dir 243 | os.environ['JS_CODE_COVERAGE_OUTPUT_DIR'] = jsvm_dir 244 | 245 | # Webdriver uses Firefox Binaries from downloaded cov build 246 | driver = webdriver.Firefox(firefox_binary='tools/firefox/firefox-bin') 247 | 248 | set_timeouts(driver) 249 | 250 | for website in websites: 251 | # All steps are stored in new folder 252 | data_folder = str(uuid.uuid4()) 253 | os.makedirs(data_folder, exist_ok=True) 254 | try: 255 | sequence = run_in_driver(website, driver) 256 | with open('{}/steps.txt'.format(data_folder), 'w') as f: 257 | f.write('Website name: ' + website + '\n') 258 | for element in sequence: 259 | f.write(json.dumps(element) + '\n') 260 | except: # noqa: E722 261 | traceback.print_exc(file=sys.stderr) 262 | close_all_windows_except_first(driver) 263 | 264 | # Add paths to Mozilla-central modules 265 | sys.path.insert(0, 'tools/mozbuild/codecoverage') 266 | sys.path.insert(0, 'tools') 267 | 268 | from lcov_rewriter import LcovFileRewriter 269 | jsvm_files = [os.path.join(jsvm_dir, e) for e in os.listdir(jsvm_dir)] 270 | rewriter = LcovFileRewriter(os.path.join('tools', 'chrome-map.json')) 271 | jsvm_output_dir = os.path.join(jsvm_dir, 'jsvm_output') 272 | os.makedirs(jsvm_output_dir, exist_ok=True) 273 | jsvm_output_file = os.path.join(jsvm_output_dir, 'jsvm_lcov_output.info') 274 | rewriter.rewrite_files(jsvm_files, jsvm_output_file, '') 275 | 276 | grcov_command = [ 277 | os.path.join('tools', 'grcov'), 278 | '-t', 'coveralls+', 279 | '-p', prefix, 280 | 'tools', gcov_dir, 281 | jsvm_output_dir, 282 | '--filter', 'covered', 283 | '--token', 'UNUSED', 284 | '--commit-sha', 'UNUSED' 285 | ] 286 | 287 | with open('output.json', 'w+') as outfile: 288 | subprocess.check_call(grcov_command, stdout=outfile) 289 | 290 | with open('tests_report.json') as baseline_rep, open('output.json') as rep: 291 | baseline_report = json.load(baseline_rep) 292 | report = json.load(rep) 293 | 294 | filterpaths.ignore_third_party_filter(report) 295 | 296 | # Create diff report 297 | diff_report = diff.compare_reports(baseline_report, report, True) 298 | 299 | with open('{}/diff.json'.format(data_folder), 'w') as outfile: 300 | json.dump(diff_report, outfile) 301 | 302 | generatehtml.generate_html(data_folder) 303 | 304 | driver.quit() 305 | 306 | return os.path.abspath(os.path.join(os.getcwd(), '{}/report'.format(data_folder))) 307 | -------------------------------------------------------------------------------- /coverage_crawler/diff.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | 3 | 4 | def diff_line(i, j, ignore_hits): 5 | if j is None: 6 | return None 7 | elif i is None: 8 | return j 9 | elif j > 0 and j > i: 10 | if ignore_hits and i == 0: 11 | return 1 12 | elif ignore_hits is False: 13 | return j - i 14 | else: 15 | return 0 16 | else: 17 | return 0 18 | 19 | 20 | def compare_source_files_objects(obj1, obj2, ignore_hits): 21 | diff_funcs = [] 22 | diff_cov = [] 23 | if obj1['name'] == obj2['name']: 24 | 25 | if obj1['coverage'] != obj2['coverage']: 26 | diff_cov = [diff_line(k, m, ignore_hits) for k, m in zip(obj1['coverage'], obj2['coverage'])] 27 | 28 | if obj1['functions'] != obj2['functions']: 29 | for func1 in obj1['functions']: 30 | for func2 in obj2['functions']: 31 | if func1['name'] == func2['name']: 32 | if func1['exec'] is False and func2['exec'] is True: 33 | diff_funcs.append(func2) 34 | 35 | if len(diff_funcs) == 0 and all(cov == 0 or cov is None for cov in diff_cov): 36 | return None 37 | else: 38 | obj1['coverage'] = diff_cov 39 | obj1['functions'] = diff_funcs 40 | return obj1 41 | 42 | 43 | def compare_reports(baseline_report, report, ignore_hits): 44 | baseline_coverage = baseline_report['source_files'] 45 | coverage = report['source_files'] 46 | source_files = [] 47 | diff_report = {} 48 | for i in baseline_coverage: 49 | for j in coverage: 50 | comp_result = compare_source_files_objects(i, j, ignore_hits) 51 | if comp_result is not None: 52 | source_files.append(comp_result) 53 | diff_report['source_files'] = source_files 54 | for name in ['git', 'repo_token', 'service_job_number', 'service_name', 'service_number']: 55 | diff_report[name] = baseline_report[name] 56 | 57 | return diff_report 58 | -------------------------------------------------------------------------------- /coverage_crawler/filterpaths.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | 3 | 4 | def ignore_third_party_filter(report): 5 | with open('mozilla-central/tools/rewriting/ThirdPartyPaths.txt') as f: 6 | third_party_paths = [path.strip('\n') for path in f] 7 | report['source_files'] = [sf for sf in report['source_files'] if not any(sf['name'].startswith(path) for path in third_party_paths)] 8 | -------------------------------------------------------------------------------- /coverage_crawler/generatehtml.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | 3 | import json 4 | import os 5 | 6 | from firefox_code_coverage import codecoverage 7 | 8 | 9 | def generate_html(data_folder): 10 | with open('{}/diff.json'.format(data_folder), 'r') as report: 11 | parsed_json = json.load(report) 12 | 13 | file_obj = open('{}/output.info'.format(data_folder), 'w') 14 | 15 | source_files = parsed_json['source_files'] 16 | file_obj.write('TN\n') 17 | for source_file in source_files: 18 | file_obj.write('SF:{}\n'.format(source_file['name'])) 19 | executed = 0 20 | 21 | # Functions 22 | if len(source_file['functions']) != 0: 23 | for function in source_file['functions']: 24 | file_obj.write('FN:{},{}\n'.format(function['start'], function['name'])) 25 | for function in source_file['functions']: 26 | if function['exec'] is True: 27 | file_obj.write('FNDA:{},{}\n'.format(1, function['name'])) 28 | executed += 1 29 | else: 30 | file_obj.write('FNDA:{},{}\n'.format(0, function['name'])) 31 | file_obj.write('FNF:{}\n'.format(len(source_file['functions']))) 32 | file_obj.write('FNH:{}\n'.format(executed)) 33 | 34 | # Branches 35 | branch_hits = 0 36 | if len(source_file['branches']) != 0: 37 | for branch in source_file['branches']: 38 | if branch['taken'] is True: 39 | file_obj.write('BRDA:{},0,{},{}\n'.format(branch['line'], branch['number'], 1)) 40 | branch_hits += 1 41 | else: 42 | file_obj.write('BRDA:{},0,{},{}\n'.format(branch['line'], branch['number'], '-')) 43 | file_obj.write('BRF:{}\n'.format(len(source_file['branches']))) 44 | file_obj.write('BRH:{}\n'.format(branch_hits)) 45 | 46 | # Lines 47 | line_number = 0 48 | cov_line_count = 0 49 | if len(source_file['coverage']) != 0: 50 | for line_number, line in enumerate(source_file['coverage'], 1): 51 | if line is not None: 52 | file_obj.write('DA:{},{}\n'.format(line_number, line)) 53 | if line > 0: 54 | cov_line_count += 1 55 | file_obj.write('LF:{}\n'.format(len(source_file['coverage']))) 56 | file_obj.write('LH:{}\n'.format(cov_line_count)) 57 | file_obj.write('end_of_record\n') 58 | file_obj.close() 59 | 60 | codecoverage.generate_html_report('mozilla-central', os.path.join(os.getcwd(), '{}/output.info'.format(data_folder)), os.path.join(os.getcwd(), '{}/report'.format(data_folder)), style_file=os.path.join(os.getcwd(), 'style.css')) 61 | -------------------------------------------------------------------------------- /coverage_crawler/latest_cov_build.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | 3 | import os 4 | import platform 5 | import subprocess 6 | import sys 7 | import tarfile 8 | import zipfile 9 | 10 | import requests 11 | import taskcluster 12 | from firefox_code_coverage import codecoverage 13 | 14 | try: 15 | from urllib.request import urlretrieve 16 | except ImportError: 17 | from urllib import urlretrieve 18 | 19 | 20 | def get_github_release_url(repo_slug): 21 | repos_url = 'https://api.github.com/repos/{}/releases/latest'.format(repo_slug) 22 | download_url = 'https://github.com/{}/releases/download/'.format(repo_slug) 23 | latest_version = requests.get(repos_url) 24 | data = latest_version.json() 25 | tag_name = data['tag_name'] 26 | return download_url, tag_name 27 | 28 | 29 | def get_taskcluster_options(): 30 | return dict( 31 | rootUrl=os.environ.get('TASKCLUSTER_ROOT_URL', 'https://taskcluster.net') 32 | ) 33 | 34 | 35 | def download_artifacts(revision=None): 36 | # Create 'tools/' directory if doesn't exist 37 | if not os.path.exists('tools'): 38 | os.makedirs('tools') 39 | 40 | options = get_taskcluster_options() 41 | index = taskcluster.Index(options) 42 | queue = taskcluster.Queue(options) 43 | 44 | if revision is None: 45 | taskId = index.findTask('gecko.v2.mozilla-central.' + 'latest.firefox.linux64-ccov-debug')['taskId'] 46 | r = requests.get('https://queue.taskcluster.net/v1/task/{}'.format(taskId)) 47 | task_data = r.json() 48 | revision = task_data['payload']['env']['GECKO_HEAD_REV'] 49 | else: 50 | r = requests.get('https://index.taskcluster.net/v1/task/gecko.v2.mozilla-central.revision.{}.firefox.linux64-debug'.format(revision)) 51 | task_data = r.json() 52 | taskId = task_data['taskId'] 53 | 54 | # Download artifacts 55 | for name in ['target.tar.bz2', 'target.code-coverage-gcno.zip', 'chrome-map.json', 'target.common.tests.tar.gz']: 56 | url = queue.buildUrl('getLatestArtifact', taskId, 'public/build/{}'.format(name)) 57 | print('Downloading {}...'.format(url)) 58 | urlretrieve(url, os.path.join('tools', name)) 59 | 60 | # Geckodriver base url fot the latest version 61 | download_url, tag_name = get_github_release_url('mozilla/geckodriver') 62 | geckodriver_url = download_url + tag_name + '/geckodriver-' + tag_name + '-' 63 | 64 | # Grcov latest version base url 65 | download_url, tag_name = get_github_release_url('marco-c/grcov') 66 | grcov_url = download_url + tag_name 67 | 68 | # OS information for correct geckodriver version 69 | bitness = platform.architecture()[0] 70 | 71 | # Complete urls according to platforms 72 | if sys.platform.startswith('linux'): 73 | grcov_url += '/grcov-linux-x86_64.tar.bz2' 74 | if bitness == '64bit': 75 | version = 'linux64.tar.gz' 76 | else: 77 | version = 'linux32.tar.gz' 78 | elif sys.platform.startswith('darwin'): 79 | grcov_url += '/grcov-osx-x86_64.tar.bz2' 80 | version = 'macos.tar.gz' 81 | elif sys.platform.startswith('cygwin') or sys.platform.startswith('win32'): 82 | grcov_url += '/grcov-win-x86_64.tar.bz2' 83 | if bitness == '64bit': 84 | version = 'win64.zip' 85 | else: 86 | version = 'win32.zip' 87 | 88 | # Download geckodriver 89 | geckodriver_archive = os.path.join('tools', version) 90 | geckodriver_url += version 91 | print('Downloading {}...'.format(geckodriver_url)) 92 | urlretrieve(geckodriver_url, geckodriver_archive) 93 | 94 | # Download grcov 95 | grcov_archive = os.path.join('tools', 'grcov.tar.bz2') 96 | print('Downloading {}...'.format(grcov_url)) 97 | urlretrieve(grcov_url, grcov_archive) 98 | 99 | # Extract and delete archives for artifacts 100 | for filename in ['tools/target.code-coverage-gcno.zip', 'tools/target.tar.bz2', geckodriver_archive, grcov_archive, 'tools/target.common.tests.tar.gz']: 101 | if filename.endswith('zip'): 102 | with zipfile.ZipFile(filename, 'r') as zip_ref: 103 | zip_ref.extractall(path='tools') 104 | elif filename.endswith('tar.gz') or filename.endswith('tar.bz2'): 105 | if filename.endswith('tar.gz'): 106 | mode = 'r:gz' 107 | else: 108 | mode = 'r:bz2' 109 | with tarfile.open(filename, mode) as tar: 110 | tar.extractall(path='tools') 111 | os.remove(filename) 112 | 113 | # Download Firefox coverage report 114 | print('Downloading coverage artifacts...') 115 | codecoverage.download_coverage_artifacts(taskId, None, None, 'ccov-artifacts') 116 | print('Generating report...') 117 | codecoverage.generate_report('tools/grcov', 'coveralls+', 'tests_report.json', 'ccov-artifacts') 118 | 119 | # Download genhtml 120 | print('Downloading genhtml...') 121 | codecoverage.download_genhtml() 122 | 123 | # Clone if the repository doesn't exist yet. Otherwise, update. 124 | print('Cloning/Updating mozilla-central repository...') 125 | if os.path.isdir('mozilla-central'): 126 | os.chdir('mozilla-central') 127 | subprocess.call(['hg', 'pull', '--rev', revision, 'https://hg.mozilla.org/mozilla-central/']) 128 | subprocess.call(['hg', 'update', '--rev', revision]) 129 | else: 130 | subprocess.call(['hg', 'clone', 'https://hg.mozilla.org/mozilla-central/', '--rev', revision]) 131 | -------------------------------------------------------------------------------- /download_artifacts.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | 3 | import argparse 4 | 5 | from coverage_crawler import latest_cov_build 6 | 7 | parser = argparse.ArgumentParser() 8 | parser.add_argument('--revision', action='store', nargs='?', help='Optional revision of the build') 9 | args = parser.parse_args() 10 | latest_cov_build.download_artifacts(args.revision) 11 | -------------------------------------------------------------------------------- /requirements.txt: -------------------------------------------------------------------------------- 1 | selenium==4.5.0 2 | six==1.16.0 3 | taskcluster==44.22.1 4 | firefox_code_coverage==1.1.6 5 | -------------------------------------------------------------------------------- /run_crawler.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | 3 | from coverage_crawler import crawler 4 | 5 | with open('websites.txt') as f: 6 | for website in f: 7 | report = crawler.run(website) 8 | -------------------------------------------------------------------------------- /setup.cfg: -------------------------------------------------------------------------------- 1 | [flake8] 2 | # no need to traverse these directories 3 | exclude = .git,__pycache__,data,tools 4 | 5 | # ignore line limit error 6 | ignore = E501 7 | -------------------------------------------------------------------------------- /setup.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | 3 | from __future__ import absolute_import 4 | 5 | import os 6 | 7 | from setuptools import find_packages 8 | from setuptools import setup 9 | 10 | here = os.path.dirname(__file__) 11 | 12 | 13 | def read_requirements(file_): 14 | requires = [] 15 | links = [] 16 | with open(os.path.join(here, file_)) as f: 17 | for line in f.readlines(): 18 | line = line.strip() 19 | 20 | if line.startswith('https://'): 21 | links.append(line + '-1.0.0') 22 | extras = '' 23 | if '[' in line: 24 | extras = '[' + line.split('[')[1].split(']')[0] + ']' 25 | line = line.split('#')[1].split('egg=')[1] + extras 26 | elif line == '' or line.startswith('#') or line.startswith('-'): 27 | continue 28 | line = line.split('#')[0].strip() 29 | requires.append(line) 30 | 31 | return sorted(list(set(requires))), links 32 | 33 | 34 | install_requires, dependency_links = read_requirements('requirements.txt') 35 | 36 | setup( 37 | name='coverage_crawler', 38 | version='1.0.0', 39 | description='A crawler to find websites that exercise code in Firefox that is not covered by unit tests', 40 | install_requires=install_requires, 41 | dependency_links=dependency_links, 42 | packages=find_packages(exclude=['contrib', 'docs', 'tests']), 43 | include_package_data=True, 44 | zip_safe=False, 45 | license='MPL2', 46 | ) 47 | -------------------------------------------------------------------------------- /style.css: -------------------------------------------------------------------------------- 1 | /* All views: initial background and text color */ 2 | body 3 | { 4 | color: #000000; 5 | background-color: #FFFFFF; 6 | } 7 | 8 | /* All views: standard link format*/ 9 | a:link 10 | { 11 | color: #284FA8; 12 | text-decoration: underline; 13 | } 14 | 15 | /* All views: standard link - visited format */ 16 | a:visited 17 | { 18 | color: #00CB40; 19 | text-decoration: underline; 20 | } 21 | 22 | /* All views: standard link - activated format */ 23 | a:active 24 | { 25 | color: #FF0040; 26 | text-decoration: underline; 27 | } 28 | 29 | /* All views: main title format */ 30 | td.title 31 | { 32 | text-align: center; 33 | padding-bottom: 10px; 34 | font-family: sans-serif; 35 | font-size: 20pt; 36 | font-style: italic; 37 | font-weight: bold; 38 | } 39 | 40 | /* All views: header item format */ 41 | td.headerItem 42 | { 43 | text-align: right; 44 | padding-right: 6px; 45 | font-family: sans-serif; 46 | font-weight: bold; 47 | vertical-align: top; 48 | white-space: nowrap; 49 | } 50 | 51 | /* All views: header item value format */ 52 | td.headerValue 53 | { 54 | text-align: left; 55 | color: #284FA8; 56 | font-family: sans-serif; 57 | font-weight: bold; 58 | white-space: nowrap; 59 | } 60 | 61 | /* All views: header item coverage table heading */ 62 | td.headerCovTableHead 63 | { 64 | text-align: center; 65 | padding-right: 6px; 66 | padding-left: 6px; 67 | padding-bottom: 0px; 68 | font-family: sans-serif; 69 | font-size: 80%; 70 | white-space: nowrap; 71 | } 72 | 73 | /* All views: header item coverage table entry */ 74 | td.headerCovTableEntry 75 | { 76 | text-align: right; 77 | color: #284FA8; 78 | font-family: sans-serif; 79 | font-weight: bold; 80 | white-space: nowrap; 81 | padding-left: 12px; 82 | padding-right: 4px; 83 | background-color: #DAE7FE; 84 | } 85 | 86 | /* All views: header item coverage table entry for high coverage rate */ 87 | td.headerCovTableEntryHi 88 | { 89 | text-align: right; 90 | color: #000000; 91 | font-family: sans-serif; 92 | font-weight: bold; 93 | white-space: nowrap; 94 | padding-left: 12px; 95 | padding-right: 4px; 96 | background-color: #A7FC9D; 97 | } 98 | 99 | /* All views: header item coverage table entry for medium coverage rate */ 100 | td.headerCovTableEntryMed 101 | { 102 | text-align: right; 103 | color: #000000; 104 | font-family: sans-serif; 105 | font-weight: bold; 106 | white-space: nowrap; 107 | padding-left: 12px; 108 | padding-right: 4px; 109 | background-color: #FFEA20; 110 | } 111 | 112 | /* All views: header item coverage table entry for ow coverage rate */ 113 | td.headerCovTableEntryLo 114 | { 115 | text-align: right; 116 | color: #000000; 117 | font-family: sans-serif; 118 | font-weight: bold; 119 | white-space: nowrap; 120 | padding-left: 12px; 121 | padding-right: 4px; 122 | background-color: #FF0000; 123 | } 124 | 125 | /* All views: header legend value for legend entry */ 126 | td.headerValueLeg 127 | { 128 | text-align: left; 129 | color: #000000; 130 | font-family: sans-serif; 131 | font-size: 80%; 132 | white-space: nowrap; 133 | padding-top: 4px; 134 | } 135 | 136 | /* All views: color of horizontal ruler */ 137 | td.ruler 138 | { 139 | background-color: #6688D4; 140 | } 141 | 142 | /* All views: version string format */ 143 | td.versionInfo 144 | { 145 | text-align: center; 146 | padding-top: 2px; 147 | font-family: sans-serif; 148 | font-style: italic; 149 | } 150 | 151 | /* Directory view/File view (all)/Test case descriptions: 152 | table headline format */ 153 | td.tableHead 154 | { 155 | text-align: center; 156 | color: #FFFFFF; 157 | background-color: #6688D4; 158 | font-family: sans-serif; 159 | font-size: 120%; 160 | font-weight: bold; 161 | white-space: nowrap; 162 | padding-left: 4px; 163 | padding-right: 4px; 164 | } 165 | 166 | span.tableHeadSort 167 | { 168 | padding-right: 4px; 169 | } 170 | 171 | /* Directory view/File view (all): filename entry format */ 172 | td.coverFile 173 | { 174 | text-align: left; 175 | padding-left: 10px; 176 | padding-right: 20px; 177 | color: #284FA8; 178 | background-color: #DAE7FE; 179 | font-family: monospace; 180 | } 181 | 182 | /* Directory view/File view (all): bar-graph entry format*/ 183 | td.coverBar 184 | { 185 | padding-left: 10px; 186 | padding-right: 10px; 187 | background-color: #DAE7FE; 188 | } 189 | 190 | /* Directory view/File view (all): bar-graph outline color */ 191 | td.coverBarOutline 192 | { 193 | background-color: #000000; 194 | } 195 | 196 | /* Directory view/File view (all): percentage entry for files with 197 | high coverage rate */ 198 | td.coverPerHi 199 | { 200 | text-align: right; 201 | padding-left: 10px; 202 | padding-right: 10px; 203 | background-color: #A7FC9D; 204 | font-weight: bold; 205 | font-family: sans-serif; 206 | } 207 | 208 | /* Directory view/File view (all): line count entry for files with 209 | high coverage rate */ 210 | td.coverNumHi 211 | { 212 | text-align: right; 213 | padding-left: 10px; 214 | padding-right: 10px; 215 | background-color: #A7FC9D; 216 | white-space: nowrap; 217 | font-family: sans-serif; 218 | } 219 | 220 | /* Directory view/File view (all): percentage entry for files with 221 | medium coverage rate */ 222 | td.coverPerMed 223 | { 224 | text-align: right; 225 | padding-left: 10px; 226 | padding-right: 10px; 227 | background-color: #FFEA20; 228 | font-weight: bold; 229 | font-family: sans-serif; 230 | } 231 | 232 | /* Directory view/File view (all): line count entry for files with 233 | medium coverage rate */ 234 | td.coverNumMed 235 | { 236 | text-align: right; 237 | padding-left: 10px; 238 | padding-right: 10px; 239 | background-color: #FFEA20; 240 | white-space: nowrap; 241 | font-family: sans-serif; 242 | } 243 | 244 | /* Directory view/File view (all): percentage entry for files with 245 | low coverage rate */ 246 | td.coverPerLo 247 | { 248 | text-align: right; 249 | padding-left: 10px; 250 | padding-right: 10px; 251 | background-color: #FF0000; 252 | font-weight: bold; 253 | font-family: sans-serif; 254 | } 255 | 256 | /* Directory view/File view (all): line count entry for files with 257 | low coverage rate */ 258 | td.coverNumLo 259 | { 260 | text-align: right; 261 | padding-left: 10px; 262 | padding-right: 10px; 263 | background-color: #FF0000; 264 | white-space: nowrap; 265 | font-family: sans-serif; 266 | } 267 | 268 | /* File view (all): "show/hide details" link format */ 269 | a.detail:link 270 | { 271 | color: #B8D0FF; 272 | font-size:80%; 273 | } 274 | 275 | /* File view (all): "show/hide details" link - visited format */ 276 | a.detail:visited 277 | { 278 | color: #B8D0FF; 279 | font-size:80%; 280 | } 281 | 282 | /* File view (all): "show/hide details" link - activated format */ 283 | a.detail:active 284 | { 285 | color: #FFFFFF; 286 | font-size:80%; 287 | } 288 | 289 | /* File view (detail): test name entry */ 290 | td.testName 291 | { 292 | text-align: right; 293 | padding-right: 10px; 294 | background-color: #DAE7FE; 295 | font-family: sans-serif; 296 | } 297 | 298 | /* File view (detail): test percentage entry */ 299 | td.testPer 300 | { 301 | text-align: right; 302 | padding-left: 10px; 303 | padding-right: 10px; 304 | background-color: #DAE7FE; 305 | font-family: sans-serif; 306 | } 307 | 308 | /* File view (detail): test lines count entry */ 309 | td.testNum 310 | { 311 | text-align: right; 312 | padding-left: 10px; 313 | padding-right: 10px; 314 | background-color: #DAE7FE; 315 | font-family: sans-serif; 316 | } 317 | 318 | /* Test case descriptions: test name format*/ 319 | dt 320 | { 321 | font-family: sans-serif; 322 | font-weight: bold; 323 | } 324 | 325 | /* Test case descriptions: description table body */ 326 | td.testDescription 327 | { 328 | padding-top: 10px; 329 | padding-left: 30px; 330 | padding-bottom: 10px; 331 | padding-right: 30px; 332 | background-color: #DAE7FE; 333 | } 334 | 335 | /* Source code view: function entry */ 336 | td.coverFn 337 | { 338 | text-align: left; 339 | padding-left: 10px; 340 | padding-right: 20px; 341 | color: #284FA8; 342 | background-color: #DAE7FE; 343 | font-family: monospace; 344 | } 345 | 346 | /* Source code view: function entry zero count*/ 347 | td.coverFnLo 348 | { 349 | text-align: right; 350 | padding-left: 10px; 351 | padding-right: 10px; 352 | background-color: #FF0000; 353 | font-weight: bold; 354 | font-family: sans-serif; 355 | } 356 | 357 | /* Source code view: function entry nonzero count*/ 358 | td.coverFnHi 359 | { 360 | text-align: right; 361 | padding-left: 10px; 362 | padding-right: 10px; 363 | background-color: #DAE7FE; 364 | font-weight: bold; 365 | font-family: sans-serif; 366 | } 367 | 368 | /* Source code view: source code format */ 369 | pre.source 370 | { 371 | font-family: monospace; 372 | white-space: pre; 373 | margin-top: 2px; 374 | } 375 | 376 | /* Source code view: line number format */ 377 | span.lineNum 378 | { 379 | background-color: #EFE383; 380 | } 381 | 382 | /* Source code view: format for lines which were executed */ 383 | td.lineCov, 384 | span.lineCov 385 | { 386 | background-color: #CAD7FE; 387 | } 388 | 389 | /* Source code view: format for Cov legend */ 390 | span.coverLegendCov 391 | { 392 | padding-left: 10px; 393 | padding-right: 10px; 394 | padding-bottom: 2px; 395 | background-color: #CAD7FE; 396 | } 397 | 398 | /* Source code view: format for lines which were not executed */ 399 | td.lineNoCov, 400 | span.lineNoCov 401 | { 402 | background-color: #FFFFFF; 403 | } 404 | 405 | /* Source code view: format for NoCov legend */ 406 | span.coverLegendNoCov 407 | { 408 | padding-left: 10px; 409 | padding-right: 10px; 410 | padding-bottom: 2px; 411 | background-color: #FF6230; 412 | } 413 | 414 | /* Source code view (function table): standard link - visited format */ 415 | td.lineNoCov > a:visited, 416 | td.lineCov > a:visited 417 | { 418 | color: black; 419 | text-decoration: underline; 420 | } 421 | 422 | /* Source code view: format for lines which were executed only in a 423 | previous version */ 424 | span.lineDiffCov 425 | { 426 | background-color: #B5F7AF; 427 | } 428 | 429 | /* Source code view: format for branches which were executed 430 | * and taken */ 431 | span.branchCov 432 | { 433 | background-color: #CAD7FE; 434 | } 435 | 436 | /* Source code view: format for branches which were executed 437 | * but not taken */ 438 | span.branchNoCov 439 | { 440 | background-color: #FF6230; 441 | } 442 | 443 | /* Source code view: format for branches which were not executed */ 444 | span.branchNoExec 445 | { 446 | background-color: #FF6230; 447 | } 448 | 449 | /* Source code view: format for the source code heading line */ 450 | pre.sourceHeading 451 | { 452 | white-space: pre; 453 | font-family: monospace; 454 | font-weight: bold; 455 | margin: 0px; 456 | } 457 | 458 | /* All views: header legend value for low rate */ 459 | td.headerValueLegL 460 | { 461 | font-family: sans-serif; 462 | text-align: center; 463 | white-space: nowrap; 464 | padding-left: 4px; 465 | padding-right: 2px; 466 | background-color: #FF0000; 467 | font-size: 80%; 468 | } 469 | 470 | /* All views: header legend value for med rate */ 471 | td.headerValueLegM 472 | { 473 | font-family: sans-serif; 474 | text-align: center; 475 | white-space: nowrap; 476 | padding-left: 2px; 477 | padding-right: 2px; 478 | background-color: #FFEA20; 479 | font-size: 80%; 480 | } 481 | 482 | /* All views: header legend value for hi rate */ 483 | td.headerValueLegH 484 | { 485 | font-family: sans-serif; 486 | text-align: center; 487 | white-space: nowrap; 488 | padding-left: 2px; 489 | padding-right: 4px; 490 | background-color: #A7FC9D; 491 | font-size: 80%; 492 | } 493 | 494 | /* All views except source code view: legend format for low coverage */ 495 | span.coverLegendCovLo 496 | { 497 | padding-left: 10px; 498 | padding-right: 10px; 499 | padding-top: 2px; 500 | background-color: #FF0000; 501 | } 502 | 503 | /* All views except source code view: legend format for med coverage */ 504 | span.coverLegendCovMed 505 | { 506 | padding-left: 10px; 507 | padding-right: 10px; 508 | padding-top: 2px; 509 | background-color: #FFEA20; 510 | } 511 | 512 | /* All views except source code view: legend format for hi coverage */ 513 | span.coverLegendCovHi 514 | { 515 | padding-left: 10px; 516 | padding-right: 10px; 517 | padding-top: 2px; 518 | background-color: #A7FC9D; 519 | } 520 | -------------------------------------------------------------------------------- /test-requirements.txt: -------------------------------------------------------------------------------- 1 | flake8==5.0.4 2 | flake8-isort==5.0.0 3 | flake8-quotes==3.3.1 4 | flake8-coding==1.3.2 5 | flake8-copyright==0.2.3 6 | flake8-debugger==4.1.2 7 | flake8-mypy==17.8.0 8 | pytest==7.1.3 9 | setuptools==65.5.0 10 | wheel==0.37.1 11 | webdriver-manager==3.8.4 12 | flask==2.2.2 -------------------------------------------------------------------------------- /tests/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mozilla/coverage-crawler/0e9132451834a60147853bc9dec220cd262b1e35/tests/__init__.py -------------------------------------------------------------------------------- /tests/crawler/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mozilla/coverage-crawler/0e9132451834a60147853bc9dec220cd262b1e35/tests/crawler/__init__.py -------------------------------------------------------------------------------- /tests/crawler/test_crawler_unittest.py: -------------------------------------------------------------------------------- 1 | # encoding: utf-8 2 | import multiprocessing 3 | import unittest 4 | 5 | from selenium import webdriver 6 | from selenium.common.exceptions import WebDriverException 7 | from webdriver_manager.firefox import GeckoDriverManager 8 | 9 | from coverage_crawler import crawler 10 | from tests.example_website import website_app 11 | from tests.example_website.website_app import WEBSITE_TITLE 12 | from tests.example_website.website_app import run_server 13 | 14 | 15 | class TestCrawler(unittest.TestCase): 16 | driver: webdriver.Firefox 17 | 18 | def setUp(self): 19 | self.driver = webdriver.Firefox(executable_path=GeckoDriverManager().install()) 20 | 21 | def tearDown(self): 22 | self.driver.quit() 23 | 24 | def test_close_all_windows_except_first(self): 25 | """ 26 | when multiple windows are open, 27 | verify that calling `close_all_windows_except_first` closes all except the first. 28 | """ 29 | _open_tab_script = 'window.open("", "new window")' 30 | self.driver.execute_script(_open_tab_script) 31 | assert (len(self.driver.window_handles) == 2), 'some windows were not opened properly.' 32 | 33 | crawler.close_all_windows_except_first(self.driver) 34 | 35 | assert (len(self.driver.window_handles) == 1), 'some windows were not closed properly.' 36 | 37 | 38 | class TestCrawlerLive(unittest.TestCase): 39 | SERVER_SETUP_TRIES = 10 40 | 41 | @classmethod 42 | def setUpClass(cls): 43 | cls.server = multiprocessing.Process(target=run_server) 44 | cls.server.start() 45 | 46 | try: 47 | test_driver = webdriver.Firefox(executable_path=GeckoDriverManager().install()) 48 | for try_id in range(cls.SERVER_SETUP_TRIES): 49 | print(f'class setup tries: {try_id}/{cls.SERVER_SETUP_TRIES}') 50 | test_driver.get(website_app.WEBSITE_URL) 51 | assert test_driver.title == WEBSITE_TITLE 52 | test_driver.quit() 53 | return 54 | except WebDriverException as e: 55 | print('got exception:', e) 56 | 57 | cls.server.terminate() 58 | cls.server.join() 59 | cls.fail(cls, 'website did not start up correctly.') 60 | 61 | @classmethod 62 | def tearDownClass(cls): 63 | cls.server.terminate() 64 | cls.server.join() 65 | 66 | def setUp(self): 67 | self.driver = webdriver.Firefox(executable_path=GeckoDriverManager().install()) 68 | 69 | def tearDown(self): 70 | self.driver.quit() 71 | 72 | def test_find_children(self): 73 | """ 74 | when example server is open, 75 | verify that `find_children` returns the expected children. 76 | """ 77 | expected_link_text = 'i am the first link in the first div' 78 | 79 | self.driver.get(website_app.WEBSITE_URL) 80 | assert (self.driver.title == WEBSITE_TITLE), f'incorrect driver title: {self.driver.title}' 81 | 82 | children = crawler.find_children(self.driver) 83 | num_children = len(children) 84 | assert (num_children == 1), f'incorrect number of children found: {num_children}' 85 | assert (children[0].text == expected_link_text) 86 | 87 | def test_get_all_attributes(self): 88 | """ 89 | when example server is open, 90 | verify that `get_all_attributes` returns the expected attributes. 91 | """ 92 | expected_attribute = 'href' 93 | 94 | self.driver.get(website_app.WEBSITE_URL) 95 | assert (self.driver.title == WEBSITE_TITLE), f'incorrect driver title: {self.driver.title}' 96 | 97 | child = crawler.find_children(self.driver)[0] 98 | attributes = crawler.get_all_attributes(self.driver, child) 99 | num_attributes = len(attributes) 100 | assert (num_attributes == 1), f'incorrect number of attributes found: {num_attributes}' 101 | assert (expected_attribute in attributes), f'expected attribute is missing: {expected_attribute}' 102 | -------------------------------------------------------------------------------- /tests/example_website/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mozilla/coverage-crawler/0e9132451834a60147853bc9dec220cd262b1e35/tests/example_website/__init__.py -------------------------------------------------------------------------------- /tests/example_website/templates/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 |
4 | 5 |i am the first paragraph of the first div 11 |
12 | i am the first link in the first div 13 |