Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using qUnit for Javascript testing

I love qUnit for JavaScript unit testing, and have successfully used it for a large web hosting platform that is almost exclusively AJAX. However, I have to run it in a browser by hand, or as a Windows scheduled task, which is not ideal.

Has anyone run jUnit tests as part of an automated test suite, like you would in (say) perl or Java?

like image 210
Hogsmill Avatar asked Dec 17 '10 09:12

Hogsmill


1 Answers

The simplest way could be running qUnit test with Selenium 2, from JUnit test. Selenium 2 opens webpages in Firefox, IE, Chrome or its own HtmlDriver and can do almost everything with a rendered page, especially with qUnit test results.

import static org.junit.Assert.*;
import org.junit.AfterClass;
import org.junit.BeforeClass;
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;

public class FooTest {

static WebDriver driver;

@BeforeClass
public static void setUpBeforeClass() throws Exception {
    driver = new FirefoxDriver();
}

@AfterClass
public static void tearDownAfterClass() throws Exception {
    driver.close();
}

@Test
public void bar() throws Exception {
    driver.get("http://location/of/qUnitTest");

    //Handling output could be as simple as checking if all 
    //test have passed or as compound as parsing all test results 
    //and generating report, that meets your needs.
    //Code below is just a simple clue.
    WebElement element = driver.findElement(By.id("blah"));
    assertFalse(element.getText().contains("test failed"));     
}   
}
like image 67
jinowolski Avatar answered Sep 17 '22 11:09

jinowolski