mercredi 16 novembre 2016

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 and print out the first line, 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 crashes due to NoSuchElementException since it's looking for a link on the wrong page. Why is this happening? Why doesn't it recognize the "Under Construction" page and act accordingly?

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://ift.tt/1bGdQeg";
    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();
}

}

Aucun commentaire:

Enregistrer un commentaire