Java tutorial
package SeleniumTest.selenium; /* * 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 static org.junit.Assert.*; import java.util.List; import org.junit.After; import org.junit.Before; import org.junit.FixMethodOrder; import org.junit.Test; 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 AdamLewandowski */ @FixMethodOrder(MethodSorters.NAME_ASCENDING) public class SeleniumTest { private WebDriver driver; private final int TIME_OUT; public SeleniumTest() { System.setProperty("webdriver.chrome.driver", "/Users/Stargarth/Documents/drivers/chromedriver"); driver = new ChromeDriver(); TIME_OUT = 8; } @Before public void resetWebData() { com.jayway.restassured.RestAssured.given().get("http://localhost:3000/reset"); } @After public void closeBrowser() { driver.quit(); } @Test public void aVerifyDomConstruction() { driver.navigate().to("http://localhost:3000/"); int rowCount = driver.findElements(By.xpath("//table[@class='table']/tbody/tr")).size(); assertEquals(rowCount, 5); } @Test public void bTestTableFilter() { driver.navigate().to("http://localhost:3000/"); WebElement searchFilter = driver.findElement(By.id("filter")); searchFilter.sendKeys("2002"); int rowCount = driver.findElements(By.xpath("//table[@class='table']/tbody/tr")).size(); assertEquals(rowCount, 2); } @Test public void cTestResultFilter() { driver.navigate().to("http://localhost:3000/"); WebElement searchFilter = driver.findElement(By.id("filter")); searchFilter.sendKeys("2002"); int rowCount = driver.findElements(By.xpath("//table[@class='table']/tbody/tr")).size(); assertEquals(2, rowCount); searchFilter.sendKeys(Keys.BACK_SPACE); int rows = driver.findElements(By.xpath("//table[@class='table']/tbody/tr")).size(); assertEquals(5, rows); } @Test public void dTestSorting() { driver.navigate().to("http://localhost:3000/"); WebElement yearFilter = driver.findElement(By.id("h_year")); yearFilter.click(); WebElement row = driver.findElement(By.xpath("//tbody/tr[td='938']")); WebElement row940 = driver.findElement(By.xpath("//tbody/tr[td='940']")); List<WebElement> TDCollection = row.findElements(By.tagName("td")); for (WebElement td : TDCollection) { if (td.getText().equalsIgnoreCase("938")) { assertEquals(td.getText(), "938"); break; } } List<WebElement> TDCollection2 = row940.findElements(By.tagName("td")); for (WebElement td : TDCollection2) { if (td.getText().equalsIgnoreCase("940")) { assertEquals(td.getText(), "940"); break; } } } @Test public void editCar() { driver.navigate().to("http://localhost:3000/"); WebElement row = driver.findElement(By.xpath("//tbody/tr[td='938']")); WebElement a = row.findElements(By.tagName("a")).get(0); a.click(); WebElement desc = driver.findElement(By.id("description")); desc.clear(); desc.sendKeys("Cool car"); WebElement saveButton = driver.findElement(By.id("save")); saveButton.click(); WebElement r = driver.findElement(By.xpath("//tbody/tr[td='938']")); List<WebElement> TDColl = r.findElements(By.tagName("td")); for (WebElement td : TDColl) { if (td.getText().equalsIgnoreCase("Cool car")) { assertEquals(td.getText(), "Cool car"); break; } } } @Test public void fCreateCarErrorCheck() { driver.navigate().to("http://localhost:3000/"); WebElement newButtonCar = driver.findElement(By.id("new")); newButtonCar.click(); WebElement saveCarButton = driver.findElement(By.id("save")); saveCarButton.click(); WebElement errMessage = driver.findElement(By.id("submiterr")); if (errMessage.getText().equalsIgnoreCase("All fields are required")) { assertEquals(errMessage.getText(), "All fields are required"); } int rowCount = driver.findElements(By.xpath("//table[@class='table']/tbody/tr")).size(); assertEquals(rowCount, 5); } @Test public void gSaveNewCar() { driver.navigate().to("http://localhost:3000/"); driver.findElement(By.id("new")).click(); driver.findElement(By.id("year")).sendKeys("2008"); driver.findElement(By.id("registered")).sendKeys("2002-5-5"); driver.findElement(By.id("make")).sendKeys("Kia"); driver.findElement(By.id("model")).sendKeys("Rio"); driver.findElement(By.id("description")).sendKeys("As new"); driver.findElement(By.id("price")).sendKeys("31000"); driver.findElement(By.id("save")).click(); List<WebElement> updatedRows = (new WebDriverWait(driver, TIME_OUT)) .until((ExpectedCondition<WebElement>) (WebDriver d) -> { return d.findElement(By.tagName("tbody")); }).findElements(By.tagName("tr")); assertTrue(updatedRows.size() == 6); assertTrue(updatedRows.get(5).findElements(By.tagName("td")).get(1).getText().equals("2008")); assertTrue(updatedRows.get(5).findElements(By.tagName("td")).get(4).getText().equals("Rio")); } }