NoMethodError: undefined method `asText' for #<Java::JavaUtil::ArrayList:0x1e03a61> - htmlunit

I'm trying to parse a simple html page http://www.brainjar.com/java/host/test.html using the jruby and htmlunit but i encountered the error undefined method 'asText'.
my code:
require 'java';
require 'commons-codec-1.7.jar';
require 'commons-collections-3.2.1.jar';
require 'commons-io-2.4.jar';
require 'commons-lang3-3.1.jar';
require 'commons-logging-1.1.1.jar';
require 'cssparser-0.9.8.jar'
require 'htmlunit-2.11.jar'
require 'htmlunit-core-js-2.11.jar'
require 'httpclient-4.2.2.jar'
require 'httpcore-4.2.2.jar'
require 'httpmime-4.2.2.jar'
require 'jetty-http-8.1.7.v20120910.jar'
require 'jetty-io-8.1.7.v20120910.jar'
require 'jetty-util-8.1.7.v20120910.jar'
require 'jetty-websocket-8.1.7.v20120910.jar'
require 'nekohtml-1.9.17.jar'
require 'sac-1.3.jar'
require 'serializer-2.7.1.jar'
require 'xalan-2.7.1.jar'
require 'xercesImpl-2.10.0.jar'
require 'xml-apis-1.4.01.jar'
java_import 'com.gargoylesoftware.htmlunit.WebClient';
def get_desc
wc = WebClient.new;
page = wc.getPage("http://www.brainjar.com/java/host/test.html");
a = page.getByXPath('/html/body/p')
ss = a
puts ss.asText
end
get_desc

Related

Escape/Bypass 404 error in Ruby (Instagram API)

I'm trying to get ruby to escape/bypass a 404 error but it doesn't seem to be working. Here's my code so far:
require 'rubygems'
require 'simple_oauth'
require 'cloudsight'
require 'rubygems'
require 'net/http'
require 'uri'
require 'json'
require 'open-uri'
require 'openssl'
require 'hpricot'
require 'nokogiri'
#T FILE TO JPG
webby = 'https://t.co/5h92pCmoPM'
OpenSSL::SSL::VERIFY_PEER = OpenSSL::SSL::VERIFY_NONE
doc = Hpricot(open(webby)).to_s
jpgstart = '<meta property=\"og:image\" content=\"'
jpgstop = '" />'
jpglink = doc[/#{jpgstart}(.*?)#{jpgstop}/m, 1]
print jpglink
This is the error I get:
C:/Users/User/Desktop/test89:18: warning: already initialized constant OpenSSL::SSL::VERIFY_PEER
C:/Ruby21/lib/ruby/2.1.0/open-uri.rb:353:in `open_http': 404 NOT FOUND (OpenURI::HTTPError)
from C:/Ruby21/lib/ruby/2.1.0/open-uri.rb:724:in `buffer_open'
from C:/Ruby21/lib/ruby/2.1.0/open-uri.rb:210:in `block in open_loop'
from C:/Ruby21/lib/ruby/2.1.0/open-uri.rb:208:in `catch'
from C:/Ruby21/lib/ruby/2.1.0/open-uri.rb:208:in `open_loop'
from C:/Ruby21/lib/ruby/2.1.0/open-uri.rb:149:in `open_uri'
from C:/Ruby21/lib/ruby/2.1.0/open-uri.rb:704:in `open'
from C:/Ruby21/lib/ruby/2.1.0/open-uri.rb:34:in `open'
from C:/Users/KVadher/Desktop/test89:20:in `<main>'
[Finished in 2.7s with exit code 1]
Does anyone have any idea how I could bypass the error?
Is this what you had in mind?
require 'rubygems'
require 'simple_oauth'
require 'cloudsight'
require 'rubygems'
require 'net/http'
require 'uri'
require 'json'
require 'open-uri'
require 'openssl'
require 'hpricot'
require 'nokogiri'
#T FILE TO JPG
webby = 'https://t.co/5h92pCmoPM'
OpenSSL::SSL::VERIFY_PEER = OpenSSL::SSL::VERIFY_NONE
begin
doc = Hpricot(open(webby)).to_s
jpgstart = '<meta property=\"og:image\" content=\"'
jpgstop = '" />'
jpglink = doc[/#{jpgstart}(.*?)#{jpgstop}/m, 1]
print jpglink
rescue OpenURI::HTTPError
# do nothing. bypass error.
end

uninitialized constant error with SitePrism page

I've added this to my spec_helper:
require 'capybara'
require 'capybara/dsl'
require 'capybara/rspec'
require 'selenium-webdriver'
require 'site_prism'
And my page is this:
class AboutPage < SitePrism::Page
end
My rspec is this:
require_relative 'spec_helper'
describe 'About Page' do
it "test" do
about = AboutPage.new
end
end
The error I keep getting is:
Failures:
1) About Page test
Failure/Error: about = AboutPage.new
NameError:
uninitialized constant AboutPage
# ./about_spec.rb:6:in `block (2 levels) in '
If AboutPage is not in the root path of controller, you must call it with a namespace like:
about = SomeNameSpace::AboutPage.new
It looks like I needed to add to my spec_helper.rb:
require_relative about_page.rb
All pages you are using in your spec files have to be required before the can be used.
If all of your site prism objects are in spec/page_objects, using rspec, you could add this line to rails_helper.rb
Dir[Rails.root.join('spec/page_objects/**/*.rb')].each { |f| require f }
This will automatically require about_page.rb and any other page objects, including subfolders.

Printing to file from Ruby pp

I'm parsing a JSON file in Ruby and want to output the results using pp to a file. How can I do that? Here's the code I'm trying:
require 'rubygems'
require 'json'
require 'pp'
json = File.read('players.json')
plyrs = JSON.parse(json)
File.open('plyrs.txt', 'a') { |fo| pp page, fo }
require "rubygems" is redundant in Ruby >= 1.9.
require "json"
require "pp"
plyrs = JSON.load("players.json")
File.open("plyrs.txt", "a"){|io| io.write(plyrs.pretty_inspect)}
Try this
require 'rubygems'
require 'json'
require 'pp'
json = File.read('players.json')
plyrs = JSON.parse(json)
File.open('plyrs.txt', 'a') { |file| file.write(pp plyrs) }
More info available at the ruby documentation

Unable to make a rake-tasks file and make it work properly

I decided to create a rake tasks for my Sinatra project and not to use the ready ones.
#Rakefile
require 'rake/testtask'
require 'rake/clean'
Dir.glob("tasks/*.rake").each { |r| import r }
#/tasks/seed.rake
require 'rubygems'
require 'bundler'
Bundler.require
require 'mongoid'
require_relative '../models/user'
namespace :db do
task :seed do
puts 'Creating a user....'
user1 = User.new email: "email1#gmail.com", password: "test123"
user1.save!
puts 'User has been created.'
end
end
#user.rb
require 'bcrypt'
require 'digest/md5'
require 'openssl'
class User
include Mongoid::Document
include Mongoid::Timestamps
#.........
#gemfile (partly)
source 'http://rubygems.org'
gem 'bcrypt-ruby', require: 'bcrypt'
And I've got the error of "Creating a user....
rake aborted!
undefined method `create!' for BCrypt::Password:Class
/home/alex/ruby_projects/service/models/user.rb:47:in `password='"
where #47 looks like
def password= pass
self.hashed_password = BCrypt::Password.create! pass, cost: 10
end
Note that in normal development everything works just fine. So I missed to require a file I think.
Your thoughts?
p.s. Even if I put
require 'bcrypt'
require 'digest/md5'
require 'openssl
to /tasks/seed.rake the error remains.
It appears you are using a non-existant method from BCrypt::Password. According to the docs, there is only a .create method and no .create! method. Switch to BCrypt::Password.create and it should work.
def password= pass
self.hashed_password = BCrypt::Password.create pass, cost: 10
end

how to run a standalone Capybara test?

I'm trying to run a test against a remote server. i.e:
require 'rubygems'
require 'capybara'
require 'capybara/dsl'
Capybara.default_driver = :selenium
Capybara.app_host = 'http://www.google.com'
module MyCapybaraTest
include Capybara
def test_google
visit('/')
end
end
question is, how do you run it?
Save
require 'rubygems'
require 'capybara'
require 'capybara/dsl'
Capybara.run_server = false
Capybara.current_driver = :selenium
Capybara.app_host = 'http://www.google.com'
module MyCapybaraTest
class Test
include Capybara::DSL
def test_google
visit('/')
end
end
end
t = MyCapybaraTest::Test.new
t.test_google
to test.rb and simply: ruby test.rb
I found this standalone cucumber thingy using the selenium driver a few days ago and got it up and running in a few minutes:
https://github.com/thuss/standalone-cucumber
I had to make a few mods:
My Gemfile is
source "http://rubygems.org"
group(:test) do
gem 'cucumber'
gem 'capybara'
gem 'rspec'
gem 'selenium-webdriver', '2.5.0'
end
And this is my env.rb
begin require 'rspec/expectations'; rescue LoadError; require 'spec/expectations'; end
require 'capybara'
require 'capybara/dsl'
require 'capybara/cucumber'
require 'selenium-webdriver'
Capybara.default_driver = :selenium
Capybara.app_host = 'http://something'
World(Capybara)

Resources