Skip to content

Commit

Permalink
fix IP地址不准确&增加弹窗自动处理&mjpeg server恢复标准格式并兼容stf
Browse files Browse the repository at this point in the history
  • Loading branch information
zhouyunpeng2 committed Dec 19, 2023
1 parent fafbabd commit 1c91804
Show file tree
Hide file tree
Showing 10 changed files with 141 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@
</BuildAction>
<TestAction
buildConfiguration = "Debug"
selectedDebuggerIdentifier = ""
selectedLauncherIdentifier = "Xcode.IDEFoundation.Launcher.PosixSpawn"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
shouldUseLaunchSchemeArgsEnv = "YES"
systemAttachmentLifetime = "keepNever">
<Testables>
Expand Down
6 changes: 5 additions & 1 deletion WebDriverAgentLib/Categories/XCUIDevice+FBHelpers.m
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,11 @@ - (NSString *)fb_wifiIPAddress
continue;
}
address = [NSString stringWithUTF8String:inet_ntoa(((struct sockaddr_in *)temp_addr->ifa_addr)->sin_addr)];
break;
temp_addr = temp_addr->ifa_next;
NSLog(@"%@:%@",interfaceName,address);
if([interfaceName containsString:@"en0"]){
break;
}
}
freeifaddrs(interfaces);
return address;
Expand Down
3 changes: 2 additions & 1 deletion WebDriverAgentLib/Commands/FBSessionCommands.m
Original file line number Diff line number Diff line change
Expand Up @@ -386,7 +386,8 @@ + (NSArray *)routes
[FBConfiguration setAnimationCoolOffTimeout:[[settings objectForKey:FB_SETTING_ANIMATION_COOL_OFF_TIMEOUT] doubleValue]];
}
if ([[settings objectForKey:FB_SETTING_DEFAULT_ALERT_ACTION] isKindOfClass:NSString.class]) {
request.session.defaultAlertAction = [settings[FB_SETTING_DEFAULT_ALERT_ACTION] lowercaseString];
//request.session.defaultAlertAction = [settings[FB_SETTING_DEFAULT_ALERT_ACTION] lowercaseString];
[request.session setAlertAction: settings[FB_SETTING_DEFAULT_ALERT_ACTION]];
}

#if !TARGET_OS_TV
Expand Down
2 changes: 2 additions & 0 deletions WebDriverAgentLib/FBAlert.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ NS_ASSUME_NONNULL_BEGIN
*/
- (BOOL)dismissWithError:(NSError **)error;

- (BOOL)actionWithAction:(nullable NSString *)action error:(NSError **)error;

/**
Clicks on an alert button, if present
Expand Down
56 changes: 56 additions & 0 deletions WebDriverAgentLib/FBAlert.m
Original file line number Diff line number Diff line change
Expand Up @@ -275,4 +275,60 @@ - (XCUIElement *)alertElement
return self.element;
}

- (BOOL)actionWithAction:(NSString *)action error:(NSError **)error {
NSArray<NSDictionary *> *actions = (id)action;
for (NSDictionary *act in actions) {
BOOL match = NO;
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", act[@"regex"]];
for (NSString *s in [self.text componentsSeparatedByString:@"\n"]) {
match = [predicate evaluateWithObject:s];
if (match) {
break;
}
}
if (match) {
id<FBXCElementSnapshot> alertSnapshot = self.alertElement.lastSnapshot;
XCUIElement *dismissButton = nil;
if (nil == dismissButton) {
NSArray<XCUIElement *> *buttons = [self.alertElement.fb_query
descendantsMatchingType:XCUIElementTypeButton].allElementsBoundByIndex;
NSString *errorReason = nil;
@try {
if (act[@"btns"]) {
NSArray<NSString *> *btns = act[@"btns"];
for (XCUIElement *btn in buttons) {
for (NSString *title in btns) {
NSPredicate *btnPredicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", title];
if ([btnPredicate evaluateWithObject:btn.wdLabel]) {
dismissButton = btn;
break;
}
}
if (dismissButton) {
break;
}
}
} else {
dismissButton = (self.alertElement.elementType == XCUIElementTypeAlert || [self.class isSafariWebAlertWithSnapshot:alertSnapshot])
? [buttons objectAtIndex:[act[@"index"] integerValue]]
: [buttons objectAtIndex:(buttons.count - [act[@"index"] integerValue] - 1)];
}
} @catch (NSException *ex) {
errorReason = ex.reason;
}
if (nil == dismissButton) {
[FBLogger logFmt:@"Original error: %@", errorReason];
}
}
if (nil == dismissButton){
return [[[FBErrorBuilder builder]
withDescriptionFormat:@"Failed to find dismiss button for alert: %@", self.alertElement]
buildError:error];
}
[dismissButton tap];
}
}
return YES;
}

@end
2 changes: 2 additions & 0 deletions WebDriverAgentLib/Routing/FBSession.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ NS_ASSUME_NONNULL_BEGIN
*/
+ (instancetype)initWithApplication:(nullable FBApplication *)application defaultAlertAction:(NSString *)defaultAlertAction;

- (void)setAlertAction:(NSString *)alertAction;

/**
Kills application associated with that session and removes session
*/
Expand Down
47 changes: 44 additions & 3 deletions WebDriverAgentLib/Routing/FBSession.m
Original file line number Diff line number Diff line change
Expand Up @@ -48,20 +48,37 @@ @implementation FBSession (FBAlertsMonitorDelegate)

- (void)didDetectAlert:(FBAlert *)alert
{
if (nil == self.defaultAlertAction || 0 == self.defaultAlertAction.length) {
if (nil == self.defaultAlertAction) {
return;
}

NSError *error;
if ([self.defaultAlertAction isEqualToString:@"accept"]) {
if ([self.defaultAlertAction isKindOfClass:[NSArray class]]){
if (![alert actionWithAction:self.defaultAlertAction error:&error]) {
[FBLogger logFmt:@"Cannot action the alert. Original error: %@", error.description];
}
}
else if ([self.defaultAlertAction isEqualToString:@"accept"]) {
if (![alert acceptWithError:&error]) {
[FBLogger logFmt:@"Cannot accept the alert. Original error: %@", error.description];
}
} else if ([self.defaultAlertAction isEqualToString:@"dismiss"]) {
if (![alert dismissWithError:&error]) {
[FBLogger logFmt:@"Cannot dismiss the alert. Original error: %@", error.description];
}
} else {
} else if ([self.defaultAlertAction hasPrefix:@"["] && [self.defaultAlertAction hasSuffix:@"]"]) {
NSError *jsonError;
NSData *objectData = [self.defaultAlertAction dataUsingEncoding:NSUTF8StringEncoding];
NSArray<NSDictionary *> *actions = [NSJSONSerialization JSONObjectWithData:objectData options:NSJSONReadingMutableContainers error:&jsonError];
if (jsonError) {
return ;
}
self.defaultAlertAction = (NSString*)actions;
if (![alert actionWithAction:self.defaultAlertAction error:&error]) {
[FBLogger logFmt:@"Cannot action the alert. Original error: %@", error.description];
}
}
else {
[FBLogger logFmt:@"'%@' default alert action is unsupported", self.defaultAlertAction];
}
}
Expand Down Expand Up @@ -127,6 +144,30 @@ + (instancetype)initWithApplication:(nullable FBApplication *)application
return session;
}

- (void)setAlertAction:(NSString *)alertAction
{
if([alertAction isKindOfClass:[NSArray class]]){
self.defaultAlertAction = alertAction;
}
else if ([alertAction hasPrefix:@"["] && [alertAction hasSuffix:@"]"]) {
NSError *jsonError;
NSData *objectData = [alertAction dataUsingEncoding:NSUTF8StringEncoding];
NSArray<NSDictionary *> *actions = [NSJSONSerialization JSONObjectWithData:objectData options:NSJSONReadingMutableContainers error:&jsonError];
if (jsonError) {
return ;
}
self.defaultAlertAction = (NSString*)actions;
}
else{
self.defaultAlertAction = [alertAction lowercaseString];
}
if(!self.alertsMonitor){
self.alertsMonitor = [[FBAlertsMonitor alloc] init];
self.alertsMonitor.delegate = (id<FBAlertsMonitorDelegate>)self;
}
[self.alertsMonitor enable];
}

- (void)kill
{
if (nil == _activeSession) {
Expand Down
2 changes: 1 addition & 1 deletion WebDriverAgentLib/Routing/FBTCPSocket.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ NS_ASSUME_NONNULL_BEGIN
@param client The client, which sent the data
*/
- (void)didClientSendData:(GCDAsyncSocket *)client;
- (void)didClientSendData:(GCDAsyncSocket *)client data:(NSData *)data;;

/**
The callback which is fired when TCP client disconnects
Expand Down
2 changes: 1 addition & 1 deletion WebDriverAgentLib/Routing/FBTCPSocket.m
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ - (void)socket:(GCDAsyncSocket *)sock didAcceptNewSocket:(GCDAsyncSocket *)newSo

- (void)socket:(GCDAsyncSocket *)sock didReadData:(NSData *)data withTag:(long)tag
{
[self.delegate didClientSendData:sock];
[self.delegate didClientSendData:sock data:data];
}

- (void)socketDidDisconnect:(GCDAsyncSocket *)sock withError:(NSError *)err
Expand Down
37 changes: 26 additions & 11 deletions WebDriverAgentLib/Utilities/FBMjpegServer.m
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ @interface FBMjpegServer()
@property (nonatomic, readonly) FBImageProcessor *imageProcessor;
@property (nonatomic, readonly) long long mainScreenID;

@property (nonatomic) BOOL sendHeader;
@end


Expand All @@ -53,6 +54,7 @@ - (instancetype)init
});
_imageProcessor = [[FBImageProcessor alloc] init];
_mainScreenID = [XCUIScreen.mainScreen displayID];
_sendHeader = TRUE;
}
return self;
}
Expand Down Expand Up @@ -112,17 +114,21 @@ - (void)streamScreenshot
- (void)sendScreenshot:(NSData *)screenshotData {
//NSString *chunkHeader = [NSString stringWithFormat:@"--BoundaryString\r\nContent-type: image/jpg\r\nContent-Length: %@\r\n\r\n", @(screenshotData.length)];
UIInterfaceOrientation orientation = UIInterfaceOrientationPortrait;
@try {
FBApplication *systemApp = FBApplication.fb_activeApplication;
orientation = systemApp.interfaceOrientation;
}@catch(NSException *e) {
NSLog(@"%@",e);
NSString *chunkHeader = [NSString stringWithFormat:@"--BoundaryString\r\nContent-type: image/jpg\r\nContent-Length: %@\r\n\r\n", @(screenshotData.length)];
if(!self.sendHeader){
@try {
FBApplication *systemApp = FBApplication.fb_activeApplication;
orientation = systemApp.interfaceOrientation;
}@catch(NSException *e) {
NSLog(@"%@",e);
}
chunkHeader = [NSString stringWithFormat:@"--BoundaryString--Content-type: image/jpg--=%ld=",(long)orientation];
}
NSString *chunkHeader = [NSString stringWithFormat:@"--BoundaryString--Content-type: image/jpg--=%ld=",(long)orientation];
//NSString *chunkHeader = [NSString stringWithFormat:@"--BoundaryString\r\nContent-type: image/jpeg\r\nContent-Length: %@\r\n\r\n", @(screenshotData.length)];
NSMutableData *chunk = [[chunkHeader dataUsingEncoding:NSUTF8StringEncoding] mutableCopy];
[chunk appendData:screenshotData];
//[chunk appendData:(id)[@"\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
if(self.sendHeader){
[chunk appendData:(id)[@"\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
}
@synchronized (self.listeningClients) {
for (GCDAsyncSocket *client in self.listeningClients) {
[client writeData:chunk withTimeout:-1 tag:0];
Expand All @@ -137,17 +143,26 @@ - (void)didClientConnect:(GCDAsyncSocket *)newClient
[newClient readDataWithTimeout:-1 tag:0];
}

- (void)didClientSendData:(GCDAsyncSocket *)client
- (void)didClientSendData:(GCDAsyncSocket *)client data:(NSData *)data
{
NSString *str2 = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
if([str2 isEqual: @"start send jpeg"]){
self.sendHeader = FALSE;
}
else{
self.sendHeader = TRUE;
}
@synchronized (self.listeningClients) {
if ([self.listeningClients containsObject:client]) {
return;
}
}

[FBLogger logFmt:@"Starting screenshots broadcast for the client at %@:%d", client.connectedHost, client.connectedPort];
//NSString *streamHeader = [NSString stringWithFormat:@"HTTP/1.0 200 OK\r\nServer: %@\r\nConnection: close\r\nMax-Age: 0\r\nExpires: 0\r\nCache-Control: no-cache, private\r\nPragma: no-cache\r\nContent-Type: multipart/x-mixed-replace; boundary=--BoundaryString\r\n\r\n", SERVER_NAME];
//[client writeData:(id)[streamHeader dataUsingEncoding:NSUTF8StringEncoding] withTimeout:-1 tag:0];
if(self.sendHeader){
NSString *streamHeader = [NSString stringWithFormat:@"HTTP/1.0 200 OK\r\nServer: %@\r\nConnection: close\r\nMax-Age: 0\r\nExpires: 0\r\nCache-Control: no-cache, private\r\nPragma: no-cache\r\nContent-Type: multipart/x-mixed-replace; boundary=--BoundaryString\r\n\r\n", SERVER_NAME];
[client writeData:(id)[streamHeader dataUsingEncoding:NSUTF8StringEncoding] withTimeout:-1 tag:0];
}
@synchronized (self.listeningClients) {
[self.listeningClients addObject:client];
}
Expand Down

0 comments on commit 1c91804

Please sign in to comment.