Vérification of html tag attributes

8 posts, 0 answers
  1. Nicolas
    Nicolas avatar
    60 posts
    Member since:
    Nov 2010

    Posted 24 Jan 2011 Link to this post

    Hello

    I'd like
    to know how we can test by coded steps, the attributes of an html tag, for example to verify that the "CONTENT" attribute  of a meta tag is "NOARCHIVE".
    example :   <META NAME="GOOGLEBOT" CONTENT="NOARCHIVE">

    thanks
  2. Stoich
    Admin
    Stoich avatar
    1 posts

    Posted 25 Jan 2011 Link to this post

    Hi Nicolas,
       this requires a bit of coding since normally you can't reach the HTML code that's in the <head> part of a page. Instead we use:
    String content = ActiveBrowser.ViewSourceString;
    to get the entire source of the page and we write our own logic to parse the <meta> tags. Here's the entire code for a step that check whether each meta-tag on a page contains content="noarchive":
    String content = ActiveBrowser.ViewSourceString;
             
            while (content.Contains("<META")||content.Contains("<meta") )
            {  
                content = content.Substring(content.IndexOf("META",StringComparison.CurrentCultureIgnoreCase)+4);
                String meta = content.Substring(0, content.IndexOf(">")-1);
                content = content.Substring(content.IndexOf(">") + 1);
     
                if (meta.Contains("CONTENT=\"NOARCHIVE\"")) {
                    Assert.IsTrue(true, "Meta-tag content=noarchive");
                    break;
                };
            }

    I hope this solution will work for you, let me know how it goes!

    Greetings,
    Stoich
    the Telerik team
    Interested in Agile Testing? Check out Telerik TV for a recording of Automated Testing in the Agile Environment
  3. Cody
    Admin
    Cody avatar
    3360 posts

    Posted 28 Jan 2011 Link to this post

    Hello Nicolas,

    Sorry about that. You're right. Here is some much cleaner and more reliable code to look for that attribute.

    [CodedStep("MyCustom Step Description")]
    public void MyCustomStep()
    {
        bool found = false;
     
        ReadOnlyCollection<Element> nodes = Find.AllByTagName("meta");
        foreach (Element metatag in nodes)
        {
            foreach (iAttribute attr in metatag.Attributes)
            {
                if (attr.Name.Equals("content", StringComparison.InvariantCultureIgnoreCase)
                    && attr.Value.Equals("noarchive", StringComparison.InvariantCultureIgnoreCase))
                {
                    found = true;
                    break;
                }
            }
        }
        Assert.IsTrue(found);
    }

    Kind regards,
    Cody
    the Telerik team
    Interested in Agile Testing? Check out Telerik TV for a recording of Automated Testing in the Agile Environment
  4. Cody
    Admin
    Cody avatar
    3360 posts

    Posted 29 Jan 2011 Link to this post

    Hello Nicolas,

    I just thought of how this can be done with one line of code:

    Assert.IsNotNull(ActiveBrowser.Find.ByExpression(new HtmlFindExpression("TagName=meta", "content=noarchive")));

    This line of code will fail the test if it can't find a <meta> tag that contains CONTENT="NOARCHIVE"


    Kind regards,
    Cody
    the Telerik team
    Interested in Agile Testing? Check out Telerik TV for a recording of Automated Testing in the Agile Environment
  5. Oleg
    Oleg avatar
    14 posts
    Member since:
    Mar 2012

    Posted 16 Mar 2012 Link to this post

    how i can change this sample for verification of text value of edit field (HTML or Silverlight?)
  6. Cody
    Admin
    Cody avatar
    3360 posts

    Posted 16 Mar 2012 Link to this post

    Hello Oleg,

    The simplest thing to do is to just record a Verification step. Then no code is required. Will that approach work for you?

    Kind regards,
    Cody
    the Telerik team
    Quickly become an expert in Test Studio, check out our new training sessions!
    Test Studio Trainings
  7. Oleg
    Oleg avatar
    14 posts
    Member since:
    Mar 2012

    Posted 17 Mar 2012 Link to this post

    Cody, in verification menu i see only visible and tooltip verification,can`t realize why. if i don`t forget it, i`ll send screenshot. But now it`s not matter, because yesterday i wrote all verification for Sliverlight and HTML in code. By the way, all data i`m loading from XML, and then verification can be in code, as far as i know, data can changing between test running
  8. Cody
    Admin
    Cody avatar
    3360 posts

    Posted 17 Mar 2012 Link to this post

    Hi Oleg,

    Ok. If you need assistance in the future we'll be here.

    Kind regards,
    Cody
    the Telerik team
    Quickly become an expert in Test Studio, check out our new training sessions!
    Test Studio Trainings
Back to Top
  Cast Your Vote at DevPro Connections! Cast Your Vote at  Win IT Pro Connections!