page_url vs navigate_to in page-object gem + Jruby - jruby

I am trying to use jruby + page-object gem + Cucumber for a proof of concept. I used the following statement.
app_url = 'https:\\google.com'
page_url(app_url)
I get a
NoMethodError: undefined method `page_url' for #
However,
navigate_to(app_url)
works fine. page_url works fine in Ruby.
Is this the way this works in jRuby? Though navigate_to works, is this any different?
Thank you for your help!

page_url is a class method provided by including the PageObject module. It sets the url for the page so you can use the visit_page factory in your test:
object MyPage
include PageObject
page_url "http://example.com/"
end
In a test somewhere:
visit_page MyPage do |page|
page.some_object_element.do_something
end
navigate_to is browser functionality exposed directly in your test via some World magic.

Related

How to render HTML using a Ruby on Rails 5 API Application

We're using Ruby on Rails 5.0.0.1 in API-Mode. What Middleware and Configurations do I need to add, in order to being able to render html instead of json in an controller.
Edit:
Thank you all for your answers.
Want I wanted is to render normal erb/haml views. not html in json like 'kartik upadhyay' mentioned. Since the Application is primary an JSON API we didn't want the full blown default Rails Installation, which mean our Application Controller extends from ActionController::API.
My Plan was to make a PdfRendering Controller which includes all Modules needed for ActionView to work. What I rather did was reading the asset pipeline (because we wanted to use sass) and inherit the application controller from ActionController::Base. as other rails apps would do.
It's not the nicest solution since you're including the whole ActionController::Base and all of it's Feature that you may not be using. But it's still slimmer that a full Blown Rails installation (especially the middleware aka. sessions etc.)
include ActionView::Layouts
include ActionController::Rendering
Add these to your controller
You can use render to string method of rails for rendering your html as string inside json, put this inside your controller:
render json: { data: render_to_string('html_file_name') }
this will render html as response like:
{"data": "<html>\n<h3>hello</h3>\n</html>"}
you can render erb/haml/slim etc. files like the following:
format.html { render :file_name }
You can simply use this gem RABL.
https://github.com/nesquena/rabl

How to render html file as haml

I'm fairly new to ruby and working on building a front-end styleguide that has html snippets I'd like to render as haml into a pre tag. I'm building a helper for middleman and have figured out how to read an HTML file and output its contents. Now I'd like to convert the html to haml and output that.
Looking around it seems like the html2haml gem is what I want to use, though the doc on that gem seems to only cover using it on the command line, whereas I'm trying to add this functionality to a helper.
Here is what I have so far for a helper
helpers do
def render_snippet(page)
p1 = ("<pre><code>").html_safe
p2 = File.read("source/"+"#{page}")
p3 = ("</code></pre>").html_safe
p0+p1+p2+p3
end
end
Here is how I'm using the helper
= render_snippet "partials/examples/typography/elements.html"
To answer your question, this is how you can make a helper to use html2haml gem outside the terminal shell commands
# some_view.html.erb
<%= render html_2_haml("home/my_partial.html") %>
# app/helpers/application_helper.rb
module ApplicationHelper
def html_2_haml(path)
file_name = path.split("/").last
path_with_underscore = path.gsub(file_name, "_#{file_name}")
system "html2haml app/views/#{path_with_underscore} app/views/#{path_with_underscore}.haml"
"#{path}.haml"
end
end
Now i'd like to say this definitely will not work in production (as it's dynamically creating a new file and hosting services like Heroku just won't allow that) but if your just making yourself a development helper for this-and-that then perhaps this could be helpful to you.
I ended up working on this some more and ended up with the following:
def render_html2haml(file)
templateSource = preserve(File.read("source/"+"#{file}"))
haml = Html2haml::HTML.new(templateSource, {:erb => nil})
content_tag(:pre, content_tag(:code, haml.render))
end

Config.ru running as server not reading html

I recently began programming and learning Ruby and JavaScript and was attempting to read my html file through my Sinatra server using a config.ru file.
The server runs, its hitting all the routes but I think there may be something wrong with the server code for the index page:
get("/") do
content_type :html
File.read( File.expand_path("../views/index.html", __FILE__) )
end
Put index.html in public folder. Sinatra will serve files in public as is. So you need to request it directly e.g. http://localhost/index.html.
If you want to handle empty route i.e. get '/' use snippet below (from here):
get '/' do
send_file File.join(settings.public_folder, 'index.html')
end
In order to be sure in settings.public_folder please check does it work correctly, does it return correct path.
Cheers!

__persistence__ NoMethodError

I'm mocking a java interface in rspec
clock = ClockInterface.new
clock.should_receive(:currentTime)
When I run rspec everything works fine but I see a warning which directs me to the following
https://github.com/jruby/jruby/wiki/Persistence
When I attempt to set
ClockInterface.__persistence__ = true
I get a NoMethodError. I'm using jruby 1.7.4
ClockInterface is an interface rather than a class, and doesn’t have the __persistent__ method, unlike classes for which that method gets added through their proxy.
To get your test to work properly, you should instead use:
clock = mock(ClockInterface)
clock.should_receive(:currentTime)

JRuby with a custom ClassLoader?

I'm trying to use a custom ClassLoader in JRuby so my gem's loaded classes don't conflict with any project that requires my gem.
To that end, I can pretty easily create a new URLClassLoader and call loadClass to get my class loaded but what I get back is an unwrapped Java Class that it doesn't seem I can do much with easily.
I'd like to be able to do something like this:
clazz = java_class_loader.loadClass 'org.foo.bar.Class'
wrapped = JRuby.wrap_class clazz
wrapped.static_method 1, 2, 3 # call a static method
wrapped::CONSTANT # get a constant
Any ideas? I've looked all over the JRuby source for a good endpoint and I've tried many of the methods out of JavaSupport like get_proxy_class or likewise to no good effect. I can get and instance of JavaClass from my class but it doesn't seem to do me much good.