├── .github └── ISSUE_TEMPLATE │ ├── bug_report.md │ └── feature_request.md ├── .gitignore ├── CODE_OF_CONDUCT.md ├── Docs ├── EnvironmentVariable.png ├── InstallationSteps123.png ├── InstallationSteps45.png ├── averageColor.png ├── dna.png ├── greyscale.png ├── strict.png └── withTolerance.png ├── LICENSE ├── Package.swift ├── README.md ├── Sources └── SUITCase │ ├── RGBAImage.swift │ ├── RGBAPixel.swift │ ├── SUITCase+SUITCaseMethod.swift │ ├── SUITCase+addNote+addImage.swift │ ├── SUITCase+collectScreenshot.swift │ ├── SUITCase+getImagePaths.swift │ ├── SUITCase+verifyScreenshot.swift │ ├── SUITCase.swift │ ├── UIDevice+modelName.swift │ ├── UIImage+Extensions.swift │ └── XCUIScreenshotProviding+averageColorIs.swift └── Tests ├── LinuxMain.swift └── SUITCaseTests ├── CompareImagesTests.swift ├── DeviceTests.swift ├── GetImagePathsTests.swift ├── RGBAImageTests.swift ├── RGBAPixelTests.swift ├── UIImageExtensionsTests.swift └── XCTestManifests.swift /.github/ISSUE_TEMPLATE/bug_report.md: -------------------------------------------------------------------------------- 1 | --- 2 | name: Bug report 3 | about: Create a report to help us improve 4 | title: '' 5 | labels: '' 6 | assignees: rzakhar 7 | 8 | --- 9 | 10 | **Describe the bug** 11 | A clear and concise description of what the bug is. 12 | 13 | **To Reproduce** 14 | Steps to reproduce the behavior: 15 | 1. 16 | 2. 17 | 3. 18 | 19 | **Expected behavior** 20 | A clear and concise description of what you expected to happen. 21 | 22 | **Screenshots** 23 | If applicable, add screenshots to help explain your problem. 24 | 25 | **Simulator (please complete the following information):** 26 | - Device: [e.g. iPhone 9] 27 | - OS: [e.g. iOS 13.4] 28 | - Xcode version: [e.g. 11.4] 29 | - macOS version: [e.g. 10.15] 30 | 31 | **Additional context** 32 | Add any other context about the problem here. 33 | -------------------------------------------------------------------------------- /.github/ISSUE_TEMPLATE/feature_request.md: -------------------------------------------------------------------------------- 1 | --- 2 | name: Feature request 3 | about: Suggest an idea for this project 4 | title: '' 5 | labels: '' 6 | assignees: rzakhar 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. 21 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | .DS_Store 2 | /.build 3 | /Packages 4 | /*.xcodeproj 5 | xcuserdata/ 6 | Package.resolved 7 | .swiftpm 8 | -------------------------------------------------------------------------------- /CODE_OF_CONDUCT.md: -------------------------------------------------------------------------------- 1 | # Contributor Covenant Code of Conduct 2 | 3 | ## Our Pledge 4 | 5 | In the interest of fostering an open and welcoming environment, we as 6 | contributors and maintainers pledge to making participation in our project and 7 | our community a harassment-free experience for everyone, regardless of age, body 8 | size, disability, ethnicity, sex characteristics, gender identity and expression, 9 | level of experience, education, socio-economic status, nationality, personal 10 | appearance, race, religion, or sexual identity and orientation. 11 | 12 | ## Our Standards 13 | 14 | Examples of behavior that contributes to creating a positive environment 15 | include: 16 | 17 | * Using welcoming and inclusive language 18 | * Being respectful of differing viewpoints and experiences 19 | * Gracefully accepting constructive criticism 20 | * Focusing on what is best for the community 21 | * Showing empathy towards other community members 22 | 23 | Examples of unacceptable behavior by participants include: 24 | 25 | * The use of sexualized language or imagery and unwelcome sexual attention or 26 | advances 27 | * Trolling, insulting/derogatory comments, and personal or political attacks 28 | * Public or private harassment 29 | * Publishing others' private information, such as a physical or electronic 30 | address, without explicit permission 31 | * Other conduct which could reasonably be considered inappropriate in a 32 | professional setting 33 | 34 | ## Our Responsibilities 35 | 36 | Project maintainers are responsible for clarifying the standards of acceptable 37 | behavior and are expected to take appropriate and fair corrective action in 38 | response to any instances of unacceptable behavior. 39 | 40 | Project maintainers have the right and responsibility to remove, edit, or 41 | reject comments, commits, code, wiki edits, issues, and other contributions 42 | that are not aligned to this Code of Conduct, or to ban temporarily or 43 | permanently any contributor for other behaviors that they deem inappropriate, 44 | threatening, offensive, or harmful. 45 | 46 | ## Scope 47 | 48 | This Code of Conduct applies both within project spaces and in public spaces 49 | when an individual is representing the project or its community. Examples of 50 | representing a project or community include using an official project e-mail 51 | address, posting via an official social media account, or acting as an appointed 52 | representative at an online or offline event. Representation of a project may be 53 | further defined and clarified by project maintainers. 54 | 55 | ## Enforcement 56 | 57 | Instances of abusive, harassing, or otherwise unacceptable behavior may be 58 | reported by contacting the project team at opensource@devexperts.com. All 59 | complaints will be reviewed and investigated and will result in a response that 60 | is deemed necessary and appropriate to the circumstances. The project team is 61 | obligated to maintain confidentiality with regard to the reporter of an incident. 62 | Further details of specific enforcement policies may be posted separately. 63 | 64 | Project maintainers who do not follow or enforce the Code of Conduct in good 65 | faith may face temporary or permanent repercussions as determined by other 66 | members of the project's leadership. 67 | 68 | ## Attribution 69 | 70 | This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4, 71 | available at https://www.contributor-covenant.org/version/1/4/code-of-conduct.html 72 | 73 | [homepage]: https://www.contributor-covenant.org 74 | 75 | For answers to common questions about this code of conduct, see 76 | https://www.contributor-covenant.org/faq 77 | -------------------------------------------------------------------------------- /Docs/EnvironmentVariable.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/devexperts/suitcase/f1020050096ba3cb0407b5b93552dfe3d193ab0a/Docs/EnvironmentVariable.png -------------------------------------------------------------------------------- /Docs/InstallationSteps123.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/devexperts/suitcase/f1020050096ba3cb0407b5b93552dfe3d193ab0a/Docs/InstallationSteps123.png -------------------------------------------------------------------------------- /Docs/InstallationSteps45.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/devexperts/suitcase/f1020050096ba3cb0407b5b93552dfe3d193ab0a/Docs/InstallationSteps45.png -------------------------------------------------------------------------------- /Docs/averageColor.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/devexperts/suitcase/f1020050096ba3cb0407b5b93552dfe3d193ab0a/Docs/averageColor.png -------------------------------------------------------------------------------- /Docs/dna.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/devexperts/suitcase/f1020050096ba3cb0407b5b93552dfe3d193ab0a/Docs/dna.png -------------------------------------------------------------------------------- /Docs/greyscale.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/devexperts/suitcase/f1020050096ba3cb0407b5b93552dfe3d193ab0a/Docs/greyscale.png -------------------------------------------------------------------------------- /Docs/strict.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/devexperts/suitcase/f1020050096ba3cb0407b5b93552dfe3d193ab0a/Docs/strict.png -------------------------------------------------------------------------------- /Docs/withTolerance.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/devexperts/suitcase/f1020050096ba3cb0407b5b93552dfe3d193ab0a/Docs/withTolerance.png -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /Package.swift: -------------------------------------------------------------------------------- 1 | // swift-tools-version:5.1 2 | 3 | /* 4 | SUITCase 5 | 6 | Copyright (c) 2020 Devexperts LLC 7 | 8 | See https://code.devexperts.com for more open source projects 9 | */ 10 | 11 | import PackageDescription 12 | 13 | let package = Package( 14 | name: "SUITCase", 15 | products: [ 16 | .library( 17 | name: "SUITCase", 18 | targets: ["SUITCase"]) 19 | ], 20 | dependencies: [ 21 | .package(url: "https://github.com/devicekit/DeviceKit.git", from: "4.0.0"), 22 | ], 23 | targets: [ 24 | .target( 25 | name: "SUITCase", 26 | dependencies: ["DeviceKit"]), 27 | .testTarget( 28 | name: "SUITCaseTests", 29 | dependencies: ["SUITCase"]) 30 | ] 31 | ) 32 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # SUITCase (ScreenshotUITestCase) 2 | --- 3 | SUITCase can verify screenshots in five different ways while testing the User Interface of iOS, iPadOS, and tvOS apps in Simulator. 4 | It has been designed to be used with XCTest and written in Swift. 5 | 6 | 7 | ## Usage 8 | Built-in XCTest assertions are great for many cases when you automate user interface testing. 9 | But some cases require testing appearance because `XCUIElement` properties do not fully describe the interface. 10 | SUITCase verifies screenshots to automate testing formatting texts, displaying images, switching themes, and much more. 11 | SUITCase currently supports recording and testing apps in Simulator only. 12 | 13 | ## Installation 14 | SUITCase is easy to install with the [Swift Package Manager.](https://developer.apple.com/documentation/swift_packages) 15 | Navigate to project settings and add a package on **Swift Packages** page. 16 | Copy-Paste a link to this repository in the **Choose Package Repository** window. 17 | Continue configuring dependency as needed. 18 | 19 | ![Steps 1, 2 and 3](Docs/InstallationSteps123.png) 20 | ![Steps 4 and 5](Docs/InstallationSteps45.png) 21 | 22 | Finally, add the Environmental Variable **IMAGES_DIR** to your Scheme. 23 | If you want to keep your reference Images with your test code, you should probably set it to **$(SOURCE_ROOT)/Images** 24 | 25 | ![Environment Variable](Docs/EnvironmentVariable.png) 26 | 27 | 28 | ## Your first test with SUITCase 29 | 1. Import `XCTest` and `SUITCase` 30 | 2. Define your testing class as a subclass of `SUITCase` (or replace `XCTestCase` with `SUITCase` if you change the existing class) 31 | 3. Use `verifyScreenshot()` to insert a screenshot assertion in test. 32 | 4. Record the reference screenshot by enabling recording in `setUp()` by adding `screenshotComparisonRecordMode = true` line. 33 | 5. Disable recording by removing this line and run your test. 34 | ```swift 35 | import XCTest 36 | import SUITCase 37 | 38 | class AppearanceTests: SUITCase { 39 | override func setUp() { 40 | super.setUp() 41 | // screenshotComparisonRecordMode = true 42 | XCUIApplication().launch() 43 | } 44 | 45 | func testMainScreen() { 46 | verifyScreenshot() 47 | } 48 | } 49 | ``` 50 | 51 | 52 | ## Features 53 | ### Recording 54 | * Enable **reference screenshots recording** by adding `screenshotComparisonRecordMode = true` line. 55 | * The recording is disabled by default. 56 | If the reference images are missing, SUITCase will save **the suggested screenshots** into the separate folder. 57 | If the collected screenshots are **unexpected**, SUITCase will save them into another separate folder as well. 58 | * There are **three screenshots naming strategies** – by the device name, by the screenshot size (default), and manual. 59 | Change strategy by setting the `screenshotComparisonNamingStrategy` variable. 60 | * You can also provide **the custom labels** to the screenshots if you want to make multiple assertions in a single test. Just set the `withLabel` argument. 61 | ```swift 62 | screenshotComparisonNamingStrategy = .imageSize 63 | XCUIDevice.shared.orientation = .portrait 64 | verifyScreenshot(withLabel: "Portrait") 65 | // Saves /Images/Reference/AppearanceTests/testMainScreen/Portrait/en_414x896.png 66 | 67 | screenshotComparisonNamingStrategy = .deviceModelName 68 | XCUIDevice.shared.orientation = .landscapeLeft 69 | verifyScreenshot(withLabel: "Landscape") 70 | // Saves /Images/Reference/AppearanceTests/testMainScreen/Landscape/en_iPhone_11.png 71 | 72 | screenshotComparisonNamingStrategy = .manual 73 | XCUIDevice.shared.orientation = .landscapeRight 74 | verifyScreenshot(withLabel: "landscapeRight") 75 | // Saves /Images/Reference/landscapeRight.png 76 | ``` 77 | 78 | 79 | ### Thresholds 80 | * Set **the global threshold** by changing `screenshotComparisonDefaultThreshold`. 81 | If unchanged, the default threshold is equal to 0.01, which means SUITCase allows a 1 percent difference between collected and referenced screenshots. 82 | * You can set the **local threshold** while keeping the global one unchanged. Set the `withThreshold` argument. 83 | ```swift 84 | verifyScreenshot() 85 | // The threshold is default and equals to 0.01 86 | 87 | screenshotComparisonGlobalThreshold = 0.02 88 | verifyScreenshot() 89 | // The global threshold is now 0.02 90 | 91 | verifyScreenshot(withThreshold: 0.03) 92 | // The global threshold is still 0.02, but for this screenshot comparison threshold equals to 0.03 93 | ``` 94 | 95 | 96 | ### XCUIElement support 97 | * You can verify **screenshots of specific elements** with `ofElement` argument. 98 | The reference image will be cropped by this element frame. 99 | * You shoud **erase dynamic element** by using `withoutElement` argument. 100 | * The `withoutQuery` argument allows you to **erase multiple elements** at once. 101 | * The erased elements are transparent on the reference images, and SUITCase compares only opaque pixels. 102 | ```swift 103 | verifyScreenshot(ofElement: app.buttons["New"]) 104 | verifyScreenshot(withoutElement: app.tabBars.element) 105 | verifyScreenshot(withoutQuery: app.images.matching(identifier: "GIF")) 106 | ``` 107 | 108 | 109 | ### Comparison methods 110 | * SUITCase includes five different comparison methods. 111 | Each method will attach the collected image, difference value, and current threshold. 112 | If difference is greater than zero, the reference and difference images will be attached as well. 113 | * You can pass the method with the `withMethod` argument: `verifyScreenshot(method: SUITCaseMethodStrict()])` 114 | * `SUITCaseMethodStrict()` \ 115 | The most accurate method, which compares original screenshots pixel by pixel. 116 | ![Strict](Docs/strict.png) 117 | * `SUITCaseMethodWithTolerance(_: Double = 0.1)` \ 118 | The default method downscales screenshots and allows configurable tolerance while comparing pixels. 119 | [Read more about this method in our blog.](https://devexperts.com/blog/getting-to-the-bottom-of-the-rgb-pixels-part-1-color-difference/) 120 | ![withTolerance](Docs/withTolerance.png) 121 | * `SUITCaseMethodGreyscaleColor(tolerance: Double = 0.1)` \ 122 | Downscales screenshots, removes color saturation, and allows configurable tolerance while comparing pixels. 123 | ![Greyscale](Docs/greyscale.png) 124 | * `SUITCaseMethodDNA(tolerance: Double = 0.1, scaleFactor: Double = 0.03)` \ 125 | Extremely dowscales screenshots, and allows configurable tolerance while comparing pixels. Inspired by [the PhotoDNA.](https://www.microsoft.com/en-us/photodna) 126 | ![DNA](Docs/dna.png) 127 | * `SUITCaseMethodAverageColor()` \ 128 | Compares the average colors of screenshots. 129 | ![Average color](Docs/averageColor.png) 130 | * You can also verify the average color without the reference screenshot by using `averageColorIs(_ uiColor: UIColor, tolerance: Double = 0.1)` \ 131 | `XCTAssert(app.buttons["Red Button"].averageColorIs(.red))` 132 | 133 | ## License 134 | SUITCase is the open-source software under [the MPL 2.0 license.](LICENSE) 135 | -------------------------------------------------------------------------------- /Sources/SUITCase/RGBAImage.swift: -------------------------------------------------------------------------------- 1 | /* 2 | SUITCase 3 | 4 | Copyright ©2020 Devexperts LLC. All rights reserved. 5 | This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. 6 | If a copy of the MPL was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/. 7 | 8 | See https://code.devexperts.com for more open source projects 9 | */ 10 | 11 | 12 | import XCTest 13 | 14 | /// A structure describing image built with RGBA pixels. 15 | public struct RGBAImage: Equatable { 16 | var pixels: [RGBAPixel] 17 | var width: Int 18 | var height: Int 19 | 20 | init(pixels: [RGBAPixel], width: Int, height: Int) { 21 | self.pixels = pixels 22 | self.width = width 23 | self.height = height 24 | } 25 | 26 | init(pixel: RGBAPixel, width: Int, height: Int) { 27 | self.pixels = Array(repeating: pixel, count: width * height) 28 | self.width = width 29 | self.height = height 30 | } 31 | 32 | init(uiImage: UIImage) { 33 | guard 34 | let cgImage = uiImage.cgImage, 35 | let colorSpace = cgImage.colorSpace else { 36 | XCTFail("Unable to unwrap UIImage") 37 | exit(0) 38 | } 39 | width = cgImage.width 40 | height = cgImage.height 41 | pixels = [] 42 | pixels.reserveCapacity(width * height) 43 | let bitsPerComponent = cgImage.bitsPerComponent 44 | let bytesPerRow = cgImage.bytesPerRow 45 | let bitmapInfo = CGImageAlphaInfo.premultipliedLast.rawValue 46 | 47 | guard 48 | let context = CGContext(data: nil, 49 | width: width, 50 | height: height, 51 | bitsPerComponent: bitsPerComponent, 52 | bytesPerRow: bytesPerRow, 53 | space: colorSpace, 54 | bitmapInfo: bitmapInfo), 55 | let data = context.data?.assumingMemoryBound(to: UInt8.self) else { 56 | XCTFail("Unable to create CGContext") 57 | exit(0) 58 | } 59 | 60 | let rectangle = CGRect(x: 0, y: 0, width: width, height: height) 61 | let bitsPerPixel = cgImage.bitsPerPixel 62 | 63 | context.draw(cgImage, in: rectangle) 64 | 65 | for rowNumber in 0...size 81 | let rgbColorSpace = CGColorSpaceCreateDeviceRGB() 82 | let bitmapInfo = CGBitmapInfo(rawValue: CGImageAlphaInfo.last.rawValue) 83 | let renderingIntent = CGColorRenderingIntent.defaultIntent 84 | 85 | guard 86 | let dataProvider = CGDataProvider(data: NSData(bytes: &pixelsCopy, 87 | length: bitmapCount * pixelMemoryLayout)), 88 | let cgimage = CGImage(width: width, 89 | height: height, 90 | bitsPerComponent: 8, 91 | bitsPerPixel: 32, 92 | bytesPerRow: width * pixelMemoryLayout, 93 | space: rgbColorSpace, 94 | bitmapInfo: bitmapInfo, 95 | provider: dataProvider, 96 | decode: nil, 97 | shouldInterpolate: true, 98 | intent: renderingIntent) else { 99 | XCTFail("Unable to get CGDataProvider") 100 | exit(0) 101 | } 102 | let uiImage = UIImage(cgImage: cgimage) 103 | return uiImage 104 | } 105 | 106 | var averageColor: RGBAPixel? { 107 | var opaquePixelsCount = 0 108 | var sumOfRed = 0 109 | var sumOfGreen = 0 110 | var sumOfBlue = 0 111 | 112 | for pixel in pixels where pixel.isOpaque { 113 | opaquePixelsCount += 1 114 | sumOfRed += Int(pixel.red) 115 | sumOfGreen += Int(pixel.green) 116 | sumOfBlue += Int(pixel.blue) 117 | } 118 | guard opaquePixelsCount > 0 else { 119 | 120 | return nil 121 | } 122 | 123 | func divideAndRound(_ sum: Int) -> UInt8 { 124 | let answer = round(Double(sum) / Double(opaquePixelsCount)) 125 | return UInt8(answer) 126 | } 127 | 128 | return RGBAPixel(red: divideAndRound(sumOfRed), 129 | green: divideAndRound(sumOfGreen), 130 | blue: divideAndRound(sumOfBlue)) 131 | } 132 | } 133 | -------------------------------------------------------------------------------- /Sources/SUITCase/RGBAPixel.swift: -------------------------------------------------------------------------------- 1 | /* 2 | SUITCase 3 | Copyright ©2020 Devexperts LLC. All rights reserved. 4 | This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. 5 | If a copy of the MPL was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/. 6 | 7 | See https://code.devexperts.com for more open source projects 8 | */ 9 | 10 | #if !os(macOS) 11 | import UIKit 12 | #endif 13 | 14 | /// A primitive structure of RGBA pixel 15 | /// - seealso https://en.wikipedia.org/wiki/RGBA_color_space 16 | struct RGBAPixel: Equatable, Hashable { 17 | var red: UInt8 18 | var green: UInt8 19 | var blue: UInt8 20 | var alpha: UInt8 21 | 22 | init(red: UInt8 = 0, 23 | green: UInt8 = 0, 24 | blue: UInt8 = 0, 25 | alpha: UInt8 = 255) { 26 | self.red = red 27 | self.green = green 28 | self.blue = blue 29 | self.alpha = alpha 30 | } 31 | 32 | init(white: UInt8 = 0, 33 | alpha: UInt8 = 255) { 34 | self.red = white 35 | self.green = white 36 | self.blue = white 37 | self.alpha = alpha 38 | } 39 | 40 | init(uiColor: UIColor) { 41 | var red: CGFloat = 0 42 | var green: CGFloat = 0 43 | var blue: CGFloat = 0 44 | var alpha: CGFloat = 0 45 | uiColor.getRed(&red, green: &green, blue: &blue, alpha: &alpha) 46 | 47 | self.red = UInt8(cgFloat: red) 48 | self.green = UInt8(cgFloat: green) 49 | self.blue = UInt8(cgFloat: blue) 50 | self.alpha = UInt8(cgFloat: alpha) 51 | } 52 | 53 | var isOpaque: Bool { alpha == 255 } 54 | 55 | /// The light intensity 56 | /// - seealso https://en.wikipedia.org/wiki/Grayscale#Converting_color_to_grayscale 57 | var lightIntensity: Double { 58 | let linearR = red.gammaExpanded 59 | let linearG = green.gammaExpanded 60 | let linearB = blue.gammaExpanded 61 | 62 | let linearY = 0.2126 * linearR + 0.7152 * linearG + 0.0722 * linearB 63 | 64 | if linearY <= 0.0031308 { 65 | return 12.92 * linearY 66 | } else { 67 | return 1.055 * pow(linearY, 1 / 2.4) - 0.055 68 | } 69 | } 70 | 71 | func intensityDistance(to pixel: RGBAPixel) -> Double { 72 | if self == pixel { 73 | return 0 74 | } else { 75 | return abs(self.lightIntensity - pixel.lightIntensity) 76 | } 77 | } 78 | 79 | // swiftlint:disable identifier_name 80 | /// The squared euclidean color distance between two pixels 81 | /// - seealso https://en.wikipedia.org/wiki/Color_difference 82 | func squaredDistance(to pixel: RGBAPixel) -> Double { 83 | if self == pixel { 84 | 85 | return 0 86 | } else { 87 | let (r1, g1, b1) = (Double(red), Double(green), Double(blue)) 88 | let (r2, g2, b2) = (Double(pixel.red), Double(pixel.green), Double(pixel.blue)) 89 | 90 | let (deltaR, deltaG, deltaB) = (r1 - r2, g1 - g2, b1 - b2) 91 | 92 | let meanR = (r1 + r2) / 510 93 | 94 | let weightedR = deltaR * deltaR * (2 + meanR) 95 | let weightedG = deltaG * deltaG * 4 96 | let weightedB = deltaB * deltaB * (3 - meanR) 97 | 98 | let weightedSum = weightedR + weightedG + weightedB 99 | let normalisedSum = weightedSum / 585225 // 9 * 255 * 255 100 | 101 | return normalisedSum 102 | } 103 | } 104 | 105 | static var black = RGBAPixel(white: 0) 106 | static var darkGray = RGBAPixel(white: 85) 107 | static var lightGray = RGBAPixel(white: 170) 108 | static var white = RGBAPixel(white: 255) 109 | static var gray = RGBAPixel(white: 128) 110 | static var red = RGBAPixel(red: 255) 111 | static var green = RGBAPixel(green: 255) 112 | static var blue = RGBAPixel(blue: 255) 113 | static var cyan = RGBAPixel(green: 255, blue: 255) 114 | static var yellow = RGBAPixel(red: 255, green: 255) 115 | static var magenta = RGBAPixel(red: 255, blue: 255) 116 | static var orange = RGBAPixel(red: 255, green: 128) 117 | static var purple = RGBAPixel(red: 128, blue: 128) 118 | static var brown = RGBAPixel(red: 153, green: 102, blue: 51) 119 | static var clear = RGBAPixel(white: 0, alpha: 0) 120 | } 121 | 122 | fileprivate extension UInt8 { 123 | init(cgFloat: CGFloat) { 124 | self = UInt8(round(Double(cgFloat) * 255)) 125 | } 126 | 127 | var gammaExpanded: Double { 128 | if self < 11 { 129 | return Double(self) / 3294.6 130 | } else { 131 | return pow((Double(self) + 14.025) / 269.025, 2.4) 132 | } 133 | } 134 | } 135 | -------------------------------------------------------------------------------- /Sources/SUITCase/SUITCase+SUITCaseMethod.swift: -------------------------------------------------------------------------------- 1 | /* 2 | SUITCase 3 | 4 | Copyright (c) 2020 Devexperts LLC 5 | 6 | See https://code.devexperts.com for more open source projects 7 | */ 8 | 9 | import XCTest 10 | 11 | @available(iOS 12.0, *) 12 | @available(tvOS 10.0, *) 13 | public protocol SUITCaseMethod { 14 | // Resizes the image, dismisses its orientation 15 | func prepareScreenshot(_ image: UIImage) -> UIImage 16 | // Compare images and return difference 17 | func compareImages(actual: UIImage, reference: UIImage) throws -> (value: Double, image: RGBAImage) 18 | } 19 | 20 | @available(iOS 12.0, *) 21 | @available(tvOS 10.0, *) 22 | /// Downscales screenshots and allows configurable tolerance while comparing pixels. 23 | public class SUITCaseMethodWithTolerance: SUITCaseMethod { 24 | var tolerance: Double 25 | 26 | public func prepareScreenshot(_ image: UIImage) -> UIImage { 27 | return image.resized(by: 1 / image.scale) 28 | } 29 | 30 | func equalityCondition(_ pixel1: RGBAPixel, _ pixel2: RGBAPixel) -> Bool { 31 | return pixel1.squaredDistance(to: pixel2) <= tolerance * tolerance 32 | } 33 | 34 | public func compareImages(actual: UIImage, 35 | reference: UIImage) throws -> (value: Double, image: RGBAImage) { 36 | let actualRGBAImage = RGBAImage(uiImage: actual) 37 | let referenceRGBAImage = RGBAImage(uiImage: reference) 38 | 39 | var opaquePixelsCount = 0 40 | var incorrectPixelsCount = 0 41 | var pixelsCounter = 0 42 | var differenceImage = RGBAImage(pixel: DiffImageColors.fillColor, 43 | width: actualRGBAImage.width, 44 | height: actualRGBAImage.height) 45 | 46 | guard (actualRGBAImage.width, actualRGBAImage.height) == (referenceRGBAImage.width, referenceRGBAImage.height) else { 47 | throw SUITCase.VerifyScreenshotError.unexpectedSize 48 | } 49 | 50 | for (actualPixel, expectedPixel) in zip(actualRGBAImage.pixels, referenceRGBAImage.pixels) { 51 | if actualPixel.isOpaque, expectedPixel.isOpaque { 52 | opaquePixelsCount += 1 53 | if !equalityCondition(actualPixel, expectedPixel) { 54 | incorrectPixelsCount += 1 55 | differenceImage.pixels[pixelsCounter] = DiffImageColors.tintColor 56 | } 57 | } else { 58 | differenceImage.pixels[pixelsCounter] = DiffImageColors.ignoredColor 59 | } 60 | pixelsCounter += 1 61 | } 62 | 63 | guard opaquePixelsCount > 0 else { 64 | throw SUITCase.VerifyScreenshotError.nothingCommon 65 | } 66 | 67 | let normalizedDifference = Double(incorrectPixelsCount) / Double(opaquePixelsCount) 68 | 69 | return (normalizedDifference, differenceImage) 70 | } 71 | 72 | struct DiffImageColors { 73 | static var ignoredColor = RGBAPixel.clear 74 | static var fillColor = RGBAPixel.white 75 | static var tintColor = RGBAPixel.black 76 | } 77 | 78 | public init(_ tolerance: Double = 0.1) { 79 | self.tolerance = tolerance 80 | } 81 | } 82 | 83 | @available(iOS 12.0, *) 84 | @available(tvOS 10.0, *) 85 | /// The most accurate method, which compares original screenshots pixel by pixel. 86 | public class SUITCaseMethodStrict: SUITCaseMethodWithTolerance { 87 | public override func prepareScreenshot(_ image: UIImage) -> UIImage { 88 | return image.resized(by: 1) 89 | } 90 | 91 | override func equalityCondition(_ pixel1: RGBAPixel, _ pixel2: RGBAPixel) -> Bool { 92 | return pixel1 == pixel2 93 | } 94 | 95 | public init() { } 96 | } 97 | 98 | @available(iOS 12.0, *) 99 | @available(tvOS 10.0, *) 100 | /// Downscales screenshots, removes color saturation, and allows configurable tolerance while comparing pixels. 101 | public class SUITCaseMethodGreyscale: SUITCaseMethodWithTolerance { 102 | override func equalityCondition(_ pixel1: RGBAPixel, _ pixel2: RGBAPixel) -> Bool { 103 | return pixel1.intensityDistance(to: pixel2) <= tolerance 104 | } 105 | 106 | public init(tolerance: Double = 0.1) { 107 | super.init(tolerance) 108 | } 109 | } 110 | 111 | @available(iOS 12.0, *) 112 | @available(tvOS 10.0, *) 113 | /// Extremely dowscales screenshots, and allows configurable tolerance while comparing pixels. 114 | public class SUITCaseMethodDNA: SUITCaseMethodWithTolerance { 115 | var scaleFactor: CGFloat 116 | 117 | public override func prepareScreenshot(_ image: UIImage) -> UIImage { 118 | return image.dna(scaleFactor: CGFloat(scaleFactor)) 119 | } 120 | 121 | override func equalityCondition(_ pixel1: RGBAPixel, _ pixel2: RGBAPixel) -> Bool { 122 | return pixel1.squaredDistance(to: pixel2) <= tolerance * tolerance 123 | } 124 | 125 | public init(tolerance: Double = 0.1, scaleFactor: CGFloat = 0.03) { 126 | self.scaleFactor = scaleFactor 127 | super.init(tolerance) 128 | } 129 | } 130 | 131 | @available(iOS 12.0, *) 132 | @available(tvOS 10.0, *) 133 | /// Compares average colors of screenshots. 134 | public class SUITCaseMethodAverageColor: SUITCaseMethod { 135 | public func prepareScreenshot(_ image: UIImage) -> UIImage { 136 | return image.resized(by: 1 / image.scale) 137 | } 138 | 139 | public func compareImages(actual: UIImage, reference: UIImage) throws -> (value: Double, image: RGBAImage) { 140 | guard let actualColor = RGBAImage(uiImage: actual).averageColor, 141 | let expectedColor = RGBAImage(uiImage: reference).averageColor else { 142 | throw SUITCase.VerifyScreenshotError.nothingCommon 143 | } 144 | let normalizedDifference = sqrt(actualColor.squaredDistance(to: expectedColor)) 145 | 146 | let width = 100 147 | let differenceImagePixels = Array(repeating: actualColor, count: width * width) 148 | + Array(repeating: expectedColor, count: width * width) 149 | let differenceImage = RGBAImage(pixels: differenceImagePixels, 150 | width: width, 151 | height: 2 * width) 152 | XCTContext.runActivity(named: "Actual color: \(actualColor)") { _ in } 153 | XCTContext.runActivity(named: "Expected color: \(expectedColor)") { _ in } 154 | return (normalizedDifference, differenceImage) 155 | } 156 | 157 | public init() { } 158 | } 159 | -------------------------------------------------------------------------------- /Sources/SUITCase/SUITCase+addNote+addImage.swift: -------------------------------------------------------------------------------- 1 | /* 2 | SUITCase 3 | 4 | Copyright (c) 2020 Devexperts LLC 5 | 6 | See https://code.devexperts.com for more open source projects 7 | */ 8 | 9 | import XCTest 10 | 11 | @available(iOS 12.0, *) 12 | @available(tvOS 10.0, *) 13 | extension SUITCase { 14 | func addNote(_ message: String) { 15 | XCTContext.runActivity(named: message) { _ in } 16 | } 17 | 18 | func addImage(_ image: UIImage, name: String) { 19 | let attachment = XCTAttachment(image: image) 20 | attachment.lifetime = .keepAlways 21 | attachment.name = name 22 | 23 | add(attachment) 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /Sources/SUITCase/SUITCase+collectScreenshot.swift: -------------------------------------------------------------------------------- 1 | /* 2 | SUITCase 3 | 4 | Copyright (c) 2020 Devexperts LLC 5 | 6 | See https://code.devexperts.com for more open source projects 7 | */ 8 | 9 | import XCTest 10 | 11 | @available(iOS 12.0, *) 12 | @available(tvOS 10.0, *) 13 | extension SUITCase { 14 | func collectScreenshot(ofElement element: XCUIElement? = nil, 15 | withoutElement ignoredElement: XCUIElement? = nil, 16 | withoutQuery ignoredQuery: XCUIElementQuery? = nil, 17 | withMethod method: SUITCaseMethod = SUITCaseMethodWithTolerance()) throws -> UIImage { 18 | var actualImage = UIImage() 19 | 20 | try XCTContext.runActivity(named: "Take a screenshot") { _ in 21 | var originPoint = CGPoint.zero 22 | 23 | if let element = element { 24 | guard element.isFullyOnScreen else { 25 | throw VerifyScreenshotError.offScreenElement(element: element) 26 | } 27 | actualImage = element.screenshot().image 28 | originPoint = element.frame.origin 29 | } else { 30 | actualImage = XCUIScreen.main.screenshot().image 31 | } 32 | 33 | // Fade the ignored elements 34 | var ignoredElements: [XCUIElement] = [] 35 | if let ignoredQuery = ignoredQuery { 36 | ignoredElements += ignoredQuery.allElementsBoundByIndex 37 | } 38 | if let ignoredElement = ignoredElement { 39 | ignoredElements.append(ignoredElement) 40 | } 41 | for element in ignoredElements { 42 | if element.exists { 43 | var ignoredElementFrame = element.frame 44 | ignoredElementFrame = ignoredElementFrame.offsetBy(dx: -originPoint.x, dy: -originPoint.y) 45 | actualImage = actualImage.clear(rectangle: ignoredElementFrame) 46 | } else { 47 | addNote("Warning: \(element) does not exist") 48 | } 49 | } 50 | 51 | actualImage = method.prepareScreenshot(actualImage) 52 | } 53 | addImage(actualImage, name: "Collected image") 54 | 55 | return actualImage 56 | } 57 | } 58 | 59 | @available(iOS 12.0, *) 60 | @available(tvOS 10.0, *) 61 | extension XCUIElement { 62 | var isFullyOnScreen: Bool { 63 | let screenFrame = CGRect(origin: CGPoint.zero, size: XCUIScreen.main.screenshot().image.size) 64 | return screenFrame.contains(self.frame) 65 | } 66 | } 67 | -------------------------------------------------------------------------------- /Sources/SUITCase/SUITCase+getImagePaths.swift: -------------------------------------------------------------------------------- 1 | /* 2 | SUITCase 3 | 4 | Copyright (c) 2020 Devexperts LLC 5 | 6 | See https://code.devexperts.com for more open source projects 7 | */ 8 | 9 | import XCTest 10 | 11 | @available(iOS 12.0, *) 12 | @available(tvOS 10.0, *) 13 | extension SUITCase { 14 | static var screenshotComparisonImagesFolder = ProcessInfo.processInfo.environment["IMAGES_DIR"] 15 | 16 | /// The enumeration of possible reference screenshots naming strategies. 17 | public enum ScreenshotComparisonNamingStrategies { 18 | /// Uses device name (e.g. "iPhone 9") as part of the reference image name. 19 | case deviceModelName 20 | /// Uses screenshot size (e.g. "375x667") as part of the reference image name. 21 | case imageSize 22 | /// Only uses a test name or a custom label (if provided) as part of the reference image name. 23 | case manual 24 | } 25 | 26 | // swiftlint:disable large_tuple 27 | func getImagePaths(withLabel customLabel: String?, 28 | imageSize: CGSize) throws -> (reference: String, suggested: String, unexpected: String) { 29 | guard let imagesFolder = SUITCase.screenshotComparisonImagesFolder, 30 | let testClassName = testClassName, 31 | var testName = testName, 32 | let deviceLanguageCode = deviceLanguageCode else { 33 | throw VerifyScreenshotError.badImagesPath 34 | } 35 | if let customLabel = customLabel { 36 | testName += "/" + customLabel 37 | } 38 | let fileName: String 39 | let filePath: String 40 | switch screenshotComparisonNamingStrategy { 41 | case .imageSize: 42 | fileName = "\(deviceLanguageCode)_\(Int(imageSize.width))x\(Int(imageSize.height)).png" 43 | filePath = [testClassName, testName, fileName].joined(separator: "/") 44 | case .deviceModelName: 45 | fileName = "\(deviceLanguageCode)_\(deviceModelName).png" 46 | filePath = [testClassName, testName, fileName].joined(separator: "/") 47 | case .manual: 48 | fileName = "\(customLabel ?? testName).png" 49 | filePath = fileName 50 | } 51 | 52 | var imagePaths: [String] = [] 53 | for subfolder in ["Reference", "Suggested", "Unexpected"] { 54 | let fullPath = [imagesFolder, subfolder, filePath].joined(separator: "/") 55 | imagePaths.append(fullPath) 56 | } 57 | 58 | return (imagePaths[0], imagePaths[1], imagePaths[2]) 59 | } 60 | } 61 | -------------------------------------------------------------------------------- /Sources/SUITCase/SUITCase+verifyScreenshot.swift: -------------------------------------------------------------------------------- 1 | /* 2 | SUITCase 3 | 4 | Copyright (c) 2020 Devexperts LLC 5 | 6 | See https://code.devexperts.com for more open source projects 7 | */ 8 | 9 | import XCTest 10 | 11 | @available(iOS 12.0, *) 12 | @available(tvOS 10.0, *) 13 | extension SUITCase { 14 | enum VerifyScreenshotError: LocalizedError { 15 | case notSimulator 16 | case offScreenElement(element: XCUIElement) 17 | case recordMode 18 | case noReference 19 | case unexpectedSize 20 | case nothingCommon 21 | case noMatch 22 | case badImagesPath 23 | case badThreshold(value: Double) 24 | 25 | public var errorDescription: String? { 26 | switch self { 27 | case .notSimulator: 28 | return "SUITCase does not support devices" 29 | case .offScreenElement(let element): 30 | return "\(element) is off-screen" 31 | case .recordMode: 32 | return "Record mode is enabled. Saved reference screenshot" 33 | case .noReference: 34 | return "Reference screenshot is missing. Saved suggested screenshot" 35 | case .unexpectedSize: 36 | return "The size of collected screenshot is unexpected" 37 | case .nothingCommon: 38 | return "Unable to compare actual and reference screenshots. The opaque areas do not overlap" 39 | case .noMatch: 40 | return "Actual and reference screenshots are not similar. Saved unexpected screenshot" 41 | case .badImagesPath: 42 | return "Unable to get images path" 43 | case .badThreshold(let value): 44 | return "Threshold \(value) is not normalised" 45 | } 46 | } 47 | } 48 | 49 | /// Verifies application screenshot or records new reference image. 50 | /// 51 | /// - Parameters: 52 | /// - element: An optional XCUIElement that needs to be verified. 53 | /// - ignoredElement: An optional XCUIElement that needs to be ignored. 54 | /// - ignoredQuery: An optional XCUIElementQuery of elements that need to be ignored. 55 | /// - customThreshold: An optional custom threshold. 56 | /// - method: An optional method of comparing pixels. 57 | /// - label: An optional screenshot label (use if test case have multiple screenshot comparisons). 58 | /// - file: The file in which failure occurred. 59 | /// Defaults to the file name of the test case in which this function was called. 60 | /// - line: The line number on which failure occurred. 61 | /// Defaults to the line number on which this function was called. 62 | public func verifyScreenshot(ofElement element: XCUIElement? = nil, 63 | withoutElement ignoredElement: XCUIElement? = nil, 64 | withoutQuery ignoredQuery: XCUIElementQuery? = nil, 65 | withThreshold customThreshold: Double? = nil, 66 | withMethod method: SUITCaseMethod = SUITCaseMethodWithTolerance(), 67 | withLabel label: String? = nil, 68 | file: StaticString = #file, 69 | line: UInt = #line) { 70 | var activityName = screenshotComparisonRecordMode ? "Record screenshot" : "Verify screenshot" 71 | if let element = element { 72 | activityName += " of \(element)" 73 | } 74 | XCTContext.runActivity(named: activityName) { _ in 75 | do { 76 | try verifyScreenshotThrowing(ofElement: element, 77 | withoutElement: ignoredElement, 78 | withoutQuery: ignoredQuery, 79 | withThreshold: customThreshold, 80 | withMethod: method, 81 | withLabel: label) 82 | 83 | } catch { 84 | XCTFail(error.localizedDescription, file: file, line: line) 85 | } 86 | } 87 | } 88 | 89 | /// Verifies application screenshot or records new reference image. 90 | /// 91 | /// - Parameters: 92 | /// - element: An optional XCUIElement that needs to be verified. 93 | /// - ignoredElement: An optional XCUIElement that needs to be ignored 94 | /// - ignoredQuery: An optional XCUIElementQuery of elements that need to be ignored 95 | /// - customThreshold: An optional custom threshold. 96 | /// - method: An optional method of comparing pixels. 97 | /// - label: An optional screenshot label (use if test case have multiple screenshot comparisons). 98 | func verifyScreenshotThrowing(ofElement element: XCUIElement? = nil, 99 | withoutElement ignoredElement: XCUIElement? = nil, 100 | withoutQuery ignoredQuery: XCUIElementQuery? = nil, 101 | withThreshold customThreshold: Double? = nil, 102 | withMethod method: SUITCaseMethod = SUITCaseMethodWithTolerance(), 103 | withLabel label: String? = nil) throws { 104 | guard UIDevice.isSimulator else { 105 | throw VerifyScreenshotError.notSimulator 106 | } 107 | 108 | let actualImage = try collectScreenshot(ofElement: element, 109 | withoutElement: ignoredElement, 110 | withoutQuery: ignoredQuery, 111 | withMethod: method) 112 | let filePaths = try getImagePaths(withLabel: label, imageSize: actualImage.scaledSize) 113 | 114 | guard !screenshotComparisonRecordMode else { 115 | try actualImage.writePNG(filePath: filePaths.reference) 116 | throw VerifyScreenshotError.recordMode 117 | } 118 | 119 | let threshold = customThreshold ?? screenshotComparisonGlobalThreshold 120 | switch threshold { 121 | case 0..<0.5: 122 | break 123 | case 0.5..<1: 124 | addNote("Warning: Threshold is dangerously high!") 125 | default: 126 | throw VerifyScreenshotError.badThreshold(value: threshold) 127 | } 128 | 129 | guard let referenceImage = UIImage(contentsOfFile: filePaths.reference) else { 130 | try actualImage.writePNG(filePath: filePaths.suggested) 131 | throw VerifyScreenshotError.noReference 132 | } 133 | 134 | let difference = try method.compareImages(actual: actualImage, 135 | reference: referenceImage) 136 | 137 | addNote("Threshold = \(String(format: "%.4f", threshold))") 138 | addNote("Difference = \(String(format: "%.4f", difference.value))") 139 | 140 | if difference.value == 0 { 141 | addNote("Collected and reference screenshots are similar") 142 | } else { 143 | addImage(referenceImage, name: "Reference image") 144 | addImage(difference.image.uiImage, name: "Difference image") 145 | if difference.value > threshold { 146 | try actualImage.writePNG(filePath: filePaths.unexpected) 147 | throw VerifyScreenshotError.noMatch 148 | } 149 | } 150 | } 151 | } 152 | -------------------------------------------------------------------------------- /Sources/SUITCase/SUITCase.swift: -------------------------------------------------------------------------------- 1 | /* 2 | SUITCase 3 | 4 | Copyright (c) 2020 Devexperts LLC 5 | 6 | See https://code.devexperts.com for more open source projects 7 | */ 8 | 9 | import XCTest 10 | 11 | /// SUITCase inherits from XCTestCase and allows comparing screenshots while testing UI. 12 | @available(iOS 12.0, *) 13 | @available(tvOS 10.0, *) 14 | open class SUITCase: XCTestCase { 15 | private lazy var testNameComponents = name 16 | .trimmingCharacters(in: .punctuationCharacters) 17 | .components(separatedBy: .whitespaces) 18 | lazy var testClassName = testNameComponents.first 19 | lazy var testName = testNameComponents.last 20 | 21 | var deviceModelName = UIDevice.modelName.replacingOccurrences(of: " ", with: "_") 22 | var deviceLanguageCode = Locale(identifier: Locale.preferredLanguages.first!).languageCode 23 | 24 | /// Enables Record Mode, which saves reference images. 25 | public var screenshotComparisonRecordMode = false 26 | /// The global threshold variable, which allows the difference between collected and reference screenshots. 27 | public var screenshotComparisonGlobalThreshold = 0.01 28 | /// Changes current reference images naming strategy. 29 | public var screenshotComparisonNamingStrategy = ScreenshotComparisonNamingStrategies.imageSize 30 | } 31 | -------------------------------------------------------------------------------- /Sources/SUITCase/UIDevice+modelName.swift: -------------------------------------------------------------------------------- 1 | /* 2 | SUITCase 3 | 4 | Copyright (c) 2020 Devexperts LLC 5 | 6 | See https://code.devexperts.com for more open source projects 7 | */ 8 | 9 | import XCTest 10 | import DeviceKit 11 | 12 | public extension UIDevice { 13 | 14 | static var modelName: String { 15 | var result = DeviceKit.Device.current.safeDescription 16 | 17 | if isSimulator { 18 | result = result.replacingOccurrences(of: "Simulator (", with: "") 19 | result.removeLast() // closing brace 20 | } 21 | 22 | return result 23 | } 24 | 25 | static let isSimulator = DeviceKit.Device.current.isSimulator 26 | 27 | } 28 | -------------------------------------------------------------------------------- /Sources/SUITCase/UIImage+Extensions.swift: -------------------------------------------------------------------------------- 1 | /* 2 | SUITCase 3 | 4 | Copyright (c) 2020 Devexperts LLC 5 | 6 | See https://code.devexperts.com for more open source projects 7 | */ 8 | 9 | import XCTest 10 | 11 | @available(iOS 12.0, *) 12 | @available(tvOS 10.0, *) 13 | extension UIImage { 14 | func writePNG(filePath: String) throws { 15 | try XCTContext.runActivity(named: "Save " + filePath) { _ in 16 | let fileURL = URL(fileURLWithPath: filePath) 17 | let folderURL = fileURL.deletingLastPathComponent() 18 | try FileManager.default.createDirectory(at: folderURL, 19 | withIntermediateDirectories: true, 20 | attributes: nil) 21 | 22 | try pngData()?.write(to: fileURL) 23 | } 24 | } 25 | 26 | func resized(size: CGSize) -> UIImage { 27 | let renderer = UIGraphicsImageRenderer(size: size) 28 | let image = renderer.image { _ in 29 | self.draw(in: CGRect(origin: CGPoint.zero, size: size)) 30 | } 31 | 32 | return image 33 | } 34 | 35 | func resized(by scaleFactor: CGFloat) -> UIImage { 36 | let newSize = CGSize(width: size.width * scaleFactor, 37 | height: size.height * scaleFactor) 38 | 39 | return resized(size: newSize) 40 | } 41 | 42 | func dna(scaleFactor: CGFloat) -> UIImage { 43 | let newSize = CGSize(width: (size.width * scaleFactor).rounded() / scale, 44 | height: (size.height * scaleFactor).rounded() / scale) 45 | return resized(size: newSize) 46 | } 47 | 48 | func clear(rectangle: CGRect) -> UIImage { 49 | var resultImage = self 50 | let innerForeground = rectangle.intersection(CGRect(origin: CGPoint.zero, size: size)) 51 | 52 | if innerForeground.size != CGSize.zero { 53 | UIGraphicsBeginImageContextWithOptions(size, false, scale) 54 | draw(at: CGPoint.zero) 55 | let context = UIGraphicsGetCurrentContext()! 56 | context.clear(innerForeground) 57 | resultImage = UIGraphicsGetImageFromCurrentImageContext()! 58 | } 59 | 60 | return resultImage 61 | } 62 | 63 | var scaledSize: CGSize { 64 | size.applying(CGAffineTransform(scaleX: scale, y: scale)) 65 | } 66 | } 67 | -------------------------------------------------------------------------------- /Sources/SUITCase/XCUIScreenshotProviding+averageColorIs.swift: -------------------------------------------------------------------------------- 1 | /* 2 | SUITCase 3 | 4 | Copyright (c) 2020 Devexperts LLC 5 | 6 | See https://code.devexperts.com for more open source projects 7 | */ 8 | 9 | import XCTest 10 | 11 | @available(iOS 12.0, *) 12 | @available(tvOS 10.0, *) 13 | extension XCUIScreenshotProviding { 14 | /// Determines average color is expected 15 | /// - Parameters: 16 | /// - uiColor: An expected color 17 | /// - tolerance: A threshold that allows minor color difference. Default is 0.1 18 | public func averageColorIs(_ uiColor: UIColor, tolerance: Double = 0.1) -> Bool { 19 | let expectedPixel = RGBAPixel.init(uiColor: uiColor) 20 | 21 | return XCTContext.runActivity(named: "Check if the average color of \(self) equals to \(expectedPixel)") { _ in 22 | let pixelImage = screenshot().image.resized(size: CGSize(width: 1, height: 1)) 23 | let rgbaImage = RGBAImage(uiImage: pixelImage) 24 | 25 | if let actualPixel = rgbaImage.pixels.first { 26 | let distance = sqrt(actualPixel.squaredDistance(to: expectedPixel)) 27 | XCTContext.runActivity(named: "Actual color: \(actualPixel)") { _ in } 28 | XCTContext.runActivity(named: "Threshold: \(tolerance)") { _ in } 29 | XCTContext.runActivity(named: "Distance: \(distance)") { _ in } 30 | return distance <= tolerance 31 | } else { 32 | XCTContext.runActivity(named: "Unable to get average color") { _ in } 33 | return false 34 | } 35 | } 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /Tests/LinuxMain.swift: -------------------------------------------------------------------------------- 1 | /* 2 | SUITCase 3 | 4 | Copyright (c) 2020 Devexperts LLC 5 | 6 | See https://code.devexperts.com for more open source projects 7 | */ 8 | 9 | import XCTest 10 | 11 | import SUITCaseTests 12 | 13 | var tests = [XCTestCaseEntry]() 14 | tests += SUITCaseTests.allTests() 15 | XCTMain(tests) 16 | -------------------------------------------------------------------------------- /Tests/SUITCaseTests/CompareImagesTests.swift: -------------------------------------------------------------------------------- 1 | /* 2 | SUITCase 3 | 4 | Copyright (c) 2020 Devexperts LLC 5 | 6 | See https://code.devexperts.com for more open source projects 7 | */ 8 | 9 | import XCTest 10 | @testable import SUITCase 11 | 12 | @available(iOS 12.0, *) 13 | @available(tvOS 10.0, *) 14 | class CompareImagesTests: XCTestCase { 15 | func assertComparison(method: SUITCaseMethod, 16 | _ image1: RGBAImage, 17 | _ image2: RGBAImage, 18 | expectedDifference: (image: RGBAImage, value: Double), 19 | file: StaticString = #file, 20 | line: UInt = #line) { 21 | do { 22 | let actualDifference = try method.compareImages(actual: image1.uiImage, 23 | reference: image2.uiImage) 24 | 25 | XCTAssertTrue(actualDifference.image == expectedDifference.image, file: file, line: line) 26 | XCTAssertEqual(actualDifference.value, 27 | expectedDifference.value, 28 | accuracy: 1e-6, 29 | file: file, 30 | line: line) 31 | } catch { 32 | XCTFail(error.localizedDescription, file: file, line: line) 33 | } 34 | } 35 | 36 | func assertComparisonError(method: SUITCaseMethod, 37 | _ image1: RGBAImage, 38 | _ image2: RGBAImage, 39 | _ expectedError: Error, 40 | file: StaticString = #file, 41 | line: UInt = #line) { 42 | XCTAssertThrowsError(try method.compareImages(actual: image1.uiImage, 43 | reference: image2.uiImage), 44 | file: file, 45 | line: line) { error in 46 | do { 47 | XCTAssertEqual(error.localizedDescription, 48 | expectedError.localizedDescription, 49 | "Unexpected error description", 50 | file: file, 51 | line: line) 52 | } 53 | } 54 | } 55 | 56 | let rgbaImage = RGBAImage(pixels: [.black, .blue, .green, .cyan, 57 | .red, .magenta, .yellow, .white, 58 | .darkGray, .gray, .lightGray, .clear], 59 | width: 4, 60 | height: 3) 61 | 62 | var lighterImage: RGBAImage { 63 | var template = rgbaImage 64 | 65 | func add16(_ component: inout UInt8) { 66 | component = component > 239 ? 255 : component + 16 67 | } 68 | 69 | for counter in 0.. [XCTestCaseEntry] { 13 | return [ 14 | testCase(SUITCaseTests.allTests) 15 | ] 16 | } 17 | #endif 18 | --------------------------------------------------------------------------------