Hi Team, I want write a program where I need to print the title of each menu and if title contains” Under Construction” then throw error

I want to write a program for http://newtours.demo.aut.com website where it throw the title of all the upper menu links (Hotel, flights, Cruises, Rentals, Destinations,Vacations). And if the page contains the title “Under Construction”then display “Link not working”.

I have written a program for it.  But i need to individually add the try — throws in each and every step. Is there any other way where I can write the try — catch — throw only once in program .

package tutorial1;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.By;

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.openqa.selenium.support.ui.Select;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.openqa.selenium.support.ui.ExpectedConditions;

class UnderConstruction {

public static void main(String[] args) throws BadNumberException, InterruptedException {
// TODO Auto-generated method stub

System.setProperty(“webdriver.chrome.driver”, “E:\\JAVA\\chromedriver.exe”);
WebDriver d = new ChromeDriver();
d.get(“Http://newtours.demoaut.com”);
String home = d.getTitle();
System.out.println(home);
WebElement v= d.findElement(By.cssSelector(“body > div > table > tbody > tr > td:nth-child(1) > table > tbody > tr > td > table > tbody > tr > td > table > tbody > tr:nth-child(2) > td:nth-child(2) > a”));
v.click();
Thread.sleep(1000);
String flights = d.getTitle();
System.out.println(flights);

WebElement i= d.findElement(By.cssSelector(“body > div > table > tbody > tr > td:nth-child(1) > table > tbody > tr > td > table > tbody > tr > td > table > tbody > tr:nth-child(3) > td:nth-child(2) > a”));
i.click();
Thread.sleep(1000);
String hotels = d.getTitle();
System.out.println(hotels);

WebElement h= d.findElement(By.xpath(“/html/body/div/table/tbody/tr/td[1]/table/tbody/tr/td/table/tbody/tr/td/table/tbody/tr[4]/td[2]/font/a”));
h.click();
Thread.sleep(1000);
String rentals = d.getTitle();
System.out.println(rentals);

WebElement a= d.findElement(By.xpath(“/html/body/div/table/tbody/tr/td[1]/table/tbody/tr/td/table/tbody/tr/td/table/tbody/tr[5]/td[2]/a”));
a.click();
Thread.sleep(1000);
String cruises = d.getTitle();
System.out.println(cruises);
WebElement n= d.findElement(By.xpath(“/html/body/div/table/tbody/tr/td[1]/table/tbody/tr/td/table/tbody/tr/td/table/tbody/tr[6]/td[2]/a”));
n.click();
Thread.sleep(1000);
String destination = d.getTitle();
System.out.println(destination);
WebElement m= d.findElement(By.xpath(“/html/body/div/table/tbody/tr/td[1]/table/tbody/tr/td/table/tbody/tr/td/table/tbody/tr[7]/td[2]/a”));
m.click();
Thread.sleep(1000);
String vacation = d.getTitle();
System.out.println(vacation);

try
{
if(d.getTitle().contains(“Under Construction:”))
{
throw new NullPointerException (“Link Not working”);
}

}
catch (NullPointerException e)
{System.out.println(e);}

}

}

Default Asked on April 24, 2017 in Selenium WebDriver.
Add Comment
  • 1 Answer(s)

    keep the check (checking the title of the page) as a separate class. You can simple call it every-time you need

    Supporter Answered on May 5, 2017.
    Add Comment
  • Your Answer

    By posting your answer, you agree to the privacy policy and terms of service.