Including External Classes

22 views
Skip to first unread message

Aeden Jameson

unread,
May 22, 2013, 2:23:39 PM5/22/13
to casp...@googlegroups.com
How does one include classes from external files that one has written themselves? For example if I have

/login_page.coffee

class LoginPage
  constructor: (@browser) ->


I want to include the LoginPage class from the login_page.coffee file in this test


/when_submitting_the_form_without_the_first_name.coffee


require './login_page.coffee' #Do I do this?

casper.start "http://somesite.com/", ->
  @test.comment("when submitting the form without the first name")

casper.thenClick '.login', ->
   login = new LoginPage(@)
   login.login('username', 'password')


when I use require and run the tests using casper test .... I get an error that the 'LoginPage' variable is undefined. What am I doing wrong?

Aeden





Nicolas Perriault

unread,
May 22, 2013, 3:02:15 PM5/22/13
to casp...@googlegroups.com
On Wed, May 22, 2013 at 8:23 PM, Aeden Jameson <aeden....@gmail.com> wrote:

> when I use require and run the tests using casper test .... I get an error
> that the 'LoginPage' variable is undefined. What am I doing wrong?

This is how to do it:

# /login_page.coffee
class LoginPage
constructor: ->
console.log('plop')

exports.LoginPage = LoginPage

# /test.coffee
{LoginPage} = require './login_page.coffee'

login = new LoginPage() // prints "plop"

++

--
Nicolas Perriault
https://nicolas.perriault.net/
Phone: +33 (0) 660 92 08 67

Aeden Jameson

unread,
May 22, 2013, 3:31:08 PM5/22/13
to casp...@googlegroups.com, nic...@perriault.net
Bless your beautiful soul Nicolas. That worked perfectly. Now, how the the requires work with static variables. For instance say I have

/login_page.coffee
class LoginPage
  @LoginButton:'#login-button'

when I try to access

/test.coffee

casper.echo LoginPage.LoginButton

I get an error "undefined is not an object (evaluating 'LoginPage.LoginButton')

I'm relatively new in the JS ecosystem. Do you have a good resource for how these kinds of import mechanics work?

Aeden
Reply all
Reply to author
Forward
0 new messages