Call inner-class method within Groovy script? - class

I have been searching for an answer for something that seems so trivial...
Anyway, here's my code:
class Test {
def done() {
println("Well done, you've completed the test!")
}
}
def test = new Test()
test.done()
Yet nothing is being printed.

Oh silly boo boo!
I was parsing the script and not evaluating it which led to it not being executed properly...

Related

Flutter & Dart: How to check/know which class has called a function?

I am trying to know which class has called a specific function. I've been looking through the docs for this, but without success. I already know how to get the name of a class, but that is something different of what I'm looking for. I found already something related for java but for dart I haven't. Maybe I'm missing something.
Let's say for example that I have a print function like so:
class A {
void printSomethingAndTellWhereYouDidIt() {
// Here I would also include the class where this function is
// being called. For instance:
print('you called the function at: ...');
//This dot-dot-dot is where maybe should go what I'm looking for.
}
}
class B {
A a = A();
void test() {
a.printSomethingAndTellWhereYouDidIt();
}
}
The output should be something like:
you called the function at: B
Please let me know if there are ways to achieve this. The idea behind is to then use this with a logger, for instance the logging package. Thank you in advance.
You can use StackTrace.current to obtain a stack trace at any time, which is the object that's printed when an exception occurs. This contains the line numbers of the chain of invocations leading up to the call, which should provide the information you need.
class A {
void printSomethingAndTellWhereYouDidIt() {
print(StackTrace.current);
}
}
class B {
A a = A();
void test() {
a.printSomethingAndTellWhereYouDidIt();
}
}
If you are doing this for debugging purposes, you can also set a breakpoint in printSomethingAndTellWhereYouDidIt to check where it was called from.

Screenshot on every failure using Scalatest

I would like to take a screenshot on every fail test in a Spec or Suite using ScalaTest.
The Scala Test website shows how to take screenshots surrounding every code that might fail with this:
withScreenshot {
drive.findElement(By.id("login")).getAttribute("value") should be ("Login")
}
There is this post that tries to explain, but I could not understand what exactly should be done.
I also found the class ScreenshotOnFailure.scala, but could not use it, once it's private and has a package restriction.
Can anyone tell me if there's a way to intercept any failure and then take a screenshot?
Just to have a final answer I'm writing the way I could solve the problem based on the approach from this post mentioned in the question.
In short, the solution ended up like this (pseudo-code).
trait Screenshots extends FunSpec {
...
override def withFixture(test: NoArgTest): Outcome = {
val outcome = test()
// If the test fails, it will hold an exception.
// You can get the message with outcome.asInstanceOf[Failure].exception
if (outcome.isExceptional) {
// Implement Selenium code to save the image using a random name
// Check: https://stackoverflow.com/questions/3422262/take-a-screenshot-with-selenium-webdriver
}
outcome
}
}
class MySpec extends Screenshots {
...
describe("Scenario A") {
describe("when this") {
it("the field must have value 'A'") {
// It will save a screenshot either if the selector is wrong or the assertion fails
driver.findElement(By.id("elementA")).getAttribute("value") should be ("A")
}
}
}
}
From this point on, all Specs that extend the Screenshot trait will intercept errors and save a screenshot.
Just to complement, surrounding areas with withScreenshot(), as mentioned in the question, saves only failure on assertions, but it does not save a screenshot when the test fails due an element not found (e.g. wrong selector).
With the code above, all failures will save a screenshot.

How to programmatically ignore/skip tests with ScalaTest?

I am running some tests with ScalaTest which rely on connections to test servers to be present. I currently created my own Spec similar to this:
abstract class ServerDependingSpec extends FlatSpec with Matchers {
def serverIsAvailable: Boolean = {
// Check if the server is available
}
}
Is it possible to ignore (but not fail) tests when this method returns false?
Currently I do it in a "hackish" way:
"Something" should "do something" in {
if(serverIsAvailable) {
// my test code
}
}
but I want something like
whenServerAvailable "Something" should "do something" in {
// test code
}
or
"Something" should "do something" whenServerAvailable {
// test code
}
I think I should define my custom tag, but I can only refer to the source code of in or ignore and I do not understand how I should plug in my custom implementations.
How should I accomplish this?
You can use assume to conditionally cancel a test:
"Something" should "do something" in {
assume(serverIsAvailable)
// my test code
}
or you can test for some condition yourself, and then use cancel:
"Something" should "do something" in {
if(!serverIsAvailable) {
cancel
}
// my test code
}
You can use Tags to achieve this:
Documentation on how to use Tags : http://www.scalatest.org/user_guide/tagging_your_tests
Adding and removing tagged test with command line parameters: http://www.scalatest.org/user_guide/using_the_runner#specifyingTagsToIncludeAndExclude
Example Code:
import org.scalatest.Tag
object ServerIsAvailable extends Tag("biz.neumann.ServerIsAvailable")
"Something" should "do something" taggedAs(ServerIsAvailable) in {
// your test here
}
Running the tests
Running the tests is a bitt tricky. It only works for testOnly and testQuick not test.
In the example testOnly is short for testOnly *
sbt "testOnly -- -l biz.neumann.ServerAvailable"
Here is some trick to skip a test based on a condition:
object WhenServerAvailable extends Tag(if (serverIsAvailable) "" else classOf[Ignore].getName)
"Something" should "do something" taggedAs WhenServerAvailable in { ... }

Scala: Generate a block that conditionally runs another block

In the Circumflex framework, you can map an URL to a block like this:
get("/foo") = {
"hello, world!"
}
which, when browsing to /foo, will show the given string as expected. Now, to write a complete web application, you almost always need some form of authentication and authorisation. I'm trying to write some kind of wrapper for the above construct, so I can write this:
get("/foo") = requireLogin {
"hello, world!"
}
The requireLogin method would then check if the user is logged in, and if yes, execute the given block. If not, however, it should do a redirect to the login page.
Now I somehow can't get the syntax right (i'm still a Scala newbie). How would you do this in a generic fashion?
Try something like this:
def executeMaybe[A](work: => A): Option[A] =
if (util.Random.nextBoolean)
Some(work)
else
None
This executes the passed code with probability 0.5, returning Some(<result delivered by work>), or returns None is the other cases. You can call it either like this:
val v = executeMaybe(42)
or with block notation:
val v = executeMaybe {
// do some work
// provide return value
}
The trick is to use a by-name parameter, signalled by the => symbol. Read more e.g. here: http://daily-scala.blogspot.com/2009/12/by-name-parameter-to-function.html
The way I asked it, Jean-Philippe's answer is correct.
But here's some information specific to Circumflex:
In the Circumflex RequestRouter, the following can be used to implement the required method:
def requireLogin (f: => RouteResponse ): RouteResponse = {
if(loggedIn) {
return f
}
else {
return sendRedirect("/login")
}
}
The reason behind this was getting clear with the hint from Jean-Philippe's answer, and once I remembered that the following call isn't an assignment of a block to some internal data, but is mapped to another method call instead.
So, the call
get("/") = {...}
is actually mapped to this:
get.update("/", {...})
The block is passed in as a By-Name parameter, so the return value of requireLogin must be the same - which, for Circumflex, is RouteResponse, and not a function.
You also can use j2ee container authentication with <login-config> and <security-constraint> stuff inside web.xml

How can I call a routine automatically when the run ends in specman?

Is there any way to specify that a function should be called when a test ends in Specman?
I'm looking for something similar to C's atexit().
haven't tried my self, but you should probably have a look at the finalize() or quit() methods which are defined for any_struct. You could try to extend it for sys.
extend sys {
finalize() is also {
// ...
};
};
Cheers,
Daniel