user2067331
user2067331

Reputation: 31

cucumber: how to enter text from keyboard into textfield?

i am using type_into_keyboard("abc") to enter text into textfield named as "username" from keyboard but it is not working.

Please help me out.

Thanks in advance.

Upvotes: 2

Views: 5084

Answers (2)

Chathura Palihakkara
Chathura Palihakkara

Reputation: 934

First you need to add this custom function to the "_steps.rb" file

Then /^I enter constant text "([^\"]*)" to text field number (\d+)$/ do |texts,index|

    index = index.to_i
    screenshot_and_raise "Index should be positive (was: #{index})" if (index<=0)
    clearTextfield(index-1)
    enterDataToTextfieldNum(texts, index-1)
    sleep(STEP_PAUSE)
end

def enterDataToTextfieldNum(text, index)
  index = index.to_i
  text = text.to_s
  if index>=0
        sleep(0.2)
        touch("textField index:#{index}")
        sleep(0.4)
        await_keyboard
        sleep(0.2)
        keyboard_enter_text("#{text}")
        sleep(0.2)
        done
        sleep(0.3)
  end
end

def clearTextfield(index)
    index = index.to_i

    if query("textField index:#{index}",:text).first
      length=query("textField index:#{index}",:text).first.length
    else
      length=0
    end
    if (index>=0)&&(length>0)
        touch("textField index:#{index}")
        await_keyboard
      for i in 0..length-1
        keyboard_enter_char("Delete",false)
      end
    end
end

Then you can call it in calabash feature file

Then I enter constant text "Old road" to text field number 3 

here i used textfileld number but if you want you can edit it to use text field name. If you need help on that please let me know. Thanks.

Upvotes: 1

fangmobile
fangmobile

Reputation: 859

use

Then I enter "abd" into the "username" input field

Upvotes: 1

Related Questions