Spike: Adding screenshot flow for the start of each test by amleszk · Pull Request #114 · XCTestHTMLReport/XCTestHTMLReport · GitHub
Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions XCTestHTMLReport/XCTestHTMLReport.xcodeproj/project.pbxproj
6 changes: 6 additions & 0 deletions XCTestHTMLReport/XCTestHTMLReport/Classes/HTMLTemplates.swift
Original file line number Diff line number Diff line change
Expand Up @@ -391,6 +391,11 @@ struct HTMLTemplates
display:none;
z-index: 1000;
}

.preview-screenshot {
background-color: white;
height: 350px;
}

#content {
height: 100%;
Expand Down Expand Up @@ -924,6 +929,7 @@ struct HTMLTemplates
</p>
[[SUB_TESTS]]
<div id=\"activities-[[UUID]]\" class=\"activities\">
[[SCREENSHOT_FLOW]]
[[ACTIVITIES]]
</div>
</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,12 +109,8 @@ struct Activity: HTML
"TIME": totalTime.timeString,
"ACTIVITY_TYPE_CLASS": cssClasses,
"HAS_SUB-ACTIVITIES_CLASS": (subActivities == nil && (attachments == nil || attachments?.count == 0)) ? "no-drop-down" : "",
"SUB_ACTIVITY": subActivities?.reduce("", { (accumulator: String, activity: Activity) -> String in
return accumulator + activity.html
}) ?? "",
"ATTACHMENTS": attachments?.reduce("", { (accumulator: String, attachment: Attachment) -> String in
return accumulator + attachment.html
}) ?? "",
"SUB_ACTIVITY": subActivities?.accumulateHTMLAsString ?? "",
"ATTACHMENTS": attachments?.accumulateHTMLAsString ?? "",
]
}
}
12 changes: 12 additions & 0 deletions XCTestHTMLReport/XCTestHTMLReport/Classes/Models/Attachment.swift
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,18 @@ struct Attachment: HTML
self.padding = padding
}

var isScreenshot: Bool {
if let type = type {
switch type {
case .png, .jpeg:
return true
default:
return false
}
}
return false
}

var fallbackDisplayName: String {
guard let type = type else { return "Attachment" }

Expand Down
13 changes: 6 additions & 7 deletions XCTestHTMLReport/XCTestHTMLReport/Classes/Models/Test.swift
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ struct Test: HTML
var activities: [Activity]?
var status: Status
var objectClass: ObjectClass
var testScreenshotFlow: TestScreenshotFlow?

var allSubTests: [Test]? {
guard subTests != nil else {
Expand Down Expand Up @@ -98,6 +99,7 @@ struct Test: HTML

let rawStatus = dict["TestStatus"] as? String ?? ""
status = Status(rawValue: rawStatus)!
testScreenshotFlow = TestScreenshotFlow(activities: activities)
}

// PRAGMA MARK: - HTML
Expand All @@ -109,16 +111,13 @@ struct Test: HTML
"UUID": uuid,
"NAME": name + (amountSubTests > 0 ? " - \(amountSubTests) tests" : ""),
"TIME": duration.timeString,
"SUB_TESTS": subTests?.reduce("", { (accumulator: String, test: Test) -> String in
return accumulator + test.html
}) ?? "",
"SUB_TESTS": subTests?.accumulateHTMLAsString ?? "",
"HAS_ACTIVITIES_CLASS": (activities == nil) ? "no-drop-down" : "",
"ACTIVITIES": activities?.reduce("", { (accumulator: String, activity: Activity) -> String in
return accumulator + activity.html
}) ?? "",
"ACTIVITIES": activities?.accumulateHTMLAsString ?? "",
"ICON_CLASS": status.cssClass,
"ITEM_CLASS": objectClass.cssClass,
"LIST_ITEM_CLASS": objectClass == .testSummary ? (status == .failure ? "list-item list-item-failed" : "list-item") : ""
"LIST_ITEM_CLASS": objectClass == .testSummary ? (status == .failure ? "list-item list-item-failed" : "list-item") : "",
"SCREENSHOT_FLOW": testScreenshotFlow?.screenshots.accumulateHTMLAsString ?? ""
]
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
//
// TestScreenshotFlow.swift
// XCTestHTMLReport
//
// Created by Alistair Leszkiewicz on 11/10/18.
// Copyright © 2018 Tito. All rights reserved.
//

import Foundation

struct TestScreenshotFlow
{
var screenshots: [ScreenshotAttachment]

init?(activities: [Activity]?) {
guard let activities = activities else {
return nil
}

let anyScreenshots = activities.trueForAny { !$0.screenshotAttachments.isEmpty }
guard anyScreenshots else {
return nil
}
screenshots = activities.flatMap { $0.screenshotAttachments.map { ScreenshotAttachment(attachment: $0) } }
}

}

fileprivate extension Sequence {
// Determines whether any element in the Array matches the conditions defined by the specified predicate.
func trueForAny(_ predicate: (Element) -> Bool) -> Bool {
return first(where: predicate) != nil
}
}

fileprivate extension Activity {

var screenshotAttachments: [Attachment] {
return attachments?.compactMap({ $0 }).filter { $0.isScreenshot } ?? []
+ subScreenshotAttachments
}

var subScreenshotAttachments: [Attachment] {
return subActivities?.compactMap({ $0 }).flatMap({ $0.screenshotAttachments }) ?? []
}
}


struct ScreenshotAttachment: HTML {
let attachment: Attachment

static let screenshot = """
<img class=\"preview-screenshot\" src=\"[[PATH]]/Attachments/[[FILENAME]]\" id=\"screenshot-[[FILENAME]]\"/>
"""

var htmlTemplate: String {
return ScreenshotAttachment.screenshot
}

var htmlPlaceholderValues: [String: String] {
return [
"PATH": attachment.path,
"FILENAME": attachment.filename
]
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,7 @@ struct TestSummary: HTML
var htmlPlaceholderValues: [String: String] {
return [
"UUID": uuid,
"TESTS": tests.reduce("", { (accumulator: String, test: Test) -> String in
return accumulator + test.html
})
"TESTS": tests.accumulateHTMLAsString
]
}
}
10 changes: 10 additions & 0 deletions XCTestHTMLReport/XCTestHTMLReport/Classes/Protocols/HTML.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,16 @@ protocol HTML
var htmlPlaceholderValues: [String: String] { get }
}

extension Sequence where Element : HTML {

var accumulateHTMLAsString: String {
return reduce("", { (accumulator: String, element: HTML) -> String in
return accumulator + element.html
})
}

}

extension HTML
{
var html: String {
Expand Down
5 changes: 5 additions & 0 deletions XCTestHTMLReport/XCTestHTMLReport/HTML/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -385,6 +385,11 @@
display:none;
z-index: 1000;
}

.preview-screenshot {
background-color: white;
height: 350px;
}

#content {
height: 100%;
Expand Down
1 change: 1 addition & 0 deletions XCTestHTMLReport/XCTestHTMLReport/HTML/test.html