BeanTest
A tiny Java web test framework built to use WebDriver/HTMLUnit within BeanShell scripts
BeanTest is a command line application that allows for writing WebDriver/HTMLUnit tests with BeanShell scripts. Specify a JSON config file:
"name": "HTMLUnitDemo",
"author": "Andrew Breksa",
"properties": {"query": "BeanShell"},
"script": "scripts\\HTMLUnitDemo.bsh",
"driver": "HTMLUnit"
and a BeanShell script:
page = webClient.getPage("http://www.google.com/search?q=" + testHandle.getProperty("query"));
Thread.sleep(1000);
testHandle.addNote("This is a test note. Window title: " + page.getTitleText());
return page.getTitleText().equals(testHandle.getProperty("query") + " - Google Search");
from the command line:
java -jar BeanShell.jar HTMLUnitDemo.json
and you get:
[{"test":"HTMLUnitDemo","notes":{"2015-02-16 04:21:02.867":"This is a test note. Window title: BeanShell - Google Search"},"result":true}]
Try it with SeleniumDemo.json!
Usage
`java -jar BeanTest.jar [options] [test1.json test2.json]`
Options:
--after, -a
An external script file to include after every test (can be used multiple
times)
Default: []
--before, -b
An external script file to include before every test (can be used
multiple times)
Default: []
--disable-helper-methods, -dhm
Disable injecting the helper methods (log,
(webDriver/selenium)/webClient) variables, etc
Default: false
--generate-test, -gt
Launch wizard to generate test json definition
Default: false
--help, -h, -?
Print this help text
Default: false
--htmlunit-warnings, -hw
If HTMLUnit should log warnings.
Default: false
--no-proxies, -np
Disables proxy support
Default: false
--proxy, -p
An HTTP proxy (host:port) (can be used multiple times)
Default: []
--proxy-file, -pf
The file containing a list of HTTP proxies with one per line (host:port)
--proxy-timeout, -pt
The server response wait time in milliseconds to check if proxies are
alive
Default: 3000
Example usage:
This runs the test HTMLUnitDemo with the proxies in the file proxies.txt, sets the script before.bsh to run before each test. HTMLUnit warnings are also enabled.
java -jar BeanTest.jar HTMLUnitDemo.json -pf proxies.txt -b scripts\before.bsh -hw
This runs the test SeleniumDemo with the proxy 192.168.0.1:8080 and sets the script after.bsh to run after each test.
java -jar BeanTest.jar SeleniumDemo.json -p 192.168.0.1:8080 -a scripts\after.bsh
TestHandle (testHandle) methods:
success() Sets the test result to true
fail() Sets the test result to fail and throws a TestFailedException, exiting the test
addNote(String) Adds a note to the report
getWebDriver() Gets the active WebDriver instance
getSelenium() Gets the active Selnium instance
getWebClient() Gets the active WebClient instance
getLocator(String) Gets a locator from the tests configuration
getProperty(String) Gets a property from the tests configuration
setData(String, Object) Sets a hashmap entry that's available between tests
getData(String) Gets an object from the hashmap data that's available between tests
getDriver() Returns either HTMLUnit or WebDriver (as a string) as per the test definition
getName() Returns the test name
See the included exmaples (HTMLUnitDemo.json and SeleniumDemo.json) and the corresponding files in /scripts for better
usage examples. Documentation on WebDriver can be found at docs.seleniumhq.org/projects/webdriver/ and
documentation on HTMLUnit can be found at htmlunit.sourceforge.net.
TODO:
- Fix the classpath/import error in Test.loadTest() that only imports if we use
import *; - Find a way to change the BeanShell log level
- Write Selenum/HTMLUnit wrapper and helper methods
- Fix log level setting issue (maybe switch to args4j)
- Fix HTMLUnit BrowserVersion serilization issue Test.browserVersion