Search code examples
c#seleniumselenium-webdrivervisual-studio-2017

Any suggestions on how to run a Selenium test so it checks whether a website exists?


I need to run a test that confirms a website can be obtained from the web. This is what I have so far but I am pretty sure it is wrong, any input or tips would be appreciated, thanks!

        var chromeOptions = new ChromeOptions();
        chromeOptions.AddArguments("headless");

        using (var driver = new ChromeDriver(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), chromeOptions))
        {
            driver.Navigate().GoToUrl("https://example.azurewebsites.net/");
            var wait = new WebDriverWait(driver, TimeSpan.FromSeconds(10));
            wait.Until(d => d.Title.StartsWith("", StringComparison.OrdinalIgnoreCase));
            Assert.AreEqual(driver.Title, "https://example.azurewebsites.net/");
        }

Solution

  • If your purpose is just to check if the site is alive or not, HttpRequest is a better way than Selenium. You can just send a simple GET request to the URL and check the status code. If the site is alive, you will get 200. I used this method for checking a router connection status before. I sent GET request to 'https://www.google.com/' and checked the status. Hope this helps.