Menu
Home
Log in / Register
 
Home arrow Computer Science arrow Building Applications with Scala
Source

IntegrationSpec.scala

We can test our main page to check if it is okay. This is a very simple test, and gets you ready for the next tests. So, here we go.

class IntegrationSpec extends PlaySpec with OneServerPerTest with OneBrowserPerTest with HtmlUnitFactory {

"Application" should {

"work from within a browser" in {

go to ("http://localhost:" + port)

pageSource must include ("Welcome to Reactive Web Store")

}

}

}

This test is very easy. We just call the main page, and we check if it contains the text

Welcome to Reactive WebStore. Let's run it.

$ activator "test-only IntegrationSpec"

The result after running this test is shown in the following image:

ProductControllerTestSpec.scala

Now we will look at the product controller test spec:

class ProductControllerTestSpec extends PlaySpec with OneServerPerSuite with OneBrowserPerSuite with HtmlUnitFactory { "ProductController" should {

"insert a new product should be ok" in {

goTo(s"http://localhost:${port}/product/add") click on id("name") enter("Blue Ball") click on id("details")

enter("Blue Ball is a Awesome and simple product")

click on id("price")

enter("17.55")

submit()

}

"details from the product 1 should be ok" in {

goTo(s"http://localhost:${port}/product/details/1") textField("name").value mustBe "Blue Ball"

textField("details").value mustBe "Blue Ball is a Awesome and simple product"

textField("price").value mustBe "17.55"

}

"update product 1 should be ok" in {

goTo(s"http://localhost:${port}/product/details/1") textField("name").value = "Blue Ball 2"

textField("details").value = "Blue Ball is a Awesome and simple product 2 "

textField("price").value = "17.66" submit()

goTo(s"http://localhost:${port}/product/details/1") textField("name").value mustBe "Blue Ball 2" textField("details").value mustBe "Blue Ball is a Awesome and simple product 2 "

textField("price").value mustBe "17.66"

}

"delete a product should be ok" in {

goTo(s"http://localhost:${port}/product/add") click on id("name") enter("Blue Ball") click on id("details")

enter("Blue Ball is a Awesome and simple product")

click on id("price")

enter("17.55")

submit()

goTo(s"http://localhost:${port}/product") click on id("btnDelete")

}

}

}

So, for the product controller, we simulate a web browser using Selenium Play's framework support. We test basic controller functionality such as inserting a new product, details for a specific product, and updating and removing a product.

For insert, we go to the new product form using goTo. We use $port as a variable. We do this because the Play framework will boot up the application for us, but we don't know in which port. So we need to use this variable in order to get to the product controller.

Then we click on each text field using the click function, and we enter values using the enter function. After filling in the whole form, we submit it using the submit function.

For details, we just go to the product details page, and we check if the text fields have the values that we are expecting. We do that using the textField.value function.

In order to check the product update function, we need to first update the product definition, and then go to details to see if the values we changed are there.

Finally, we test the delete function. For this function, we need to click on a button. We need to set the ID of the button in order to have this working. We need to do a small refactoring in our UI to have the ID there.

 
Source
Found a mistake? Please highlight the word and press Shift + Enter  
< Prev   CONTENTS   Next >
 
Subjects
Accounting
Business & Finance
Communication
Computer Science
Economics
Education
Engineering
Environment
Geography
Health
History
Language & Literature
Law
Management
Marketing
Mathematics
Political science
Philosophy
Psychology
Religion
Sociology
Travel