ArtemPetrov
ArtemPetrov

Reputation: 53

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();
}

}

Upvotes: 4

Views: 135

Answers (3)

Anuradha Agarwal
Anuradha Agarwal

Reputation: 44

I do not find anything wrong in your logic.Infact I copied your code and just replaced firefox driver with IE driver and it worked as expected.Below was the console output I got on running the code:

> Home" is working. "Flights" is working. "Hotels" is under
> construction. "Car Rentals" is under construction. "Cruises" is
> working. "Destinations" is under construction. "Vacations" is under
> construction. "SIGN-ON" is working. "REGISTER" is working. "SUPPORT"
> is under construction. "CONTACT" is under construction. "your
> destination" is under construction. "featured vacation destinations"
> is under construction. "Register here" is working. "Business Travel @
> About.com" is working.

Upvotes: 0

JeffC
JeffC

Reputation: 25611

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();
    }
}

Upvotes: 1

Solomon Raja
Solomon Raja

Reputation: 1800

Your code works fine in my Chrome browser. Your problem could be the speed of the webdriver. You can use WebDriverWait which is an explicit wait for a particular element.

Try below modified code

for (String t : linkTexts) {
        WebDriverWait wait = new WebDriverWait(driver, 60);
        wait.until(ExpectedConditions.elementToBeClickable(driver.findElement(By.linkText(t))));
        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.");
        }
        try {
            Thread.sleep(2000);
        } catch (InterruptedException e1) {         
            e1.printStackTrace();
        }
        driver.navigate().back();
    }

I am able to get the out as below

"Home" is working.
"Flights" is working.
"Hotels" is under construction.
"Car Rentals" is under construction.
"Cruises" is working.
"Destinations" is under construction.
"Vacations" is under construction.
"SIGN-ON" is working.
"REGISTER" is working.
"SUPPORT" is under construction.
"CONTACT" is under construction.
"your destination" is under construction.
"featured vacation destinations" is under construction.
"Register here" is working.
"Business Travel @ About.com" is working.
"Salon Travel" is working.

Upvotes: 0

Related Questions