add a junit and selenium based test case of the JS compiled version of the kotlin standard ilbrary test cases

This commit is contained in:
James Strachan
2012-06-15 21:09:56 +01:00
parent 508759ca8b
commit 965c433421
7 changed files with 244 additions and 0 deletions
+1
View File
@@ -53,6 +53,7 @@
<module>tools/kotlin-maven-plugin</module>
<module>tools/kotlin-js-library</module>
<module>tools/kotlin-js-tests</module>
<module>tools/kotlin-js-tests-junit</module>
<module>tools/kdoc</module>
<module>tools/kdoc-maven-plugin</module>
@@ -0,0 +1,7 @@
## Kotlin Standard Library JS Tests JUnit Runner
This module runs the QUnit JavaScript tests for the Kotlin Standard library inside JUnit using [Selenium WebDriver](http://seleniumhq.org/projects/webdriver/) to run the QUnit code inside any of the drivers available.
This means we can run the tests automatically in any CI server like TeamCity, Jenkins, Hudson and use any WebDriver provider; such as for FireFox / Chrome / IE etc.
By default we use the HtmlUnitDriver so it runs using an in memory HTML / JavaScript engine.
@@ -0,0 +1,65 @@
<?xml version="1.0" encoding="UTF-8"?>
<project
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-project</artifactId>
<version>0.1-SNAPSHOT</version>
<relativePath>../../pom.xml</relativePath>
</parent>
<artifactId>kotlin-js-tests-junit</artifactId>
<packaging>jar</packaging>
<dependencies>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-stdlib</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>2.23.1</version>
</dependency>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-htmlunit-driver</artifactId>
<version>2.23.1</version>
</dependency>
</dependencies>
<build>
<sourceDirectory>src/main/kotlin</sourceDirectory>
<testSourceDirectory>src/test/kotlin</testSourceDirectory>
<plugins>
<plugin>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-maven-plugin</artifactId>
<version>${project.version}</version>
<executions>
<execution>
<id>compile</id>
<phase>compile</phase>
<goals>
<goal>compile</goal>
</goals>
</execution>
<execution>
<id>test-compile</id>
<phase>test-compile</phase>
<goals>
<goal>test-compile</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
@@ -0,0 +1,58 @@
package org.jetbrains.kotlin.js.qunit
import java.util.List
import kotlin.test.*
import org.openqa.selenium.By
import org.openqa.selenium.WebDriver
import org.openqa.selenium.WebElement
/**
* Waits up to a *maxMills* time for a predicate to be true, sleeping for *sleepMillis*
* and retrying until the timeout fails
*/
public fun waitFor(maxMillis: Long, sleepMillis: Long = 100, predicate: () -> Boolean): Boolean {
val end = System.currentTimeMillis() + maxMillis
while (true) {
if (predicate()) {
return true
}
val now = System.currentTimeMillis()
if (now >= end) break
val delta = end - now
val delay = sleepMillis
Thread.sleep(delay)
}
return false
}
/**
* Helper class to find QUnit tests using Selenium
*/
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
}
assertNotNull(resultsElement, "No qunit test elements could be found in ${driver.getCurrentUrl()}")
return resultsElement!!.findElements(By.tagName("li")).filterNotNull()
}
public fun findTestName(element: WebElement): String {
return element.getAttribute("id") ?: "unknown test name for $element"
}
public fun runTest(element: WebElement): Unit {
var result: String = ""
waitFor(5000) {
result = element.getAttribute("class") ?: "no result"
!result.startsWith("run")
}
assertEquals("pass", result, "test result for test case ${findTestName(element)}")
}
}
@@ -0,0 +1,82 @@
/**
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
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.htmlunit.HtmlUnitDriver;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
/**
*/
@RunWith(Parameterized.class)
public class SeleniumTest {
protected static WebDriver driver = createDriver();
public static HtmlUnitDriver createDriver() {
HtmlUnitDriver answer = new HtmlUnitDriver(true);
answer.setJavascriptEnabled(true);
return answer;
}
protected static SeleniumQUnit tester = new SeleniumQUnit(driver);
private final WebElement element;
private String name;
public SeleniumTest(WebElement element) {
this.element = element;
this.name = tester.findTestName(element);
}
@Test
public void run() {
tester.runTest(element);
}
@Override
public String toString() {
return "test " + name;
}
@Parameterized.Parameters
public static List<Object[]> findTestElements() throws IOException, InterruptedException {
File file = new File("../kotlin-js-tests/src/test/web/index.html");
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 };
list.add(args);
}
return list;
}
}
@@ -0,0 +1,24 @@
package tests
import org.junit.*
import org.junit.runners.AllTests
import org.junit.runner.RunWith
import org.junit.runners.Parameterized
import org.junit.runners.Parameterized.Parameters
import java.util.List
/*
[RunWith(javaClass<Parameterized>())]
public class SeleniumTest(val id: String) {
Test public fun checkQUnitTest(): Unit {
println("Testing $id")
}
class object {
Parameters public fun findSeleniumUris(): List<String> {
return arrayList("a", "b")
}
}
}
*/
@@ -0,0 +1,7 @@
## Kotlin Standard Library JS Tests
This module generates the JavaScript code for a number of JUnit test cases from the stdlib module.
The tests can then be ran inside any browser by opening the **src/test/web/index.html* file in this directory to run the test cases using [Qunit](http://qunitjs.com/)
To run the tests in JUnit as part of the maven build, see the **../kotlin-js-tests-runner* module