From a1aa636d8856c97b8edb1c091b3919a45a6d6716 Mon Sep 17 00:00:00 2001 From: Kazuaki Matsuo Date: Sat, 20 Jul 2024 17:42:15 -0700 Subject: [PATCH] fix: do nothing for an empty array in w3c actions (#308) * fix: do nothing for an empty array in w3c actions * apply the same change in key actions * add test case * revert removed code for safe * tweak spaces --- .../IntegrationTests/AMW3CActionsTests.m | 21 ++++++++++++++++ .../Utilities/FBW3CActionsSynthesizer.m | 25 ++++++++++++++----- 2 files changed, 40 insertions(+), 6 deletions(-) diff --git a/WebDriverAgentMac/IntegrationTests/AMW3CActionsTests.m b/WebDriverAgentMac/IntegrationTests/AMW3CActionsTests.m index 20ddf98..3ee358d 100644 --- a/WebDriverAgentMac/IntegrationTests/AMW3CActionsTests.m +++ b/WebDriverAgentMac/IntegrationTests/AMW3CActionsTests.m @@ -370,4 +370,25 @@ - (void)testKeys XCTAssertEqualObjects(edit.value, @"NBA"); } +- (void)testKeysWithEmptyActions +{ + [self switchToEditsTab]; + XCUIElement *edit = self.testedApplication.textFields.firstMatch; + [edit click]; + + NSArray *> *gesture = + @[@{ + @"type": @"key", + @"id": @"keyboard", + @"actions": @[], + }, + ]; + NSError *error; + XCTAssertTrue([self.testedApplication fb_performW3CActions:gesture + elementCache:nil + error:&error]); + XCTAssertNil(error); + XCTAssertEqualObjects(edit.value, @""); +} + @end diff --git a/WebDriverAgentMac/WebDriverAgentLib/Utilities/FBW3CActionsSynthesizer.m b/WebDriverAgentMac/WebDriverAgentLib/Utilities/FBW3CActionsSynthesizer.m index 1cb2c56..acdc4cc 100644 --- a/WebDriverAgentMac/WebDriverAgentLib/Utilities/FBW3CActionsSynthesizer.m +++ b/WebDriverAgentMac/WebDriverAgentLib/Utilities/FBW3CActionsSynthesizer.m @@ -260,7 +260,7 @@ - (nullable NSValue *)positionWithError:(NSError **)error } return nil; } - + XCUIElement *element = isOriginAnElement ? (XCUIElement *)origin : nil; NSNumber *x = [self.actionItem objectForKey:FB_ACTION_ITEM_KEY_X]; NSNumber *y = [self.actionItem objectForKey:FB_ACTION_ITEM_KEY_Y]; @@ -272,7 +272,7 @@ - (nullable NSValue *)positionWithError:(NSError **)error } return nil; } - + if (nil != element) { if (nil == x && nil == y) { return [self hitpointWithElement:element @@ -283,13 +283,13 @@ - (nullable NSValue *)positionWithError:(NSError **)error positionOffset:[NSValue am_valueWithCGPoint:CGPointMake(x.floatValue, y.floatValue)] error:error]; } - + if ([origin isKindOfClass:NSString.class] && [origin isEqualToString:FB_ORIGIN_TYPE_VIEWPORT]) { return [self hitpointWithElement:nil positionOffset:[NSValue am_valueWithCGPoint:CGPointMake(x.floatValue, y.floatValue)] error:error]; } - + // origin == FB_ORIGIN_TYPE_POINTER if (nil == self.previousItem) { NSString *errorDescription = [NSString stringWithFormat:@"There is no previous item for '%@' action item, however %@ is set to '%@'", self.actionItem, FB_ACTION_ITEM_KEY_ORIGIN, FB_ORIGIN_TYPE_POINTER]; @@ -734,7 +734,7 @@ @implementation FBW3CActionsSynthesizer shouldCancelNextItem = YES; continue; } - + if (nil == self.elementCache) { [result addObject:actionItem]; continue; @@ -874,7 +874,7 @@ @implementation FBW3CActionsSynthesizer NSArray *> *actionItems = [actionDescription objectForKey:FB_KEY_ACTIONS]; if (nil == actionItems || 0 == actionItems.count) { - NSString *description = [NSString stringWithFormat:@"It is mandatory to have at least one gesture item defined for each action. Action with id '%@' contains none", actionId]; + NSString *description = [NSString stringWithFormat:@"It is mandatory to have at least one gesture item defined for each action. Action with id '%@' contains none", actionId]; if (error) { *error = [[FBErrorBuilder.builder withDescription:description] build]; } @@ -959,6 +959,19 @@ - (nullable XCSynthesizedEventRecord *)synthesizeWithError:(NSError **)error } return nil; } + NSArray *> *actionItems = [action objectForKey:FB_KEY_ACTIONS]; + if (nil == actionItems) { + NSString *description = [NSString stringWithFormat:@"It is mandatory to have at least one item defined for each action. Action with id '%@' contains none", actionId]; + if (error) { + *error = [[FBErrorBuilder.builder withDescription:description] build]; + } + return nil; + } + if (0 == actionItems.count) { + [FBLogger logFmt:@"Action items in the action id '%@' had an empty array. Skipping the action.", actionId]; + continue; + } + [actionIds addObject:actionId]; [actionsMapping setObject:action forKey:actionId]; }