如何在不使用坐标方法的情况下在XCode UI测试中点击字符串语句的特定部分/单词

时间:2017-02-16 07:30:56

标签: ios xcode swift3 xcode-ui-testing

我想点击"条款&条件"在我的应用程序中存在以下行:

"我同意条款&条件和隐私政策"

"条款和条件"上面一行中的字符串不是一个链接,它作为整个docker run的一部分存在,我想点击具体的"条款和条件"。虽然我能够使用以下代码行提取字符串语句:

TextView

2 个答案:

答案 0 :(得分:0)

这是我使用的[Objective-C]样本:

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];
}

希望它会有所帮助。

答案 1 :(得分:0)

如果您的应用的功能是点击"条款和条件"做某事,但点击字符串的其他部分没有,可能会有另一个视图不是UILabel(staticText),它只跨越文本的部分说"条款和条件"。查找该视图而不是staticText。

我的猜测是,它将是常规的UIView(otherElement)或按钮。为其添加辅助功能标识符,然后点击该视图,而不是尝试查找UILabel的特定部分。