0

I just started to use ScalaTest and I want to do some initialisation before my feature test.

my test looks something like this:

class InvoiceTests extends FeatureSpec with GivenWhenThen with ShouldMatchers {



    feature("Traffic Light Test") {

//this is where I want to initialise

        scenario("test 1") {
            val something = Invoice(bla bla bla)

            Given("A connection to the system")
            login.isConnected shouldBe true

            When("an invoice that was uploaded to the system")
            val invoiceAction = new InvoiceActions(driver,conf.getString("web.environment"))
            invoiceAction.uploadInvoice(something)

            And("the invoice is processed with information to be ready to reclaim")
            invoiceAction.fillInvoice(something)

            Then("Inovice status should be ready to reclaim")
            invoiceAction shouldBe 'isReadyToReclaim
        }

       scenario("test 2") {
            val something = Invoice(bla bla bla)

            Given("A connection to the system")
            login.isConnected shouldBe true

            When("an invoice that was uploaded to the system")
            val invoiceAction = new InvoiceActions(driver,conf.getString("web.environment"))
            invoiceAction.uploadInvoice(something)

            And("the invoice is processed with information to be ready to reclaim")
            invoiceAction.fillInvoice(something)

            Then("Inovice status should be ready to reclaim")
            invoiceAction shouldBe 'isReadyToReclaim
        }

}

So I have 2 scenarios here, test 1 and test 2, but I want to for them to have the same driver object and to login once before the whole feature starts. so where do I put this initializations code:

val driver = new FirefoxDriver()
    val conf = ConfigFactory.load()
    val loginObj: LoginActions = new LoginActions(driver, conf.getString("web.environment"))
    loginObj.login(conf.getString("user.nir.username"),conf.getString("user.nir.pass"))

cause I cannot put it between the feature and the first scenario.

thanks!

JohnBigs
  • 2,691
  • 3
  • 31
  • 61

2 Answers2

1

You can put it between the feature and scenario, right where you want it, but make it a lazy val. That way it will be in scope in both scenarios, but only initialized upon first use, when the first scenario is executed.

Trouble with that though is don't you need to close it at the end? If so, you can move it to the top level, keep it lazy, then use BeforeAndAfterAll's afterAll() method to close it. (And if you have other features that lazily initialize resources, your afterAll() method can close those as well.)

Bill Venners
  • 3,549
  • 20
  • 15
0

According to documentation for FeatureSpec http://doc.scalatest.org/2.2.6/#org.scalatest.FeatureSpec There are two traits that assume to provide such functionality: BeforeAndAftereEach & BeforeAndAfterAll

But they will not work in your case, as one of them executes before each scenario, while another execute only once in a test class.

On the same page they suggest implement method (withDriver for you case, and invoke it explicitly at first line of feature).

vvg
  • 6,325
  • 19
  • 36