Sameera De Silva
2 min readSep 4, 2019

Webdriver how to check the availability of a site’s url and the host ?

As a software test automation engineer, based on the availability of my testing site , I want to execute my suite . Problem I am facing it , before run my plan I want to check the site is available , or else my scripts will be started to execute , even though my site is down .

Solution- Before running your plan , in before suite method using java.net you can check your host and site are available , usually in @BeforeClass or @BeforeSuite so if the site is available only your suite get executed .

Sample code is given below .

package com.webdriver.blogs;import java.awt.datatransfer.UnsupportedFlavorException;import java.io.IOException;import java.net.HttpURLConnection;import java.net.InetAddress;import java.net.URL;import java.util.concurrent.TimeUnit;import org.openqa.selenium.WebDriver;import org.openqa.selenium.chrome.ChromeDriver;import org.openqa.selenium.chrome.ChromeOptions;import org.testng.Assert;import org.testng.annotations.AfterSuite;import org.testng.annotations.BeforeClass;import org.testng.annotations.Test;public class ExecutionbasedOnTReachabilityOfSite {WebDriver driver;@BeforeClass()public void setUp() throws IOException {System.setProperty("webdriver.chrome.driver", System.getProperty("user.dir") + "\\Jar_files\\chromedriver.exe");ChromeOptions options = new ChromeOptions();// To start Chrome in Maximized browser windowoptions.addArguments("start-maximized");// To remove Chrome is being controlled by automated test softwareoptions.addArguments("disable-infobars");driver = new ChromeDriver(options);driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);driver.manage().window().maximize();//you can use String HostInetAddress addressSite = InetAddress.getByName("google.com");System.out.println("================================="+addressSite.getHostAddress());String host=addressSite.getHostAddress();// Or using DNS of the  hostInetAddress address = InetAddress.getByName("facetwirrer.co");boolean reachable = address.isReachable(10000);//timeout in millisecondsSystem.out.println("Is host reachable? " + reachable);Assert.assertEquals(true, reachable, "The host is unavaiable");URL url = new URL("https://www.google.com/");HttpURLConnection httpURLConnect=(HttpURLConnection)url.openConnection();httpURLConnect.setConnectTimeout(3000);httpURLConnect.connect();Assert.assertEquals(200, httpURLConnect.getResponseCode(), "The site is unavaiable");System.out.println("Is site reachable? yes " + httpURLConnect.getResponseCode());}@Test(priority = 1)public void navigateToWebSite() throws UnsupportedFlavorException, IOException {// Navigate to the URLdriver.get("https://www.google.com/");System.out.println("The title of the Site is " + driver.getCurrentUrl());}@AfterSuite()public void shutDown() {driver.close();driver.quit();try {Runtime.getRuntime().exec("taskkill /F /IM chromedriver.exe");} catch (IOException e1) {e1.printStackTrace();}System.out.println("***************End of testing shutdown happend **************************");}}

No responses yet