Skip to content

Instantly share code, notes, and snippets.

@TeresaP
Created April 4, 2014 16:49
Show Gist options
  • Save TeresaP/9978566 to your computer and use it in GitHub Desktop.
Save TeresaP/9978566 to your computer and use it in GitHub Desktop.
Example for storing Calabash Backdoor functions outside of the Application Delegate
@interface CalabashBackdoor : NSObject
+(NSString *) backdoorMethodName:(NSString *)string;
@end
@implementation CalabashBackdoor
// One of the Backdoor methods
+ (NSString *) backdoorMethodName:(NSString *)string
{
return @"Result";
}
@end
backdoor('calabashBackdoor','backdoorMethodName')
backdoor('calabashBackdoor','backdoorMethodName:')
backdoor('calabashBackdoor','backdoorMethodName:parameterValue')
// Calabash Automation Methods Proxy
-(id *)calabashBackdoor:(NSString *)string;
// Calls the corresponding method inside the CalabashBackdoor
// Syntax from Ruby Code:
// backdoor('calabashBackdoor','functionName:parameter')
// backdoor('calabashBackdoor','functionName')
// backdoor('calabashBackdoor','functionName:')
// This will call the functionName function and pass in the desired parameter, if it exists
- (id *)calabashBackdoor:(NSString *)string
{
// Break string into a function name and its parameter (if it exists)
NSArray *userInput = [string componentsSeparatedByString: @":"];
// Method above removes : but the method name must have it, so put it back on the end
NSString *methodToCall = [NSString stringWithFormat:@"%@:", userInput[0]];
if (userInput.count > 1 && [[CalabashBackdoor class] respondsToSelector:(NSSelectorFromString(methodToCall))])
{
return (id *)[[CalabashBackdoor class] performSelector: NSSelectorFromString(methodToCall) withObject:userInput[1]];
}
else if (userInput.count == 1 && [[CalabashBackdoor class] respondsToSelector:(NSSelectorFromString(methodToCall))])
{
return (id *)[[CalabashBackdoor class] performSelector: NSSelectorFromString(methodToCall)];
}
else
{
NSString *detailsStr = [NSString stringWithFormat:@"You must define the method (selector) '%@' in CalabashBackdoor.\n",
methodToCall];
NSString *usage0 = [NSString stringWithFormat:@"backdoor('calabashBackdoor','%@')",
methodToCall];
NSString *usage1 = [NSString stringWithFormat:@"backdoor('calabashBackdoor','%@<arg>')",
methodToCall];
NSString *reasonStr = [NSString stringWithFormat:@"The application delegate does not respond to selector '%@'.",
methodToCall];
return (id*) [NSDictionary dictionaryWithObjectsAndKeys:
@"FAILURE", @"Outcome",
reasonStr, @"Reason for Error",
detailsStr, @"Details",
usage0, @"Usage for method without a parameter",
usage1, @"Usage for method with a parameter",
nil];
}
}
@lucatorella
Copy link

isn't better to have the calabash backdoor defined in a category of the app delegate, and add this category only to the calabash target?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment