I am new to automating tests using Ruby (in combination with Selenium) and struggling to use the name of a constant instead of its value in my test_steps.rb.
I'm using the 3 files below:
LoginPage.rb
:
class LoginPage < Base
FIELDS = {
'Username' => '//form[@id="login"]//input[@name="j_username"]',
}
Base.rb
:
def fill_field(field, value)
find(:xpath, field, wait: 15).set(value)
#logger("Filling field with xpath '" + field + "' with value '" +
value + "'.") > this works fine but prints the xpath
p "field: " + field
log = field.scan(/\w+(?:'\w+)*/).last
logger("Filling field '" + log + "' with value '" + value + "'.")
test_steps.rb:
And(/^logs in as user "([^"]*)"$/) do |userName|
$login_name = search_excel('testdata/accounts.xlsx', userName, 1)
@pg.fill_field(LoginPage::FIELDS['Username'], $login_name)
When executing the scenario I see this in my command prompt:
Given a user is on the oCRM login page # features/step_definitions/test_steps.rb:13
"field: //form[@id=\"login\"]//input[@name=\"j_username\"]"
28/12/2016 13:03:50 Filling field 'j_username' with value 'D0X02703'.
Note that the line in Base.rb
prints the value for the constant matching "field"
'//form[@id="login"]//input[@name="j_username"]'
I would like it to print the constant itself: "LoginPage::FIELDS['Username']"
. The target is to print this in my logging:
"Filling field 'Username' with value 'DOX02703'."
I've tried printing "field.name"
, "field.value"
and played around with local_variables
, but no success yet. Help would be very welcome!
Also tried the other way around with definition below but ofcourse then it won't pick up the constant value and look for xpath "LoginPage::FIELDS['Username']"
:
def fill_fields(page, type, field, value)
find(:xpath, page + "::" + type + "['" + field + "']", wait: 15).set(value)