WebDriver prints invalid conditional expressions

I am learning WebDriver and just trying to check the links on the demo website. The code in the loop should recognize the “Under Construction” page by its name, print the first line, and then return to the base URL. But for some reason this does not happen. The very first “building” link that it receives (recognized places to stay) is not recognized as such, it requests the wrong line for printing, and then instead of returning, it fails due to a NoSuchElementException exception, because it is looking for a link to wrong page. Why is this happening? Why doesn’t it act on the page title "Under Construction"?

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

}
+4
3

, , linkTexts ... . , hrefs , .

...

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

Chrome. -. WebDriverWait, .

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

"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.
0

. Infact firefox IE-, . , :

> 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.
0
source

Source: https://habr.com/ru/post/1661005/


All Articles