开发者

Validating links inside a page

I have a help link in my application. When the user clicks on this link, a new browser window is opened with help content. There is a tree like menus on the left hand side. For each menu when clicked, corresponding help content appears on the right hand panel. I want to write an automated script that will go through all the menus on the left hand side and make sure those are valid. I also need to verify the links in the content if any are valid or not. HTML structure is complex with too many cascaded framesets and frames like below.

I am currently using selenium 2.0 (webdriver api). I know that using webdriver I can switch to frames, click on each menu, grab all the links and validate them. Since the HTML is a little complex, my automation code is getting complicated too.

Is there any other way of doing this? Perhaps jQuery or some other JS library? The page is not using jQuery but I can inject one and use jQuery API if that is desired.

Thanks for your help!

<html>
  <head>
  <frameset>
     <frame id="toolbar">
     <frame scrolling="auto">
       <html>
          <head>
          <frameset id="whPfset">
            <frame id="minibar_navpane">
              <html>
                <head>
                 <frameset>
          开发者_如何学JAVA          <frame id="minibar">
                    <frame id="navpane">
                      //here are all the menus on left hand side
                                   //here I want to verify if the menu links are working
                  </frameset>
               </html>
             <frame>
             <frame id="topic">
            //here is the content displayed for a selected menu
                //here I want to verify if the links in the content if any are valid or not
          </frameset>
        </html>
      </frame>
    </frameset>
</html> 


This will loop through all a tags on the page. Requires jQuery.

$('a').each(function(index,element){
    //do something
});


To add to Jack's answer, you could assign all the links you want to look at a special class, like class="checkme", then you could do:

$('a.checkme').each(function(index,element){
    //do something
});

which will give a little performance boost. What would even work better, is if all the links you want to check are inside of a specific div or container with an ID set, you could do:

$('#divID a.checkme').each(function(index,element){
    //do something
});

Which would give you the best performance boost, and BTW, inside the object loop you can access the href tag like so:

$(this).attr('href'); // returns url set
$(this).attr('href', 'hxxp://whatever.com'); // sets the href to a different value.

Again, all of this requires you include jquery.


This is what I ended up doing. I was not able to switch to frames using jQuery somehow. I tried $('#navpane').contents().find("a"); This didn't work. Perhaps I had to use a similar cascading approach in jQuery as well for switching frames like Webdriver. For example something like $('#frame1').contents().find("#frame2").contents().find("#andsoon"); So I ended up using WebDrivers API to switch frames, click on links, validating link 'href's and image 'src'. This turned out to work well without much of complexity and without using jQuery which I initially thought otherwise. The tricky part is driver.switchTo().defaultContent(); while you are jumping from frames to frames. Thanks all for your help and pointers.

@FindBy(css="frame[scrolling='auto']")
WebElement top;
@FindBy(css="frameset#whPfset>frame#minibar_navpane")
WebElement mininav;
@FindBy(css="frameset#whPfset>frame#topic")
WebElement topic;
@FindBy(css="frameset>frame#navpane")
WebElement nav;
@FindBy(css="iframe#tocIFrame")
WebElement tocFrame;
public int switchToLeftNaviGationFrame(){
    driver.switchTo().defaultContent();
    switchToFrame(top); 
    switchToFrame(mininav);
    switchToFrame(nav);
    switchToFrame(tocFrame);
    return 0;
}
public int switchToFrame(WebElement frame){
      try {
        driver.switchTo().frame(frame);
        return 0;
    } catch (Exception e) {
        e.printStackTrace();
            return 1;
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜