Forum Discussion

Nimeshika's avatar
Nimeshika
Contributor
11 years ago

How to validate the color of single line in text object

Hello,

        I have  a TextBlockClass , System.Windows.Controls.TextBlock where only the heading line is in Bold Red. Rest of the text is in normal black font. I want to validate that the header is is in bold red. I could not spy only the header text. When i used text recognition I was able to spy the entire text block but not the heading line seperately.

      I tried foreground property but was not clear on that kind of validation.



   Can someone help me in how i can validate that the heading is in bold red in the textBlock?
  • Hi Nimeshika,

    There should be separate objects for TextBlock text lines even if they're not spyable. Check TextBlock's child objects in the Object Browser - you should see something like this:



    Once you find the needed object, you can check its FontWeight and Foreground.Color properties to verify the style. Here's an example:

    // JScript
    function Test()
    {
      var header = ...; // "Path" to your header object

      // Check the font weight
      var strFontWeigth = header.FontWeight.ToString().OleValue;
      if (strFontWeigth == "Bold")
      {
        Log.Checkpoint("The header is in Bold.");
      }
      else
      {
        Log.Error("The header is not in Bold; it's " + strFontWeigth);
      }

      // Check the font color
      var strColor = header.Foreground.Color.ToString().OleValue;
      if (strColor == "#FFFF0000")
      {
        Log.Checkpoint("The header is red.");
      }
      else
      {
        Log.Error("The header is not red (#FFFF0000); it's " + strColor);
      }
    }

     

  • HKosova's avatar
    HKosova
    SmartBear Alumni (Retired)

    Hi Nimeshika,

    There should be separate objects for TextBlock text lines even if they're not spyable. Check TextBlock's child objects in the Object Browser - you should see something like this:



    Once you find the needed object, you can check its FontWeight and Foreground.Color properties to verify the style. Here's an example:

    // JScript
    function Test()
    {
      var header = ...; // "Path" to your header object

      // Check the font weight
      var strFontWeigth = header.FontWeight.ToString().OleValue;
      if (strFontWeigth == "Bold")
      {
        Log.Checkpoint("The header is in Bold.");
      }
      else
      {
        Log.Error("The header is not in Bold; it's " + strFontWeigth);
      }

      // Check the font color
      var strColor = header.Foreground.Color.ToString().OleValue;
      if (strColor == "#FFFF0000")
      {
        Log.Checkpoint("The header is red.");
      }
      else
      {
        Log.Error("The header is not red (#FFFF0000); it's " + strColor);
      }
    }

     

  • Hello Helen,

                       Thank you very much for guiding me. The logic you provided worked .
    • Nimeshika's avatar
      Nimeshika
      Contributor

      Hello Helen, 

             I want to apply similar validation for another text but it gave an error as  'Foreground' is null or not an object.

      I am attaching the screeenshot of the obj.Foreground.Color in properties. Is there a way to fetch that the color of text is red , using data in screenshot? 

       

       

      Color.PNG

      • HKosova's avatar
        HKosova
        SmartBear Alumni (Retired)

        Hi Nimeshika,

        From your image, it seems that the same approach should work - convert the color to a string:

         

        var strColor = obj.Foreground.Color.ToString().OleValue;

        and compare it to "#FFFF0000".