如何在XCode UI测试中的字符串语句的特定零件/单词无需使用坐标方法



我想在我的应用中使用以下行中的" term& cresition":

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

上行中的"条款和条件"字符串不是链接,它作为整个TextView的一部分存在,我想利用特定的"条款和条件"。尽管我能够使用以下代码行提取字符串语句:

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

如果您的应用程序的功能是点击"条款和条件"可以做的事情,但是敲击字符串的其他部分没有uilabel(统计版本)仅涵盖文本的一部分,即"条款和条件"。寻找该视图而不是统计文章。

我的猜测是它将是常规的Uiview(其他元素)或按钮。向其添加可访问性标识符,然后点击该视图,而不是尝试找到Uilabel的特定部分。

这是我使用的[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];
}

希望它会有所帮助。

最新更新