Functional Tests with Grails, Geb & PhantomJS not working - grails

I am running Spock tests with Gebish & HtmlUnitDriver. So far so good.
For reasons of speed I am changing the driver to PhantomJS: http://phantomjs.org/
my GebConfigs.groovy looks like this:
import org.openqa.selenium.phantomjs.PhantomJSDriver
import org.openqa.selenium.remote.DesiredCapabilities
import org.openqa.selenium.WebDriver
import org.openqa.selenium.Dimension
driver {
ArrayList cliArgsCap = new ArrayList();
cliArgsCap.add("--web-security=false");
cliArgsCap.add("--ssl-protocol=any");
cliArgsCap.add("--ignore-ssl-errors=true");
DesiredCapabilities desiredCapabilities = new DesiredCapabilities()
desiredCapabilities.setCapability(PhantomJSDriverService.PHANTOMJS_CLI_ARGS, cliArgsCap);
PhantomJSDriver d = new PhantomJSDriver(desiredCapabilities)
d
}
as you see, no matter what a PhantomJSDriver should be used for tests.
my BuildConfig.groovy
test "org.gebish:geb-spock:$gebVersion"
test "org.seleniumhq.selenium:selenium-chrome-driver:$seleniumVersion"
test "org.seleniumhq.selenium:selenium-remote-driver:$seleniumVersion"
test "org.seleniumhq.selenium:selenium-htmlunit-driver:$seleniumVersion"
test "org.seleniumhq.selenium:selenium-support:$seleniumVersion"
test "org.spockframework:spock-grails-support:0.7-groovy-2.0"
test "org.grails:grails-datastore-test-support:1.0-grails-2.4"
test( "com.github.detro.ghostdriver:phantomjsdriver:1.1.0" ) {
transitive = false
}
the phantomjs.exe is located in:
C:\Apps\selenium\phantomjs\phantomjs.exe
and C:\Apps\selenium\phantomjs is in my PATH. i can run it through cmd: phantomjs in interactive mode.
problem
for some unclear reason geb is still using a HtmlUnitDriver. see log:
Error |
2014-08-15 14:01:26,630 [JS executor for com.gargoylesoftware.htmlunit.WebClient#527a4013] ERROR
javascript.StrictErrorReporter - runtimeError: message=[The data necessary to complete this
operation is not yet available.] sourceName=[http://localhost:8080/myapp/aui/js/aui-all.js] line=
[1] lineSource=[null] lineOffset=[0]
any ideas why phantomjsdriver is not used in this case?

I have phantomjs up and running with a Grails 2.0.4 app. My buildConfig.groovy looks like this:
def gebVersion = "0.9.2"
def seleniumVersion = "2.35.0"
...
test "org.gebish:geb-spock:$gebVersion"
test "org.gebish:geb-junit4:$gebVersion"
test( "com.github.detro.ghostdriver:phantomjsdriver:1.0.1" ) {
transitive = false
exclude "xml-apis"
exclude "xercesImpl"
}
test ("org.seleniumhq.selenium:selenium-remote-driver:$seleniumVersion") { // Needed by phantomjsdriver
exclude "xerces"
exclude "xercesImpl"
}
hope that helps
Update: had to work a little bit on my own example, but the result is a working Grails 2.4.2 app with phantomjs: https://github.com/rdmueller/SO25324259 (sorry for the horrible formatting - only saw it after my checkin).
btw: I didn't get your DesiredCapabilities to work :-(
PS: did you know the report feature? https://github.com/rdmueller/SO25324259/blob/master/test/functional/DemoSpec.groovy#L14
It takes a screenshot with your headless phantomjs browser - really cool feature!

Related

Tagged Karate test run not finishing on jenkins [duplicate]

This question already has answers here:
Karate Tests Stuck on Running Forever
(2 answers)
Closed 1 year ago.
Sometimes when I run a test on jenkins with logical OR tag selection (karate.options="--tags #vld,#command-composition") the tests are executed unitl the end but will never step out of
Results results = Runner.parallel(getClass(),5 , karateOutputPath);
This is definetly a problem concerning karate AND jenkins because when executed on a local machine, the problem never occured.
Also when the tags are selected separatelly (eg. "--tags #vld" or "--tags #command-composition") the problem does not occure. Even on jenkins.
This is my parallel runner class used for executing the tests in parallel. I added some logging debug outputs in order to clarify where the execution is running into an infinite loop.
The execution is not stepping out of test execution:
Results results = Runner.parallel(getClass(),5 , karateOutputPath);
and therefore everything after (including)
logger.debug("DEBUG-LOG: AFTER TEST - BEFORE REPORT GENERATION");
is never executed.
#KarateOptions(tags = {"~#ignore"})
public class ParallelRunner {
private static final Logger logger = LoggerFactory.getLogger("com.intuit.karate");
#Test
public void testParallel() throws IOException {
// RESET LAST USED ICCID
Helper.resetLastIccid();
String outputPath = "build";
String karateOutputPath = outputPath + "/surefire-reports";
logger.debug("DEBUG-LOG: BEFORE TEST");
Results results = Runner.parallel(getClass(),5 , karateOutputPath);
logger.debug("DEBUG-LOG: AFTER TEST - BEFORE REPORT GENERATION");
generateReport(karateOutputPath);
logger.debug("DEBUG-LOG: AFTER GEN REPORT");
assertTrue("scenarios failed", results.getFailCount() == 0);
}
private static void generateReport(String karateOutputPath) {
logger.debug("DEBUG-LOG: IN GEN REPORT");
Collection<File> jsonFiles = FileUtils.listFiles(new File(karateOutputPath), new String[] {"json"}, true);
List<String> jsonPaths = new ArrayList(jsonFiles.size());
jsonFiles.forEach(file -> jsonPaths.add(file.getAbsolutePath()));
Configuration config = new Configuration(new File("build"), "MH Conan Mobile Systemtest");
ReportBuilder reportBuilder = new ReportBuilder(jsonPaths, config);
reportBuilder.generateReports();
}
}
I also played arround with the number of used threads and parallel execution==false. Nothing seems to help.
I also compared the surefire-reports output folder between a local run and a jenkins build. All surefire reports for the executed features are there but on jenkins the results-json.txt and timeline.html is missing.
So, is there anyone out there who has also experienced this kind of behaviour? Or has someone further ideas how to debug or at least narrow down this problem somehow?
Help is very much appreciated! :)
Karate-Version: 0.9.2 (latest)
With two features who respectively have the #feature1 and #feature2 tags, when running
mvn clean install -Dkarate.options="--tags #feature1,#feature2"
Both features are played. That's not an AND tag selection but an OR tag selection.
With
mvn clean install -Dkarate.options="--tags #feature1 --tags #feature2"
None of the two features are played. That's an AND selection.
Appart from that, have you tried creating a minimal karate project, with just a couple of features that are just doing a few simple steps each, and to launch it on Jenkins?

Grails Functional Testing Plugin and RESTful Tests

I just grabbed the latest version of the functional testing plugin and it's focus, at least from the docs, has changed a bit. This is not a bad thing. The HtmlUnit integration is nice. But now, there isn't anything in the docs about RESTful testing of web services. However, what I used to do with functionaltestplugin.FunctionalTestCase still works, which is great.
For example:
void testCertifyEmployerCertifierNotFound() {
post('/employerCertification/certifyEmployer') {
headers['Content-Type'] = 'application/json'
body {
"""
{
'employerName': 'ACME',
'certifierExteralId': '1234556',
'certifyingUserId': '123445'
}
"""
}
}
assertStatus 200
assertContentType "application/json"
def model = this.response.contentAsString
def map = JSON.parse(model)
assertFalse(map.success)
assertNotNull(map.errorCode)
assertTrue(map.errorCode == EmployerCertificationService.ERROR_RESPONSE.CERTIFIER_NOT_FOUND.toString())
}
Is this plugin still the "defacto" plugin to use for functional web service testing and is my approach above still valid?
if your target is testing REST request/response I suggest you to use rest client builder plugin. You don't need complex browser-simul plugins. Use it is very simple in just two steps after installing it:
add event to manage functional tests in scripts/_Events.groovy: this is a system grail file used to hook some events at runtime. Just copy and paste this snippet:
eventAllTestsStart = {
if (getBinding().variables.containsKey("functionalTests")) {
functionalTests << "functional"
}
}
Now you can create functional tests in test/functional folder, rememeber to ends filename with Spec, grails will not find any tests if you forget this.
This is an example:
import grails.plugins.rest.client.RestBuilder
import spock.lang.Specification
class AuthenticationSpec extends Specification {
String baseUrl = "http://localhost:8080/grouply-backend"
void "test login wrong credentials"() {
given:
RestBuilder restBuilder = new RestBuilder()
when: "sending wrong credential"
def response = restBuilder.post("${baseUrl}/auth/login") {
json {
username = 'foo'
password = 'bar'
}
}
then: "authentication http error should happen"
response.status == 401
}
}
run tests with $ grails test-app functional:
I have just tried to use both functional testing plugin and webtest plugin with almost-current Grails 2.4.2 and I am sad to report that they are both borked. :(
Functional testing plugin has been practically abandoned for at least 9 months now. On December 2013 a critical bug that makes all of tests written using this plugin not working has been reported. There was no response from plugin developer regarding it up to the day I am writing this. And as this developer, Marc Palmer has switched to iOS development and consultancy in August 2014 I don't believe this bug will ever be resolved.
Webtest plugin has been last updated more than 3 years ago and it "requires Grails 1.2.RC2+". I run into ugly error of missing webtest.jar when running it and it looks like this plugin just haven't been updated to current Grails version. Also its syntax is not very Groovy-like and not nice.
Fortunately Geb integration for Grails plugin is supposed to work with current Grails versions. :) In fact in does only work with Grails 2.3.1+, as of writing these words has been last time updated in June 2014 and it's example tests project have been updated to Grails 2.4.3, release just a few days ago already, so it is very up-to-date. However I haven't used this project yet. Also looking at is's example code I am not sure it the best choice for RESTful API testing - it's more of a GUI web application testing tool..
Although is probably not very "grails style" i guess it should be possible to start up the whole stack with the spring context and run integration rest tests using apache HttpClient.

Spock Integration Test fails when run with other integration tests-succeeds in isolation using integration:spock

I am adding a first spock integration test to an existing set of tests for a grails 2.1.1 application. The test runs and tests pass when run using:
grails test-app integration:spock CreditServiceSpec
(Yes, everything is in the default package - fixing this is a ton of work that will not be approved...file under technical debt.)
However, when I run all the tests (grails test-app), unit test pass, spock unit tests pass, integration tests pass, but I get the following failure for spock integration:
| Completed 818 integration tests, 0 failed in 104001ms
| Running 1 spock test...
| Failure: CreditServiceSpec
| groovy.lang.GroovyRuntimeException: failed to invoke constructor: public org.codehaus.groovy.grails.test.support.GrailsTestAutowirer(org.springframework.context.ApplicationContext) with arguments: [] reason: java.lang.IllegalArgumentException
at grails.plugin.spock.IntegrationSpec.$spock_initializeSharedFields(IntegrationSpec.groovy:33)
| Completed 0 spock test, 0 failed in 33ms
| Tests PASSED - view reports in /Users/*/projects/GrailsPlugins/DomainServices/target/test-reports
I get the exact same exception whether I run the full test I built or the following, very strip down example:
import grails.plugin.spock.IntegrationSpec
class CreditServiceSpec extends IntegrationSpec {
def setup() {}
def cleanup() {}
public void "sample"() {
setup:"Nothing to do here."
expect:"This is the truest of truths..."
true == true
}
}
I did crack open IntegrationSpec and looked at line 33:
#Shared private autowirer = new GrailsTestAutowirer(applicationContext)
But determining how/why the applicationContext is not being passed in properly is beyond me and, perhaps, is the thrust of my question.
Has anyone encountered this kind of behavior and found a way to get spock integration to play nice with other tests? Thanks.
It looks like Grails 2.1.1 had several issues with Spock tests in the Integration scope. Jeff's comment and Peter's in particular sound like the issue you were having; basically the ApplicationHolder was null or an empty list.
The parent task lists Grails 2.2.5 as the fix version. Any chance you can upgrade to that (or some even later version) and see if the problem persists?
There have also been cases where a simple grails clean has fixed issues like this.
I had a problem with the exact same symptom.
I was using the BuildTestData plugin and used the #Build annotation in a IntegrationSpec, but using the #Build with use a transformation wich extended the #TestFor transformation which is incompatible with the Intengration runtime.
So just remove the #Build annotations and it will run.

Error trying to run geb tests with cucumber-jvm

I am trying to get some geb tests running in a Grails project within IntelliJ but am having trouble & after 2 days of web searching & trying different things am still stuck. We did have some Groovy tests running fine, previously, but no luck so far with geb. We're using cucumber-jvm.
Currently I am getting the following error:
Error Error executing script TestApp: groovy.lang.MissingMethodException:
No signature of method: GebConfig.environments() is applicable for argument types:
(GebConfig$_run_closure2) values: [GebConfig$_run_closure2#4ad3727e]
My GebConfig.groovy file is in test/functional, and contains:
import org.openqa.selenium.htmlunit.HtmlUnitDriver
import org.openqa.selenium.firefox.FirefoxDriver
import org.openqa.selenium.chrome.ChromeDriver
// Use htmlunit as the default
// See: http://code.google.com/p/selenium/wiki/HtmlUnitDriver
driver = {
def driver = new HtmlUnitDriver()
driver.javascriptEnabled = true
driver
}
environments {Not sure
chrome {-Dgeb.env=chrome
driver = { new ChromeDriver() }
}
firefox {
driver = { new FirefoxDriver() }
}
}
I have the .feature file also in test/functional. The step def's are in test/functional/steps & contains:
package steps
import geb.*
this.metaClass.mixin (cucumber.runtime.groovy.EN)
this.metaClass.mixin (cucumber.runtime.groovy.Hooks)
def browser = new Browser()
Given (~"I am on the front page") {
browser.go("http://localhost:8081/whereisOne")
}
There is also a shared_driver.groovy class in test/functional/setup that was created for the straight Groovy tests, & I'm not sure if this is still needed.
The run config command is: test-app --stacktrace, with VM options: -Dgeb.env=chrome
Any help appreciated, thank you!
Is the -Dgeb.env=chrome in the code? If so, you have to remove this line. Everything else looks fine.

"Test a little, code a little" in Grails project, using Easyb or Spock, in IntelliJ

My original issue is described perfectly by this post: I want to follow TDD:
write a small test
watch it fail
write just enough code to make it succeed
watch it succeed
repeat
I am working on a Grails project in IntelliJ. If all I want is to write normal JUnit tests, the above post solves everything:
Head to /test/unit
Put some test code in a "class Xyz extends GroovyTestCase" class
Hit Shift F10
JUnit report pops up within a second or two
The problem is that I would like to use one of the very cool "describe-in-english" testing setups, like Easyb or Spock.
What do I do? It would be magic to just start with the auto-generated Test class Grails makes for me, then cram Spock stuff into it. Obviously I can't use "extends" twice. Does this give the gist of what I'm trying to do though?
class Xyz extends GroovyTestCase extends spock.lang.Specification {
//void testSomething() {
// fail "Implement me"
//}
def "length of Spock's and his friends' names"() {
expect:
name.size() == length
where:
name | length
"Spock" | 5
"Kirk" | 4
"Scotty" | 6
}
}
Extend spock classes, not groovy's. You can choose from UnitSpec, ControllerSpec, IntegrationSpec and others as listed in source code. Spock will take care of the rest.
-Use "grails install-templates"
-Change the Tests templates to something along the lines of:
#artifact.package#
import grails.test.mixin.*
import org.junit.*
import spock.lang.Specification
#TestFor(#artifact.testclass#)
class #artifact.name# extends Specification {
}
-Write the test code in Spock (or normal JUnit code)
-IntelliJ->Run->Edit Configurations. Add New Configuration of the JUnit type. Test kind: <All in package/All in UnitTest directory/All in one UnitTest class/etc.>
-(Shortcut: Cursor over method name, or class name->ctrl-shift-F10)
On my original question:
In retrospect, I was getting hung-up on the "run all in directory" part of that blog post. Current IntelliJ DOES let you run all JUnit tests in a directory, or the entire project.
Once I understood that, the next step was to realize that Spock tests ARE JUnit tests. Make a "class Xyz extends Specification {}" class in the test/unit directory, fill it with Spock code, and Run as... JUnit. Magic!

Resources