Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds the ability to run bash script through appium-for-mac #30

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions AppiumForMac/Server/Controller/AfMSessionController.h
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ extern NSString * const kCookieDiagnosticsDirectory;
- (BOOL)isElementDisplayed:(id)element;
-(BOOL) clickElement:(id)element;
-(void) closeWindow;
- (NSString *)executeShellScript:(NSString *)script;
-(NSDictionary*) pageSource;
-(NSInteger) pidForProcessName:(NSString*)processName;
-(SystemEventsProcess*) processForName:(NSString*)processName;
Expand Down
26 changes: 26 additions & 0 deletions AppiumForMac/Server/Controller/AfMSessionController.m
Original file line number Diff line number Diff line change
Expand Up @@ -574,6 +574,32 @@ -(void) closeWindow
[self.currentWindow performAction:@"AXCancel"];
}

- (NSString *)executeShellScript:(NSString *)script
{
NSTask *task = [[NSTask alloc] init];
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I assume NSTask starts with an empty environment by default. Shouldn't we inherit the current system env by default?

[task setLaunchPath:@"/bin/bash"];

NSArray *arguments = [NSArray arrayWithObjects:
@"-c" ,
[NSString stringWithFormat:@"%@", script],
nil];

NSLog(@"running bash script:'%@'", script);
[task setArguments:arguments];

NSPipe *pipe = [NSPipe pipe];
[task setStandardOutput:pipe];

NSFileHandle *file = [pipe fileHandleForReading];

[task launch];
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The launch method is deprecated. Please use only supported APIs.
https://developer.apple.com/documentation/foundation/nstask/1414189-launch?language=objc


NSData *data = [file readDataToEndOfFile];

NSString *output = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
return output;
}

-(NSString*) currentApplicationName
{
return self._currentApplicationName;
Expand Down
3 changes: 3 additions & 0 deletions AppiumForMac/Server/Handlers/AfMHandlers.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,10 @@
// POST /session/:sessionId/forward
// POST /session/:sessionId/back
// POST /session/:sessionId/refresh

// POST /session/:sessionId/execute
- (AppiumMacHTTPJSONResponse *)post_execute:(NSString*)path data:(NSData*)postData;

// POST /session/:sessionId/execute_async

// GET /session/:sessionId/screenshot
Expand Down
11 changes: 11 additions & 0 deletions AppiumForMac/Server/Handlers/AfMHandlers.m
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,17 @@ - (AppiumMacHTTPJSONResponse *)post_url:(NSString*)path data:(NSData*)postData
// POST /session/:sessionId/execute
// Inject a snippet of JavaScript into the page for execution in the context of the currently selected frame. The executed script is assumed to be synchronous and the result of evaluating the script is returned to the client.

- (AppiumMacHTTPJSONResponse *)post_execute:(NSString*)path data:(NSData*)postData
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it should be also possible to pass additional environment variables

{
return [self executeWebDriverCommandWithPath:path data:postData onMainThread:YES commandBlock:^(AfMSessionController *session, NSDictionary *commandParams, int *statusCode)
{
// The bash script to run
NSString *script = (NSString*)[commandParams objectForKey:@"script"];
NSString *scriptResult = [session executeShellScript: script];
return [AppiumMacHTTPJSONResponse responseWithJson:scriptResult status:kAfMStatusCodeSuccess session:session.sessionId];
}];
}

// POST /session/:sessionId/execute_async
// Inject a snippet of JavaScript into the page for execution in the context of the currently selected frame. The executed script is assumed to be asynchronous and must signal that is done by invoking the provided callback, which is always provided as the final argument to the function.

Expand Down