added better error messages and test case names for the JUnit running of QUnit tests

This commit is contained in:
James Strachan
2012-06-16 08:25:32 +01:00
parent 5e2521046c
commit 895b49d509
2 changed files with 29 additions and 6 deletions
@@ -7,9 +7,9 @@ import org.openqa.selenium.WebDriver
import org.openqa.selenium.WebElement import org.openqa.selenium.WebElement
/** /**
* Waits up to a *maxMills* time for a predicate to be true, sleeping for *sleepMillis* * Waits up to a *maxMills* time for a predicate to be true, sleeping for *sleepMillis*
* and retrying until the timeout fails * and retrying until the timeout fails
*/ */
public fun waitFor(maxMillis: Long, sleepMillis: Long = 100, predicate: () -> Boolean): Boolean { public fun waitFor(maxMillis: Long, sleepMillis: Long = 100, predicate: () -> Boolean): Boolean {
val end = System.currentTimeMillis() + maxMillis val end = System.currentTimeMillis() + maxMillis
while (true) { while (true) {
@@ -40,11 +40,17 @@ public class SeleniumQUnit(val driver: WebDriver) {
resultsElement != null resultsElement != null
} }
assertNotNull(resultsElement, "No qunit test elements could be found in ${driver.getCurrentUrl()}") assertNotNull(resultsElement, "No qunit test elements could be found in ${driver.getCurrentUrl()}")
return resultsElement!!.findElements(By.tagName("li")).filterNotNull() return resultsElement!!.findElements(By.xpath("li")).filterNotNull()
} }
public fun findTestName(element: WebElement): String { public fun findTestName(element: WebElement): String {
return element.getAttribute("id") ?: "unknown test name for $element" fun defaultName(name: String?) = name ?: "unknown test name for $element"
try {
val testNameElement = element.findElement(By.xpath("descendant::*[@class = 'test-name']"))
return defaultName(testNameElement!!.getText())
} catch (e: Exception) {
return defaultName(element.getAttribute("id"))
}
} }
public fun runTest(element: WebElement): Unit { public fun runTest(element: WebElement): Unit {
@@ -53,6 +59,21 @@ public class SeleniumQUnit(val driver: WebDriver) {
result = element.getAttribute("class") ?: "no result" result = element.getAttribute("class") ?: "no result"
!result.startsWith("run") !result.startsWith("run")
} }
assertEquals("pass", result, "test result for test case ${findTestName(element)}") if ("pass" != result) {
var message: String? = null
try {
val messageElement = element.findElement(By.xpath("descendant::*[@class = 'test-message']"))!!
message = messageElement.getText()
} catch (e: Exception) {
// ignore
}
val testName = "${findTestName(element)} result: $result"
val fullMessage = if (message != null) {
testName + message
} else {
"test result for test case $testName"
}
fail(fullMessage)
}
} }
} }
@@ -52,6 +52,8 @@ public class SeleniumTest {
public SeleniumTest(WebElement element) { public SeleniumTest(WebElement element) {
this.element = element; this.element = element;
this.name = tester.findTestName(element); this.name = tester.findTestName(element);
System.out.println("Test name: " + name);
} }
@Test @Test