how to Clear or replace contents of input field using selenium? - ruby

How can i clear the contents of an input field using ruby selenium?
i.e when page loads up there are values in the input filed but i want to replace them with new ones.
thank you

Come on guys... You can't be serious...
With selenium to clear an input field do this: selenium.type("") in ruby too.
And backspacing as many times as there are letters... Come on.. you don't do stuff like that.
Or in Ruby syntax: #browser.type "idofthefield", ""

This is how i cleared and replaced with new text in my Ruby based web portal test
#driver.find_element(:xpath, "//input[#name='ip-address']").clear
#driver.find_element(:xpath, "//input[#name='ip-address']").send_keys(new_ip_address)

Related

Ruby and Excel Data Extraction

I am learning Ruby and trying to manipulate Excel data.
my goal:
To be able to extract email addresses from an excel file and place them in a text file one per line and add a comma to the end.
my ideas:
i think my answer lies in the use of spreadsheet and File.new.
What I am looking for is direction. I would like to hear any tips or rather hints to accomplish my goal. thanks
Please do not post exact code only looking for direction would like to figure it out myself...
thanks, karen
UPDATE::
So, regex seems to be able to find all matching strings and store them into an array. I´m having some trouble setting that up but should be able to figure it out....but for right now to get started I will extract only the column labeled "E Mail"..... the question I have now is:
`parse_csv = CSV.parse(read_csv, :headers => true)`
The default value for :skip_blanks is set to false.. I need to set it to true but nowhere can I find the correct syntax for doing so... I was assumming something like
`parse_csv = CSV.parse(read_csv, :headers => true :skip_blanks => true)`
But no.....
save your excel file as csv (comma separated value) and work with Ruby's libraries
besides spreadsheet (which can read and write), you can read Excel and other file types with with RemoteTable.
gem install remote_table
and
require 'remote_table'
t = RemoteTable.new('/path/to/file.xlsx', headers: :first_row)
when you write the CSV, as #aug2uag says, you can use ruby's standard library (no gem install required):
require 'csv'
puts [name, email].to_csv
Personally, I'd keep it as simple as possible and use a CSV.
Here is some pseudocode of how that would work:
read in your file line by line
extract your fields using regex, or cell count (depending on how consistent the email address location is), and insert into an arry
iterate through the array and write the values in the fashion you wish (to console, or file)
The code in the comment you had is a great start, however, puts will only write to console, not file. You will also need to figure out how you are going to know you are getting the email address.
Hope this helps.

Choosing form fields by label using Mechanize?

I originally wrote 800 lines to do this, site by site. However, on talking to a couple of people, it seems like my code is way longer than it needs to be.
So, I've got an idea of what you'd do in Python, with a particular Egg, but I'm working with Ruby. So, does anyone have any idea how to enter details in a form field, based on what the label for it is, rather than the id/name? Using Mechanize.
Let's say your html looks like:
<label>Foo</label>
<input name="foo_field">
You can get the name of the input following a specific label:
name = page.at('label[text()="Foo"] ~ *[name]')[:name]
#=> "foo_field"
and use that to set the form value
form[name] = 'bar'

How can I use Nokogiri to find specific text/words on a webpage?

I am new to nokogiri, but it looks like this would be the tool that I would use to scrape a webpage. I am looking for specific words on a webpage. The words are "Valid", "Requirements Met", and "Requirements Not". I am using watir to drive through the website. I currently have:
page = Nokogiri::HTML.parse(browser.html)
to get the html, but I am not sure where to go from here.
Thanks for the help!
If you are using Watir to drive the website, I would suggest using Watir to check for the text. You can get all the text on the page using:
ie.text #Where ie is a Watir::IE
You could then check to see if it has those words are included (by comparing to a regex):
if ie.text =~ /Valid|Requirements Met|Requirements Not/
#Do something if the words are on the page
end
That said, if you are looking for a specific bits of text, you can use Watir to look specifically for those elements (and avoid parsing text or html). If you can provide an HTML sample of what you are working on, we can help find a more robust solution.
I am not sure why you are using both. You could get the page using 'net/http' or mechanize if you just want to check for text. Anyways, you can check for text in watir with browser.text.match 'Valid', same for nokogiri with page.text.match 'Valid'.
You should also be able to use the .text method from Justin's answer along with the standard ruby string .include? method which returns true or false.
if browser.text.include? /Valid|Requirements Met|Requirements Not/
#code to execute if text found
else
#code to execute if text not found
end
This also makes it easy to have a single line validation step if that is what you are after
if using rspec/cucumber
browser.text.should include /Valid|Requirements Met|Requirements Not/
if using test:Unit
assert browser.text.include? /Valid|Requirements Met|Requirements Not/

Getting text from a form and evaluate in selenium using ruby

I am testing a registration form and one of the questions before submitting is:
"What is 8 + 4?"
The values will be different every time. I am using Selenium 2 with Ruby and want to to see if anyone knows how to a) get the text from the question and then b) compute the addition and return the correct answer in the field.
Thanks!
Using ruby code try to get the values in between is and ? Store this in Variable and now split the text with deliminator + now you will get the Values in array in different location. Now you can add these two and use for your evaluation. I don't have idea about ruby but its possible in PHP as I have done the same. Its should be in Ruby too.
Getting text between is and ? can be done strpos() in php that gives the position of the string see similar function for ruby.

How do you check the page for text using Selenium 2 and Firefox?

I am trying to check to see if text is present using Selenium 2 and Firefox but cant seem to find the method to use. I tried to use the method is_text_present which seems to be what everyone says work but will not work for me. I get the returned error:
NoMethodError: undefined method `is_text_present' for# Selenium::WebDriver::Driver:0x1017232e0 browser=:firefox
How do you check the page for text using Selenium 2 and Firefox?
When I tried this stack overflow option "Finding text in page with selenium 2" it did not work for me, I believe it doesn't work because I am using Ruby to do my test, not Java.
If you don't know in which element should be your text, you can use :
driver.page_source.include? 'TEXT_TO_SEARCH'
Otherwise you can use as Llelong and Thomp suggested :
driver.find_element(:id=>"ELEMENT_ID").text.include? 'TEXT_TO_SEARCH'
driver.find_element(:class=>"ELEMENT_CLASS").text.include? 'TEXT_TO_SEARCH'
You can find the text in JAVA using the following code snippet...
Try using the same for Ruby:-
driver.getPageSource().contains("TEXTTOSEARCH");
require 'selenium-webdriver'
br = Selenium::WebDriver.for :firefox
br.get "http://cnn.com"
br.find_element(:id=>"cnnMainPage")
br.find_element(:id=>"cnnMainPage").text.include? "Mideast"
I tested this in the irb, so you may run into timing problems (may have to wait for the element to be present first).
I'm not sure how to do it in Ruby, but you should be able to call the getPageSource() method, and check to see if that contains the string of text you're looking for.
If you can't find the text in the source, you probably want to identify the exact element that contains the text and call the getText() method on that element. For example, these are some common identifiers for elements:
driver.findElement(By.xpath("xpathstring")).getText()
driver.findElement(By.className("className")).getText()
driver.findElement(By.name("elementname")).getText()
driver.findElement(By.id("idname")).getText()
There are several more element identifiers, you'll have to consult the documentation if these don't work for you.
this worked for me...
driver.page_source.should_not include 'Login failed'

Resources