NewCarTest.java Source code

Java tutorial

Introduction

Here is the source code for NewCarTest.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 org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import resources.ScreenShotRule;

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

    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 newCarErrorTest() {
        navigateToField("new").click();
        navigateToField("year").sendKeys("2008");
        navigateToField("registered").sendKeys("2002-5-5");
        navigateToField("make").sendKeys("Kia");
        navigateToField("model").sendKeys("Rio");
        navigateToField("description").sendKeys("As new");
        navigateToField("price").sendKeys("31000");
        navigateToField("save").click();
        shouldWaitForPageToLoad();

        List<WebElement> cars = getRowsOfCars();
        String year = getCarFieldValue(cars.get(5), 1);
        String make = getCarFieldValue(cars.get(5), 3);
        String model = getCarFieldValue(cars.get(5), 4);
        String price = getCarFieldValue(cars.get(5), 6);
        assertThat(cars.size(), is(6));
        assertThat(year, is("2008"));
        assertThat(make, is("Kia"));
        assertThat(model, is("Rio"));
        assertThat(price, is("31.000,00 kr."));
    }

    private String getCarFieldValue(WebElement rowElement, int fieldNo) {
        return rowElement.findElements(By.tagName("td")).get(fieldNo).getText();
    }

    public void shouldWaitForPageToLoad() {
        WebDriverWait wait = new WebDriverWait(driver, 10);
        wait.until(ExpectedConditions.presenceOfElementLocated(By.id("tbodycars")));
    }

    private WebElement navigateToField(String field) {
        return driver.findElement(By.id(field));
    }

    private List<WebElement> getRowsOfCars() {
        WebElement table = driver.findElement(By.id("tbodycars"));
        return table.findElements(By.tagName("tr"));
    }
}