Unable to click on available date using selenium ruby - ruby

I'm not able to click on available date in the calendar. Past dates are greyed out so trying to get today's date and click on it. I have tried execute script, click() and perform() but none of them worked.
today_date = Date.today.strftime('%d')
element = #driver.find_element(:xpath,"//td[contains(#class,\"CalendarDay__default\")][contains(#aria-label, '#{today_date}')]")
##driver.execute_script("arguments[0].click;", element )
#driver.action.move_to(ele).click(ele).perform
I also tried loop but td element is not displayed as some of elements are greyed out. Not sure how to select displayed elements?
today_date= Date.today.strftime('%d')
date_picker= #driver.find_element(:xpath,"//*[contains(#class,'SingleDatePicker_picker')]")
columns=date_picker.find_elements(:tag_name, "td")
calendar_date=columns.map(&:text).reject(&:empty?)
columns.each do |col|
# This returns true
puts "include date: #{calendar_date.include?today_date}"
if calendar_date.include?today_date
# Elemement is not displayed
puts "td displayed: #{col.displayed?}"
# Not clickable
col.click
end
end
Please find html below.

It's kind of hard to know what will work exactly without being able to work with the actual html, but my guess is that the first thing that is getting matched by that locator is not what you want. Try with this:
element = browser.td(aria_label: Time.now.strftime("%A, %B %d, %Y"))
Or you can select the first non-disabled element regardless of date:
element = browser.td(aria_disabled: 'false')
Edit: just realized your code is Selenium even though the label is Watir. The XPath equivalent to the above are:
".//td[#aria-label=#{Time.now.strftime("%A, %B %d, %Y")}]"
".//td[#aria-disabled='false']"

Related

Can Not Click The Same Element Class Using Watir

I have the following screen:
And I use the following Ruby script to click the "Add New" button:
vendorTab = driver.a id: "tab-master-tab-vendor"
vendorTab.wait_until_present
if vendorTab.exists?
vendorTab.click
end
addNewButton = driver.button class: ['btn btn-primary']
addNewButton.wait_until_present
if addNewButton.exists?
addNewButton.click
end
But, when I move to another tab and try to click the same "Add New" button, the Ruby script doesn't work.
Is there anything wrong with my Ruby code?
buildingTypeTab = driver.a id: "tab-master-tab-building"
buildingTypeTab.wait_until_present
if buildingTypeTab.exists?
buildingTypeTab.click
end
addNewButton = driver.button class: ['btn btn-primary']
addNewButton.wait_until_present
if addNewButton.exists?
addNewButton.click
end
I Appreciate your help. Thank you very much.
I guess all of these tabs are part of the same web page? I.e., all in the same HTML?
If that is the case, driver.button class: ['btn btn-primary'] is going to stop when it finds the first instance in the HTML, but that isn't the button you are looking for every time (it's the button in the first tab, where your script worked as you expected).
The best options in my mind are
find a way to uniquely identify the button in each tab (for example, use id instead of class if possible), or
pull all the buttons into a collection and click the button using its collection index after you figure out which index aligns with each tab. For example,
button_collection = browser.buttons(:class, ['btn', 'btn-primary'])
button_collection[2].click # Will click the 3rd button in the collection
After reading the suggestions from pjd,
I modified it a bit and got it working like this:
buildingTypeTab = driver.a id: "tab-master-tab-building"
buildingTypeTab.wait_until_present
if buildingTypeTab.exists?
buildingTypeTab.click
end
addNewButton = driver.button(:class => ['btn btn-primary'], :index => 2)
addNewButton.wait_until_present
if addNewButton.exists?
addNewButton.click
end
As pjd said, yes all these tabs are part of the same HTML
Thank you.

Capture selected value from list using webdriver and Ruby

I'm using selenium webdriver with ruby. I've written a script that will fill in a form. One field is a dropdown list. What I would like to do is capture the value I selected in the list.
For example: If I had a list of cars and I selected Honda I would like to capture the value in the field (Honda) and place it in a variable for me to use later.
I hope I'm making sense.
You can use below code select list items:
cars_select = driver.find_element(:id=> "cars_list")
//use id of your dropdownlist
options = cars_select.find_elements(:tag_name=>"option")
options.each do |el|
if (el.value == "Honda")
el.select()
var selected_car = el.value;
break
end
end
If you are selecting from dropdown with:
browser.select_list(id: 'some_id').option(text: 'some_value').select
Then you can store that value in a variable, like below:
var1=browser.select_list(id: 'some_id').option(text: 'some_value').value
Hope this will work. If not, then provide your dropdown part html, and explain more. I am using watir-webdriver, try if this work for selenium-webdriver.

Parsing element text with capybara-webkit

I'm new to Ruby and Capybara and I'm trying to use capybara-webkit to scrape a website. All of the data I'm interested in lies in td tags with certain properties.
Where form is a particular form element I'm looking at, the following code works:
form.all('td').detect do |td|
if td['valign'] == 'top' && td['nowrap'] != 'nowrap'
print "#{td.text}\n"
end
end
The contents of all of the td elements I'm interested in are printed out correctly. However, when I try to then parse the text with a regex:
form.all('td').detect do |td|
if td['valign'] == 'top' && td['nowrap'] != 'nowrap'
print "#{td.text}\n"
val1, val2 = td.match(/(\d)(\d)/).captures # The real regex is more complex
end
end
...suddenly only the first td element is read/parsed. I've tried even just pushing each td.text value into an array for later parsing, but the same thing occurs. I've even tried making a clone of the td.text string and operating on that—no luck. There doesn't seem to be any sort of timeout on the page that would change the HTML elements. Absolutely no clue what could be causing this.
Any thoughts?

checking if all elements in a drop down menu are present with selenium testing using ruby?

HI
how can i check if all elements in an array i created are present in a drop down menu using selenium testing?
i have something like this but dosent seem to work
ANIMALS = ["snake","cat","dog"]
def validate_all_animals_exist(selenium)
ANIMALS.each { |animal| assert selenium.is_element_present(animal), "Expected category [#{animal}] to be present" }
end
thanks in advance
You need to use the verifySelectOptions call
verifySelectOptions(selectLocator,
pattern) Generated from
getSelectOptions(selectLocator)
Arguments:
* selectLocator - an element locator identifying a drop-down menu
Returns:
an array of all option labels in the specified select drop-down
Gets all option labels in the specified select drop-down.
So it would be
assert_equal "123123", page.get_select_options("foo").join(",")

How to click on an AutoCompleteExtender with Watin

For my acceptance testing I'm writing text into the auto complete extender and I need to click on the populated list.
In order to populate the list I have to use AppendText instead of TypeText, otherwise the textbox looses focus before the list is populated.
Now my problem is when I try to click on the populated list. I've tried searching the UL element and clicking on it; but it's not firing the click event on the list.
Then I tried to search the list by tagname and value:
Element element = Browser.Element(Find.By("tagname", "li") && Find.ByValue("lookupString"));
but it's not finding it, has anyone been able to do what I'm trying to do?
The shorter version of that is:
string lookupString = "string in list";
Element list = Browser.Element("li", Find.ByText(new Regex(lookupString)));
list.MouseDown();
Regexs will do a partial match so you don't need to specify .* either side and use string.Format. This assumes however that the lookupString doesn't contain any characters special to Regexs, they'd need to be escaped.
In case someone has the same problem. It works with the next code:
string lookupString = "string in list";
Regex lookup = new Regex(string.Format(".*{0}.*", lookupString));
Element list = Browser.Element("li", Find.ByText(lookup));
list.MouseDown();

Resources