seleniumTester.java Source code

Java tutorial

Introduction

Here is the source code for seleniumTester.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 static org.hamcrest.CoreMatchers.is;
import org.junit.AfterClass;
import org.junit.Assert;
import static org.junit.Assert.assertThat;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.FixMethodOrder;
import org.junit.runners.MethodSorters;
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.ExpectedCondition;
import org.openqa.selenium.support.ui.WebDriverWait;

/**
 *
 * @author abj
 */
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
public class seleniumTester {
    static WebDriver driver;
    static final int WAIT_MAX = 4;

    public seleniumTester() {

    }

    @BeforeClass
    public static void init() {
        System.setProperty("webdriver.gecko.driver", "C:\\Users\\abj\\Documents\\drivers\\geckodriver.exe");
        System.setProperty("webdriver.chrome.driver", "C:\\Users\\abj\\Documents\\drivers\\chromedriver.exe");
        com.jayway.restassured.RestAssured.given().get("http://localhost:3000/reset");
        driver = new ChromeDriver();
        driver.get("http://localhost:3000");
    }

    @AfterClass
    public static void tearDown() {
        driver.quit();
        com.jayway.restassured.RestAssured.given().get("http://localhost:3000/reset");
    }

    @Test
    public void test1() {
        (new WebDriverWait(driver, WAIT_MAX)).until((ExpectedCondition<Boolean>) (WebDriver d) -> {
            WebElement e = d.findElement(By.tagName("tbody"));
            List<WebElement> rows = e.findElements(By.tagName("tr"));

            return rows.size() == 5;
        });
    }

    @Test
    public void test2() throws Exception {
        WebElement element = driver.findElement(By.id("filter"));
        element.sendKeys("1999");
        (new WebDriverWait(driver, WAIT_MAX)).until(new ExpectedCondition<Boolean>() {
            public Boolean apply(WebDriver d) {
                WebElement e = d.findElement(By.tagName("tbody"));
                List<WebElement> rows = e.findElements(By.tagName("tr"));

                return rows.size() == 2;
            }
        });

    }

    @Test
    public void test3() {
        WebElement element = driver.findElement(By.id("filter"));
        element.sendKeys(Keys.CONTROL + "a");
        element.sendKeys(Keys.DELETE);

        (new WebDriverWait(driver, WAIT_MAX)).until(new ExpectedCondition<Boolean>() {
            public Boolean apply(WebDriver d) {
                WebElement e = d.findElement(By.tagName("tbody"));
                List<WebElement> rows = e.findElements(By.tagName("tr"));

                Assert.assertThat(rows.size(), is(5));
                return rows.size() == 5;
            }
        });

    }

    @Test
    public void test_4() {
        WebElement element = driver.findElement(By.id("h_year"));
        element.click();
        WebElement e = driver.findElement(By.tagName("tbody"));
        List<WebElement> rows = e.findElements(By.tagName("tr"));

        WebElement firstRow = rows.get(0);
        String firstRow_id = firstRow.findElement(By.tagName("td")).getText();
        WebElement lastRow = rows.get((rows.size() - 1));
        String lastRow_id = lastRow.findElement(By.tagName("td")).getText();

        Assert.assertThat(firstRow_id, is("938"));
        Assert.assertThat(lastRow_id, is("940"));
    }

    @Test
    public void test_5() {
        List<WebElement> tds = driver.findElement(By.id("tbodycars")).findElements(By.cssSelector("tr")).get(0)
                .findElements(By.tagName("td"));
        tds.get(7).findElements(By.tagName("a")).get(0).click();
        WebElement element = driver.findElement(By.id("description"));
        element.sendKeys(Keys.CONTROL + "a");
        element.sendKeys(Keys.DELETE);
        element.sendKeys("Cool car");
        driver.findElement(By.id("save")).click();

        (new WebDriverWait(driver, WAIT_MAX)).until(new ExpectedCondition<Boolean>() {
            public Boolean apply(WebDriver d) {
                WebElement e = d.findElement(By.tagName("tbody"));
                List<WebElement> rows = e.findElements(By.tagName("tr"));
                String result = null;
                for (int i = 0; i < rows.size(); i++) {
                    if (rows.get(i).findElements(By.tagName("td")).get(0).getText().equalsIgnoreCase("938")) {
                        result = rows.get(i).findElements(By.tagName("td")).get(5).getText();
                        break;
                    }
                }
                assertThat(result, is("Cool car"));
                return true;
            }
        });
    }

    @Test
    public void test_6() {
        driver.findElement(By.id("new")).click();
        driver.findElement(By.id("save")).click();
        int size = driver.findElement(By.id("tbodycars")).findElements(By.cssSelector("tr")).size();

        String errorMsg = driver.findElement(By.id("submiterr")).getText();
        assertThat(errorMsg, is("All fields are required"));
        assertThat(size, is(5));

    }

    @Test
    public void test_7() {
        driver.findElement(By.id("new")).click();

        driver.findElement(By.id("year")).sendKeys("1994");
        driver.findElement(By.id("registered")).sendKeys("1995-5-5");
        driver.findElement(By.id("make")).sendKeys("Ford");
        driver.findElement(By.id("model")).sendKeys("Probe GT");
        driver.findElement(By.id("description")).sendKeys("Old");
        driver.findElement(By.id("price")).sendKeys("100000");

        driver.findElement(By.id("save")).click();

        List<WebElement> cars = driver.findElement(By.tagName("tbody")).findElements(By.tagName("tr"));
        assertThat(cars.size(), is(6));

        List<WebElement> carDetails = cars.get(5).findElements(By.tagName("td"));
        assertThat(carDetails.get(1).getText(), is("1994"));
        assertThat(carDetails.get(2).getText(), is("5/5/1995"));
        assertThat(carDetails.get(3).getText(), is("Ford"));
        assertThat(carDetails.get(4).getText(), is("Probe GT"));
        assertThat(carDetails.get(5).getText(), is("Old"));
        assertThat(carDetails.get(6).getText(), is("100.000,00 kr."));
    }

}