Question

I've looked around the internet but without much success as well as checked through here.

I'm new to ruby watir and mini test but i'm trying to write some automation that checks the value in a select list drop down and if it finds a value it changes it a 2nd value. If it finds the 2nd value the next time it runs i want it to change the first value.

I've created variables of;

$Title1 = "Dr"
$Title2 = "Mr"

and the code I'm trying to run is attempting to use an if, then, else series of statements;

#Title
if $browser.select_list(id: "title") == $Title2 then
   $browser.select_list(:id, "title").select($Title1)
else
if $browser.select_list(id: "title") == $Title1 then
$browser.select_list(:id, "title").select($Title2)
end

however, nothing happens and no errors are triggered.

Any help would be greatly appreciated

Was it helpful?

Solution

Problem

The problem is that the if statements are comparing apples to oranges (ie two different things). You are comparing a Watir::SelectList, from $browser.select_list(id: "title"), with a string, from $Title2.

For Watir elements, the == is used to check if two elements are the same element on the page. The element will never equal a string.

Solution

What you actually want to compare to the Title2 is the selected options of the select list. Watir select lists have a selected? method for checking this.

You could do something like:

if $browser.select_list(id: "title").selected?($Title2)
  $browser.select_list(:id, "title").select($Title1)
else
  $browser.select_list(:id, "title").select($Title2)
end
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top