1

I want to test if there are no broken links on the website by getting all the links in list, clicking them and getting response if they are working. Can you suggest me a way to do it in c#?

    namespace billingtest
{
    [TestClass]
    public class test
    {
        FirefoxDriver driver;

        [TestInitialize()]
        public void SyncDriver()
        {
            driver = new FirefoxDriver();
            driver.Manage().Window.Maximize();
        }

        [TestMethod]
        public void testwithadmin()
        {

            driver.Navigate().GoToUrl("http://localhost:52982");
            driver.FindElement(By.Id("UserNameOrEmail")).SendKeys("aa");
            driver.FindElement(By.Id("Password")).SendKeys("aa");
            driver.FindElement(By.XPath(".//*[@id='main']/form/div[3]/input")).Click();
            driver.FindElement(By.XPath(".//*[@id='content-main']/div/div/a[3]")).Click();

            //Get all links, click them one by one and get response if they are working
        }

        [TestCleanup]
        public void TearDown()
        {
            driver.Quit();
        }
    }
}
safary
  • 325
  • 2
  • 7
  • 26
  • There are probably much better, faster, more reliable ways of doing this...it depends on what your opinion of "does the link work?"...is it providing that a 200 gets returned? Is it the presence of a particular element on the page? Is it the title of the web page? Is it when some particular data is loaded? – Arran Sep 24 '15 at 15:21

2 Answers2

0

Check it using getTitle(); if it return something then go ahead and if return false (means blank) then print that link.

Vaibhav
  • 2,516
  • 1
  • 13
  • 8
0

There are a few things you could do depending on what you are looking for as a PASS. You could start by getting all the links on the page and iterating through the collection. That code is below. If you want to stay in the browser, you could click on each link and open it in a new browser window and verify the page. If you don't care about staying in the browser, you could get the href from each link and use this answer to validate the URLs.

ReadOnlyCollection<IWebElement> links = driver.FindElements(By.TagName("a"));
foreach (IWebElement link in links)
{
    String href = link.GetAttribute("href");
    // do something with href
}
Community
  • 1
  • 1
JeffC
  • 22,180
  • 5
  • 32
  • 55