Gradle task rule for Selenium testing

on Wednesday, 2 October 2013
I've been working quite closely with one of the testers who's written a test suite using Selenium. He had initially wrote a single gradle task for each browser.

As i hope you can see its a little clunky:


task intTestFirefox(type: Test) {
maxParallelForks = 2
systemProperties['LOCAL_DRIVER'] = false
systemProperties['REMOTE_DRIVER'] = true
systemProperties['SAUCE_DRIVER'] = false
systemProperties['GRID_HOST'] = 'localhost'
systemProperties['GRID_PORT'] = '4444'
systemProperties['BROWSER'] = 'firefox'
systemProperties['PLATFORM'] = 'WINDOWS'
systemProperties['BASE_TEST_RESULTS_PATH'] = "$buildDir/Screenshots/$version"
systemProperties['APPLICATION_URL'] = 'http://someurl'
testClassesDir = sourceSets.inttest.output.classesDir
classpath = sourceSets.inttest.runtimeClasspath
useTestNG()
}
task intTestChrome(type: Test) {
maxParallelForks = 2
systemProperties['LOCAL_DRIVER'] = false
systemProperties['REMOTE_DRIVER'] = true
systemProperties['SAUCE_DRIVER'] = false
systemProperties['GRID_HOST'] = 'localhost'
systemProperties['GRID_PORT'] = '4444'
systemProperties['BROWSER'] = 'chrome'
systemProperties['webdriver.chrome.driver'] = 'C:/Automation/lib/chromedriver.exe'
systemProperties['PLATFORM'] = 'WINDOWS'
systemProperties['BASE_TEST_RESULTS_PATH'] = "$buildDir/Screenshots/$version"
systemProperties['APPLICATION_URL'] = 'http://someurl'
testClassesDir = sourceSets.inttest.output.classesDir
classpath = sourceSets.inttest.runtimeClasspath
useTestNG()
}
So, I set out to make this more flexible and minimize the amount of code in the build file. Using rules which are provided by the Gradle API I was able to write the following task:


tasks.addRule('Rule Usage: intTest<Browser>') { String taskName ->
if(taskName.startsWith('intTest')) {
task(taskName, type: Test) {
ext.browser = taskName - 'intTest'
systemProperties['LOCAL_DRIVER'] = false
systemProperties['REMOTE_DRIVER'] = true
systemProperties['SAUCE_DRIVER'] = false
systemProperties['BROWSER'] = ext.browser
systemProperties['GRID_HOST'] = 'localhost'
systemProperties['GRID_PORT'] = '4444'
systemProperties['PLATFORM'] = 'WINDOWS'
systemProperties['BASE_TEST_RESULTS_PATH'] = "$buildDir/Screenshots/$version"
systemProperties['APPLICATION_URL'] = 'http://someurl'
maxParallelForks = 2
testClassesDir = sourceSets.inttest.output.classesDir
classpath = sourceSets.inttest.runtimeClasspath
useTestNG()
}
}
}
Now all thats required is "gradle intTestChrome" or "gradle intTestFirefox".... perfect!

0 comments:

Post a Comment