Skip to content

Commit

Permalink
Support for Swift4
Browse files Browse the repository at this point in the history
  • Loading branch information
Shashikant86 committed Jun 6, 2017
1 parent a359add commit be01ea2
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 200 deletions.
20 changes: 10 additions & 10 deletions Sources/XCFitAssertions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,37 +11,37 @@ import XCTest

@available(OSX 10.11, *)
extension XCFit {

open func thenIShouldSee(_ element: XCUIElement) {
XCTAssertTrue(element.exists)
}

open func thenElemenShouldHavePlaceholderValue(_ element: XCUIElement, _ value : String) {
XCTAssertEqual(element.placeholderValue, value)
}

open func thenElementShouldHaveTitle(_ element: XCUIElement, _ title: String) {
XCTAssertEqual(element.title, title)
}

open func thenElementShouldBeEnabled(_ element: XCUIElement) {
XCTAssertTrue(element.isEnabled)
}

open func thenElementShouldBeVisible(_ element: XCUIElement) {
XCTAssertTrue(element.isVisible)
XCTAssertTrue(element.isHittable)
}

open func thenElementShouldBeHittable(_ element: XCUIElement) {
XCTAssertTrue(element.isHittable)
}

open func thenElementShouldSelected(_ element: XCUIElement) {
XCTAssertTrue(element.isSelected)
}

open func thenIShouldSeeAnAlert() {
XCTAssertTrue(XCUIApplication().alerts.count > 1)
}

}
192 changes: 4 additions & 188 deletions Sources/XCFitWaiter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,208 +11,24 @@ import Foundation

@available(OSX 10.11, *)
extension XCFit {

open func whenIexpectElementToAppear(_ element: XCUIElement) {
let result = waitForElementToAppearCommpleted(element)
XCTAssertTrue(result)
}

open func whenIexpectElementTimeOut(_ element: XCUIElement) {
let result = waitForElementToAppearTimedOut(element)
XCTAssertTrue(result)
}

open func whenIexpectElementIncorrectOrder(_ element: XCUIElement) {
let result = waitForElementToAppearIncorrectOrder(element)
XCTAssertTrue(result)
}

open func whenIexpectElementInvertedFulfillment(_ element: XCUIElement) {
let result = waitForElementToAppearinvertedFulfillment(element)
XCTAssertTrue(result)
}
}


@available(OSX 10.11, *)
open class XCFitWaiter: XCFit {

open func waitUntilElementExist(element: XCUIElement) {
let exists = NSPredicate(format: "exists == true AND hittable == true AND enabled == true")
self.expectation(for: exists, evaluatedWith: element, handler: nil)
self.waitForExpectations(timeout: 60, handler: nil)
XCTAssert(element.exists)
}

}

@available(OSX 10.11, *)
extension XCUIElement {

open func waitAndTap(testCase: XCTestCase, file: String = #file, line: UInt = #line) {
let predicate = NSPredicate(format: "exists == true AND hittable == true AND enabled == true")
wait(with: testCase, for: predicate, file: file, line: line)
tap()
}

open func waitForAppearance(testCase: XCTestCase, file: String = #file, line: UInt = #line) {
wait(with: testCase, for: NSPredicate(format: "exists == true"), file: file, line: line)
}

open func waitForDisappearance(testCase: XCTestCase, file: String = #file, line: UInt = #line) {
wait(with: testCase, for: NSPredicate(format: "exists == false"), file: file, line: line)

}

private func wait(with testCase: XCTestCase, for predicate: NSPredicate, file: String = #file, line: UInt = #line) {
let timeout: TimeInterval = 90
testCase.expectation(for: predicate, evaluatedWith: self, handler: nil)
testCase.waitForExpectations(timeout: timeout) { error in
guard error != nil else { return }
let message = "Failed to find \(self) after \(timeout) seconds."
testCase.recordFailure(withDescription: message, inFile: file, atLine: line, expected: true)
}
}

}

extension XCTestCase {
@available(OSX 10.11, *)
open func waitForElementToAppearOnScreen(element: XCUIElement,
file: String = #file, line: UInt = #line) {
let existsPredicate = NSPredicate(format: "exists == true")
expectation(for: existsPredicate,
evaluatedWith: element, handler: nil)

waitForExpectations(timeout: 20) { (error) -> Void in
if (error != nil) {
let message = "Failed to find \(element) after 5 seconds."
self.recordFailure(withDescription: message,
inFile: file, atLine: line, expected: true)
}
}
}
}

@available(iOS 9.0, OSX 10.11, *)

extension XCUIElement {

public var isVisible: Bool {
return exists && !frame.isEmpty && XCUIApplication().windows.element(boundBy: 0).frame.contains(frame)
}

}

@available(OSX 10.11, *)
extension XCTestCase {
/**
* Waits for the default timeout until `element.exists` is true.
*
* @param element the element you are waiting for
* @see waitForElementToNotExist()
*/
@available(OSX 10.11, *)
open func waitForElementToExist(element: XCUIElement) {
waitForElement(element: element, toExist: true)
}

/**
* Waits for the default timeout until `element.exists` is false.
*
* @param element the element you are waiting for
* @see waitForElementToExist()
*/
@available(OSX 10.11, *)
open func waitForElementToNotExist(element: XCUIElement) {
waitForElement(element: element, toExist: false)
}

/**
* Waits for the default timeout until the activity indicator stop spinning.
*
* @note Should only be used if one `ActivityIndicator` is present.
*/
open func waitForActivityIndicatorToFinish() {

let spinnerQuery = XCUIApplication().activityIndicators

let expression = { () -> Bool in
return (spinnerQuery.element.value! as AnyObject).integerValue != 1
}
waitFor(expression: expression, failureMessage: "Timed out waiting for spinner to finish.")
}

// MARK: Private

@available(OSX 10.11, *)
private func waitForElement(element: XCUIElement, toExist: Bool) {
let expression = { () -> Bool in
return element.exists == toExist
}
waitFor(expression: expression, failureMessage: "Timed out waiting for element to exist.")
}

private func waitFor(expression: () -> Bool, failureMessage: String) {
let startTime = NSDate.timeIntervalSinceReferenceDate

while (!expression()) {
if (NSDate.timeIntervalSinceReferenceDate - startTime > 2.0) {
raiseTimeOutException(message: failureMessage)
}
CFRunLoopRunInMode(CFRunLoopMode.defaultMode, 0.1, Bool(0))
}
}

private func raiseTimeOutException(message: String) {
NSException(name: NSExceptionName(rawValue: "Timeout Failure"), reason: message, userInfo: nil).raise()
}
}

extension XCTestCase {
@available(OSX 10.11, *)
open func waitForHittable(element: XCUIElement, waitSeconds: Double, file: String = #file, line: UInt = #line) {
let existsPredicate = NSPredicate(format: "hittable == true")
expectation(for: existsPredicate, evaluatedWith: element, handler: nil)

waitForExpectations(timeout: waitSeconds) { (error) -> Void in
if (error != nil) {
let message = "Failed to find \(element) after \(waitSeconds) seconds."
self.recordFailure(withDescription: message,
inFile: file, atLine: line, expected: true)
}
}
}

@available(OSX 10.11, *)
open func waitForNotHittable(element: XCUIElement, waitSeconds: Double, file: String = #file, line: UInt = #line) {
let existsPredicate = NSPredicate(format: "hittable == false")
expectation(for: existsPredicate, evaluatedWith: element, handler: nil)

waitForExpectations(timeout: waitSeconds) { (error) -> Void in
if (error != nil) {
let message = "Failed to find \(element) after \(waitSeconds) seconds."
self.recordFailure(withDescription: message,
inFile: file, atLine: line, expected: true)
}
}
}




@available(OSX 10.11, *)
open func waitForExists(element: XCUIElement, waitSeconds: Double, file: String = #file, line: UInt = #line) {
let existsPredicate = NSPredicate(format: "exists == true")
expectation(for: existsPredicate, evaluatedWith: element, handler: nil)

waitForExpectations(timeout: waitSeconds) { (error) -> Void in
if (error != nil) {
let message = "Failed to find \(element) after \(waitSeconds) seconds."
self.recordFailure(withDescription: message,
inFile: file, atLine: line, expected: true)
}
}
}

}
4 changes: 2 additions & 2 deletions XCFit.podspec
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Pod::Spec.new do |s|
s.name = 'XCFit'
s.version = '5.0.0'
s.version = '5.0.2'
s.summary = 'Full Stack BDD for iOS and macOS Apps with Swift, Xcode using XCUITest, Cucumberish, FitNesse and friends.'

s.description = <<-DESC
Expand All @@ -15,7 +15,7 @@ XCFit is a full stack BDD framework for iOS apps with Swift supports Protocol Or

s.social_media_url = 'https://twitter.com/Shashikant86'

s.ios.deployment_target = '9.0'
s.ios.deployment_target = '10.0'
s.source_files = 'Sources/*.swift'
s.framework = "XCTest"
s.xcconfig = { "ALWAYS_EMBED_SWIFT_STANDARD_LIBRARIES" => "NO" }
Expand Down

0 comments on commit be01ea2

Please sign in to comment.