在我的 XCUITest 中解析 JSON 文件时出现问题

Issue while parsing JSON file in my XCUITest

JSON 文件:“凭据”

[
    
{
        
"name" : Tom Harry
        
"email" : tomharry@abc.com
        
"password" : tomharry123
    
},
    
    {
        "name" : Sam Billings
        "email" : sambillings@abc.com
        "password" : sambillings789
    }
]

实用程序文件:

import Foundation
import XCTest

class Utils {
    
    static func loadData(filename : String) -> [Any] {
        let filePath = Bundle(for: self).path(forResource: filename, ofType: "json") ?? "default"
        let url = URL(fileURLWithPath: filePath)
        do {
            let data = try Data(contentsOf: url)
            let json = try JSONSerialization.jsonObject(with: data, options: .mutableContainers)
            let array =  json as! [Any]
            if array.isEmpty {
                XCTFail("Source file \(filename) is empty.")
            }
            return array
            }
            catch {
                    XCTFail("File \(filename) not found.")
                    return []
                }
    }
}

测试文件:

import XCTest

class UITests: XCTestCase {
    var app : XCUIApplication!

    override func setUpWithError() throws {
        launchApp()
        continueAfterFailure = false
    }

    func launchApp() {
        app = XCUIApplication()
        app.launch()
        print("APPLICATION IS LAUNCHED")
    }
    
    func signUp(fullName : String, email : String, password : String) {
        let fullNameTextField = app.textFields.matching(identifier: "full name").element
        let emailTextField = app.textFields.matching(identifier: "email").element
        let passwordTextField = app.textFields.matching(identifier: "password").element
        if fullNameTextField.exists {
            fullNameTextField.tap()
            fullNameTextField.typeText(fullName)
        }
        if emailTextField.exists {
            emailTextField.tap()
            emailTextField.typeText(email)
        }
        
        if passwordTextField.exists {
            passwordTextField.tap()
            passwordTextField.typeText(password)
        }
        
        
        
    }
  
    func register() {
        let registerButton = app.buttons.matching(identifier: "register").element
        XCTAssert(registerButton.waitForExistence(timeout: 5), "REGISTER BUTTON IS NOT PRESENT")
        if registerButton.exists {
            print("REGISTER BUTTON IS PRESENT")
            registerButton.tap()
            print("REGISTER BUTTON IS TAPPED")
        }
    }
    
    func testSignUp(){
        let dataSource = Utils.loadData(filename: "credentials")
        for iteration in dataSource {
            guard let user = iteration as? [String : Any] else {return}
            let fullname = user["name"] as! String
            let email = user["email"] as! String
            let password = user["password"] as! String
            signUp(fullName: fullname, email: email, password: password)
        }
    
    }
    
    func testflowtest() {
        register()
        testSignUp()
    }
        
}

在 运行 测试文件中的 testFlowTest 函数后,显示 "credentials file is not found" 错误。

我想用 JSON 文件中的姓名、电子邮件和密码填写注册文本字段。

这是使用后显示错误的图片 XCTFail("Error: \(error)")

您的 JSON 格式不正确。键和值需要有引号,并且 key/value 对需要用逗号分隔:

[
    {
        "name" : "Tom Harry",
        "email" : "tomharry@abc.com",        
        "password" : "tomharry123"    
    },
    
    {
        "name" : "Sam Billings",
        "email" : "sambillings@abc.com",
        "password" : "sambillings789"
    }
]

这是 paste-able 到 Playground 中的代码,表明这是可解析的(如果用原始 JSON 替换它,将会失败并出现同样的错误):

let data = """
[
    {
        "name" : "Tom Harry",
        "email" : "tomharry@abc.com",
        "password" : "tomharry123"
    },
    
    {
        "name" : "Sam Billings",
        "email" : "sambillings@abc.com",
        "password" : "sambillings789"
    }
]
""".data(using: .utf8)!

do {
    let json = try JSONSerialization.jsonObject(with: data, options: .mutableContainers)
    print(json)
} catch {
    print(error)
}