What do test automation frameworks provide that my approach of scripting with Selenium Webdriver doesn't? - xpath

I am a newbie to software test automation and have written the following test script in Selenium Webdriver and Ruby binding. It performs user actions (clicks, enters, fills up values etc.). I have put basic asserts that match the screen text value with value I provide. Below is the code:
puts "Test Run 1 has started ""["+Time.now.strftime('%H:%M:%S CST')+"]"
require "selenium-webdriver"
require "colorize"
profile = Selenium::WebDriver::Firefox::Profile.new
profile['browser.cache.disk.enable'] = false
browser = $browser = Selenium::WebDriver.for :firefox, :profile => profile
browser.manage().window().maximize();
browser.get "https://cameleon-6945--dev.cs11.cloudforce.com"
main_window = browser.window_handle
browser.find_element(name:"username").clear()
browser.find_element(name:"username").send_keys "abcd#vertex.com"
browser.find_element(name:"pw").send_keys "1234"
browser.find_element(name:"Login").click
browser.find_element(link_text:"Cameleon Quotes").click
wait = Selenium::WebDriver::Wait.new(:timeout => 45)
#Open Test Run 1 created quote
wait.until {browser.find_element(:css,"#bodyCell > div.bRelatedList > div.hotListElement > div > div.pbBody > table > tbody > tr.dataRow.even.first > th > a")}
browser.find_element(:css,"#bodyCell > div.bRelatedList > div.hotListElement > div > div.pbBody > table > tbody > tr.dataRow.even.first > th > a").click
wait.until {browser.find_element(:xpath,"/html/body/div[3]/div[3]/div[2]/div[3]/div[2]/div[2]/div[2]/form/div[3]/div/div[2]/div[2]/div/div/div/table")}
#browser.save_screenshot "Cart Overview - RegressionRun # "+Time.now.strftime('%Y-%m-%d %H%M%S')+".jpeg"
#wait.until {browser.find_element(:css,"body > div.mainPartBox > div.boxBody > div.main > div.processBar > div.backgroundProcessBarMiddle > a:nth-child(7) > div.processBarElement.noSelected > div")}
#browser.find_element(:css,"body > div.mainPartBox > div.boxBody > div.main > div.processBar > div.backgroundProcessBarMiddle > a:nth-child(7) > div.processBarElement.noSelected > div").click
#wait.until {browser.find_element(:css,"body > div.CombinedBox > div.boxBody > div.main > div:nth-child(8) > iframe")}
#wait.until {browser.find_element(:xpath,"/html/body/div[3]/div[3]/div[2]/div[3]/iframe")}
browser.manage.timeouts.page_load = 35
#browser.switch_to.frame(cart_frame)
puts "\n\n"
puts "Assertions to verify cart content values\n\n".yellow
element_value1 = browser.find_element(:css,"#total > tbody > tr > td:nth-child(4) > span").text
if element_value1 == "$314,507.30"
puts 'Contract Sales Price = ' +element_value1
puts 'Value as expected in the cart, Test Passed'
else
puts 'Test failed, Contract Sales price value does not match the expected value'
end
puts "\n"
element_value2 = browser.find_element(:css,"#total > tbody > tr > td:nth-child(6) > span").text
if element_value2 == "$157,253.65"
puts 'Contract Cost Price = ' +element_value2
puts 'Value as expected in the cart, Test Passed'
else
puts 'Test failed, Contract Cost price value does not match the expected value'.red
end
puts "\n"
element_value3 = browser.find_element(:css,"#total > tbody > tr > td:nth-child(8) > span").text
if element_value3 == "50.00"
puts 'Contract gross margin = ' +element_value3
puts 'Value as expected in the cart, Test Passed'
else
puts 'Test failed, Gross margin value does not match the expected value'
end
puts "\n\n\t\t\t\t\tTest Case 1 passed successfully, proceeding to Test Case 2 - Generate Document\n".green
It's not integrated with any framework, it's just a test script.
My questions are:
Is this test totally worthless if compared to tools like Cucumber/Capybara etc.?
I have captured most of the elements using XPaths and this way the only way since it lacked classes, ids etc. Now if there's a minor change in page structure, like a new div is introduced, this script will fail with NoElemenFoundError. Is there any way we can avoid it? Or this is the only way test scripts are written and we need to update them regularly with new developments?

That exercise is worth doing once for your own experience. A couple of ways that using rspec for example would be an improvement are that rspec gives you a way to organize multiple tests, and that it boils down the results of your tests to a single indicator of success or failure, so that you can just look at the last line of output (or have your CI server look at the exit status) instead of reading pages of messages to see whether all your tests passed. Cucumber differs from rspec in that it allows you to read your entire test in English without reading any code in between, which is extremely valuable for thinking through requirements and might allow you to collaborate on your tests with non-programmers. Capybara provides methods that do much what you're doing in your script but are more succinct.
Sensitivity to page structure is an issue even with more sophisticated tools. One way to minimize the issue is to not assert any more of the page structure than you have to. If there is only one table on the page and only one row of results then td:nth-child(6) is all the selector you need. Another way to deal with that issue is to add IDs or classes to your pages to support testing.

I would say it is worth to have such scripts, but as soon as your number of scripts grows you need to manage them in a way where minimum maintenance is required.
So you can use various strategies to design a framework where you can have central object repository and test data. Refer here! for more details on designs.
I agree with Dave, to avoid assert on page structure. And if you maintain a central repository then there would be little modification needed if there is changes in structure of a page.

Related

Scan/Match incorrect input error messages

I am trying to count the correct inputs from the user. An input looks like:
m = "<ex=1>test xxxx <ex=1>test xxxxx test <ex=1>"
The tag ex=1 and the word test have to be connected and in this particular order to count as correct. In case of an invalid input, I want to send the user an error message that explains the error.
I tried to do it as written below:
ex_test_size = m.scan(/<ex=1>test/).size # => 2
test_size = m.scan(/test/).size # => 3
ex_size = m.scan(/<ex=1>/).size # => 3
puts "lack of tags(<ex=1>)" if ex_test_size < ex_size
puts "Lack of the word(test)" if ex_test_size < test_size
I believe it can be written in a better way as the way I wrote, I guess, is prone to errors. How can I make sure that all the errors will be found and shown to the user?
You might use negative lookarounds:
#⇒ ["xxx test", "<ex=1>"]
m.scan(/<ex=1>(?!test).{,4}|.{,4}(?<!<ex=1>)test/).map do |msg|
"<ex=1>test expected, #{msg} got"
end.join(', ')
We scan the string for either <ex=1> not followed by test or vice versa. Also, we grab up to 4 characters that violate the rule for the more descriptive message.

Ruby - URL to Markdown

TOTAL rookie here.
I'm working on customizing a script made by Brett Terpstra - http://brettterpstra.com/2013/11/01/save-pocket-favorites-to-nvalt-with-ifttt-and-hazel/
Mine is a different use: I'd like to save my pinboard bookmarks with a specific tag to a file in dropbox in Markdown.
I feed it a text file such as:
Title: Yesterday is over.
URL: http://www.jonacuff.com/blog/want-to-change-the-world-get-doing/
Tags: 2md, 2wcx, 2pdf
Date: June 20, 2013 at 06:20PM
Image: notused
Excerpt: You can't start the next chapter of your life if you keep re-reading the last one.
And it outputs the markdown file.
Everything works great except when the 'excerpt' (see above) is more than one line. Sometimes it's a couple of paragraphs. When that happens, it stops working. When I hit enter from the command line, it's still waiting for more input.
Here's an example of a file that it doesn't work on:
Title: Talking ’bout my Generation.
URL: http://blog.greglaurie.com/?p=8881
Tags: 2md, 2wcx, 2pdf
Date: June 28, 2013 at 09:46PM
Image: notused
Excerpt: Contrast two men from the 19th century: Max Jukes and Jonathan Edwards.
Max Jukes lived in New York. He did not believe in Christ or in raising his children in the way of the Lord. He refused to take his children to church, even when they asked to go. Of his 1,026 descendants:
•300 were sent to prison for an average term of 13 years
•190 were prostitutes
•680 were admitted alcoholics
His family, thus far, has cost the state in excess of $420,000 and has made no contribution to society.
Jonathan Edwards also lived in New York, at the same time as Jukes. He was known to have studied 13 hours a day and, in spite of his busy schedule of writing, teaching, and pastoring, he made it a habit to come home and spend an hour each day with his children. He also saw to it that his children were in church every Sunday. Of his 929 descendants:
•430 were ministers
•86 became university professors
•13 became university presidents
•75 authored good books
•7 were elected to the United States Congress
•1 was Vice President of the United States
Edwards’ family never cost the state one cent.
We tend to think that our decisions only affect ourselves, but they have ramifications for generations to come.
Here's a screenshot of what it looks like after I run the command: https://www.dropbox.com/s/i9zg483k7nkdp6f/Screenshot%202013-11-22%2016.39.17.png
I'm hoping it's something easy. Any ideas?
#!/usr/bin/env ruby
# Works with IFTTT recipe https://ifttt.com/recipes/125999
#
# Set Hazel to watch the folder you specify in the recipe.
# Make sure nvALT is set to store its notes as individual files.
# Edit the $target_folder variable below to point to your nvALT
# ntoes folder.
require 'date'
require 'open-uri'
require 'net/http'
require 'fileutils'
require 'cgi'
$target_folder = "~/Dropbox/messx/urls2md"
def url_to_markdown(url)
res = Net::HTTP.post_form(URI.parse("http://heckyesmarkdown.com/go/"),{'u'=>url,'read'=>'1'})
if res.code.to_i == 200
res.body
else
false
end
end
file = ARGV[0]
begin
input = IO.read(file).force_encoding('utf-8')
headers = {}
input.each_line {|line|
key, value = line.split(/: /)
headers[key] = value.strip || ""
}
outfile = File.join(File.expand_path($target_folder), headers['Title'].gsub(/["!*?'|]/,'') + ".txt")
date = Time.now.strftime("%Y-%m-%d %H:%M")
date_added = Date.parse(headers['Date']).strftime("%Y-%m-%d %H:%M")
content = "Title: #{headers['Title']}\nDate: #{date}\nDate Added: #{date_added}\nSource: #{headers['URL']}\n"
tags = false
if headers['Tags'].length > 0
tag_arr = header s['Tags'].split(", ")
tag_arr.map! {|tag|
%Q{"#{tag.strip}"}
}
tags = tag_arr.join(" ")
content += "Keywords: #{tags}\n"
end
markdown = url_to_markdown(headers['URL']).force_encoding('utf-8')
if markdown
content += headers['Image'].length > 0 ? "\n\n> #{headers['Excerpt']}\n\n---#{markdown}\n" : "\n\n"+markdown
else
content += headers['Image'].length > 0 ? "\n\n![](#{headers['Image']})\n\n#{headers['Excerpt']}\n" : "\n\n"+headers['Excerpt']
end
File.open(outfile,'w') {|f|
f.puts content
}
if tags && File.exists?("/usr/local/bin/openmeta")
%x{/usr/local/bin/openmeta -a #{tags} -p "#{outfile}"}
end
# FileUtils.rm(file)
rescue Exception => e
puts e
end
How about this? Modify your input.each_line area accordingly:
headers = {}
key = nil
input.each_line do |line|
match = /^(?<key>\w+)\s*:\s*(?<value>.*)/.match(line)
value = line
if match
key = match[:key].strip
headers[key] = match[:value].strip
else
headers[key] += line
end
end
First, splitting on just ":" is dangerous since that can be in content. Instead, a (simplified from code) regex of /^\w+:.*/ will match "Word: Content". Since the lines after the "Excerpt:" aren't prefixed, you need to hang on to the last seen key, and just append if there's no key for this line. You may need to add a newline in there, depending on what you're doing with that header information, but it seems to work.

Force to end n if in haml

I have an if:
-12.times do |control|
-dia += 1
-if control == 1
%a#hoy{:href=>'/dias/algo'}<
-else
%a{:href=>'/dias/algo'}<
=dia
%span=dias[rand(7)]
The problem is I need =dia and span elements inside the anchor tag in both cases (true/false), and when I quit one identation it fails, because haml will end the if (which is also normal).
Is there any way to force end an if? I have tried it in many ways, but couldn't find the right way if it exist.
Thanks.
-12.times do |control|
-dia += 1
%a{:id => control == 1 ? "hoy" : "", :href=>'/dias/algo'}<
=dia
%span=dias[rand(7)]
Didn't test it but it should work ...

conditional re-download of website data based on timestamp

In simple form I re-download a file from my account on a website if my local copy of data.csv is older than 1 hour:
# Mission: make sure data*.csv is most current whenever called
def updateData
return if File.exists?("data.csv") && (Time.now - File::Stat.new("data.csv").mtime) < 3600
$agent = Mechanize.new
$agent.pluggable_parser.default = Mechanize::Download
$page = $agent.get("http://website.com/login.jsp")
# login etc.
$agent.get("/getdata!downLoad.action").save("data.csv")
end
However they mentioned that updates to my data are only published thrice daily: at 16:45, 18:45, and 22:45.
Question:
How do I make my code more intelligent about grabbing the update only if my copy is older than the last update time (including yesterday's)?
Some array ["16:45", "18:45", "22:45"] could help but I'm not sure what next in Ruby.
Something like this could do it :
require 'time'
current = Time.now.strftime("%H%M")
past = File::Stat.new("data.csv").mtime.strftime("%H%M")
if (current > '2245' and past < '2245') or (current > '1845' and past < '1845') or (current > '1645' and past < '1645') or (File::Stat.new("data.csv").mtime.day != Time.now.day and current > '1645')
#update
end
You will also need to change the way you store mtime. It will need to be in the form hhmm. You would set mtime like this mtime = Time.now.hour.to_s + Time.now.min.to_s when you create the csv.

Ruby command line option that lists operations to perform

Is it possible for me to do something like this:
>ruby some_file.rb
>Your options are:
> 1. delete file blah.txt
> 2. delete file blah2.txt
> 3. delete file blah3.txt
> x to exit
> 1
> blah.txt was deleted
> 1. delete file blah.txt
> 2. delete file blah2.txt
> 3. delete file blah3.txt
> x to exit
> x
>
And this would read it's configuration from a file which would have all the files to delete.
I want this to be in a loop, so it keeps asking what to do unless you press 'x'.
How could you do this in Ruby?
You can read user input from standard input using Kernel#gets. This should hopefully point you in the right direction.
You need to create the configuration file your talking about that has all of the files you
need to delete.
Read the configuration file
Create a menu based on the configuration file
Read in user input and perform operations.
You can start from here:
def menu(choice)
case choice
when 1
# do_something
when 2
# ...
end
end
while ((a = gets) != "x\n")
menu(a)
end
as #Hunter McMillen stated you can use some configuration file to create the case-when programmatically.
if ARGV.size > 0
if FileTest.exists? ARGV[0]
files=[]
IO.readlines(ARGV[0]).each{|l| files.push(l.chop) if FileTest.exists? l.chop}
while files.size > 0
files.each_with_index{|f,i| puts "#{i+1}. #{f}"}
puts "x to exit"
opt = $stdin.gets.chop
break if opt == 'x'
opt = opt.to_i
puts "#{files.delete_at(opt-1)} was deleted" if opt > 0 && opt <= files.size && File.delete(files[opt-1]) > 0
end
end
end

Resources