0

I want to tap on "Terms & Conditions" in the following line exists in my App:

"I agree to the Terms & Conditions and Privacy Policy"

The "Terms and Conditions" string in the above line is not a link, it exists as part of whole TextView and I want to tap on specific "Terms and Conditions". Though I am able to extract the string statement using following line of code :

let app = XCUIApplication()

let strValue = app.textViews.element(boundBy: 0).value as! String

print(strValue)
GGamba
  • 13,140
  • 3
  • 38
  • 47
  • You can check answers [here](https://stackoverflow.com/questions/54801019/how-to-tap-on-specific-word-of-a-label-in-xcode10-ui-testing) I think it will help – Eugene Lezov Oct 12 '22 at 10:11

2 Answers2

0

If the functionality of your app is that tapping on "Terms and Conditions" does something, but tapping on other parts of the string does not, there will likely be another view which is not a UILabel (staticText) which only spans the part of the text that says "Terms and Conditions". Look for that view instead of the staticText.

My guess is that it will either be a regular UIView (otherElement) or a button. Add an accessibility identifier to it and then tap that view, rather than trying to find a specific part of the UILabel.

Oletha
  • 7,324
  • 1
  • 26
  • 46
  • Actually from dev perspective its on the basis of "Static Text's RANGE", on which an event is called. From Automation perspective, I am able to get the range of "Terms and Conditions" but not able to tap on that. – Jovil Pasrija Feb 21 '17 at 07:27
-1

here is a [Objective-C] sample that I use:

text = @"I agree to the Terms & Conditions and Privacy Policy"



- (void)setText:(NSString *)text forLabel:(UILabel *)label
{
    NSString *wholeNoteText = text;
    termsRange = [text rangeOfString:@"Terms & Conditions"]; 
    policyRange = [text rangeOfString:@"Privacy Policy")];

    NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:wholeNoteText attributes:nil];

    NSDictionary *linkAttributes = @{
        NSForegroundColorAttributeName: YourAppColor,
        NSFontAttributeName: YourFavouriteFont
    };

    [attributedString setAttributes:linkAttributes range:termsRange];
    [attributedString setAttributes:linkAttributes range:policyRange];

    label.attributedText = attributedString;

    [label addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTapOnLabel:)]];
    [label setUserInteractionEnabled:YES];

    // Create instances of NSLayoutManager, NSTextContainer and NSTextStorage
    layoutManager = [[NSLayoutManager alloc] init];
    textContainer = [[NSTextContainer alloc] initWithSize:CGSizeZero];
    textStorage = [[NSTextStorage alloc] initWithAttributedString:attributedString];

    // Configure layoutManager and textStorage
    [layoutManager addTextContainer:textContainer];
    [textStorage addLayoutManager:layoutManager];

    // Configure textContainer
    textContainer.lineFragmentPadding = 0.0;
    textContainer.lineBreakMode = label.lineBreakMode;
    textContainer.maximumNumberOfLines = label.numberOfLines;
}

- (void)handleTapOnLabel:(UITapGestureRecognizer *)tapGestureOnLabel
{
    CGPoint locationOfTouchInLabel = [tapGestureOnLabel locationInView:tapGestureOnLabel.view];
    CGSize labelSize = tapGestureOnLabel.view.frame.size;
    CGRect textBoundingBox = [layoutManager usedRectForTextContainer:textContainer];
    CGPoint textContainerOffset = CGPointMake((labelSize.width - textBoundingBox.size.width) * 0.5 - textBoundingBox.origin.x,
                                              (labelSize.height - textBoundingBox.size.height) * 0.5 - textBoundingBox.origin.y);
    CGPoint locationOfTouchInTextContainer = CGPointMake(locationOfTouchInLabel.x - textContainerOffset.x,
                                                         locationOfTouchInLabel.y - textContainerOffset.y);

    locationOfTouchInTextContainer.y -= 10; // WARNING magic number

    NSInteger indexOfCharacter = [layoutManager characterIndexForPoint:locationOfTouchInTextContainer
                                                       inTextContainer:textContainer
                              fractionOfDistanceBetweenInsertionPoints:nil];

    UIViewController *controller = [[TermsOfServiceVC alloc] init];

    if (NSLocationInRange(indexOfCharacter, termsRange))
        controller = [[PolicyVC alloc] init];
    [self.navigationController pushViewController:controller animated:YES];
}

Hope it will help.

Constantin Saulenco
  • 2,353
  • 1
  • 22
  • 44