Capture selected value from list using webdriver and Ruby - 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.

Related

Posting data on website using Mechanize Nokogiri Selenium

I need to post data on a website through a program.
To achieve this I am using Mechanize Nokogiri and Selenium.
Here's my code :
def aeiexport
# first Mechanize is submitting the form to identify yourself on the website
agent = Mechanize.new
agent.get("https://www.glou.com")
form_login_AEI = agent.page.forms.first
form_login_AEI.util_vlogin = "42"
form_login_AEI.util_vpassword = "666"
# this is suppose to submit the form I think
page_compet_list = agent.submit(form_login_AEI, form_login_AEI.buttons.first)
#to be able to scrap the page you end up on after submitting form
body = page_compet_list.body
html_body = Nokogiri::HTML(body)
#tds give back an array of td
tds = html_body.css('.L1').xpath("//table/tbody/tr[position()>1]/td")
# Checking my array of td with some condition
tds.each do |td|
link = td.children.first # Select the first children
if link.html = "2015 32 92 0076 012"
# Only consider the html part of the link, if matched follow the previous link
previous_td = td.previous
previous_url = previous_td.children.first.href
#following the link contained in previous_url
page_selected_compet = agent.get(previous_url)
# to be able to scrap the page I end up on
body = page_selected_compet.body
html_body = Nokogiri::HTML(body)
joueur_access = html_body.search('#tabs0head2 a')
# clicking on the link
joueur_access.click
rechercher_par_numéro_de_licence = html_body.css('.L1').xpath("//table/tbody/tr/td[1]/a[1]")
pure_link_rechercher_par_numéro_de_licence = rechercher_par_numéro_de_licence['href']
#following pure_link_rechercher_par_numéro_de_licence
page_submit_licence = agent.get(pure_link_rechercher_par_numéro_de_licence)
body_submit_licence = page_submit_licence.body
html_body = Nokogiri::HTML(body_submit_licence)
#posting my data in the right field
form.field_with(:name => 'lic_cno[0]') == "9511681"
1) So far what do you think about this code, Do you think there is an error in there
2) This part is the one I am really not sure about : I have posted my data in the right field but now I need to submit it. The problem is that the button I need to click is like this:
<input type="button" class="button" onclick="dispatchAndSubmit(document.JoueurRechercheForm, 'rechercher');" value="Rechercher">
it triggers a javascript function onclick. I am triying Selenium to trigger the click event. Then I end up on another page, where I need to click a few more times.. I tried this:
driver.find_element(:value=> 'Rechercher').click
driver.find_element(:name=> 'sel').click
driver.find_element(:value=> 'Sélectionner').click
driver.find_element(:value=> 'Inscrire').click
But so far I have not succeeded in posting the data.
Could you please tell me if selenium will enable me to do what I need to do. If can I do it ?
At a glance your code can use less indentation and more white space/empty lines to separate the internal logic of AEIexport (which should be changed to aei_export since Ruby uses snake case for method names. You can find more recommendations on how to style ruby code here).
Besides the style of your code, an error I found at the beginning of your method is using an undefined variable page when defining form_login_AEI.
For your second question, I'm not familiar with Selenium; however since it does use a real web browser it can handle JavaScript. Watir is another possible solution.
An alternative would be to view the page source (i.e. in Firebug) and understand what the JavaScript on the page does. Then use Mechanize to follow the link manually.

Getting an id's value in Watir

I have been lumbered with a Ruby, Watir IE test program which I need to change.
It currently does this:-
browser.frame(:id,"ultraposuidialoghtm0").div(:class," content").button(:id,"ok").click
This issue I have is the first div ID can change, 0, 1, 2 etc.
How do I create a var that shows me the ID on the page?.
i.e. my = browser.frame(:id)
What would be the correct syntax?
I can't try this, but judging from the documentation you should be able to do it using a regex:
browser.frames(:id, /ultraposuidialoghtm/).each do |frame|
id = frame.id
# do whatever
end

Selenium Webdriver + Ruby regex: Can I use regex with find_element?

I am trying to click an element that changes per each order like so
edit_div_123
edit_div_124
edit_div_xxx
xxx = any three numbers
I have tried using regex like so:
#driver.find_element(:css, "#edit_order_#{\d*} > div.submit > button[name=\"commit\"]").click
#driver.find_element(:xpath, "//*[(#id = "edit_order_#{\d*}")]//button").click
Is this possible? Any other ways of doing this?
You cannot use Regexp, like the other answers have indicated.
Instead, you can use a nifty CSS Selector trick:
#driver.find_element(:css, "[id^=\"edit_order_\"] > div.submit > button[name=\"commit\"]").click
Using:
^= indicates to find the element with the value beginning with your criteria.
*= says the criteria should be found anywhere within the element's value
$= indicates to find the element with with your criteria at the end of the value.
~= allows you to find the element based on a single criteria when the actual value has multiple space-seperated list of values.
Take a look at http://net.tutsplus.com/tutorials/html-css-techniques/the-30-css-selectors-you-must-memorize/ for some more info on other neat CSS tricks you should add to your utility belt!
You have no provided any html fragment that you are working on. Hence my answer is just based on the limited inputs provided your question.
I don't think WebDriver APIs support regex for locating elements. However, you can achieve what you want using just plain XPath as follows:
//*[starts-with(#id, 'edit_div_')]//button
Explanation: Above xpath will try to search all <button> nodes present under all elements whose id attribute starts with string edit_div_
In short, you can use starts-with() xpath function in order to match element with id format as edit_div_ followed by any number of characters
No, you can not.
But you should do something like this:
function hasClass(element, className) {
var re = new RegExp('(?:^|\\s+)' + className + '(?:\\s+|$)');
return re.test(element.className);
}
This worked for me
#driver.find_element(:xpath, "//a[contains(#href, 'person')]").click

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