user248658 user248658 - 16 days ago 6
Java Question

WebDriver prints wrong conditional statement

I'm learning WebDriver and just trying to check the links on demoaut website. The code in the loop is supposed to recognize "Under Construction" page by its title, print out the first line, and then go back to base url. But that doesn't happen for some reason. The very first "under construction" link it gets to (featured vacation destinations) is not recognized as such, prompts the wrong line to be printed, and then instead of going back it crashes due to NoSuchElementException since it's looking for a link on the wrong page. Why is this happening? Why doesn't it act based on the title of "Under Construction" page?

import java.util.List;

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.*;
import org.openqa.selenium.firefox.FirefoxDriver;

public class CheckLinks {

public static void main(String[] args) {
String baseUrl = "http://newtours.demoaut.com/";
System.setProperty("webdriver.gecko.driver", "C:\\Workspace_e\\geckodriver.exe");
WebDriver driver = new FirefoxDriver();
String underConsTitle = "Under Construction: Mercury Tours";
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 e : linkElements) {
linkTexts[i] = e.getText();
i++;
}

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

}

Answer

After you click the first link, all the references in linkTexts will become stale... even if you return to the page. What you need to do is to store all the hrefs in a List and then navigate to each one and check the title of the page.

I would write it this way...

public class CheckLinks
{
    public static void main(String[] args) throws UnsupportedFlavorException, IOException
    {
        String firefoxDriverPath = "C:\\Users\\Jeff\\Desktop\\branches\\Selenium\\lib\\geckodriver-v0.11.1-win32\\geckodriver.exe";
        System.setProperty("webdriver.gecko.driver", firefoxDriverPath);
        WebDriver driver = new FirefoxDriver();
        driver.manage().window().maximize();

        String baseUrl = "http://newtours.demoaut.com/";
        driver.get(baseUrl);
        List<WebElement> links = driver.findElements(By.tagName("a"));
        List<String> hrefs = new ArrayList<>();
        for (WebElement link : links)
        {
            hrefs.add(link.getAttribute("href"));
        }
        System.out.println(hrefs.size());
        String underConsTitle = "Under Construction: Mercury Tours";
        for (String href : hrefs)
        {
            driver.get(href);
            System.out.print("\"" + href + "\"");
            if (driver.getTitle().equals(underConsTitle))
            {
                System.out.println(" is under construction.");
            }
            else
            {
                System.out.println(" is working.");
            }
        }
        driver.close();
        driver.quit();
    }
}
Comments