Swift 在XCode中编写模块化UI测试
我对XCode上的UI测试还很陌生。我发现record函数非常酷,它工作得很好,但只要你有决策点,代码就打破了枯燥的原则。因此,我的想法是编写模块化测试并根据用户登录状态、角色等调用它们 问题是我没有找到一种方法将我的函数标记为非测试(它们是在适当的时候从主入口点调用的助手)。这就是我想到的Swift 在XCode中编写模块化UI测试,swift,xcode,testing,xcode-ui-testing,Swift,Xcode,Testing,Xcode Ui Testing,我对XCode上的UI测试还很陌生。我发现record函数非常酷,它工作得很好,但只要你有决策点,代码就打破了枯燥的原则。因此,我的想法是编写模块化测试并根据用户登录状态、角色等调用它们 问题是我没有找到一种方法将我的函数标记为非测试(它们是在适当的时候从主入口点调用的助手)。这就是我想到的 func testExample() { let exists = NSPredicate(format: "exists == true") let app = XCUIApplicatio
func testExample() {
let exists = NSPredicate(format: "exists == true")
let app = XCUIApplication()
if(!app.staticTexts[""].exists){
testLaunchScreen()
}
testAsRole1()
}
func testLaunchScreen(){
let app = XCUIApplication()
let scrollViewsQuery = app.scrollViews
scrollViewsQuery.otherElements.containing(.image, identifier:"image0").element.swipeLeft()
app.buttons["Continue"].tap()
testFacebookLoginScreen()
}
func testFacebookLoginScreen() {
let app = XCUIApplication()
// set up an expectation predicate to test whether elements exist
let exists = NSPredicate(format: "exists == true")
// as soon as your sign in button shows up, press it
let facebookSignInButton = app.buttons["Join with Facebook!"]
expectation(for: exists, evaluatedWith: facebookSignInButton, handler: nil)
facebookSignInButton.tap()
self.waitForExpectations(timeout: 10, handler: nil)
if(app.staticTexts["Confirm"].exists){
// create a reference to the button through the webView and press it
let webView = app.descendants(matching: .webView)
webView.buttons["OK"].tap()
} else {
let webViewsQuery = app.webViews
webViewsQuery/*@START_MENU_TOKEN@*/.textFields["Email or Phone"]/*[[".otherElements[\"Welcome to Facebook\"]",".otherElements[\"main\"].textFields[\"Email or Phone\"]",".textFields[\"Email or Phone\"]"],[[[-1,2],[-1,1],[-1,0,1]],[[-1,2],[-1,1]]],[0]]@END_MENU_TOKEN@*/.typeText("user@tfbnw.net")
webViewsQuery/*@START_MENU_TOKEN@*/.secureTextFields["Facebook Password"]/*[[".otherElements[\"Welcome to Facebook\"]",".otherElements[\"main\"].secureTextFields[\"Facebook Password\"]",".secureTextFields[\"Facebook Password\"]"],[[[-1,2],[-1,1],[-1,0,1]],[[-1,2],[-1,1]]],[0]]@END_MENU_TOKEN@*/.typeText("password")
webViewsQuery.buttons["Login"].tap()
}
// wait for your app to return and test for expected behavior here
self.waitForExpectations(timeout: 10, handler: nil)
}
func testAsRole1(){
///
}
问题是所有的testXXX
函数都是按顺序运行的,这不是我想要的,我只想运行testExample。我发现了一些关于扩展的问题,但我认为这些问题不适合我的情况
编辑:
我发现这似乎是一个非常好的库,但我希望能够记录序列,并将它们命名为可以重用的函数,我认为这样更易于维护
非常感谢XCTest将
XCTestCase
子类中以test
开头的每个方法作为独立测试来对待。只需更改方法名称。或者,您可以将它们放在一个单独的类中,该类不继承自XCTestCase
,但这样您就失去了在那里定义的方便方法
从:
测试方法是没有参数、不返回值且名称以小写单词“Test”开头的实例方法
因此,有几种不同的方法可以修改这些方法,使它们不被视为测试 我在考虑
测试前缀约定,但我想:不。。这将被记录在某个地方提到
So+1对你来说,-100对苹果来说,因为它没有让它变得更明显。如果您可以链接文档,我会接受您的回复:)