Search code examples
ruby-on-railsrubycucumbercapybarasite-prism

What's the best way to reuse your step definition in ruby siteprism


im having some issue trying to create a reusable step definition using siteprism let say feature file is

Given that im on the site
Then i should see a "stack over" text
And i should see a "ask" text
And i should see a "question" text

then on my step definition will be

I want to have arg1 to be dynamic and this logic will check if its true

Then (/^i should see a "(.*?)" text$/) do |arg1|
@common_page = CommonLib.new
@ref = arg1.gsub(/\s+/,'')
expect(@common_page.*@ref*.text).to eq (arg1)
end

Then on my page def will be

class CommonLib < siteprism::page

element :stackover, "#text_header"
element :ask, "#text_ask"
element :question, "#text_question"

the issue im having is this expect(@common_page.@ref.text).to eq (arg1)

the mapping is wrong @ref need to use the data it got like 'stackover', 'ask' and 'question' and map in the CommonLib page def


Solution

  • Calling #text and using the eq matcher is generally a bad idea since it bypasses Capybaras builtin retry behavior and can cause flaky tests on dynamically changing pages. Instead you should use have_text or the :text option passed to the finder

    expect(@common_page.send(@ref)).to have_text(arg1)
    

    or

    expect(@common_page.send(@ref, text: arg1)).to be
    

    Also, is there a reason you've made @common_page and @ref instance variables, they seem like they should just be regular variables that go out of scope at the end of the test.