【发布时间】:2015-12-17 19:37:49
【问题描述】:
我正在 Xcode 中编写脚本来运行一些 UI 测试,我想使用一些全局变量。
我的第一次尝试是在@interface 中声明一个类型为strong 的变量,如下所示:
@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]]));
}
【问题讨论】:
-
您发布的一小段代码没有声明任何变量。它声明了两个属性。也许您应该使用更相关的代码来更新您的问题,以显示您的实际操作并解释您遇到的确切问题。
标签: objective-c xctest