【发布时间】:2020-09-28 02:55:42
【问题描述】:
我需要在几部 iPhone 和几部 iPad 上进行测试。我现在通过以下菜单手动切换目标设备。
此任务会消耗大量时间。所以我想自动切换目标设备。我现在正在使用 Xcode 11.5。你能告诉我怎么做吗?
import SwiftUI
struct ContentView: View {
@State var flag = false
var body: some View {
VStack {
Text(flag ? "Apple" : "Peach")
.accessibility(identifier: "Text")
Button(action: {
self.flag.toggle()
}) {
Text("Update")
}
.accessibility(identifier: "Button")
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
import XCTest
class swiftui_playgroundUITests: XCTestCase {
override func setUpWithError() throws {
// Put setup code here. This method is called before the invocation of each test method in the class.
// In UI tests it is usually best to stop immediately when a failure occurs.
continueAfterFailure = false
// In UI tests it’s important to set the initial state - such as interface orientation - required for your tests before they run. The setUp method is a good place to do this.
}
override func tearDownWithError() throws {
// Put teardown code here. This method is called after the invocation of each test method in the class.
}
func addScreenshot(screenshot: XCUIScreenshot) {
let attachment = XCTAttachment(screenshot: screenshot)
attachment.lifetime = .keepAlways
add(attachment)
}
func testExample() throws {
// UI tests must launch the application that they test.
let app = XCUIApplication()
app.launch()
// Use recording to get started writing UI tests.
// Use XCTAssert and related functions to verify your tests produce the correct results.
let text = app.staticTexts["Text"]
XCTAssertEqual(text.label, "Peach")
addScreenshot(screenshot: app.windows.firstMatch.screenshot())
let button = app.buttons["Button"]
button.tap()
XCTAssertEqual(text.label, "Apple")
addScreenshot(screenshot: app.windows.firstMatch.screenshot())
}
}
【问题讨论】: