Last active
December 11, 2019 01:26
-
-
Save Hi-Fi/f18f40b59d1743e5b284 to your computer and use it in GitHub Desktop.
Robot framework test runner in Gradle with Java proxy support and brower/driver download
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import org.apache.tools.ant.taskdefs.condition.Os | |
import org.apache.commons.io.FileUtils | |
buildscript { | |
repositories { | |
mavenCentral() | |
} | |
dependencies { | |
classpath "commons-io:commons-io:2.4" | |
} | |
} | |
task downloadIE32Driver { | |
def outputFile = file("$buildDir/webdriver/iedriver32.zip") | |
inputs.property("ieDriverVersion", ieDriverVersion) | |
outputs.file(outputFile) | |
doLast { | |
FileUtils.copyURLToFile(new URL("http://selenium-release.storage.googleapis.com/${ieDriverVersion}/IEDriverServer_Win32_${ieDriverVersion}.0.zip"), outputFile) | |
} | |
} | |
task unzipIE32Driver(type: Copy) { | |
def outputDir = file("$buildDir/webdriver/ie32Driver") | |
dependsOn downloadIE32Driver | |
outputs.dir(outputDir) | |
from(zipTree(downloadIE32Driver.outputs.files.singleFile)) | |
into(outputDir) | |
} | |
task downloadIE64Driver { | |
def outputFile = file("$buildDir/webdriver/iedriver64.zip") | |
inputs.property("ieDriverVersion", ieDriverVersion) | |
outputs.file(outputFile) | |
doLast { | |
FileUtils.copyURLToFile(new URL("http://selenium-release.storage.googleapis.com/${ieDriverVersion}/IEDriverServer_x64_${ieDriverVersion}.0.zip"), outputFile) | |
} | |
} | |
task unzipIE64Driver(type: Copy) { | |
def outputDir = file("$buildDir/webdriver/ie64Driver") | |
dependsOn downloadIE64Driver | |
outputs.dir(outputDir) | |
from(zipTree(downloadIE64Driver.outputs.files.singleFile)) | |
into(outputDir) | |
} | |
task downloadChromeDriver { | |
def outputFile = file("$buildDir/webdriver/chromedriver.zip") | |
inputs.property("chromeDriverVersion", chromeDriverVersion) | |
outputs.file(outputFile) | |
doLast { | |
def driverOsFilenamePart | |
if (Os.isFamily(Os.FAMILY_WINDOWS)) { | |
driverOsFilenamePart = "win32" | |
} else if (Os.isFamily(Os.FAMILY_MAC)) { | |
driverOsFilenamePart = "mac32" | |
} else if (Os.isFamily(Os.FAMILY_UNIX)) { | |
driverOsFilenamePart = Os.isArch("amd64") ? "linux64" : "linux32" | |
} | |
FileUtils.copyURLToFile(new URL("http://chromedriver.storage.googleapis.com/${chromeDriverVersion}/chromedriver_${driverOsFilenamePart}.zip"), outputFile) | |
} | |
} | |
task unzipChromeDriver(type: Copy) { | |
def outputDir = file("$buildDir/webdriver/chromedriver") | |
dependsOn downloadChromeDriver | |
outputs.dir(outputDir) | |
from(zipTree(downloadChromeDriver.outputs.files.singleFile)) | |
into(outputDir) | |
} | |
task downloadPhantomJs { | |
def osFilenamePart | |
if (Os.isFamily(Os.FAMILY_WINDOWS)) { | |
osFilenamePart = "windows.zip" | |
} else if (Os.isFamily(Os.FAMILY_MAC)) { | |
osFilenamePart = "macosx.zip" | |
} else if (Os.isFamily(Os.FAMILY_UNIX)) { | |
osFilenamePart = Os.isArch("amd64") ? "linux-x86_64.tar.bz2" : "linux-i686.tar.bz2" | |
} | |
def filename = "phantomjs-$phantomJsVersion-$osFilenamePart" | |
def outputFile = file("$buildDir/webdriver/$filename") | |
inputs.property("phantomJsVersion", phantomJsVersion) | |
outputs.file(outputFile) | |
doLast { | |
FileUtils.copyURLToFile(new URL("https://bitbucket.org/ariya/phantomjs/downloads/$filename"), outputFile) | |
} | |
} | |
task unzipPhantomJs(type: Copy) { | |
def outputDir = file("$buildDir/webdriver/phantomjs/$phantomJsVersion") | |
dependsOn downloadPhantomJs | |
outputs.dir(outputDir) | |
def archive = downloadPhantomJs.outputs.files.singleFile | |
from(Os.isFamily(Os.FAMILY_MAC) || Os.isFamily(Os.FAMILY_WINDOWS) ? zipTree(archive) : tarTree(archive)) | |
into(outputDir) | |
eachFile { FileCopyDetails fcp -> | |
fcp.relativePath = new RelativePath(!fcp.directory, *fcp.relativePath.segments[1..-1]) | |
} | |
} | |
task downloadFirefox { | |
def filename = "firefox-${firefoxVersion}.zip" | |
def outputFile = file("$buildDir/webdriver/$filename") | |
inputs.property("firefoxVersion", firefoxVersion) | |
outputs.file(outputFile) | |
doLast { | |
FileUtils.copyURLToFile(new URL("<link to portable FF>"), outputFile) | |
} | |
} | |
task unzipFirefox(type: Copy) { | |
def outputDir = file("$buildDir/webdriver/firefox/$firefoxVersion") | |
dependsOn downloadFirefox | |
outputs.dir(outputDir) | |
def archive = downloadFirefox.outputs.files.singleFile | |
from(Os.isFamily(Os.FAMILY_MAC) || Os.isFamily(Os.FAMILY_WINDOWS) ? zipTree(archive) : tarTree(archive)) | |
into(outputDir) | |
eachFile { FileCopyDetails fcp -> | |
fcp.relativePath = new RelativePath(!fcp.directory, *fcp.relativePath.segments[1..-1]) | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import org.apache.tools.ant.taskdefs.condition.Os | |
import java.util.UUID | |
ext { | |
// The drivers we want to use | |
drivers = ["firefox", "chrome", "phantomJs"] | |
ext { | |
chromeDriverVersion = '2.16' | |
ieDriverVersion = '2.47.0' | |
phantomJsVersion = '1.9.7' | |
firefoxVersion = '39.0.3' | |
proxyPort = 9000 | |
} | |
} | |
apply plugin: 'groovy' | |
apply plugin: 'maven' | |
apply from: 'browserDownloads.gradle' | |
apply from: 'proxySettings.gradle' | |
repositories { | |
mavenCentral() | |
} | |
dependencies { | |
runtime('com.codeborne:phantomjsdriver:1.2.1') | |
runtime('com.trilead:trilead-ssh2:1.0.0-build219') | |
runtime('com.github.markusbernhardt:robotframework-selenium2library-java:1.4.0.8') | |
} | |
def baseDir = project.projectDir | |
def browser = 'firefox' | |
def jvmArguments = ["-Djava.util.logging.config.file=$baseDir/logging.properties"] | |
def generateHTMLReports = "true" | |
def targerDir = file('target') | |
def imageDir = file('images') | |
def targetImagesDir = file('target/robotReport/images') | |
targerDir.mkdirs() | |
imageDir.mkdirs() | |
targetImagesDir.mkdirs() | |
task removeDirs << { | |
delete 'target' | |
delete 'images' | |
} | |
task cleanAndMakeDirs(dependsOn: removeDirs) << { | |
targerDir.mkdirs() | |
imageDir.mkdirs() | |
targetImagesDir.mkdirs() | |
} | |
task runRobotTests << { | |
startProxy.execute() | |
println "Proxy at build.gradle: "+proxyPort | |
if (project.hasProperty("generateHTML")) { | |
generateHTMLReports = generateHTML | |
} | |
def UUID = UUID.randomUUID().toString() | |
if (project.hasProperty("env")) { | |
environment = env.toUpperCase() | |
} | |
javaexec { | |
main = 'org.robotframework.RobotFramework' | |
classpath = sourceSets.main.runtimeClasspath | |
jvmArgs = jvmArguments | |
ignoreExitValue = true | |
args '--pythonpath', '$PYTHONPATH'+";$baseDir/binaries" | |
args '--variable', "BROWSER:$browser" | |
args '--variable', "proxyPort:${proxyPort}" | |
args '--outputdir', 'target' | |
args '--output', "$UUID"+"_output" | |
args '--log', 'NONE' | |
args '--report', 'NONE' | |
args '--xunit', "xUnit/$UUID"+"_xUnit" | |
if (project.hasProperty("tests")) { | |
args '--test', tests | |
} | |
if (project.hasProperty("wtags")) { | |
args '--include', wtags | |
} | |
if (project.hasProperty("wotags")) { | |
args '--exclude', wotags | |
} | |
if (project.hasProperty("testName")) { | |
args '--name', testName | |
} | |
args 'tests' | |
} | |
copy { | |
from 'images' | |
into 'target/robotReport/images' | |
} | |
if (generateHTMLReports == "true") { | |
generateReports.execute() | |
} | |
} | |
task generateReports << { | |
javaexec { | |
main = 'org.robotframework.RobotFramework' | |
classpath = sourceSets.main.runtimeClasspath | |
jvmArgs = jvmArguments | |
args 'rebot' | |
args '--outputdir', 'target/robotReport' | |
args '--removekeywords', 'WUKS' | |
if (project.hasProperty("reportName")) { | |
args '--name', reportName | |
} | |
args 'target/*.xml' | |
} | |
} | |
task replaceSlashes << { | |
String logFile = new File( 'target/robotReport/log.html' ).getText( 'UTF-8' ).replaceAll("%5C","/") | |
new File( 'target/robotReport/log.html' ).write( logFile, 'UTF-8' ) | |
String reportFile = new File( 'target/robotReport/report.html' ).getText( 'UTF-8' ).replaceAll("%5C","/") | |
new File( 'target/robotReport/report.html' ).write( reportFile, 'UTF-8' ) | |
} | |
task runWithPhantom(dependsOn: 'unzipPhantomJs') << { | |
browser = 'phantomjs' | |
def phantomJsFilename = Os.isFamily(Os.FAMILY_WINDOWS) ? "phantomjs.exe" : "bin/phantomjs" | |
def phantomPath = new File(unzipPhantomJs.outputs.files.singleFile, phantomJsFilename).absolutePath | |
jvmArguments.add("-Dphantomjs.binary.path=$phantomPath") | |
runRobotTests.execute() | |
} | |
task runWithIE32(dependsOn: 'unzipIE32Driver') << { | |
browser = 'ie' | |
def ie32DriverPath = new File(unzipIE32Driver.outputs.files.singleFile, 'IEDriverServer.exe').absolutePath | |
jvmArguments.add("-Dwebdriver.ie.driver=$ie32DriverPath") | |
runRobotTests.execute() | |
} | |
task runWithIE64(dependsOn: 'unzipIE64Driver') << { | |
browser = 'ie' | |
def ie64DriverPath = new File(unzipIE64Driver.outputs.files.singleFile, 'IEDriverServer.exe').absolutePath | |
jvmArguments.add("-Dwebdriver.ie.driver=$ie64DriverPath") | |
runRobotTests.execute() | |
} | |
task runWithChrome(dependsOn: 'unzipChromeDriver') << { | |
browser = 'googlechrome' | |
def chromedriverFilename = Os.isFamily(Os.FAMILY_WINDOWS) ? "chromedriver.exe" : "chromedriver" | |
def chromeDriverPath = new File(unzipChromeDriver.outputs.files.singleFile, chromedriverFilename).absolutePath | |
jvmArguments.add("-Dwebdriver.chrome.driver=$chromeDriverPath") | |
runRobotTests.execute() | |
} | |
task runWithFF(dependsOn: 'unzipFirefox') << { | |
browser = 'firefox' | |
def firefoxPath = new File(unzipFirefox.outputs.files.singleFile, 'App/Firefox/firefox.exe').absolutePath | |
jvmArguments.add("-Dwebdriver.firefox.bin=$firefoxPath") | |
runRobotTests.execute() | |
} | |
task runWithLocalFF << { | |
runRobotTests.execute() | |
} | |
generateReports.finalizedBy(replaceSlashes) | |
defaultTasks 'runWithPhantom' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import net.lightbody.bmp.BrowserMobProxy | |
import net.lightbody.bmp.BrowserMobProxyServer | |
import net.lightbody.bmp.filters.ResponseFilter | |
import org.apache.http.HttpResponse | |
import net.lightbody.bmp.util.HttpMessageContents | |
import net.lightbody.bmp.util.HttpMessageInfo | |
buildscript { | |
repositories { | |
mavenCentral() | |
} | |
dependencies { | |
classpath('org.apache.httpcomponents:httpcore:4.4.4') | |
classpath('org.seleniumhq.selenium:selenium-java:2.48.2') | |
classpath('net.lightbody.bmp:browsermob-core-littleproxy:2.1.0-beta-3') | |
} | |
} | |
task startProxy << { | |
BrowserMobProxy bmProxy = new BrowserMobProxyServer() | |
bmProxy.start(0) | |
proxyPort = bmProxy.getPort() | |
println "Proxy initialized to port: "+proxyPort | |
bmProxy.addRequestFilter({request, contents, messageInfo -> | |
if (messageInfo.dump().contains("processUserDetails.do")) { | |
def cookieFile = new File("accessCookies.tmp") | |
request.message.headers().get("Cookie")?.split(";").each { | |
cookieFile << it+"\n" | |
} | |
} | |
return null | |
}) | |
bmProxy.addResponseFilter({response, contents, messageInfo -> | |
if (response.message.toString().contains("Set-Cookie")) { | |
response.message.headers().each { | |
if (it.key.equals("Set-Cookie")) { | |
//println it | |
} | |
} | |
} | |
}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment