Writing your first test using Casperjs

Casperjs is a wonderful open source navigation, scripting and testing utility written in JS which uses PhantomJS webkit , which is a headless browser. It simplifies the process of defining a full navigation scenario and provides some high level methods for doing tasks such as – entering text, clicking, logging, navigation and logging events.

I had to spend some days to get my first test case running, so here I am making my notes which will benefit me in the longer run. ( In case you have more info, please leave a comment)

1. Download and set your environment paths for casperjs and phantomjs ( refer – previous post)

2. Create a folder say C:\JSplayground ( in my case) which will house your casperjs cases.

3. You can either use VS to write your new casperjs script or even a simple tool notepad++ will suffice.

4. Below is the code, I used, which you can also use to get your first case running. It’s pretty straight forward thing. You can copy the code and create a file with extension .js . In my case – sampletest.js

 

5. From the above code, If you have not figured out already, setting the capturePage as true or false drops the screen shot of the application under test in the same folder as your .js file. Ensure that the casper.userAgent is set to the same , as I was searching for long since some websites render and open differently for different browsers.

6. Once you have created your .js file, you can run now this script from your location by using the below command

 

7. I have given the option of using –ignore-ssl-errors=yes –ssl-protocol=any  so that even if your url is https:// your tests will run. BTB, Phantomjs had really bad support for SSL until 2.0 release. ( unfortunately I am on a newer version of Phantomjs and Casperjs is not equipped to handle 2.0. However, thanks to rdpanek , he has released a fix for CasperJS bootstrap/ Phantomjs 2.0 fix)

screenshot_Wed_Feb_18_10.34.54

Phantomjs & Casperjs – Installation Simplified

To run Phantomjs & Casperjs on your system, here is the installation steps you need to follow. Though these are pretty simple ones, but the lack of proper documentation can actually send you on a leather hunt just to get your first case running. So here it is simplified.

1. Download Phantomjs from here.

2. Unzip the entire package to say c:\phantomjs . Ensure that the phantomjs.exe is copied from the subfolder bin to man folder. So your final folder should look somewhat like that.

screenshot_Tue_Feb_17_21.52.36

3. Now head start –> right click my computer, choose properties –> Advanced system properties – > select advanced tab and click on Environment Variables.  Append ;C:\phantomjs to your PATH environment variable. Feel free to modify your installation and update the PATH settings to reflect the same

screenshot_Tue_Feb_17_21.56.04screenshot_Tue_Feb_17_21.58.31

4. Now download Casperjs from their home page

5. Extract the contents to c:\casperjs

6. Ensure you add this PATH into your environment as shown above.

7. Restart system and try these two commands to make sure the PATHs are properly set.

phantomjsscreenshot_Tue_Feb_17_22.06.07

You are now all set!

Adding code snippets to blogger using windows live writer

Finally the day has come where I am able to use Windows live writer ( by far the best free blogging client) along with code snippets. After some research , I found a simple work around as most of the content available was for the wordpress platform.

Head over to https://gist.github.com and create a free account. Once you do that, create a new public Gist as in the snapshot below

screenshot_Tue_Feb_17_23.12.35

screenshot_Tue_Feb_17_23.13.33

Copy the Embed URL into the source view of the Windows live writer.

VB script code to keep the system alive and prevent locking

Use the following code: (Save it as .vbs and execute, it will run for ever). Kill the wscript from the task manager when u want to stop.


----
You can replace the NUMLOCK with either CAPSLOCK if it creates issues with your UI testing

www.CodeNirvana.in

Powered by Blogger.

Translate

Total Pageviews

Copyright © T R I A G E D T E S T E R