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.

Need help on Broken links checking.

srinisrini Members
edited November -1 in Sahi - Open Source
Broken links checking.


I have one requirement to automate URL checking (broken link).
I have one web page. This web page will have around 30 links (pagination will be there if there are more results). User can click on these links. When user will click on the link, user will get redirected to the target website means that link is valid other wise Invalid link.we have to log the both results pass/fail into the one file. Following is the example:

1. I will login to the website
2. I will select city from dropdown (selecting City from City Dropdown and/or Industry from Industry Dropdown (for example .NET)
3. I will get search result page. This web page will have listing of jobs/any information with hyperlink.
4. Here I have to check all the links, whether they are valid or not (should not be a broken link), if it is a broken link we should log a details (Pass/Fail).

Right now I am manually checking each and every link whether it is valid or it is a broken link. Since there are 30 links in the web page, I need to click 30 times (there could be thousands of links to check for their validity).

How to automate this process? using SAHI......

Comments

  • srinisrini Members
    Hi Narayan,

    Can you please tell me the solution.....
  • narayannarayan Administrators
    OK, Here goes.
    <browser>
    function getLinks(){
        var links = window.document.links;
        var store = [];
        for (var i=0; i<links.length; i++){
            store[store.length] = {text:_sahi._getText(links[i]), url:links[i].href};
        }
        return store;
    }
    </browser>
    
    _set($links, getLinks());
    
    for (var $j=0; $j<$links.length; $j++){
        _navigateTo($links[$j].url);
        _wait(10000, _sahi.loaded);  // This should not have been necessary. Will look into it.
        if (_condition(_containsText(document.body, "Either the remote server is down or not reachable"))){
            _log($links[$j].text + " (" + $links[$j].url + ") is broken", "failure");
        }else{
            _log($links[$j].text + " (" + $links[$j].url + ") is fine", "success");
        }
    }
    
  • srinisrini Members
    Hi Narayan,

    Thanks for your support and sorry for the late reply, this is working fine but my requirement is i dont want to check all the links in the page. i need only one section the section will be like below.

    Name City Website Address


    Website - Link
    Website - Link
    Website - Link
    Website - Link
    Website - Link
  • narayannarayan Administrators
    Are the links inside a div or a table?
    Is there any pattern that matches all these urls?
  • srinisrini Members
    Hi Narayan,

    There is no any pattern that matches all these urls. but when i am doing object spy below information is displayed in the SAHI window.

    _assertExists(_cell(_table(12), 2, 1));
    _assertEqual("", _getText(_cell(_table(12), 2, 1)));
    _assertContainsText("", _cell(_table(12), 2, 1));

    when i am doing object spy for website link under Website Address column the below information is displayed in the SAHI window.

    _assertExists(_link("Website"));
    _assertExists(_link("Website[1]"));
    _assertExists(_link("Website[2]"));
    _assertExists(_link("Website[3]"));
    _assertExists(_link("Website[4]"));
  • narayannarayan Administrators
    Do a view source on the page and email it to me at narayan@sahi.co.in
  • Hello,

    I'm trying to figure out if a link contains a certain text. I use the getLinks() function you have posted in this thread.

    _set($links, getLinks());

    var $tif = "blabla";

    for (var $j=0; $j<$links.length; $j++)
    {
    if (_containsText($links[$j].url, $tif))
    {
    _log($links[$j].url + " contains " + $tif);
    }
    }

    When I am running this script I see in the log:

    /index_en.html contains blabla at 20-Oct-2009 16:26:01

    This is what I don't understand, obviously the url does not contain the text "blabla"

    I have installed Sahi V3 Release 2009-09-22, it happens with IE and Firefox
  • narayannarayan Administrators
    Popeye,

    _containsText only works in the context of the browser (or when called as part of Browser Action APIs)
    _containsText actually takes a dom object and a string.
    In your case use
    if ($links[$j].url.indexOf($tif) != -1)
    

    instead of
    if (_containsText($links[$j].url, $tif))
    

    If you are curious to know why it was returning true, _containsText($links[$j].url, $tif) actually resolves to a simple string equal to say "_containsText(\"/index_en.html\", \"blabla\")". To check, try this in your script:
    var $tif = "blabla";
    for (var $j=0; $j<$links.length; $j++)
    {
        var $x = "" + _sahi._containsText($links[$j].url, $tif);
        _log($x); // This will log the string.
        if (_containsText($links[$j].url, $tif))
        {
            _log($links[$j].url + " contains " + $tif);
        }
    }
    
  • Hi Narayan,

    thanks a lot for your help. Now it works as expected !
  • Hello Narayan,

    Above is not working in my machine.
    Please help.

    Thanks and Regards
    Nawaz Ahmed
  • Hi nawaz,

    Kindly create a new thread for your issue.

    Could you please tell us what exactly you have done and where you are facing problem. Please be more specific about your issue.

    What is the error message you are getting?

    Regards
    Sumitra
  • narayan wrote:
    Are the links inside a div or a table?
    Is there any pattern that matches all these urls?


    Hi Narayan,

    How will i get the links inside a div?

    Thanks.
  • use $linkarray= _collect("_link", "/./", _in(_div("id")))
Sign In or Register to comment.