2

I have a navigation bar with 2 links on the right side. When you click one of the link, youre taken to the respective page, and the link will be highlighted. I want to check that this is the case when running my automation test. How can I go about doing this?

The following is my java code

@Test
public void testProductNavBar(){
    assertTrue(driver.findElement(By.xpath("/html/body/nav[2]/div/div/ul/li[1]/a")).isSelected());
}

When I run this, the test fails with the following error

java.lang.AssertionError at org.junit.Assert.fail(Assert.java:86)
at org.junit.Assert.assertTrue(Assert.java:41)
at org.junit.Assert.assertTrue(Assert.java:52)

I'm not sure if this is the right way to go about this. Im still new to this so any help is much appreciated!

Curious
  • 282
  • 4
  • 21
Michelle Ashwini
  • 910
  • 2
  • 13
  • 28

4 Answers4

3

That depends on how the link is highlighted. It's probably via a CSS class. Inspect the link (right click on it, your browser will have something that lets you inspect it), then compare its CSS to the CSS of the other link.

Assuming that the difference is a class called "active", then you would do something like

assertTrue(driver.findElement(By.id(ID_OF_LINK)).getAttribute("class").contains("active"));

That's pseudocode. Your language / webdriver may have a better way of checking for a CSS class on a WebElement.

(aside: everything you want your tests to interact with should have an ID; put an ID on it or ask the page author to do that)

Paul Hicks
  • 13,289
  • 5
  • 51
  • 78
1

I've managed to get this to work the way I want. I've used the .isEnabled() option as shown below;

@Test
public void testProductNavBar(){
    assertTrue(driver.findElement(By.xpath("/html/body/nav[2]/div/div/ul/li[1]/a")).isEnabled());
}
Michelle Ashwini
  • 910
  • 2
  • 13
  • 28
0

I don't think isSelected() will work in your case. To check the highlight UI, you can perfer @Paul Hicks answer For more robust test you can check whether the page gets correctly loaded with expected URL.

After clicking you can wait for some time to get the page load and then can check the currently open URL by driver.getCurrentUrl() and then compare it. You can even check for http responses, for that refer Http response

Community
  • 1
  • 1
Gaurav Lad
  • 1,788
  • 1
  • 16
  • 30
-1

Hi Michelle Ashwini to verify that a link is active using selenium you can use below sample code to do that

Use selenium's isDisplayed()

Description : Is this element displayed or not? This method avoids the problem of having to parse an element's "style" attribute. Returns: Whether or not the element is displayed

System.setProperty("webdriver.chrome.driver", "D:\\eclipseProject\\####\\src\\com\\smokealignstar\\chromedriver_win32 (1)\\chromedriver.exe");
        driver = new ChromeDriver();
        driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
        driver.get("your URL"); 
        try{
            // below line will return true only if link represented by below line is active and working 
            // otherwise false (if link is not valid or broken)
            driver.findElement(By.xpath("/html/body/nav[2]/div/div/ul/li[1]/a")).isDisplayed();
        }catch(Exception e){
            e.printStackTrace();
        }
Rajnish Kumar
  • 2,828
  • 5
  • 25
  • 39
  • @ User's on statckoverflow if you think answer given by any user is not useful then sure please down vote it but also please given reason's why you have down voted in comment section as well thanks. – Rajnish Kumar Apr 04 '16 at 05:32