Ruby 'require' errors with Selenium webdriver script - ruby

I'm trying to execute a script to test some user credentials, and I keep getting errors relating to 'require'. The script takes a csv of usernames and passwords, logs them into a site, and groups them into pass/fail lists.
I confess I didn't write the script- it was provided to us by an outside coder. I've taken a few CS101 classes, and can parse what it's doing and modify it for different domains and credential files, but I'm a total ruby beginner.
=========================
require 'selenium-webdriver'
require 'rubygems'
require 'csv'
pass_file = File.open('pass.txt', 'w')
fail_file = File.open('fail.txt', 'w')
in_file = File.open('test credentials.csv', 'r')
pass_file.puts "xxxxxx"
fail_file.puts "xxxxxx"
CSV.foreach(in_file, :col_sep => ',') do |row|
driver = Selenium::WebDriver.for :firefox
driver.get 'https://www.xxxxxx.com/'
driver.find_element(:id => 'UserNameEntry').send_keys row[0]
driver.find_element(:id => 'UserPasswordEntry').send_keys row[1]
driver.find_element(:id => 'xxxxxxlNameEntry').clear
driver.find_element(:id => 'xxxxxxNameEntry').send_keys "xxxxxx"
driver.find_element(:xpath => '//*[#id="cmdLoginButton"]/span').click
driver.switch_to.window(driver.window_handles[1])
if driver.title == "Welcome!"
pass_file.puts row[0]
elsif driver.title == "Login Unsuccessful"
fail_file.puts row[0]
end
driver.quit
end
==================
When I run it, at first I was getting "`require': no such file to load -- selenium-webdriver (LoadError)
from xxxxx.rb:1
Then I read on a forum that the requirement for gems should come first. After putting require 'rubygems' before require 'selenium-webdriver', now I get this error:
/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/csv.rb:308:in initialize': can't convert File into String (TypeError)
from /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/csv.rb:308:inopen'
from /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/csv.rb:308:in open_reader'
from /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/csv.rb:94:inforeach'
I'm feeling so confused...any help appreciated.

You need to install the gems before you can require them. For example, the selenium-webdriver gem can be found here
Selenium WebDriver
You run gem install selenium-webdriver in a terminal program.

Try to remove space from csv file name, for example:
in_file = File.open('test_credentials.csv', 'r')
As I can see from the stacktrace, error occurs when reading csv file.

If there is not space between test credentials.csv then remove that. Check that there is _ present or not
If there is a space between test credentials.csv then write file name in your code as follows in_file = File.open("test\ credentials.csv", 'r')

If you use Bundler, add require bundler/setup at the top.

Please try this.
gem update --system

Related

No such file or directory # rb_sysopen ruby and csv

First of all I would like to say that I'm new to Ruby and if I'm not able to give you a good picture of what I'm trying to solve, that is the reason.
I'm trying to convert URLs into images and I've looked around for answers but I can't seem to find an answer that works for me. The file has around 70,000+ links and I'm also trying to name these at the same time. I'm using ruby 2.3.0 if that is relevant.
Code --
require 'open-uri'
require 'tempfile'
require 'uri'
require 'csv'
def downloadFile(path,url)
begin
open(path, "wb+") do |file|
file << open(url).read
end
return true
rescue
return false
end
end
puts Dir.pwd
CSV.foreach("C/Users/b40ssr/RubymineProjects/Bygma/convert/konvertera.CSV", headers:true) do |row|
downloadFile(row[0], row[1])
end
So the error that I'm getting is
C:/Ruby23/lib/ruby/2.3.0/csv.rb:1265:in `initialize': No such file or directory # rb_sysopen - C/Users/b40ssr/RubymineProjects/Bygma/convert/konvertera.CSV (Errno::ENOENT)
I understand that there is something wrong with the directory but I cant seem to figure out what it is.
First of all, you can use relative path or just use "C:/"
Second, You are trying to open each row of CSV file ??
CSV.foreach("C/Users/b40ssr/RubymineProjects/Bygma/convert/konvertera.CSV"). This will iterate over each rows in CSV file.
Do you want to open each CSV file inside a directory ??

GzipReader each_line method missing in Rubinius

I am trying to read a gzipped file using Zlib:GzipReader. This works as expected using ruby 1.9.3 but I am getting a method_missing error for each_line when using Rubinius.
Is there any way to read a gzipped file using Rubinius?
require 'zlib'
Zlib::GzipReader.open("lines.txt.gz").each_line { |line|
puts "#{line}"
}
Kernel(Zlib::GzipReader)#each_line (method_missing) at kernel/delta/kernel.rb:81
I believe this is a bug in Rubinius, you should consider opening an issue for it with the project. However, this workaround should get you going:
require 'zlib'
require 'stringio'
file = File.read("lines.txt.gz")
lines = Zlib::GzipReader.new(StringIO.new(file)).read

include file to ruby testScript

I have multiple ruby test cases for selenium-webdriver and all the files are sharing the same user name and password to login to my account. Is there a way to create a global file and include that file in these test cases instead of typing them over and over again, something like #include?
Here is the part of the code that needs to be shared between other test cases:
def setup
#driver = Selenium::WebDriver.for :firefox
#base_url = "http://localhost:3000/"
#driver.manage.timeouts.implicit_wait = 30
#verification_errors = []
#facebook_ID = "xxxxxxxxxx#xxx.xxx"
#facebook_password = "xxxxxxx"
#facebook_receiver_friend = "John Smith"
end
There are multiple ways to do it.
You could use
require 'setup'
where setup.rb is the common file that has all the setting up variables/functions.
You could also use a YAML file. More information here. Where all your config attributes can be defined.
Also put this is at the top of the file $:.unshift '.' . This is so that your test file can "discover" your setup.rb file which is routable from the home directory.
Note - Even though you will save the file as setup.rb you will require as only setup.
I got my answer:
eval File.open('setup.dat').read
This will insert what I have in setup.dat as a text wherever I need it.

Reload rubygems in irb?

I've this script right now.
def r(this)
require this
puts "#{this} is now loaded."
rescue LoadError
puts "The gem '#{this}' is missing."
puts "Should I install it? [y/n]"
data = gets
if data =~ /yes|y/i
puts "Installing #{this}, hold on."
if `gem install #{this}` =~ /Successfully/i
load this
end
else
puts "Okey, goodbye."
end
end
That makes it possible to require libs on the fly.
Like this: r "haml".
The problem is that I can't load the gem after it has been installed.
Using load this or load File.expand_path("~/.irbrc") does not work.
Here is an example.
>> r "absolutize"
The gem 'absolutize' is missing.
Should I install it? [y/n]
y
Installing absolutize, hold on
LoadError: no such file to load -- absolutize
>> require "absolutize"
LoadError: no such file to load -- absolutize
>> exit
$ irb
>> require "absolutize"
=> true
Is there a way to reload rubygems or irb on the fly?
I did not try, but I think you might be looking for Gem.clear_paths
Reset the dir and path values. The next time dir or path is requested, the values will be calculated from scratch. This is mainly used by the unit tests to provide test isolation.
You can reset irb by calling exec('irb')
Just remove the file from ´$"´:
require 'erb' # Loaded.
require 'erb' # Does nothing.
$".delete_if {|e| e =~ /erb\.(?:rb|so)/} # Remove "erb" from loaded libraies.
require 'erb' # Reloaded (with warnings if the first require was successful).
See http://www.zenspider.com/Languages/Ruby/QuickRef.html#19

private method `split' called for nil:NilClass (NoMethodError)

I'm trying to use domainatrix with nokogiri and am coming up with a holdup. Being relatively new to ruby, I've tried every syntax variation on the Domainatrix.parse function I can to get the a href's to parse properly. They do print during the "puts" command but when I uncomment the domainatrix code problems start:
require 'rubygems'
require 'domainatrix'
require 'anemone'
require 'open-uri'
require 'nokogiri'
doc = Nokogiri::HTML(open("http://www.cnn.com"))
doc.xpath('//a/#href').each do |node|
linkage = node.text
puts linkage
url = Domainatrix.parse(linkage)
print url.domain
print url.public_suffix
end
Anyone have any ideas on this? I think it is just a syntax issue or perhaps I cannot use the Domainatrix function where I'm using it?
It was getting snagged on some improperly formatted URLs.

Resources