Hi we run Geb tests on the Spock framework. I am trying to take a screenshot in a module using report "Screenshot". It does not recognize the report function as it does on a Spec. How should I go about taking screenshots in a module.
Here is a example code which is in a module.
try{
$(By.xpath("//button[#ng-click=\"ok()\"]")).click()
}
catch (Throwable t){
failures.add("\n Could not click on the Ok button after the Ticket created successfully message appeared")
report "Failure"
}
The report() method is available on Browser class and an instance of that class is available as browser property inside of modules, so you can call it from within a module using:
browser.report("Failure")
Use the parent class GebReportingSpec (instead of GebSpec) to create a report of all your tests.
When you only want a screenshot of failing tests, use the config value reportOnTestFailureOnly=true in your GebConfig.groovy
Related
I am using the Spock Reports extension in a Geb test. I am trying to find if there is any way to get the logging messages to display in the Spock report.
I have something roughly like this:
#Slf4j
class SpockReportExample extends GebReportingSpec {
def expectedVar = "5"
when: "I click the button."
button.click()
then: "The new value is displayed."
def value = formElement.value()
value==expectedValue
log.info("The new value is $value")
}
I'd like to see the the log stating the actual value to be output in my spock report, but I can't find a way. I've tried sending the log at info, warning, debug, error, and trace levels but no luck. Is this possible?
If you happen to use Athaydes Spock Reports you can write messages to the report by using
reportInfo "This is a message"
See https://github.com/renatoathaydes/spock-reports#how-to-use-it
This is possible since V1.4.0
Don't use #Slf4j just use println. spock, junit, and so on capture standard out and add it to the test results.
I have Jenkins ver. 2.7.4 and I want to see custom messages in report
besides stack trace. What do I need to do for this?
If you are writing a jenkins plugin and you've subclassed Notifier, then you can log to the build output using an instance of BuildListener, like so:
Helper method:
private void logger(BuildListener listener, String message){
listener.getLogger().println(message);
}
Example Usage:
logger(listener, "Verbose Logging Enabled");
You can see a real world example of this in the source code for the packagecloud plugin for jenkins
See: https://wiki.jenkins-ci.org/display/JENKINS/Packagecloud+Plugin
Problem statement:
How to execute a function at the end of all specification files have been executed using spock framework.
Explantion: I am using geb-spock framework for automation.
I have few specification files. I want to run a function after all specification files have been executed.
I want something like AfterSuite in TestNG. How can i get the feature of AfterSuite in spock. cleanupSpec will be called after every specification file is executed.
Thanks,
Debasish
The simple answer is: no. There's nothing like before or after suite methods in spock since spock is JUnit based and JUnit does not handle such methods. If you siÄ™ tool like maven or gradle maybe you can use task's lifecycle methods.
You can use the JUnit 4 SuiteRunner Look at this answer
#RunWith(Suite.class)
#SuiteClasses({ TestSpec.class, TestSpec.class })
public class CompleteTestSuite {
#BeforeClass
public static void setUpClass() {
System.out.println("Master setup");
}
#AfterClass public static void tearDownClass() {
System.out.println("Master tearDown");
}
}
If you are using build tool as for example Gradle you may wire it on a build configuration level after tests are finished.
In Grails 2.0.4, I'm trying to write a controller unit test which invokes the static SpringSecurityUtils.reauthenticate. The test returns a NullPointerException on that invocation. In a debugger, I can see that none of the Groovy dynamic properties (declaredMethods,etc.) of SpringSecurityUtils are populated.
I do note that when running the tests, the "Configuring Spring Security Core" log message is emitted after the unit-test failure. Here is a sample test:
class ReproTest {
void testSpringSecurityUtils() {
String.valueOf(true) // OK: a public final class from the JDK
URLUtils.isRelativeURL("foo") // OK: a class from another plugin
SpringSecurityUtils.reauthenticate "user", "pw" // fails, NPE
}
}
My initial reaction is that maybe plugins aren't accessible during unit tests, but if so, why is the URLUtils call working? And why does the test get "far enough" to initialize the plugin, but after the tests have completed?
For a unit test the container isn't starting up. No Spring injection or "grails goodness" is happening. You see in the logs that the plugin is initializing after the unit tests run, because it [container] does start for the integration tests. If you want to test the SpringSecurityUtils, although guessing it is already tested properly in the plugin, you would want to write an integration test.
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!