18 January 2026:


This forum is now archived and is in read-only mode. Please continue discussions on our improved new Sahi Pro Community forum.



Sahi Pro is an enterprise grade test automation platform which can automate web, mobile, API, windows and java based applications and SAP.

How to capture webpage title in the script

TinaTina Members
edited November -1 in Sahi - Open Source
Hi There,

I want to capture the title of webpage in my script, but i am not able to do so..Can you please suggest me something?

Regards,
Tina

Comments

  • Hi Tina,

    To get the title of the webpage use:

    var $Title = window.document.title;
    _alert($Title);

    Regards
    Sumitra
  • TheeranTheeran Members
    edited December 2011
    Hi Tina,

    Try _title() API to get title of the webpage.

    _alert(_title());

    would display a Popup window with the title of the webpage.

    Regards
    Theeran.
  • Thanks Theeran & Sumitra for this.

    Can you please tell me how to check meta tags of a page?

    if i want to check this :-
    <meta name="description" content=" Rich Foods List, Nutrition Facts and Rankings. Compare options and find healthier eats. Practical Nutrition Information and personalized analysis totally free." />

    then what i have to write ?

    Thanks in advance.
    Regards,
    Tina
  • Hi Tina,

    Try with the following Script,
    <browser>
    function getMetaTagHtml(){
        var $b=document.getElementsByTagName("meta")[0].content;
        return $b;
    }
    </browser>
    var $c;
    _set($c,getMetaTagHtml());
    _assertEqual("String to be matched",$c);
    

    Assertion can be done with any of the meta Tag attributes and you can check your meta Tag.

    Regards
    Theeran.
  • Hi Theeran,

    Its not working :(

    Can you please suggest something else?

    Thanks & Regards,
    Tina
  • Hi Tina,

    If your source page contains more than one meta tags then you will have to know the meta tag on which you want to do assertion.

    In your case, it should be:
    <browser>
    function getMetaTagHtml(){
        var $b=document.getElementsByTagName("meta")[0].content;
        return $b;
    }
    </browser>
    var $c;
    _set($c,getMetaTagHtml());
    _assertEqual("Rich Foods List, Nutrition Facts and Rankings. Compare options and find healthier eats. Practical Nutrition Information and personalized analysis totally free.",$c);
    

    Let me know if this solves your issue.

    Regards
    Sumitra
  • Hi Sumitra,

    Thanks, your code is working. But I don't know why I am facing problem in matching two strings.

    i am doing it like this :-


    function metacheck($pageTitle,$meta_description)
    {
    var pTitle = _title();
    var $b=document.getElementsByTagName("meta")[3].content;

    if(pTitle == $pageTitle){
    _log('Correct page title', 'success');
    }
    else{
    _log('Incorrect page title', 'failure');
    }

    if($b == $meta_description){
    _log('Correct meta description', 'success');
    }
    else{
    _log('Incorrect meta description', 'failure');
    }

    _alert($b);
    _alert($meta_description);

    }



    and passing this function :-

    //seo check
    var $seo_title = "Rich Foods - Ranked List";
    var $seo_description =" Rich Foods List, Nutrition Facts and Rankings. Compare options and find healthier eats. Practical Nutrition Information and personalized analysis totally free.";
    _include("nr_common.sah");

    metacheck($seo_title,$seo_description);



    Can you please tell me, where i am doing mistake?


    Thanks,
    Tina
  • Hi Tina,

    Try with this code:
    function metacheck($pageTitle,$meta_description) 
    {
    var $Title = _title();
    _set($b, document.getElementsByTagName("meta")[3].content);
        if($Title == $pageTitle){
        _log('Correct page title', 'success');
        }
        else{
        _log('Incorrect page title', 'failure');
        }
    
        if($b == $meta_description){
        _log('Correct meta description', 'success');
        }
        else{
        _log('Incorrect meta description', 'failure');
        }
    _alert($b);
    _alert($meta_description);
    }
    

    Check if this solves your issue.

    Regards
    Sumitra
  • Thanks Sumitra. Its working now :)
This discussion has been closed.