我在Xcode中編寫腳本來運行一些UI測試,我想使用一些全局變量。
我第一次嘗試是聲明的變量在@interface
與strong
類型,像這樣:爲什麼@interface中聲明的變量中的值不會在XCTest中的方法之間持續存在?
@interface Extended_Tests : XCTestCase
@property (strong) NSMutableArray *list;
@property (strong) NSString *name;
@end
,但沒有奏效。
我最終使用老式C方法來聲明方法外的變量。
我的問題是,爲什麼不工作?爲什麼變量中的值不會在所有方法中持續存在?
編輯: 我的方法:
- (void)testMultiUser1 {
[[[XCUIApplication alloc] init] launch];
XCUIApplication *app = [[XCUIApplication alloc] init];
[app.buttons[@"Sign-in button"] tap];
sleep(5);
user1 = [app.staticTexts elementBoundByIndex:0].label;
[app.otherElements[@"LibraryView"] tap];
sleep(5);
_list = [[NSMutableArray alloc] init];
for(int i = 0; i < 3; i++){
XCUIElementQuery *file1 = [[app.cells elementBoundByIndex:i] descendantsMatchingType:XCUIElementTypeStaticText];
NSString *number = [file1 elementBoundByIndex:0].label;
[_list addObject:number];
}
XCTAssert(_list);
}
我預計這將變量保存到陣列_list這樣我就可以在這樣的另一種方法使用它:
-(void)testMultiUser3{
//Go into Library and make sure top 3 files are different from user1
XCUIApplication *app = [[XCUIApplication alloc] init];
[app.otherElements[@"LibraryView"] tap];
sleep(5);
NSMutableArray *user2files = [[NSMutableArray alloc] init];
for(int i = 0; i < 3; i++){
XCUIElementQuery *list1 = [[app.cells elementBoundByIndex:i] descendantsMatchingType:XCUIElementTypeStaticText];
NSString *number = [list1 elementBoundByIndex:0].label;
[user2files addObject:number];
}
XCTAssert(!([user2files[0] isEqualToString:_list[0]] && [user2files[1] isEqualToString:_list[1]] && [user2files[2] isEqualToString:_list[2]]));
}
您發佈的一小段代碼沒有聲明任何變量。它聲明瞭兩個屬性。也許你應該有更多相關的代碼顯示你真正做了,並解釋你有什麼確切的問題,更新你的問題。 – rmaddy