Skip to content

Commit 2caa376

Browse files
committed
Fix UI tests
1 parent 35dcd2e commit 2caa376

File tree

10 files changed

+90
-48
lines changed

10 files changed

+90
-48
lines changed

ios-base/Home/Views/HomeViewController.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,12 @@ private extension HomeViewController {
9292
subviews: [welcomeLabel, logOutButton, deleteAccountButton, getProfileButton]
9393
)
9494
activateConstraints()
95+
setupAccessibility()
96+
}
97+
98+
func setupAccessibility() {
99+
getProfileButton.accessibilityIdentifier = "GetMyProfileButton"
100+
logOutButton.accessibilityIdentifier = "LogoutButton"
95101
}
96102

97103
private func activateConstraints() {

ios-base/Onboarding/Views/SignInViewController.swift

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,13 @@ private extension SignInViewController {
9696
])
9797

9898
activateConstrains()
99+
setupAccessibility()
100+
}
101+
102+
func setupAccessibility() {
103+
logInButton.accessibilityIdentifier = "SignInButton"
104+
emailField.accessibilityIdentifier = "EmailTextField"
105+
passwordField.accessibilityIdentifier = "PasswordTextField"
99106
}
100107

101108
func activateConstrains() {

ios-base/Onboarding/Views/SignUpViewController.swift

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,14 @@ private extension SignUpViewController {
108108
])
109109

110110
activateConstrains()
111+
setupAccessibility()
112+
}
113+
114+
func setupAccessibility() {
115+
signUpButton.accessibilityIdentifier = "SignUpButton"
116+
emailField.accessibilityIdentifier = "EmailTextField"
117+
passwordField.accessibilityIdentifier = "PasswordTextField"
118+
passwordConfirmationField.accessibilityIdentifier = "ConfirmPasswordTextField"
111119
}
112120

113121
func activateConstrains() {

ios-baseUITests/NetworkMocker+Stubs.swift

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
//
88

99
import Foundation
10+
import RSSwiftNetworking
1011

1112
internal enum NetworkStub {
1213
case signUp(success: Bool)
@@ -42,15 +43,38 @@ internal enum NetworkStub {
4243
return success ? "GetProfileSuccessfully" : "GetProfileFailure"
4344
}
4445
}
46+
47+
var headers: [String: String]? {
48+
switch self {
49+
case .signUp(let success), .signIn(let success):
50+
if success {
51+
return [
52+
HTTPHeader.uid.rawValue: "uid",
53+
HTTPHeader.client.rawValue: "client",
54+
HTTPHeader.token.rawValue: "accessToken",
55+
HTTPHeader.expiry.rawValue: "\(Date.distantFuture.timeIntervalSinceNow)",
56+
HTTPHeader.contentType.rawValue: "application/json"
57+
]
58+
}
59+
return nil
60+
default:
61+
return nil
62+
}
63+
}
4564
}
4665

4766
extension NetworkMocker {
4867

49-
func stub(with networkStub: NetworkStub, method: HTTPMethod) {
68+
func stub(
69+
with networkStub: NetworkStub,
70+
method: HTTPMethod,
71+
customHeaders: [String: String]? = nil
72+
) {
5073
stub(
5174
url: networkStub.urlString,
5275
responseFilename: networkStub.responseFileName,
53-
method: method
76+
method: method,
77+
customHeaders: customHeaders ?? networkStub.headers
5478
)
5579
}
5680
}

ios-baseUITests/NetworkMocker.swift

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -32,23 +32,30 @@ internal class NetworkMocker {
3232
public func stub(
3333
url: String,
3434
responseFilename: String,
35-
method: HTTPMethod = .GET
35+
method: HTTPMethod = .GET,
36+
customHeaders: [String: String]? = nil
3637
) {
3738
let testBundle = Bundle(for: type(of: self))
3839
let filePath = testBundle.path(forResource: responseFilename, ofType: "json") ?? ""
3940
let fileURL = URL(fileURLWithPath: filePath)
4041

4142
do {
42-
let jsonObject = try Data(contentsOf: fileURL, options: .uncached).jsonObject()
43-
guard let jsonObject = jsonObject else {
44-
XCTFail("A valid JSON couldn't be parsed")
45-
return
46-
}
43+
let data = try Data(contentsOf: fileURL, options: .uncached)
44+
let jsonObject = try data.jsonObject()
4745

4846
let response: ((HttpRequest) -> HttpResponse) = { _ in
49-
HttpResponse.ok(.json(jsonObject))
47+
if let customHeaders = customHeaders {
48+
return HttpResponse.raw(200, "OK", customHeaders) { bodyWriter in
49+
try bodyWriter.write(data)
50+
}
51+
}
52+
guard let jsonObject = jsonObject else {
53+
XCTFail("A valid JSON couldn't be parsed")
54+
return .badRequest(.none)
55+
}
56+
return HttpResponse.ok(.json(jsonObject))
5057
}
51-
58+
5259
switch method {
5360
case .GET: server.GET[url] = response
5461
case .POST: server.POST[url] = response
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
{
2-
"user": {}
2+
"id": null
33
}
Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
{
2-
"user": {
3-
"id": 1,
4-
"email": "[email protected]",
5-
"first_name": "FirstName",
6-
"last_name": "LastName",
7-
"username": "test"
8-
}
2+
"id": 1,
3+
"email": "[email protected]",
4+
"first_name": "FirstName",
5+
"last_name": "LastName",
6+
"username": "test"
97
}
Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,9 @@
11
{
2-
"user": {
3-
"id": 1,
4-
"email": "[email protected]",
5-
"provider": "email",
6-
7-
"first_name": "FirstName",
8-
"last_name": "LastName",
9-
"username": "test",
10-
"created_at": "2017-02-23T13:54:33.283Z",
11-
"updated_at": "2017-02-23T13:54:33.425Z"
12-
}
2+
"id": 1,
3+
"email": "[email protected]",
4+
"provider": "email",
5+
6+
"first_name": "FirstName",
7+
"last_name": "LastName",
8+
"username": "test"
139
}
Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,9 @@
11
{
2-
"user": {
3-
"id": 1,
4-
"email": "[email protected]",
5-
"provider": "email",
6-
7-
"first_name": "FirstName",
8-
"last_name": "LastName",
9-
"username": "test",
10-
"created_at": "2017-02-23T13:54:33.283Z",
11-
"updated_at": "2017-02-23T13:54:33.425Z"
12-
}
2+
"id": 1,
3+
"email": "[email protected]",
4+
"provider": "email",
5+
6+
"first_name": "FirstName",
7+
"last_name": "LastName",
8+
"username": "test"
139
}

ios-baseUITests/ios_baseUITests.swift

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,13 @@ class ios_baseUITests: XCTestCase {
1414

1515
let networkMocker = NetworkMocker()
1616

17-
override func setUp() {
18-
super.setUp()
17+
override func setUpWithError() throws {
18+
try super.setUpWithError()
1919
app = XCUIApplication()
2020
app.launchArguments = ["Automation Test"]
2121

22-
try? networkMocker.setUp()
23-
networkMocker.stubLogOut()
22+
try networkMocker.setUp()
23+
networkMocker.stub(with: .logOut, method: .DELETE)
2424
app.logOutIfNeeded(in: self)
2525
}
2626

@@ -68,15 +68,15 @@ class ios_baseUITests: XCTestCase {
6868
func testAccountCreation() {
6969
app.launch()
7070

71-
networkMocker.stubSignUp()
71+
networkMocker.stub(with: .signUp(success: true), method: .POST)
7272

7373
app.attemptSignUp(
7474
in: self,
7575
7676
password: "holahola"
7777
)
7878

79-
networkMocker.stubGetProfile()
79+
networkMocker.stub(with: .profile(success: true), method: .GET)
8080
let getMyProfile = app.buttons["GetMyProfileButton"]
8181
waitFor(element: getMyProfile, timeOut: 10)
8282
getMyProfile.tap()
@@ -92,7 +92,7 @@ class ios_baseUITests: XCTestCase {
9292
func testSignInSuccess() {
9393
app.launch()
9494

95-
networkMocker.stubLogIn()
95+
networkMocker.stub(with: .signIn(success: true), method: .POST)
9696

9797
app.attemptSignIn(in: self,
9898
@@ -105,7 +105,7 @@ class ios_baseUITests: XCTestCase {
105105
func testSignInFailure() {
106106
app.launch()
107107

108-
networkMocker.stubLogIn(shouldSucceed: false)
108+
networkMocker.stub(with: .signIn(success: false), method: .POST)
109109

110110
app.attemptSignIn(in: self,
111111

0 commit comments

Comments
 (0)