verify text using OR statement

51 views
Skip to first unread message

Litha K

unread,
May 17, 2012, 5:24:18 PM5/17/12
to Watir General
Hi all,

In watir-webdriver, is it possible to do something like:

browser.text.should == 'foo' or 'bar'

and should fail if its anything other than 'foo' 'bar'

Any thoughts please?

Thanks,
Jay

Anthony Hallett

unread,
May 17, 2012, 7:19:39 PM5/17/12
to watir-...@googlegroups.com
try
browser.text.should == 'foo' || browser.text.should == ''bar' 

Oscar Rieken

unread,
May 17, 2012, 8:14:15 PM5/17/12
to watir-...@googlegroups.com
I kind of don't understand why you would have a test that could be one thing or could be something else why not just have scenarios that check for each thing

but then again I am more of the thought one assertion per test/step



John Fitisoff

unread,
May 17, 2012, 9:43:39 PM5/17/12
to watir-...@googlegroups.com
Not sure what you're using but regular expressions are a really good way to do this sort of thing concisely. This one matches foo or bar anywhere in the text string but you can be as selective as you need to:

browser.text =~ /(foo|bar)/



From: Litha K <lith...@googlemail.com>
To: Watir General <watir-...@googlegroups.com>
Sent: Thursday, May 17, 2012 2:24 PM
Subject: [wtr-general] verify text using OR statement
--
Before posting, please read http://watir.com/support. In short: search before you ask, be nice.

watir-...@googlegroups.com
http://groups.google.com/group/watir-general

Litha K

unread,
May 18, 2012, 4:26:29 AM5/18/12
to Watir General
Great thanks guys!!

I know its kind of bit weird, but this is just a stub page where it
returns foo or bar. Anything else is an error.

Regards,
Jay



On May 18, 2:43 am, John Fitisoff <jfitis...@yahoo.com> wrote:
> Not sure what you're using but regular expressions are a really good way to do this sort of thing concisely. This one matches foo or bar anywhere in the text string but you can be as selective as you need to:
>
> browser.text =~ /(foo|bar)/
>
> ________________________________
>  From: Litha K <litha...@googlemail.com>
> To: Watir General <watir-...@googlegroups.com>
> Sent: Thursday, May 17, 2012 2:24 PM
> Subject: [wtr-general] verify text using OR statement
>
> Hi all,
>
> In watir-webdriver, is it possible to do something like:
>
> browser.text.should == 'foo' or 'bar'
>
> and should fail if its anything other than 'foo' 'bar'
>
> Any thoughts please?
>
> Thanks,
> Jay
>
> --
> Before posting, please readhttp://watir.com/support. In short: search before you ask, be nice.
>
> watir-general@googlegroups.comhttp://groups.google.com/group/watir-general
> watir-genera...@googlegroups.com

Oscar Rieken

unread,
May 18, 2012, 10:58:34 AM5/18/12
to watir-...@googlegroups.com
I can understand that the page can be one or the other but when you are running your test you should know which one you will see
if its all left up to chance I would think it would make things harder to actually have confidence in your tests whats you say you dont end up with a line like
browser.text.should =~ /(foo|bar|baz|biz|buz)/

how much value does that actually provide?

I would probably do something like

if i was using cucumber:

strange_webpage.feature

Scenario: I should see foo
Given I visit "/"
Then I should see "foo"

Scenario: I should see bar
Given I visit "/"
Then I should see "bar"

step_definition.rb

Then /^I should see "([^"]*)"$/ do |expected_result|
#<< but i would probably find out what the element really is and not just that some text exist in the page
#something like
#browser.div(:id => 'something').text
  browser.text.should == expected_result
end

if I was using rspec:

describe 'Simple page assertions' do
  it 'should display foo' do
    browser.goto 'www.strangepage.com'
    browser.text.should == 'foo'
  end

  it 'should display bar' do
    browser.goto 'www.strangepage.com'
    browser.text.should == 'bar'
  end
end


Chuck van der Linden

unread,
May 18, 2012, 12:11:09 PM5/18/12
to watir-...@googlegroups.com
I have to agree with Oscar here.  A stub that returns one of two things ought to have some primative logic as to what it returns when.  If it's returning two different things, randomly that seems highly un-usual and it's difficult for me to see where that would be providing value (e.g. why in the world go to the effort of randomizing what is returned by a stub, it's not at all useful for unit testing, or nearly any kind of testing for that matter, and that's the primary reason to go to the effort to create a stub in the first place) 

On Fri, May 18, 2012 at 4:26 AM, Litha K <lith...@googlemail.com> wrote:
Great thanks guys!!

I know its kind of bit weird, but this is just a stub page where it
returns foo or bar. Anything else is an error.

Regards,
Jay



On May 18, 2:43 am, John Fitisoff <jfitis...@yahoo.com> wrote:
> Not sure what you're using but regular expressions are a really good way to do this sort of thing concisely. This one matches foo or bar anywhere in the text string but you can be as selective as you need to:
>
> browser.text =~ /(foo|bar)/
>
> ________________________________
>  From: Litha K <litha...@googlemail.com>
> To: Watir General <watir-general@googlegroups.com>

> Sent: Thursday, May 17, 2012 2:24 PM
> Subject: [wtr-general] verify text using OR statement
>
> Hi all,
>
> In watir-webdriver, is it possible to do something like:
>
> browser.text.should == 'foo' or 'bar'
>
> and should fail if its anything other than 'foo' 'bar'
>
> Any thoughts please?
>
> Thanks,
> Jay
>
> --
> Before posting, please readhttp://watir.com/support. In short: search before you ask, be nice.
>
> watir-general@googlegroups.comhttp://groups.google.com/group/watir-general


--
Before posting, please read http://watir.com/support. In short: search before you ask, be nice.

watir-...@googlegroups.com

Reply all
Reply to author
Forward
0 new messages