         let line = Int(currentStepLocation.line)
         guard filePath != file, lineNumber != line else { return }
         test.recordFailure(withDescription: description, inFile: file, atLine: line, expected: false)
+
+        if automaticScreenshotsBehaviour.contains(.onFailure) {
+            test.attachScreenshot(name: "Failed \"\(currentStepName)\"")
+        }
     }
 
     func gherkinStepsAndMatchesMatchingExpression(_ expression: String) -> [(step: Step, match: NSTextCheckingResult)] {

            // Run the step
            state.currentStepDepth += 1
            state.currentStepLocation = (file, line)
            step.function(matchStrings)
            state.currentStepLocation = nil
            state.currentStepDepth -= 1
        }

        }
    }
    
}

private var automaticScreenshotsBehaviour: AutomaticScreenshotsBehaviour = []

public struct AutomaticScreenshotsBehaviour: OptionSet {
    public let rawValue: Int
    public init(rawValue: Int) {
        self.rawValue = rawValue
    }

    public static let onFailure    = AutomaticScreenshotsBehaviour(rawValue: 1 << 0)
    public static let beforeStep   = AutomaticScreenshotsBehaviour(rawValue: 1 << 1)
    public static let afterStep    = AutomaticScreenshotsBehaviour(rawValue: 1 << 2)
}

extension XCTestCase {
    public static func setAutomaticScreenshotsBehaviour(_ behaviour: AutomaticScreenshotsBehaviour) {
        automaticScreenshotsBehaviour = behaviour
    }

    func attachScreenshot(name: String) {
        let screenshot = XCUIScreen.main.screenshot()
        let attachment = XCTAttachment(screenshot: screenshot)
        attachment.name = name
        add(attachment)
    }
}

/**
