Showing posts with label Selenium. Show all posts
Showing posts with label Selenium. Show all posts

Wednesday 25 April 2018

How to Perform Drag and Drop using Selenium Web Driver

We have already seen Actions class in Selenium and learnt basics about it. If you want Read Here for more details.Basically action class is used to perform various mouse and keyboard driven actions such as click and hold, mouse over etc. In this blog post we are going to use Actions class in Selenium to perform Drag and Drop actions. 

Many websites offers Drag and Drop and hence we will see how we can automate it.

Video Tutorial -


For Demo purpose I am using sample Drag and Drop -

https://jqueryui.com/droppable/

Lets see the simple Test case which performs drag and drop -


import java.util.List;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.interactions.Actions;

public class DragAndDrop {
 
ChromeDriver driver;
 
 @Before
 public  void setup(){
   
  System.setProperty("webdriver.chrome.driver", "C:\\Softwares\\chromedriver_win32\\chromedriver.exe");
  
      driver=new ChromeDriver();

       driver.manage().window().maximize();
  
        // start the application
  
       driver.get("https://jqueryui.com/droppable/");
  
 }
 
 @Test
 public void testBootStrap() throws Exception
 {
 
  // Add 10 seconds wait
  Thread.sleep(10000);
   
  // Create object of actions class
  Actions act=new Actions(driver);
   
  driver.switchTo().frame(driver.findElement(By.xpath("//iframe[@class='demo-frame']")));
  // find element which we need to drag
  WebElement drag=driver.findElement(By.xpath("//*[@id='draggable']"));
   
  // find element which we need to drop
  WebElement drop=driver.findElement(By.xpath("//*[@id='droppable']"));
   
  // this will drag element to destination
  act.dragAndDrop(drag, drop).build().perform();
  
  // Add 2 seconds wait
  Thread.sleep(2000);
    }
 
@After
public void tearDown()
{ 
 driver.quit();
}


}

How to Handle Bootstrap Dropdown in Selenium WebDriver

In our previous post we have already seen How to Handle Dropdowns in Selenium WebDriver . In this post we will see how to handle Bootsrap Dropdown.
Bootstrap dropdowns and interactive dropdowns which are dynamically positioned and formed using list of <ul> and <li> html tags.

Below is the simple example of Bootstrap Dropdown-

https://www.w3schools.com/bootstrap/bootstrap_dropdowns.asp

Video Tutorial - 
 

Let us see sample selenium code -

package test;

import java.util.List;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;

public class BootstrapDropDown {
 
 ChromeDriver driver;
 
 @Before
 public  void setup(){
   
  System.setProperty("webdriver.chrome.driver", "C:\\Softwares\\chromedriver_win32\\chromedriver.exe");
  
      driver=new ChromeDriver();

       driver.manage().window().maximize();
  
        // start the application
  
       driver.get("https://www.w3schools.com/bootstrap/bootstrap_dropdowns.asp");
  
 }
 
 @Test
 public void testBootStrap() throws Exception
 {
        // First we have to click on menu item then only dropdown items will display
  
       driver.findElement(By.xpath(".//*[@id='menu1']")).click();
  
  
        // adding 2 seconds wait to avoid Sync issue
  
        Thread.sleep(2000);
  
  
  
        // Dropdown items are coming in <a> tag so below xpath will get all
  
        // elements and findElements will return list of WebElements
  
        List<WebElement> list = driver.findElementsByXPath("//ul[@class='dropdown-menu test']//li/a");
  
  
  
        // We are using enhanced for loop to get the elements
  
        for (WebElement ele : list)
  
        {
  
           // for every elements it will print the name using innerHTML
  
           System.out.println("Values " + ele.getAttribute("innerHTML"));
  
  
  
           // Here we will verify if link (item) is equal to Java Script
  
           if (ele.getAttribute("innerHTML").contains("JavaScript")) {
  
              // if yes then click on link (iteam)
  
              ele.click();
  
  
  
              // break the loop or come out of loop
  
              break;
  
           }
  
        }
  
        // here you can write rest piece of code
  
    }
 
@After
public void tearDown()
{ 
 driver.quit();
}

}

Monday 26 March 2018

How to Handle Dropdown in Selenium webdriver

Hey guys in this post we are going to learn about how to handle simple dropdowns in Selenium Webdriver.For handling dropdowns Selenium already provides Select class that has some predefined method which help is a lot while working with Dropdown.

In the example given below we are using select by index , select by value and select by visible dropdown text. These are three different ways by which you can select dropdown values. plus we can so some more verification on dropdowns by verifiyting selected dropdown fields and we can also fetch all the dropdown data using selenium webdriver.

Try out below code and learn how we can automate dropdown testing for anyweb application using selenium webdriver.

Video Tutorial -



import java.util.List;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.Select;

public class DropDownTest {

 WebDriver driver;
 WebElement month_dropdown;
 
 @Before
 public void setup()
 {
  System.setProperty("webdriver.chrome.driver", "C:\\Softwares\\chromedriver_win32\\chromedriver.exe");
  
      driver=new ChromeDriver();

       driver.manage().window().maximize();

       driver.get("http://www.facebook.com");
       
     month_dropdown=driver.findElement(By.id("month"));
 }
 
 @Test
 public void testSelectByIndex()
 {
  
  Select month=new Select(month_dropdown);
   
   month.selectByIndex(4);
  
 }
 @Test
 public void testSelectByValues()
 {
  
   
   Select month=new Select(month_dropdown);
   
   month.selectByValue("5");
 }
 @Test
 public void testSelectByVisisbleField()
 {
  
   
   Select month=new Select(month_dropdown);
   
   month.selectByVisibleText("Aug");
 }
 @Test
 public void testSelectedOption()
 {
  
   
   Select month=new Select(month_dropdown);
   
   WebElement first_value=month.getFirstSelectedOption();
   
   String value=first_value.getText();
   
   System.out.println("select dropdown value is- "+value);
 }
 @Test
 public void testAllDropDownOptions()
 {
   
  Select month=new Select(month_dropdown);
   
  List<WebElement> dropdown=month.getOptions();
   
   for(int i=0;i<dropdown.size();i++){
   
   String drop_down_values=dropdown.get(i).getText();
   
   System.out.println("dropdown values are "+drop_down_values);
   
   }
   
   
 }
 
 @After
 public void teardown()
 {
  driver.quit();
 }
 
}

Wednesday 21 March 2018

How to Disable Chrome notifications popup in Selenium WebDriver

Many Times we have faced issues that chrome browser is showing notification pop while executing selenium test cases which causes your test cases to fail. In this post we are going to look at simple code which you can use to hide or disable notification pop-up displayed on chrome browser. 



Video Tutorial -



import java.util.HashMap;
import java.util.Map;

import org.junit.Test;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
 
public class HandlePopupTest {
 
 @Test
 public  void test() throws Exception {
  
                // Create object of HashMap Class
  Map<String, Object> prefs = new HashMap<String, Object>();
              
                // Set the notification setting it will override the default setting
  prefs.put("profile.default_content_setting_values.notifications", 2);
 
                // Create object of ChromeOption class
  ChromeOptions options = new ChromeOptions();
 
                // Set the experimental option
  options.setExperimentalOption("prefs", prefs);
 
                // pass the options object in Chrome driver
  System.setProperty("webdriver.chrome.driver", "C:\\Softwares\\chromedriver_win32\\chromedriver.exe");
  WebDriver driver = new ChromeDriver(options);
  driver.get("https://www.facebook.com/");
  driver.manage().window().maximize();
  
  
  
 }
 
}

Tuesday 20 March 2018

How to Automate Radio button and Checkbox in Selenium webdriver

In this post we are going to learn about how to test checkboxes and radio buttons using selenium.Before jumping into that
let us see how the html code for checkbox and radio button looks like.
   
HTML For Checkbox
<input type=”checkbox”>
   
HTML For Radio Button
<input type=”radio”>


The main difference between radio button and checkbox is checkbox you can select multiple but for radio button, only one selection is possible.
we can perform click action using selenium click() method but before that it is very important to verify / test some conditions listed below-

  •     isEnabled()-You need to verify whether radio button or checkbox is enabled.
  •     isDisplayed()-You need to verify whether radio button or checkbox is Displayed on UI or not.
  •     isSelected()-You need to verify whether checkbox and radio button is default selected or not.
Video Tutorial - 
 

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;

import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;

 
public class RadioButtonTest {

WebDriver driver;


@Test
 
     public  void test() throws Exception {
 
 System.setProperty("webdriver.chrome.driver", "C:\\Softwares\\chromedriver_win32\\chromedriver.exe");
 
    WebDriver driver=new ChromeDriver();

      driver.manage().window().maximize();

      driver.get("http://www.facebook.com");
        
         WebElement female_radio_button=driver.findElement(By.id("u_0_b"));
 
         boolean status=female_radio_button.isDisplayed();
 
         System.out.println("Female radio button is Displayed >>"+status);
 
          boolean enabled_status=female_radio_button.isEnabled();
 
          System.out.println("Female radio button is Enabled >>"+enabled_status);
 
        boolean selected_status=female_radio_button.isSelected();
 
          System.out.println("Female radio button is Selected >>"+selected_status);
 
          female_radio_button.click();
 
        boolean selected_status_new=female_radio_button.isSelected();
 
          System.out.println("Female radio button is Selected >>"+selected_status_new);
          driver.quit();
 
     }
 

}

You can practice with same code for chekboxes. Do try and share your feedback, suggestions and questions in the comment section below.

Wednesday 17 January 2018

How to Capture Screenshot when test failes with Selenium Webdriver

As we know it is very important to know the reason behind test failure we need to capture screenshot when test case fails. The reasons behind test failure can be a bug in application or error in our automation scripts so to know the exact problem it is very important to capture screenshot on test case failure.

I have already wrote a post on how to take screenshot you can refer that Post HERE. In this post we will see how to capture screenshot in case of test case failure.

1. ITestResult Interface  - this will provide us the test case execution status and test case name.
2. @AfterMethod - An annotation of TestNG which will execute after every test execution irrespective of  test case pass or fail @AfterMethod will always execute.

Lets take look complete code -




package captureScreenshot;
 
// Import all classes and interface
import java.io.File;
import library.Utility;
import org.apache.commons.io.FileUtils;
import org.openqa.selenium.By;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.ITestResult;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.Test;
 
public class FacebookScreenshotExample {
 
// Create Webdriver reference
WebDriver driver; 
System.setProperty("webdriver.chrome.driver", "C:\\Users\\Anuja.AnujaPC\\Downloads\\chromedriver_win32\\chromedriver.exe");
 driver=new ChromeDriver(); 

 @Test
public void captureScreenshot() throws Exception
{
 
// Initiate Chrome browser

 
// Maximize the browser
driver.manage().window().maximize();
 
// Pass application url
driver.get("http://www.facebook.com");
 
// Here we are forcefully passing wrong id so that it will fail our testcase
driver.findElement(By.xpath(".//*[@id='emailasdasdas']")).sendKeys("QA Automated");
 
 
}
 
 
 
 
 
// It will execute after every test execution 
@AfterMethod
public void tearDown(ITestResult result)
{
 
// Here will compare if test is failing then only it will enter into if condition
if(ITestResult.FAILURE==result.getStatus())
{
try 
{
// Create refernce of TakesScreenshot
TakesScreenshot ts=(TakesScreenshot)driver;
 
// Call method to capture screenshot
File source=ts.getScreenshotAs(OutputType.FILE);
 
// Copy files to specific location here it will save all screenshot in our project home directory and
// result.getName() will return name of test case so that screenshot name will be same
FileUtils.copyFile(source, new File("./ScreenShots/"+result.getName()+".png"));
 
System.out.println("Screenshot taken");
} 
catch (Exception e)
{
 
System.out.println("Exception while taking screenshot "+e.getMessage());
} 
 
 
 
}
// close application
driver.quit();
}
 
 
 
}

Tuesday 16 January 2018

How to Scroll using Selenium WebDriver

In this post we are going to talk about how exactly we can scroll this using Selenium. Unfortunately Selenium does not have inbuilt method which allow us to scroll into view but, we can scroll into view in Selenium using JavaScript executor.


Now let us see the detailed code on how to Scroll using Selenium WebDriver



package test;


import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;


public class ScrollTest {
 
     @Test
 public  void test() {

  // Start browser
  System.setProperty("webdriver.chrome.driver", "C:\\Softwares\\chromedriver_win32\\chromedriver.exe");
  WebDriver driver=new ChromeDriver();


  // Maximize browser

  driver.manage().window().maximize();



  // Pass application URL

  driver.get("http://www.qaautomated.com");



  // Create instance of Javascript executor

  JavascriptExecutor je = (JavascriptExecutor) driver;



  //Identify the WebElement which will appear after scrolling down

  WebElement element = driver.findElement(By.xpath(".//*[@id='colophonpbt']"));



  // now execute query which actually will scroll until that element is not appeared on page.

  je.executeScript("arguments[0].scrollIntoView(true);",element);



  // Extract the text and verify

  System.out.println(element.getText());
  
  //close browser
  driver.quit();

  }
     
}

Friday 6 January 2017

How to Open New Tab with Selenium WebDriver

Selenium WebDriver  tool do not have any built In API using which we can directly use to open new tab. Normally we are using CTRL + t Keys to open new tab In Browser and cltr + tab to switch between the tabs. We will use this in our selenium test case to open another tab inside a browser and switch to tab. 

Check out the Example below in details to understand how to open a new tab, how to open particular url in the new tab, how to switch between two tabs  and how to close the tabs.




import java.util.Iterator;
import java.util.Set;
import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.AfterClass;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

public class BromiumTests {

 WebDriver driver;
 Utils util;

 @BeforeTest
 public void setUp() {
  System.setProperty("webdriver.chrome.driver", "C:\\Softwares\\chromedriver_win32\\chromedriver.exe");
  driver = new ChromeDriver();
  driver.manage().window().maximize();
  driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
  
 }

 
 
 @Test
 public void testOpenYoutube() {
  driver.get("https://www.youtube.com");
  
  driver.get("https://www.youtube.com/watch?v=9NXEnGiOeUU");
  driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);

 }
        @Test
 public void testOpenGmail() {
  switchToNewTab();
  driver.get("http://www.gmail.com");
  driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
 }
       public void openNewTab() {
  ((JavascriptExecutor) driver).executeScript("window.open('about:blank','_blank');");
 }

 public void switchToNewTab() {
  openNewTab();
  String subWindowHandler = null;

  Set<String> handles = driver.getWindowHandles();
  Iterator<String> iterator = handles.iterator();
  while (iterator.hasNext()) {
   subWindowHandler = iterator.next();
  }
  driver.switchTo().window(subWindowHandler);
 }

 @AfterClass
 public void tearDown() {
  driver.quit();
 }
 

}

How to Capture ScreenShot with Selenium WebDriver

Taking screenshot while testing is very important for automation so that you can point out and catch the exact bug. In this post we will learn about how to take screen shot when selenium test case fails. It is more important to have a screenshot when the selenium test fail but you can add the code if you want to capture screen shot when selenium test passes.
Once you know how to use it then you can use it anywhere you want. So lets get started . . .


Take ScreenShot with Selenium WebDriver

Video Tutorial Is Also Available -


import java.io.File;

import java.io.IOException;

import org.apache.commons.io.FileUtils;

import org.openqa.selenium.OutputType;

import org.openqa.selenium.TakesScreenshot;

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.firefox.FirefoxDriver;

import org.testng.annotations.Test;

public class ScreenshootGoogle {

 @Test
 public void TestJavaS1()
{
// Open chrome browser
 System.setProperty("webdriver.chrome.driver", "C:\\Users\\Anuja.AnujaPC\\Downloads\\chromedriver_win32\\chromedriver.exe");
  WebDriver driver=new ChromeDriver();

// Maximize the window
driver.manage().window().maximize();

// Pass the url
driver.get("http://www.qaautomated.com");

// Take screenshot and store as a file format
File src= ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
try {
 // now copy the  screenshot to desired location using copyFile //method
FileUtils.copyFile(src, new File("C:/selenium/error.png"));
}

catch (IOException e)
 {
  System.out.println(e.getMessage());

 }
 }

Sunday 23 October 2016

Handling Keyboard and Mouse Events with Selenium WebDriver


In many cases we will come across a scenario where we want to automate special type of Keyboard and Mouse events and for which Selenium Web Drover Provided Advance User Interface API with class names Actions and  Action.Handling keyboard events and mouse events in Selenium is very simple so let us start with it.

Let us see how we can use the Special Events in our Selenium Tests
Video Tutorial -


Test Scenario - Consider you want to perform mouse over action on the About Me menu as per given in below screenshot. As you observe the my site menu when you mouse over , it will change back ground color so lets see how we can test it using Selenium.



Test Case -


ackage mypackage;

import org.openqa.selenium.*;
import org.openqa.selenium.chrome.ChromeDriver;

import org.openqa.selenium.interactions.Action;
import org.openqa.selenium.interactions.Actions;

public class myclass {

public static void main(String[] args) {
String baseUrl = "http://www.qaautomated.com/";
       System.setProperty("webdriver.chrome.driver", 
         "C:\\chromedriver_win32\\chromedriver.exe");
        WebDriver driver=new ChromeDriver();

        driver.get(baseUrl);           
        WebElement aboutMe= driver.findElement(By.id("aboout_me"));
    
        
        Actions builder = new Actions(driver);
        Action mouseOverAboutMe = builder
                .moveToElement(aboutMe)
                .build();
        
        String bColor = td_Home.getCssValue("background-color");
        System.out.println("Before Mouse hover: " + bColor );        
        mouseOverAboutMe.perform();        
        bColor = td_Home.getCssValue("background-color");
        System.out.println("After Mouse hover: " + bColor );
        driver.quit();
}
}

Most Frequently Used  Keyboard and Mouse Events APIs -

1. clickAndHold() - Clicks without releasing at the pointed mouse location.

2. contextClick() -
Performs a context-click (Right Click) at the pointed mouse location.

3. doubleClick()-
Performs a double-click at the pointed mouse location.

4. dragAndDrop(source, target) -
Performs click-and-hold at the location of the source element, moves to the location of the target element, then releases the mouse.
source- web element to emulate button down at.
target- web element to move to and release the mouse at.

5.dragAndDropBy(source, x-offset, y-offset) - Performs click-and-hold at the location of the source element, moves by a given offset, then releases the mouse.
source- web element to emulate button down at.
xOffset- offcet ofhorizontal move.
yOffset- offset o fvertical move.

6.keyDown(modifier_key) -
Does not release the modifier key - subsequent interactions may assume it's kept pressed.
modifier key  sample =Keys.ALT, Keys.SHIFT, or Keys.CONTROL etc.

7.keyUp(modifier _key) -
Releases already pressed modified key.

8.moveByOffset(x-offset, y-offset) -
Moves the mouse from its current position by the given offset.
x-offset- horizontal offset. ( negative value =  moving the mouse left)
y-offset- vertical offset(negative value = moving the mouse up)

9. moveToElement(toElement) -
Moves the mouse to the middle of the element

10.release() -Releases the pressed left mouse button at the current mouse location pointer.

11.sendKeys(onElement, charsequence) -
Sends a series of keystrokes onto the element.

Please share this with your friends and leave your feedback in comment section.

Saturday 22 October 2016

Access Web Elements inside Table with Selenium



Accessing Web Elements Inside a table using xpath is very important to learn as you will need it to write automated Selenium Test Cases.As it is very difficult for developers to provide ids for each table element hence we need to use Xpath for accessing each cell element. So this post is very useful to understand accessing table using Selenium.

Accessing Table Cell Element -

HTML Code -
 

<html>
<body>
<table id="table1">
<table>
<tr>
<td>first </td>
<td>second </td>
</tr>
<tr>
<td>third</td>
<td>four </td>
</tr>
<table>
</body>
</htmt>

Xpath Writing - Let us write xpath for clicking on cell with text "thrid"

As we know XPath locators  start with a double forward slash "//" and then followed by the parent tag name. Since we are dealing with tables, the parent element will be the <table> tag. The first part of our XPath will be "//table".

The element with tab <tbody> is under <table> means <tbody> is child tag of  of <table>. Now oue xpath will be modified as "//table/tbody".

Now inside <tbody> we need to access the second <tr> and first <td> so now xpath will be modofied as "//table/tbody.tr[2]/td[1]".

WebElement thirdCell = driver.findElement(By.Xpath("//table/tbody/tr[2]/td[1]"));  

Accessing Nested Table Cell Element -

HTML Code -


<html>
<body>
<!-- Outer Table -->
<table id="table1">
<tbody>
<tr>
<td>first </td>
<td>second </td>
</tr>
<tr>
<td>third</td>
<!-- Inner Table -->
<table id="table2">
<tbody>
<tr>
<td>first 1</td>
<td>second 2</td>
</tr>
<tr>
<td>third 3</td>
<td>four 4 </td>
</tr>
</tbody>
<table>
</tr>
</tbody>
<table>
</body>
</htmt>

Xpath Writing -

Xpath for locating "second 2"


WebElement thirdCell = driver.findElement(By.Xpath("//table/tbody/tr[2]/td[2]
                                                 +"/table/tbody/tr[1]/td[2]"));

Accessing table elment using attributes -

When the there are multiple tables in HTML code or if the tables are too much nested we can use any of the attributes provided in html code such as Id, Width, height, values etc. Refer the first html code where table id is given as table 1 then we can write xpath as per shown below 


WebElement thirdCell = driver.findElement(By.Xpath("//table[@id=\"table1\"]/tbody/tr[2]/td[1]"));  


Selenium Test to Check Links in Web Page are working

Testing all the links inside a web page is working fine or not is most important testing scenario. We can test this scenario very easily with selenium. As we know the links will be inside html tag <a> we can use By.tagName("a") locator and use an iterator in java to make the process simple.

This Simple example will help you perform various types of testing like -

1. Testing broken links on webpage using selenium.
2. Clicking on a link or click on all links on  web page to verify links are working fine.
3. Counting number of Links on web page.
4. Getting List of Links on web page.
5. Find out list of non working links on webpage.

Example -

Consider we want to test all links in homepage of www.qaautomated.com

Test Case -

Check out below test case and read the descriptions mentioned in the comment section to understand the flow.

package selenium.tests;

import java.util.List;



import java.util.concurrent.TimeUnit;
import org.openqa.selenium.*;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

public class TestAllLinks { 
 
 public static void main(String[] args) {
        String baseUrl = "http://www.qaautomated.com/";
        System.setProperty("webdriver.chrome.driver", 
         "C:\\Users\\chromedriver_win32\\chromedriver.exe");
        WebDriver driver=new ChromeDriver();
        String notWorkingUrlTitle = "Under Construction: QAAutomated";
        driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);

        driver.get(baseUrl);
        List<WebElement> linkElements = driver.findElements(By.tagName("a"));
        String[] linkTexts = new String[linkElements.size()];
        int i = 0;

        //extract the link texts of each link element
        for (WebElement elements : linkElements) {
            linkTexts[i] = elements.getText();
            i++;
        }

        //test each link
        for (String t : linkTexts) {
            driver.findElement(By.linkText(t)).click();
            if (driver.getTitle().equals(notWorkingUrlTitle )) {
                System.out.println("\"" + t + "\""
                        + " is not working.");
            } else {
                System.out.println("\"" + t + "\""
                        + " is working.");
            }
            driver.navigate().back();
        }
        driver.quit();
    }
}

Output -

"Home" is working fine
"About Me" is working fine
"Contact Us" is not working ....

If you find this useful please share the post with your friends using share options given below. You can write your feedback and suggestion in the comment section.