Java tutorial
/* * 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 EditCarTest { 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 editCarTest() { String carId = "938"; WebElement car = navigateToCar(carId); WebElement button = navigateToEditButton(car); button.click(); navigateToField("description").clear(); navigateToField("description").sendKeys("Cool Car"); navigateToField("save").click(); WebElement editedCar = navigateToCar(carId); String editedDescription = getCarFieldValue(editedCar, 5); assertThat(editedDescription, is("Cool Car")); } private String getCarFieldValue(WebElement rowElement, int fieldNo) { return rowElement.findElements(By.tagName("td")).get(fieldNo).getText(); } private WebElement navigateToField(String editField) { return driver.findElement(By.id(editField)); } private WebElement navigateToEditButton(WebElement rowElement) { return rowElement.findElements(By.tagName("td")).get(7).findElements(By.tagName("a")).get(0); } private WebElement navigateToCar(String carId) { WebElement tableOfCars = driver.findElement(By.id("tbodycars")); List<WebElement> carsList = tableOfCars.findElements(By.tagName("tr")); WebElement car = null; for (int i = 0; i < carsList.size(); i++) { if (carsList.get(i).findElements(By.tagName("td")).get(0).getText().equalsIgnoreCase(carId)) { car = carsList.get(i); break; } } return car; } }