watir print/put all visible links - ruby

$browser.links.each do |link|
puts link.attribute_value("class")
end
How do I get all the visible/existing links in the put statement?

You can also write it by using shorter syntax like this:
puts $browser.links.find_all(&:present?).map(&:class_name)

This will output value of class attribute for all existing links on the page:
$browser.links.each {|link| puts link.attribute_value("class")}
This will output value of class attribute for all visible links on the page:
$browser.links.each {|link| puts link.attribute_value("class") if link.visible?}

If you require the class name of the links, you can use
$browser.links.each {|link| puts link.class_name if link.visible?}
or if you require any specific attribute of the link, you can use
$browser.links.each {|link| puts link.attribute_value("attribute_name") if link.visible?}

Related

How to get the feature name in Cucumber hooks.rb

I need a variable that contains the name of the feature: since it's a feature, do it; since it is another, do that.
I tried these ways, but no success:
Examples:
1)
After do |scenario|
puts scenario.feature.name
end
2)
After do |scenario|
puts scenario.scenario_outline.feature.name
end
p.s.: Translated by Google
I believe this would just be
After do |scenario|
puts scenario.name
end
Finally I found it!
Before do |s|
feature(s.location) # => #<struct Cucumber::Core::Test::Location::Precise
end
def feature(location)
string = File.read(location.file)
document = ::Gherkin::Parser.new.parse(string)
document[:feature]
end
The solution is in the clink below:
https://github.com/cucumber/cucumber-ruby/issues/1432

How to convert a string to an object reference in ruby

I want to take in a page name in a gherkin step then set it as an object reference (I think that's what it's called.) Then I can use that to interact with page elements. Is there an easier way to do this in ruby?
When(/^I am on the (.*) page$/) do |page|
case page
when "home page"
#current_page = #home_page
when "my account page"
#current_page = #my_account_page
end
end
Then
When(/^I click the (.*)$/) do |element|
#current_page.send(element).click
end
You can use instance_variable_get:
When(/^I am on the (.*) page$/) do |page|
#current_page = instance_variable_get("##{page}")
end
This will work if the page values perfect match your instance variables, e.g.
page = "my_account_page"
# The following two lines are equivalent
instance_variable_get("##{page}")
#my_account_page

Getting all unique URL's using nokogiri

I've been working for a while to try to use the .uniq method to generate a unique list of URL's from a website (within the /informatics path). No matter what I try I get a method error when trying to generate the list. I'm sure it's a syntax issue, and I was hoping someone could point me in the right direction.
Once I get the list I'm going to need to store these to a database via ActiveRecord, but I need the unique list before I get start to wrap my head around that.
require 'nokogiri'
require 'open-uri'
require 'active_record'
ARGV[0]="https://www.nku.edu/academics/informatics.html"
ARGV.each do |arg|
open(arg) do |f|
# Display connection data
puts "#"*25 + "\nConnection: '#{arg}'\n" + "#"*25
[:base_uri, :meta, :status, :charset, :content_encoding,
:content_type, :last_modified].each do |method|
puts "#{method.to_s}: #{f.send(method)}" if f.respond_to? method
end
# Display the href links
base_url = /^(.*\.nku\.edu)\//.match(f.base_uri.to_s)[1]
puts "base_url: #{base_url}"
Nokogiri::HTML(f).css('a').each do |anchor|
href = anchor['href']
# Make Unique
if href =~ /.*informatics/
puts href
#store stuff to active record
end
end
end
end
Replace the Nokogiri::HTML part to select only those href attributes that matches with /*.informatics/ and then you can use uniq, as it's already an array:
require 'nokogiri'
require 'open-uri'
require 'active_record'
ARGV[0] = 'https://www.nku.edu/academics/informatics.html'
ARGV.each do |arg|
open(arg) do |f|
puts "#{'#' * 25} \nConnection: '#{arg}'\n #{'#' * 25}"
%i[base_uri meta status charset content_encoding, content_type last_modified].each do |method|
puts "#{method.to_s}: #{f.send(method)}" if f.respond_to? method
end
puts "base_url: #{/^(.*\.nku\.edu)\//.match(f.base_uri.to_s)[1]}"
anchors = Nokogiri::HTML(f).css('a').select { |anchor| anchor['href'] =~ /.*informatics/ }
puts anchors.map { |anchor| anchor['href'] }.uniq
end
end
See output.

In Jekyll, how can I programmatically modify permalinks for pages?

In a Jekyll site with many pages (not blog posts), I want to tweak the permalink of each page programatically. I tried a Generator plugin, something like:
module MySite
class MySiteGenerator < Jekyll::Generator
def generate(site)
site.pages.each do |page|
page.data['permalink'] = '/foo' + page.url
# real world manipulation of course more complicated
end
end
end
end
But although this would run and set the page.data['permalink'] field, the output was still the same.
Is there something I'm doing wrong, or is there a different way entirely of doing this? Thanks!
It can be easier to override the page class with something like this :
module Jekyll
class Page
alias orig_permalink permalink
def permalink
permalink = orig_permalink
newPermalink = "foo/#{permalink}"
end
end
end
Not tested.

How do I search a YAML file?

I have a YAML file books.yaml:
- !ruby.object:Book
title: Ruby for Newbz
author: LeeRoy Jenkins
category: Educational
I already have a method that adds books to this file, but I need a method that can search the YAML file using a regular expression. If no book matches the title then it should raise an exception NoBookfound. If there are any matches, that list should be returned to the caller.
Here is my existing code:
require 'yaml'
require './book'
class Library
attr_accessor :books
def initialize file_name = false
#books = file_name ? YAML::load(File.read(file_name)) : []
end
def add_book(book)
#books.push(book)
end
def search_library(file_name , book)
if
YAML::load(File.read(file_name)).include?(book) == false
raise 'No Book Found'
end
end
end
This is something I tried for the exception portion, but I feel like I'm way off. I'm really new to Ruby and this seems to be a pretty hard task. Does anyone have any ideas?
What you need is to test the class from the loaded object:
book = YAML::load(File.read(file_name))
raise 'No Book Found' unless book.kind_of? Book
There are also some kind_of? alternatives, which you can read about the differences on this nice question
Based on your post, I guess that book param is a Regexp object.
I think you should remove the file_name param from search method, since the file is already loaded on initialize method.
My implemetation would be:
def search_library term
#books.find_all {|b| b.title =~ term}
end
Since you already have "#books" in an enumerable you can use the select method to find books whose title have the search term as a substring.
def search_library(term)
matches = #books.select { |x| x.title.index(term) }
raise "No books have title like '#{term}'" if matches.empty?
matches
end

Resources