簡體   English   中英

在我的 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 function 后,顯示"credentials file is not found"錯誤。

我想用名稱 email 和 JSON 文件中的密碼填寫注冊文本字段。

這是使用XCTFail("Error: \(error)")后顯示錯誤的圖像

在此處輸入圖像描述

您的 JSON 格式不正確。 鍵和值需要有引號,鍵/值對需要用逗號分隔:

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

這是可粘貼到 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)
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM