0

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

B_B
  • 95
  • 1
  • 9
  • the whole point of this one is so you can re-use your step def in different feature file – B_B May 10 '16 at 09:32
  • Haven't tried but I think you could use 'send' as: expect(@common_page.send(@ref).text).to eq arg1. – Sam May 10 '16 at 15:09
  • i have tried that one but it did not work its saying "undefined stackover" – B_B May 10 '16 at 16:14

1 Answers1

2

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.

Thomas Walpole
  • 48,548
  • 5
  • 64
  • 78
  • Thank you for your respond. I have tested both of them they work fine. I will try to remove all the .text and eq and change them – B_B May 10 '16 at 19:04