可以用Cocos2d进行UIAutomation测试吗?

时间:2012-02-06 02:01:59

标签: cocos2d-iphone bdd ios-ui-automation zucchini

是否可以将UIAutomation与cocos2d或任何opengl应用程序一起使用?

具体来说,我想使用zucchini framework来测试我的cocos2d游戏,但无论如何都只使用了UIAutomation。

2 个答案:

答案 0 :(得分:1)

您可以在Zucchini中创建自定义步骤并指定要点按的坐标,例如

'Choose the red bird' : ->
   target.tap({x:278, y:36})

'Press Fire' : ->
   target.tap({x:170, y:260}) 

答案 1 :(得分:0)

所以,我开始使用calabash-iOS并扩展其后门。这仅适用于初学者,但通过此功能,您可以获得当前CCScene的可访问性标签,这样您就可以检查当前正在使用的屏幕,从而用于脚本操作。我不习惯使用objc运行时,但是你可以看到它可以获得属性,方法等。更多的挖掘,它应该可以包含更多的功能,并希望能够包装cocos2d CCNode结构。这是一项正在进行的工作。

要使用此功能,您需要安装https://github.com/calabash/calabash-ios,然后在app delegate中实现以下功能。不要忘记在代码中将.accessibilityLabel设置为@“menu”,@“game”或类似内容。最理想的是,只有* -cal目标,您不希望在生成版本中使用此代码。

-(NSString*)calabashBackdoor:(NSString*)aIgnorable {
    DLog(@"calabashBackdoor: %@", aIgnorable);

//  UIApplication* app = [UIApplication sharedApplication];
    if (aIgnorable != nil) {
        NSArray* p = [aIgnorable componentsSeparatedByString:@" "];
        NSString* command = [p objectAtIndex:0];

        if ([command isEqualToString:@"getCurrentSceneLabel"]) {
            CCDirector* director = [CCDirector sharedDirector];
            DLog(@"director.runningScene.accessibilityLabel: %@", director.runningScene.accessibilityLabel);
            return director.runningScene.accessibilityLabel;
        }
        else if ([command isEqualToString:@"class_copyMethodList"]) {
            CCDirector* director = [CCDirector sharedDirector];
            id inspectThisObject = director.runningScene;
            DLog(@"inspectThisObject: %@, %@", [inspectThisObject class], inspectThisObject);
            unsigned int count;

            // To get the class methods of a class, use class_copyMethodList(object_getClass(cls), &count).
            Method* methods = class_copyMethodList(object_getClass(inspectThisObject), &count);
            //NSMutableString* returnstring = [NSMutableString string];
            NSMutableArray* arrayOfMethodnames = [NSMutableArray array];
            if (methods != NULL) {
                for (int i = 0; i < count; i++) {
                    Method method = methods[i];
                    NSString* stringMethod = NSStringFromSelector(method_getName(method)); //NSStringFromSelector(method->method_name);
                    [arrayOfMethodnames addObject:stringMethod];
                }
                // An array of pointers of type Method describing the instance methods implemented by the class—any instance methods implemented by superclasses are not included. The array contains *outCount pointers followed by a NULL terminator. You must free the array with free().
                free(methods);
            }
            DLog(@"arrayOfMethodnames: %@", arrayOfMethodnames);
            return [arrayOfMethodnames componentsJoinedByString:@","];
        }
        else if ([command isEqualToString:@"class_copyPropertyList"]) {
            CCDirector* director = [CCDirector sharedDirector];
            id inspectThisObject = director.runningScene;
            DLog(@"inspectThisObject: %@, %@", [inspectThisObject class], inspectThisObject);
            unsigned int count;

//          An array of pointers of type objc_property_t describing the properties declared by the class. Any properties declared by superclasses are not included. The array contains *outCount pointers followed by a NULL terminator. You must free the array with free().
//
//          If cls declares no properties, or cls is Nil, returns NULL and *outCount is 0.
//          
            objc_property_t* properties = class_copyPropertyList(object_getClass(inspectThisObject), &count);

            NSMutableArray* arrayOfProperties = [NSMutableArray array];
            if (properties != NULL) {
                for (int i = 0; i < count; i++) {
                    objc_property_t property = properties[i];
                    const char* CCS = property_getName(property);
                    NSString* str = [NSString stringWithUTF8String:CCS];
                    [arrayOfProperties addObject:str];
                }
                free(properties);
            }
            DLog(@"arrayOfProperties: %@", arrayOfProperties);
            return [arrayOfProperties componentsJoinedByString:@","];           
        }
        else {
            DLog(@"Unhandled command: %@", command);
        }
    }

    return @"calabashBackdoor nil!";
}

在Prefix.pch中添加此

#ifdef DEBUG
#   define DLog( s, ... ) NSLog( @"<%p %@:(%d)> %@", self, [[NSString stringWithUTF8String:__FILE__] lastPathComponent], __LINE__, [NSString stringWithFormat:(s), ##__VA_ARGS__] )
#else
#   define DLog(...) /* */
#endif
#define ALog(...) NSLog(__VA_ARGS__)

当你获得calabash-ios并运行时,请在step_definitions / somesteps.rb中添加:

Then(/^I backdoor (.+)$/) do |x|
  backdoor("calabashBackdoor:", x)
end