activities
latest
false
重要 :
请注意此内容已使用机器翻译进行了部分本地化。
工作流活动
Last updated 2024年10月22日

VerifyTextEquivalence

Verifies if two texts are equivalent, using multiple comparison options, including the following comparison types:
  • Comparing words.
  • Comparing lines.
  • Comparing characters as char variables.

VerifyTextEquivalence(string, string, ComparisonType, ComparisonOptions)

VerifyTextEquivalence(
    string baselineText,
    string targetText,
    [ComparisonType comparisonType],
    [ComparisonOptions opts]
)VerifyTextEquivalence(
    string baselineText,
    string targetText,
    [ComparisonType comparisonType],
    [ComparisonOptions opts]
)
baselineText 字符串
The base text that is used as a reference for comparison.
targetText 字符串
The text that is compared against the base text.
comparisonType ComparisonType (可选)
The type of comparison to use when comparing texts. The following options are available:
  • ComparisonType.Char: Compares every character (char) in the texts.
  • ComparisonType.Line: Compares every line in the texts.
  • ComparisonType.Word: Compares every word in the texts.
opts CompareDocumentsOptions
The specific comparison options that should be used for this operation. To create an object of type CompareDocumentsOptions, use the TestingOptions.CompareText Class.

返回值

ComparisonResult

The result of the comparison action, stored within a ComparisonResult variable. You can call the AreEquivalent or Differences properties on the ComparisonResult to display if they are equivalent, and if not, to display the differences.

示例

Check the following examples for using the VerifyTextEquivalence API:
  1. Comparing text by character:
    In this example, we are comparing two string variables, initialText and modifiedText. The comparison is done at the character level. The wildcard rule and regex rule provided along with WithIgnoreWildcardRule and WithIgnoreRegexRule methods are used to ignore specific patterns in the comparison. For instance, the wildcard rule R?d and the regex rule (f|F)ox allow us to ignore certain words that are present in the variables, such as Red, and Fox. In this scenario, the usePlaceholder parameter was set to false, because we do not want to have a placeholder name for the ignored patterns.
    The WithGenerateHtml method outputs an HTML report highlighting the differences.
    var initialText = "Red fox jumps over the lazy fox";
    var modifiedText = "Blue fox leaps over the lazy dog";
    
    var charCompareResult = testing.VerifyTextEquivalence(
        initialText, modifiedText, 
        ComparisonType.Character, 
        TestingOptions.CompareText()
            .WithGenerateHtml(@".\HtmlCompareResults\CharacterTextCompare.html")
            .WithIgnoreWildcardRule("WildcardRule", "R?d", false)
            .WithIgnoreRegexRule("RegexRule", @"(f|F)ox", false)
    );
    
    // Log any differences
    foreach (var diff in charCompareResult.Differences)
    {
        Log(diff.Operation.ToString());
        Console.WriteLine(diff.Text);
        Console.WriteLine("------------------------------------------------------------------------------");
    }var initialText = "Red fox jumps over the lazy fox";
    var modifiedText = "Blue fox leaps over the lazy dog";
    
    var charCompareResult = testing.VerifyTextEquivalence(
        initialText, modifiedText, 
        ComparisonType.Character, 
        TestingOptions.CompareText()
            .WithGenerateHtml(@".\HtmlCompareResults\CharacterTextCompare.html")
            .WithIgnoreWildcardRule("WildcardRule", "R?d", false)
            .WithIgnoreRegexRule("RegexRule", @"(f|F)ox", false)
    );
    
    // Log any differences
    foreach (var diff in charCompareResult.Differences)
    {
        Log(diff.Operation.ToString());
        Console.WriteLine(diff.Text);
        Console.WriteLine("------------------------------------------------------------------------------");
    }
    
  2. Comparing text by line:
    In this example, we are comparing two string variables, initialText and modifiedText. The comparison is done at the line level. The wildcard rule and regex rule provided along with WithIgnoreWildcardRule and WithIgnoreRegexRule methods are used to ignore specific patterns in the comparison. For instance, the wildcard rule R?d and the regex rule (f|F)ox allow us to ignore certain words that are present in the variables, such as Red, and Fox. In this scenario, the usePlaceholder parameter was set to true, because we want to have a placeholder name for the ignored patterns.
    The WithGenerateHtml method outputs an HTML report highlighting the differences.
    var initialText = @"Red fox jumps over the lazy fox";
    var modifiedText = @"Blue fox leaps over the lazy dog";
    
    var lineCompareResult = testing.VerifyTextEquivalence(
        initialText, modifiedText, 
        ComparisonType.Line, 
        TestingOptions.CompareText()
            .WithGenerateHtml(@".\HtmlCompareResults\LineTextCompare.html")
            .WithIgnoreWildcardRule("WildcardRule", "R?d", true)
            .WithIgnoreRegexRule("RegexRule", @"(f|F)ox", true)
    );
    
    // Log any differences
    foreach (var diff in lineCompareResult.Differences)
    {
        Log(diff.Operation.ToString());
        Console.WriteLine(diff.Text);
        Console.WriteLine("------------------------------------------------------------------------------");
    }var initialText = @"Red fox jumps over the lazy fox";
    var modifiedText = @"Blue fox leaps over the lazy dog";
    
    var lineCompareResult = testing.VerifyTextEquivalence(
        initialText, modifiedText, 
        ComparisonType.Line, 
        TestingOptions.CompareText()
            .WithGenerateHtml(@".\HtmlCompareResults\LineTextCompare.html")
            .WithIgnoreWildcardRule("WildcardRule", "R?d", true)
            .WithIgnoreRegexRule("RegexRule", @"(f|F)ox", true)
    );
    
    // Log any differences
    foreach (var diff in lineCompareResult.Differences)
    {
        Log(diff.Operation.ToString());
        Console.WriteLine(diff.Text);
        Console.WriteLine("------------------------------------------------------------------------------");
    }
    
  3. Comparing text by word:
    In this example, similar to the previous one, we are comparing two string variables: initialText and modifiedText. The comparison is done at the word level. The wildcard rule and regex rule provided along with WithIgnoreWildcardRule and WithIgnoreRegexRule methods are used to ignore specific patterns in the comparison. For instance, the wildcard rule R?d and the regex rule (f|F)ox allow us to ignore certain words that are present in the variables, such as Red, and Fox.
    The WithGenerateHtml method outputs an HTML report highlighting the differences.
    var initialText = "Red fox jumps over the lazy fox";
    var modifiedText = "Blue fox leaps over the lazy dog";
    
    var wordCompareResult = testing.VerifyTextEquivalence(
        initialText, modifiedText, 
        ComparisonType.Word, 
        TestingOptions.CompareText()
            .WithGenerateHtml(@".\HtmlCompareResults\LineWordCompare.html")
            .WithIgnoreWildcardRule("WildcardRule", "R?d", false)
            .WithIgnoreRegexRule("RegexRule", @"(f|F)ox", true)
    );
    
    // Log any differences
    foreach (var diff in wordCompareResult.Differences)
    {
        Log(diff.Operation.ToString());
        Console.WriteLine(diff.Text);
        Console.WriteLine("------------------------------------------------------------------------------");
    }var initialText = "Red fox jumps over the lazy fox";
    var modifiedText = "Blue fox leaps over the lazy dog";
    
    var wordCompareResult = testing.VerifyTextEquivalence(
        initialText, modifiedText, 
        ComparisonType.Word, 
        TestingOptions.CompareText()
            .WithGenerateHtml(@".\HtmlCompareResults\LineWordCompare.html")
            .WithIgnoreWildcardRule("WildcardRule", "R?d", false)
            .WithIgnoreRegexRule("RegexRule", @"(f|F)ox", true)
    );
    
    // Log any differences
    foreach (var diff in wordCompareResult.Differences)
    {
        Log(diff.Operation.ToString());
        Console.WriteLine(diff.Text);
        Console.WriteLine("------------------------------------------------------------------------------");
    }
    
The returned result from VerifyTextEquivalence includes a Differences property, which you can use to print out the differences between texts.

此页面有帮助吗?

获取您需要的帮助
了解 RPA - 自动化课程
UiPath Community 论坛
Uipath Logo White
信任与安全
© 2005-2024 UiPath。保留所有权利。