Showing posts with label Grails. Show all posts
Showing posts with label Grails. Show all posts

Monday, May 13, 2013

Take photo by WebDriver in Grails

Some steps:
1. Download Selenium
2. Code to take photo

Start.
1. Download Selenium.
Please access this link to download selenium.http://code.google.com/p/selenium/downloads/detail?name=selenium-server-standalone-2.32.0.jar&can=2&q=
After that copy it to lib folder of grails.

2. Code to take photo.

WebDriver driver = new FirefoxDriver();
driver.get(grailsApplication?.config.grails.serverURL + "/${nickname}/profile");
File file = driver.getScreenshotAs(OutputType.FILE);
driver.close()
println(file.toString());


Good luck to you.

Wednesday, January 2, 2013

Coverage in Grails with IntelJ

All step to run coverage in IntelJ
Step1: Click right and choose Run .. with coverage

IDE will run test after that ask you. you want show coverage

Step2: Click Add to Active Suites button



Step3: Check Coverage

With line red is test not run here. line green test run here.
You can show report in bottom of IDE

Good luck to you

Thursday, December 6, 2012

Integration Test in Grails

To write code Integration Test we need 2 steps:
- Create new Object and Save them to database
- Create some functions test
  • Send request and receipt response.
  • Create new functions test require parameters
* Some special cases
And run by command line
grails test-app -integration -Dgrails.env=test ProfileServiceControllerTests

Step 1:
Create new object and save them to database. Please create object relation and save them before you save main object.

Example:
Students is main object relate with Subjects. Please create new Subjects and save it before you save Students object.

Step 2: Create some function tests
Send request by controller


def controller = new FeedServiceController()
controller.response.reset()      
        
controller.request.requestMethod = HttpMethod.GET
controller.params.sessionToken =  sessionToken
        
controller.feedBySessionToken()
JSONElement responseJSON = controller.response.json
        
println responseJSON.toString(4)

1. New controller and reset all response.
2. Set params for request.
3. Call function of controller
4. Get response and check from response.

* Some special cases:
1. Post file for controller to test upload file


 File testFile = new File("web-app/images/favicon.ico")
 MultipartFile file = new MockMultipartFile("avatar", 'avarta.ico', 'image/ico', testFile.readBytes())

 def controller = new ProfileServiceController()
 controller.response.reset()

 //Tets for vartar empty
 controller.request.setMethod("POST");
 controller.params.sessionToken = sessionToken
      
 controller.updateAvatar()
 JSONElement json = controller.response.json

New file by File and MultipartFile (note: 'avarta' word is field name that controller getFile. controller.getFile('avarta'))

New controller and send request with params is token

2. Test with function have token
- Send request to get token
- Send other request to get response to test
(You must reset response and request params by 2 function.  controller.response.reset() and controller.params.clear())





Unit Test in Grails

To test Unit Test we need 2 steps:
- Create new Object in setUp function.
- Create some function tests.

Step 1:
In this example, I create class from abstract class. With class Feed is abstract class.


void setUp() {
        feed = [jsonForRelatedEntity: { -> 
                [
                    id: 1,
                    title: "Feed Title", 
                    date: new Date('2010/10/11')                 
                ]
            }] as Feed
    }

Step 2:
Create some function test.


/**
    * Test function json
    * @author tien.nguyen
    */
    void testJSON() {
        def json = feed.json()
        
        assertEquals "Feed Title",  json.title
        assertEquals new Date('2010/10/11'), json.date        
        
        println json
    }

Good luck to you.

Tuesday, November 27, 2012

Install Grails in MacOS

To install Grails in MacOS we need
1. Java install in MacOS
2. Download Grails and add enviroment for Grails and Java
3. Testing

Now we start
1. Install java in MacOS
Go to Launchpad in bottom menu of Mac and search Java you will Java Preference and install it
Or you go to top menu of Mac choose item menu Go -> Utilites and search Java Preference.

Install it

2. Down load grails
Please go http://grails.org/ to lastest version of grails. You extract to a folder that you want run grails

Create variable environment by terminal.
sudo su
vim .bash_profile

Add list code

export GRAILS_HOME=/Users/kissconcept/Software/grails-2.1.1
export PATH=$PATH:$GRAILS_HOME/bin
export JAVA_HOME=$(/usr/libexec/java_home)
export PATH=$PATH:$JAVA_HOME/bin

After run command line grails

Now you finish install grails

3. Testing
You can test by command line
java -version
grails -version


Good luck to you

Tuesday, November 13, 2012

Grails - Connect with mysql

Default Grails connect with HSQLDB. But we need connect mysql
Go to conf folder. Change file DataSoure.groovy


dataSource {
    pooled = true
    driverClassName = "com.mysql.jdbc.Driver"
    username = "root"
    password = "password"
    dialect = "org.hibernate.dialect.MySQL5InnoDBDialect"
}

Go to environment change url database
url = "jdbc:mysql://localhost:3306/racetrack_dev"

Go to file BuildConfig.groovy
uncomment runtime dependencies with mysql 
When run grails will download mysql driver.

Check mysql database

Good luck to you


Monday, November 12, 2012

Grails - Some Command Line

Run app
grails run-app

Run Test app
grails test-app 

Deploy app
grails war
grails dev war (with dev is environment variable)

Change config server
-server -Xmx512M -XX:MaxPermSize=256m

Generate all file for class
grails generate-all hellnetbean.Book //note need project name in header

Create domain
grails create-domain-class book

Lesion 1: Introduction about Groovy on Grails

What is Grails ?
The same Ruby on Rails. Grails is web server to run application by groovy. You do not choose web server to run application Grails. Default will choose Jetty Serverlet.
Grails auto use database HSQLDB this is database rude java.

What is Groovy?
Groovy is programming language was inherit from java. It don't replace Java that it is support java. It help some java developer short their coding.

Install Grails?
Download Grails from http://grails.org/Download.
- Extract file that you download
- Set Environment variable JAVA_HOME and GRAILS_HOME: Please go to /etc/environment add JAVA_HOME and GRAILS_HOME (is path go to folder content java and grails that you extract). Remember set PATH for JAVA_HOME and GRAILS_HOME go to bin folder. Continue run "source   /etc/environment" to update new environment variable.
(Please see introduction in link from Grails: http://grails.org/Installation)

Create new application
Create new folder and use command line
grails create-app {project-name}
Grails will auto generate structure project.

Run application
Please go to folder your project. run command line grails run-app after open browser with domain http://localhost:8080/{project-name}