SHARE
TWEET

CasperJS Sample

NikitaMundhada Jul 26th, 2013 560 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /**Run script as:
  2. casperjs meetup_casper_demo.js --app_url=http://dl.dropboxusercontent.com/u/36619447/live-demos/Get-a-fan/Sephora.html.
  3. **/
  4.  
  5. /**Initialization part -- creates a Casper object with the desired options**/
  6.  
  7. var casper = require('casper').create({ verbose:true,
  8.                         logLevel:'info',
  9.                         waitTimeout:10000,
  10.                         /**IMPORTANT:set viewport size as the defualt is too small
  11.                         and it will be detected as mobile device.**/
  12.                         viewportSize:{width: 900, height:700}
  13.                 });
  14.                
  15. /**The app_url was passed as a command line parameter. We read this here**/
  16.  
  17. app_url = casper.cli.get("app_url");
  18. casper.echo("Casper CLI passed options: %s" %app_url);
  19.  
  20.  
  21. /**This starts off the browser on the app_url page(the page on which the Shopsocially app is running in our case)**/
  22.  
  23. casper.start(app_url, function(){
  24. });
  25.  
  26.  
  27. /**This is fired on document.ready() of the app_url page
  28. We first check if the Shopsocially App sidebar is present.Then we click on it.This will open and iframe.
  29. IMPORTANT :If we want to do any activity in an iframe, then we need to switch to it.
  30. In this case, we need to click on the 'Already Liked it' link which is inside an iframe,
  31. we will switch to it **/
  32.  
  33. casper.then(function(){
  34.    this.test.assertExists('#ssmi_getafan_sidebar_image', 'Get-a-Fan App Sidebar loaded.');
  35.    this.click('#ssmi_getafan_sidebar_image a');
  36.    this.page.switchToChildFrame("ssmi_getafan_iframe");
  37.    this.test.assertResourceExists('http://d1qbqkkh49kht1.cloudfront.net/9c193b151ecfad21e78966f30a151abf.jpeg', 'Sephora model image exists');
  38.    this.test.assertExists('#already_liked', 'Clicking on "Already Liked" of Get-a-Fan App.');
  39.    this.click('#already_liked');
  40.  
  41.  /**All communication with remote DOM MUST be done using evaluate function.  Here, I want to see the values of cookies set in remote DOM**/
  42.  
  43.    this.evaluate(function(){
  44.                 alert("Cookies are:" + document.cookie);
  45.         });
  46.    
  47. });
  48.  
  49.  
  50. /**Since there is some time between clicking on 'Already Liked It?' and coupon fetching, we wait for the selector to appear in DOM**/
  51. casper.waitForSelector('#coupon_area > span', function() {
  52.          this.test.assertTextExists('Thank you!', 'Thank you message displayed correctly.');
  53.          this.test.assertSelectorHasText('#coupon_area > span', 'Special-20');
  54.        
  55. });
  56.  
  57.  
  58.  
  59. casper.run(function(){
  60.     /**We dump the test pass and failure information to the terminal**/
  61.        
  62.         require('utils').dump(casper.test.getFailures());
  63.         require('utils').dump(casper.test.getPasses());
  64.         /**We exit PhantomJS and print test results to a file**/
  65.        
  66.         casper.test.renderResults(true, 0, 'test-results.xml');
  67. });
RAW Paste Data
Want to get better at JavaScript?
Learn to code JavaScript in 2017
Pastebin PRO Summer Special!
Get 40% OFF on Pastebin PRO accounts!
Top