JS tests: fixed waiting to finish qunit tests in SeleniumTest.

This commit is contained in:
Zalim Bashorov
2014-10-09 19:50:33 +04:00
parent 2d634931f6
commit 49e2262ca7
4 changed files with 23 additions and 30 deletions
@@ -17,13 +17,14 @@ public fun waitFor(maxMillis: Long, sleepMillis: Long = 100, predicate: () -> Bo
}
val now = System.currentTimeMillis()
if (now >= end) break
val delta = end - now
val delay = sleepMillis
Thread.sleep(delay)
}
return false
}
val TIMEOUT: Long = 5000
/**
* Helper class to find QUnit tests using Selenium
*/
@@ -33,12 +34,16 @@ public class SeleniumQUnit(val driver: WebDriver) {
* Returns all the test cases found in the current driver's page
*/
public fun findTests(): List<WebElement> {
var resultsElement: WebElement? = null
waitFor(5000) {
resultsElement = driver.findElement(By.id("qunit-tests"))
resultsElement != null
val qunitContainer = driver.findElement(By.id("qunit"))!!
val success = waitFor(TIMEOUT) {
qunitContainer.getAttribute("class") == "done"
}
assertTrue(success, "Tests timed out after $TIMEOUT milliseconds.")
var resultsElement = driver.findElement(By.id("qunit-tests"))
assertNotNull(resultsElement, "No qunit test elements could be found in ${driver.getCurrentUrl()}")
return resultsElement!!.findElements(By.xpath("li"))?.filterNotNull() ?: arrayListOf<WebElement>()
}
@@ -18,19 +18,11 @@
package tests;
import org.jetbrains.kotlin.js.qunit.SeleniumQUnit;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxProfile;
import org.openqa.selenium.htmlunit.HtmlUnitDriver;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
/**
*/
@@ -48,8 +40,8 @@ public class SeleniumFireFox extends SeleniumTest {
return answer;
}
public SeleniumFireFox(WebElement element) {
super(element);
public SeleniumFireFox(WebElement element, String name) {
super(element, name);
}
@@ -17,6 +17,7 @@
*/
package tests;
import com.gargoylesoftware.htmlunit.BrowserVersion;
import org.jetbrains.kotlin.js.qunit.SeleniumQUnit;
import org.junit.Test;
import org.junit.runner.RunWith;
@@ -24,12 +25,10 @@ import org.junit.runners.Parameterized;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.htmlunit.HtmlUnitDriver;
import com.gargoylesoftware.htmlunit.BrowserVersion;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
/**
@@ -51,13 +50,9 @@ public class SeleniumTest {
protected static SeleniumQUnit tester = new SeleniumQUnit(driver);
private final WebElement element;
private String name;
public SeleniumTest(WebElement element) {
public SeleniumTest(WebElement element, String name) {
this.element = element;
this.name = tester.findTestName(element);
System.out.println("Test name: " + name);
}
@Test
@@ -65,23 +60,17 @@ public class SeleniumTest {
tester.runTest(element);
}
@Override
public String toString() {
return "test " + name;
}
@Parameterized.Parameters
@Parameterized.Parameters(name = "{1}")
public static List<Object[]> findTestElements() throws IOException, InterruptedException {
String uri = "../kotlin-js-tests/src/test/web/index.html" + testQueryString;
File file = new File(uri);
driver.get("file://" + file.getCanonicalPath());
Thread.sleep(500);
List<WebElement> tests = tester.findTests();
// System.out.println("TESTS are: " + tests);
List<Object[]> list = new ArrayList<Object[]>();
for (WebElement test : tests) {
Object[] args = new Object[] { test };
Object[] args = new Object[] { test, tester.findTestName(test) };
list.add(args);
}
return list;
@@ -16,5 +16,12 @@
<body>
<div id="qunit"></div>
<div id="qunit-fixture">test markup</div>
<script>
document.getElementById("qunit").className = "running";
QUnit.done(function () {
document.getElementById("qunit").className = "done"
});
</script>
</body>
</html>