Skip to content

Commit 407a856

Browse files
refactor: extract attribute block building logic into helper method
Moved the logic for constructing attribute blocks into a separate method for clarity and future extensibility. Also replaced direct type checks with a reusable fb_supportsPlaceholder helper on FBXCElementSnapshotWrapper.
1 parent 4f9415a commit 407a856

File tree

3 files changed

+51
-39
lines changed

3 files changed

+51
-39
lines changed

WebDriverAgentLib/Categories/FBXCElementSnapshotWrapper+Helpers.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,11 @@ NS_ASSUME_NONNULL_BEGIN
9696
*/
9797
- (nullable NSValue *)fb_hitPoint;
9898

99+
/**
100+
@return YES, if the element type is one that supports placeholder text
101+
*/
102+
- (BOOL)fb_supportsPlaceholder;
103+
99104
@end
100105

101106
NS_ASSUME_NONNULL_END

WebDriverAgentLib/Categories/FBXCElementSnapshotWrapper+Helpers.m

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,13 @@ - (NSValue *)fb_hitPoint
162162
return [NSValue valueWithCGPoint:result.hitPoint];
163163
}
164164

165+
- (BOOL)fb_supportsPlaceholder {
166+
XCUIElementType elementType = self.elementType;
167+
return elementType == XCUIElementTypeTextField
168+
|| elementType == XCUIElementTypeSearchField
169+
|| elementType == XCUIElementTypeSecureTextField;
170+
}
171+
165172
@end
166173

167174
inline static BOOL isSnapshotTypeAmongstGivenTypes(id<FBXCElementSnapshot> snapshot, NSArray<NSNumber *> *types)

WebDriverAgentLib/Categories/XCUIApplication+FBHelpers.m

Lines changed: 39 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -202,46 +202,11 @@ + (NSDictionary *)dictionaryForElement:(id<FBXCElementSnapshot>)snapshot
202202
info[@"label"] = FBValueOrNull(wrappedSnapshot.wdLabel);
203203
info[@"rect"] = wrappedSnapshot.wdRect;
204204

205-
// Define the base attribute blocks that apply to all elements.
206-
NSDictionary<NSString *, NSString * (^)(void)> *baseAttributeBlocks = @{
207-
FBExclusionAttributeFrame: ^{
208-
return NSStringFromCGRect(wrappedSnapshot.wdFrame);
209-
},
210-
FBExclusionAttributeEnabled: ^{
211-
return [@([wrappedSnapshot isWDEnabled]) stringValue];
212-
},
213-
FBExclusionAttributeVisible: ^{
214-
return [@([wrappedSnapshot isWDVisible]) stringValue];
215-
},
216-
FBExclusionAttributeAccessible: ^{
217-
return [@([wrappedSnapshot isWDAccessible]) stringValue];
218-
},
219-
FBExclusionAttributeFocused: ^{
220-
return [@([wrappedSnapshot isWDFocused]) stringValue];
221-
}
222-
};
223-
224-
NSMutableDictionary<NSString *, NSString *(^)(void)> *attributeBlocks;
225-
226-
// Add placeholderValue only for elements where it is meaningful (e.g., text input fields).
227-
switch (snapshot.elementType) {
228-
case XCUIElementTypeTextField:
229-
case XCUIElementTypeSecureTextField:
230-
case XCUIElementTypeSearchField: {
231-
// Copy base attributes to a mutable dictionary so we can add placeholderValue.
232-
attributeBlocks = [baseAttributeBlocks mutableCopy];
233-
attributeBlocks[FBExclusionAttributePlaceholderValue] = ^{
234-
return (NSString *)FBValueOrNull(wrappedSnapshot.wdPlaceholderValue);
235-
};
236-
break;
237-
}
238-
default:
239-
// Use the base attributes as-is if placeholderValue is not applicable.
240-
attributeBlocks = [baseAttributeBlocks copy];
241-
break;
242-
}
205+
NSMutableDictionary<NSString *, NSString *(^)(void)> *attributeBlocks = [self attributeBlockMapForSnapshot:snapshot
206+
wrappedSnapshot:wrappedSnapshot];
243207

244-
NSSet *nonPrefixedKeys = [NSSet setWithObjects:FBExclusionAttributeFrame, FBExclusionAttributePlaceholderValue, nil];
208+
NSSet *nonPrefixedKeys = [NSSet setWithObjects:FBExclusionAttributeFrame,
209+
FBExclusionAttributePlaceholderValue, nil];
245210

246211
for (NSString *key in attributeBlocks) {
247212
if (excludedAttributes == nil || ![excludedAttributes containsObject:key]) {
@@ -272,6 +237,41 @@ + (NSDictionary *)dictionaryForElement:(id<FBXCElementSnapshot>)snapshot
272237
return info;
273238
}
274239

240+
// Private helper that builds all attribute blocks for a given snapshot.
241+
// Includes both base attributes and any element-specific ones (e.g. placeholder for text inputs, etc.).
242+
+ (NSMutableDictionary<NSString *, NSString *(^)(void)> *)attributeBlockMapForSnapshot:(id<FBXCElementSnapshot>)snapshot
243+
wrappedSnapshot:(FBXCElementSnapshotWrapper *)wrappedSnapshot
244+
{
245+
// Base attributes common to every element
246+
NSMutableDictionary<NSString *, NSString *(^)(void)> *blocks =
247+
[@{
248+
FBExclusionAttributeFrame: ^{
249+
return NSStringFromCGRect(wrappedSnapshot.wdFrame);
250+
},
251+
FBExclusionAttributeEnabled: ^{
252+
return [@([wrappedSnapshot isWDEnabled]) stringValue];
253+
},
254+
FBExclusionAttributeVisible: ^{
255+
return [@([wrappedSnapshot isWDVisible]) stringValue];
256+
},
257+
FBExclusionAttributeAccessible: ^{
258+
return [@([wrappedSnapshot isWDAccessible]) stringValue];
259+
},
260+
FBExclusionAttributeFocused: ^{
261+
return [@([wrappedSnapshot isWDFocused]) stringValue];
262+
}
263+
} mutableCopy];
264+
265+
// Text-input placeholder (only for elements that support inner text)
266+
if ([wrappedSnapshot fb_supportsPlaceholder]) {
267+
blocks[FBExclusionAttributePlaceholderValue] = ^{
268+
return (NSString *)FBValueOrNull(wrappedSnapshot.wdPlaceholderValue);
269+
};
270+
}
271+
272+
return blocks;
273+
}
274+
275275
+ (NSDictionary *)accessibilityInfoForElement:(id<FBXCElementSnapshot>)snapshot
276276
{
277277
FBXCElementSnapshotWrapper *wrappedSnapshot = [FBXCElementSnapshotWrapper ensureWrapped:snapshot];

0 commit comments

Comments
 (0)