mirror of
https://github.com/spring-projects/spring-petclinic.git
synced 2025-07-17 21:15:50 +00:00
Added more logging on the tests and added a wait
This commit is contained in:
parent
3b79e03be0
commit
c4dd55342d
4 changed files with 52 additions and 7 deletions
|
@ -41,7 +41,9 @@ public class AddOwnerIT extends SeleniumBaseIT {
|
|||
new Select(driver.findElement(By.name("type"))).selectByValue("hamster");
|
||||
driver.findElement(By.name("name")).submit();
|
||||
|
||||
waitForPageToLoad();
|
||||
Assert.assertTrue(pageContainsText("Thumper"));
|
||||
setTestFinished();
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -22,7 +22,7 @@ public class AddVisitIT extends SeleniumBaseIT {
|
|||
@Category(SeleniumBaseIT.class)
|
||||
public void addOwnerTest() {
|
||||
driver.findElement(By.name("lastName")).submit();
|
||||
|
||||
|
||||
//Go to first owner
|
||||
WebElement table = driver.findElement(By.tagName("table"));
|
||||
List<WebElement> cells = table.findElements(By.xpath("tr/td"));
|
||||
|
@ -35,7 +35,9 @@ public class AddVisitIT extends SeleniumBaseIT {
|
|||
fillTextField(By.name("name"), "foobar");
|
||||
driver.findElement(By.name("name")).submit();
|
||||
|
||||
waitForPageToLoad();
|
||||
Assert.assertNotNull(driver.findElement(By.xpath("//table//tr/td/dl/dd/[contains(text(), 'foobar')]")));
|
||||
setTestFinished();
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -23,6 +23,9 @@ public class FindOwnerIT extends SeleniumBaseIT {
|
|||
driver.get(BASE_URL+"/owners/find");
|
||||
fillTextField(By.name("lastName"),"Coleman");
|
||||
driver.findElement(By.name("lastName")).submit();
|
||||
Assert.assertTrue(driver.findElementsByXPath("//*[text()='Jean Coleman']").size() == 1);
|
||||
waitForPageToLoad();
|
||||
Assert.assertTrue("Could not find \"Jean Coleman\" on the current page. This is the html of the current page: "+getHTML(),driver.findElements(By.xpath("//*[text()='Jean Coleman']")).size() == 1);
|
||||
setTestFinished();
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -1,13 +1,17 @@
|
|||
package nl.utwente.bpsd.selenium;
|
||||
|
||||
import org.junit.After;
|
||||
import org.openqa.selenium.By;
|
||||
import org.openqa.selenium.chrome.ChromeDriver;
|
||||
import org.openqa.selenium.*;
|
||||
import org.openqa.selenium.remote.Augmenter;
|
||||
import org.openqa.selenium.remote.DesiredCapabilities;
|
||||
import org.openqa.selenium.remote.RemoteWebDriver;
|
||||
import org.openqa.selenium.support.ui.ExpectedCondition;
|
||||
import org.openqa.selenium.support.ui.WebDriverWait;
|
||||
|
||||
import java.io.File;
|
||||
import java.net.MalformedURLException;
|
||||
import java.net.URL;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
/**
|
||||
*
|
||||
|
@ -15,14 +19,15 @@ import java.net.URL;
|
|||
* @since 21-6-2017.
|
||||
*/
|
||||
public class SeleniumBaseIT {
|
||||
protected final RemoteWebDriver driver;
|
||||
protected final WebDriver driver;
|
||||
public static final String BASE_URL = "http://pet-clinic:8080/";
|
||||
private boolean testFinished = false;
|
||||
// public static final String BASE_URL = "http://localhost:8080/";
|
||||
|
||||
public SeleniumBaseIT() throws MalformedURLException {
|
||||
// System.setProperty("webdriver.chrome.driver","C:\\Users\\marti\\Downloads\\chromedriver_win32\\chromedriver.exe");
|
||||
// this.driver = new ChromeDriver();
|
||||
this.driver = new RemoteWebDriver(new URL("http://selenium:4444/wd/hub"), DesiredCapabilities.firefox());
|
||||
this.driver = new Augmenter().augment(new RemoteWebDriver(new URL("http://selenium:4444/wd/hub"), DesiredCapabilities.firefox()));
|
||||
driver.get(BASE_URL);
|
||||
}
|
||||
|
||||
|
@ -31,13 +36,46 @@ public class SeleniumBaseIT {
|
|||
driver.findElement(by).sendKeys(text);
|
||||
}
|
||||
|
||||
protected void setTestFinished() {
|
||||
testFinished = true;
|
||||
}
|
||||
|
||||
@After
|
||||
public void afterTest() {
|
||||
if (!testFinished) {
|
||||
if (driver instanceof TakesScreenshot) {
|
||||
File screenshot = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
|
||||
Logger.getLogger(this.getClass().getName()).warning(screenshot.getAbsolutePath());
|
||||
}
|
||||
}
|
||||
driver.close();
|
||||
}
|
||||
|
||||
|
||||
protected boolean pageContainsText(String text) {
|
||||
return driver.findElementsByXPath("//*[text()='"+text+"']").size() == 1;
|
||||
return driver.findElements(By.xpath("//*[text()='"+text+"']")).size() == 1;
|
||||
}
|
||||
|
||||
public String getHTML() {
|
||||
return driver.findElement(By.xpath("//html")).getAttribute("innerHTML");
|
||||
}
|
||||
|
||||
protected void waitForPageToLoad() {
|
||||
waitFor(new PageLoadedExpectedCondition());
|
||||
}
|
||||
|
||||
private void waitFor(ExpectedCondition<Boolean> condition) {
|
||||
new WebDriverWait(driver, 3).until(condition);
|
||||
}
|
||||
|
||||
private class PageLoadedExpectedCondition implements ExpectedCondition<Boolean> {
|
||||
|
||||
@Override
|
||||
public Boolean apply(WebDriver webDriver) {
|
||||
if (webDriver instanceof JavascriptExecutor) {
|
||||
return ((JavascriptExecutor)webDriver).executeScript("return document.readyState").equals("complete");
|
||||
}
|
||||
throw new ClassCastException("This webdriver is not able to execute javascript.");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue