Skip to content

Instantly share code, notes, and snippets.

@onmyway133
Created August 10, 2018 20:35
Show Gist options
  • Save onmyway133/edfeeb6344daf13e1ff080239d62bf35 to your computer and use it in GitHub Desktop.
Save onmyway133/edfeeb6344daf13e1ff080239d62bf35 to your computer and use it in GitHub Desktop.
import XCTest
class LoginTests: XCTestCase {
var app: XCUIApplication!
func testLogin() {
continueAfterFailure = false
app = XCUIApplication()
app.launch()
passLogin()
}
}
extension LoginTests {
func passLogin() {
// Tap login
app.showFacebookLoginFormButton.tap()
wait(for: app.staticTexts["Facebook"], timeout: 5) // This requires a high timeout
// There may be location permission popup when showing map
handleLocationPermission()
if app.isAlreadyLoggedInSafari {
app.okButton.tap()
// Show map
let map = app.maps.element(boundBy: 0)
wait(for: map, timeout: 2)
XCTAssertTrue(map.exists)
// Need to interact with the app for interruption monitor to work
app.tap()
} else {
// Choose norsk
wait(for: app.norwegianText, timeout: 1)
app.norwegianText.tap()
app.emailTextField.tap()
app.emailTextField.deleteAllText()
app.emailTextField.typeText("[email protected]")
app.passwordCoordinate.tap()
app.typeText("Bob Alageaiecghfb Sharpeman")
// login
app.facebookLoginButton.tap()
// press OK
app.okButton.tap()
// Show map
let map = app.maps.element(boundBy: 0)
wait(for: map, timeout: 2)
XCTAssertTrue(map.exists)
// Need to interact with the app for interruption monitor to work
app.tap()
}
}
fileprivate func handleLocationPermission() {
addUIInterruptionMonitor(withDescription: "Location permission", handler: { alert in
alert.buttons.element(boundBy: 1).tap()
return true
})
}
}
fileprivate extension XCUIApplication {
var showFacebookLoginFormButton: XCUIElement {
return buttons["Continue with Facebook"]
}
var isAlreadyLoggedInSafari: Bool {
return buttons["OK"].exists || staticTexts["Du har allerede godkjent Blue Sea."].exists
}
var okButton: XCUIElement {
return buttons["OK"]
}
var norwegianText: XCUIElement {
return staticTexts["Norsk (bokmål)"]
}
var emailTextField: XCUIElement {
let predicate = NSPredicate(format: "placeholderValue == %@", "E-post eller mobil")
return textFields.element(matching: predicate)
}
var passwordCoordinate: XCUICoordinate {
let vector = CGVector(dx: 1, dy: 1.5)
return emailTextField.coordinate(withNormalizedOffset: vector)
}
var facebookLoginButton: XCUIElement {
return buttons["Logg inn"]
}
}
extension XCTestCase {
func wait(for duration: TimeInterval) {
let waitExpectation = expectation(description: "Waiting")
let when = DispatchTime.now() + duration
DispatchQueue.main.asyncAfter(deadline: when) {
waitExpectation.fulfill()
}
// We use a buffer here to avoid flakiness with Timer on CI
waitForExpectations(timeout: duration + 0.5)
}
/// Wait for element to appear
func wait(for element: XCUIElement, timeout duration: TimeInterval) {
let predicate = NSPredicate(format: "exists == true")
let _ = expectation(for: predicate, evaluatedWith: element, handler: nil)
// We use a buffer here to avoid flakiness with Timer on CI
waitForExpectations(timeout: duration + 0.5)
}
}
extension XCUIApplication {
// Because of "Use cached accessibility hierarchy"
func clearCachedStaticTexts() {
let _ = staticTexts.count
}
func clearCachedTextFields() {
let _ = textFields.count
}
func clearCachedTextViews() {
let _ = textViews.count
}
}
extension XCUIElement {
func deleteAllText() {
guard let string = value as? String else {
return
}
let lowerRightCorner = coordinate(withNormalizedOffset: CGVector(dx: 0.9, dy: 0.9))
lowerRightCorner.tap()
let deletes = string.characters.map({ _ in XCUIKeyboardKeyDelete }).joined(separator: "")
typeText(deletes)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment