0

I am new to PageObject (and ruby)...

I am having issues handling the Alert popup when user ID is not provided.

Here is the code and scenario:

When I click 'sign in' button without user ID then I am expecting an alert with a message, which I am verifying in my script.

currently I get an error: NoMethodError: undefined methodalert' for nil:NilClass`

cucumber Then statement: Then I should see an popup message 'enter card number'

step definition: `

Then(/^I should see an popup message "([^"]*)"$/) do |popup_msg|
  on(LoginPage).alert_msg
end

***Page object code:***
class LoginPage
  require_relative 'common'

  include PageObject
  include Common

  text_field(:collector, :id => 'j_col')
  text_field(:password, :id => 'j_password1')
  button(:sign_in, :value => 'Sign in')


  def login_to_nectar (collector = FigNewton.collector.colid, password = FigNewton.collector.password)
   self.collector = collector
   self.password = password
   sign_in
  end

  span(:pwd_error_msg, :css => 'p.error-message')


  def alert_msg
    message = @curr_page.alert do
      sign_in.click
    end
    message.should == 'enter card numbe'
    @alert_msg
  end

end

`

....LATEST INFO....progress

@Johnson thanks...now I am getting the error:

Selenium::WebDriver::Error::UnhandledAlertError: Modal dialog present: "enter card number. "

I can see that PageObject is handling the popup. My new code below:

def alert_msg
    alert do
      self.sign_in.click
    end
    @alert_msg
end

I call this in step definition in the step before verifying the message as so....

When(***without a card number$/) do
  on(HomePage).click_login
  on(LoginPage).login_to_nectar('', FigNewton.collector.password)
  @current_page.**alert_msg**
end

Then***
  on(LoginPage).**alert_msg**.should == popup_msg
end
Asif
  • 1
  • 3
  • Johnson thanks for pointing me in the right direction...however now I am getting the following error: – Asif Sep 30 '14 at 07:40
  • Is the question answered now? I'm unclear as to what the status is. – Jared Darling Oct 01 '14 at 22:51
  • Yes it is...After the initial changes I made and was getting the modal related error. I realized in my page class my login_to_nectar method was calling 'sign_in' element, so in effect this was being called twice (2nd time in alert_msg method). I took it out of 'login_to_nectar method and all worked. – Asif Oct 03 '14 at 08:13
  • If none of the other answers below fit, maybe put what you just said in an answer and accept it. This helps others who come along and view your question. – Jared Darling Oct 03 '14 at 17:37

2 Answers2

1

Error message NoMethodError: undefined method 'alert' for nil:NilClass means that your @curr_page is nil, and thus does not have a defined method of #alert. Take a look at where you're defining this instance variable. It is the culprit.

Johnson
  • 1,510
  • 8
  • 15
0

After the initial changes I made and was getting the modal related error. I realized in my page class my login_to_nectar method was calling 'sign_in' element, so in effect this was being called twice (2nd time in alert_msg method). I took it out of 'login_to_nectar method and all worked. Thanks..

Asif
  • 1
  • 3