SortingCarsTest.java Source code

Java tutorial

Introduction

Here is the source code for SortingCarsTest.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

import java.util.List;
import java.util.concurrent.TimeUnit;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Rule;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import resources.ScreenShotRule;

/**
 *
 * @author favl
 */
public class SortingCarsTest {

    private static WebDriver driver;

    @BeforeClass
    public static void setUp() throws Exception {
        System.setProperty("webdriver.gecko.driver", "/Users/favl/Downloads/geckodriver");
        driver = new FirefoxDriver();
        driver.manage().window().maximize();
        driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
        driver.get("http://localhost:3000");
    }

    @AfterClass
    public static void tearDown() throws Exception {
        driver.quit();
    }

    /**
     * Takes a screenshot when a test fails.
     */
    @Rule
    public ScreenShotRule screenShotRule = new ScreenShotRule(driver);

    @Test
    public void sortingCarsTest() {
        WebElement sortByYear = driver.findElement(By.id("h_year"));
        sortByYear.click();
        // The table should by now have rendered and cars switched position
        WebElement tableOfCars = driver.findElement(By.id("tbodycars"));
        List<WebElement> sortedCars = tableOfCars.findElements(By.tagName("tr"));
        final String firstCar = sortedCars.get(0).findElements(By.tagName("td")).get(0).getText();
        final String lastCar = sortedCars.get(4).findElements(By.tagName("td")).get(0).getText();
        assertThat(firstCar, is("938"));
        assertThat(lastCar, is("940"));
    }
}