0

When I used selenium_webdriver and shows firefox, works fine; Now I migrate to headless browser, and configurate Poltergeist/PhantomJS in my automated tests, and not work's. Capybara / Poltergeist doesnt find elements anymore.

I mapped my elements with siteprism, here my example: element: link, "a[href='\my_link']"

When my tests run, throw this error: Capybara::ElementNotFound: Unable to find css "a[href='/my_link']"

But when I use the method find_all capybara/poltergeist find my element:

2016-08-26T19:27:21 [DEBUG] WebPage - evaluateJavaScript "(function() { return (function() { return PoltergeistAgent.stringify((function (name, args) {\n      return __poltergeist.externalCall(name, args);\n    }).apply(this, PoltergeistAgent.JSON.parse(\"[\\\"find\\\",[\\\"css\\\",\\\"a[href='/my_link']\\\"]]\"))) })(); })()"
2016-08-26T19:27:21 [DEBUG] WebPage - evaluateJavaScript result QVariant(QString, "{\"value\":[557]}")
{"command_id":"fc012779-4b24-4659-afba-14180c3aa717","response":{"page_id":1,"ids":[557]}} 
=> []#<Capybara::Result:0x3fd82e69da98>

This is my configuration of Poltergeist driver:

#env.rb

Capybara.default_max_wait_time = 10
    Capybara.register_driver :poltergeist do |app|
    options = {
        :js_errors => true,
        :timeout => 120,
        :debug => true,
        :window_size => [1280, 1024],
        :phantomjs_logger => File.open(FILE_PATH + "test_phantomjs.log", "w+"),
        :phantomjs_options => ['--load-images=no', '--disk-cache=false', '--ignore-ssl-errors=yes', '--debug=true', '--ssl-protocol=TLSv1'],
        :inspector => false,
    }
    Capybara::Poltergeist::Driver.new(app, options)
   end
    Capybara.javascript_driver = :poltergeist
    Capybara.default_driver = :poltergeist
    Capybara.current_driver = :poltergeist 

steps.rb

 Given(/^first step (\w+)$/) do |client_label|
  @client = @client[client_label] ||= get_client(@clients.values.map{|s| s.name})
  @app = App.new
  @app.home.load
end

And(/^step example (\w+)$/) do |var|
  @var = var
  @app.home.action(@var)
end

app.rb

class App
def home
    Home.new
end

def login
    Login.new
end

end

home.rb

class Home < SitePrism::Page
set_url "https://#{USERNAME}:#{PASSWORD}@#{URL}"

element :link_account,  "a[href='/account']"
element :link_1, "a[href='/link_1']"
element :link_2, "a[href='/link_2']"
element :link_3, "a[href='/link_3']"

##########################################
##########Methods of this page############
##########################################

def action(element)
    element = mapping_elements(element)
    test(element)
end

 def mapping_elements(var)
        case var
        when "option1"
            element = link_1
        when "option2"
            element = link_2
        when "option3"
            element = link_3
        end
    end
end

utils.rb

def test(element)
  click_element(element)
end

def click_element(element)
 element.click
end

I used :inspector => true in my Poltergeist register driver, to open chrome to view my page;

I used chrome console document.querySelectorAll("a[href='/link_3']") to select this element.

And returns the right element; [<a class=​"foobar" title=​"Title" href=​"/​link_3​">​text​</a>​]

If someone help me , I really appreciate;

Tks.

Jess
  • 53
  • 1
  • 1
  • 5
  • Can you post your test code? Do you have responsive web design on your page? – Michał Młoźniak Aug 26 '16 at 23:09
  • Your output from find_all actually shows that it's finding one element but it's not being returned due to failing one of the filters, most likely visibility - use save_screenshot to see what the page actually looks like, and make sure the link is actually visible. Another thing to check is what version of PhantomJS you are using - make sure its 2.1.1+ – Thomas Walpole Aug 27 '16 at 19:36
  • @TomWalpole I used save_screeenshot , but when I see the file, the image is blank; I tried to use save_page instead, and displays the html after scenario failed. And I see the element in the page =/ – Jess Aug 28 '16 at 14:25
  • @MichałMłoźniak what part of my code? – Jess Aug 28 '16 at 14:30
  • @user3722709 Your spec that is causing this error. – Michał Młoźniak Aug 28 '16 at 14:31
  • @MichałMłoźniak my spec? please, can you be more specific? I dont understand =/ – Jess Aug 28 '16 at 14:35
  • @user3722709 Capybara (default) doesn't find elements that aren't visible. Since your screenshot is blank it would tend to imply the element isn't visible. Whether thats because you screenshot before the page had rendered or theres an error in the page I don't know. Usually when using Capybara that is taken care of by Capybaras automatic waiting behavior, but SitePrism provides the "feature" of disabling that. You can try calling the SitePrism generated `wait_until_link3_visible` before `element = link3` (or whichever link you're using) - Also what version of PhantomJS are you using? – Thomas Walpole Aug 28 '16 at 16:43
  • @TomWalpole My PhantomJS version is 2.1.1; – Jess Aug 28 '16 at 17:14
  • @TomWalpole I put `wait_until_link3_visible` before `element = link3`; And raise an other error: `execution expired (SitePrism::TimeOutWaitingForElementVisibility)` – Jess Aug 28 '16 at 17:21
  • @user3722709 Ok so assuming the link should actually be visible and not overlapped by something else in a 1280x1024 page, that tells you the page isn't loading properly in PhantomJS - Is this a JS driven page where you're using ES6 methods that aren't supported by PhantomJS ? Without access to the html and relevant JS anything else is just going to be random guesses – Thomas Walpole Aug 28 '16 at 17:26
  • @TomWalpole I turn on `js_errors`, and throws 2 errors when execute the test: `{"command_id":"a992b623-d8f4-47c4-89c1-837a89b827c2","error":{"name":"Poltergeist.JavascriptError","args":[[{"message":"TypeError: undefined is not a constructor (evaluating 'o.play()')","stack":"TypeError: undefined is not a constructor (evaluating 'o.play()')\n at https://url.js:1 in yBgVideo\n at https://url2.js:16 in onload"}]]}}` – Jess Aug 28 '16 at 17:36
  • @user3722709 Ok - that would have been good to know originally since you're config above shows that you had it turned on already. PhantomJS doesn't support HTML5 audio/video elements, so it looks like you have JS errors in your onload handler which are probably preventing the rest of the page from properly loading. If the video element isn't critical to the page you need to change your JS to catch the errors and prevent them from killing the whole page loading, if they are critical you can't use poltergeist/phantomjs for these tests – Thomas Walpole Aug 28 '16 at 18:02
  • @Tom Walpole OMG! Do you have an alternative tool for Poltergeist/PhantomJs (headless browsers). – Jess Aug 29 '16 at 02:41
  • The only other headless browser for use with Capybara is capybara-webkit - if you want to try that make sure to build it with Qt 5.5.1 - IIRC Qt 5.6 doesn't work with it - and Qt 4 is so old that it doesnt support most modern stuff. Your other option would be to continue using Poltergeist for tests that don't require audio/video stuff and then use selenium (not headless) for the tests that do require audio/video stuff. – Thomas Walpole Aug 29 '16 at 02:45

1 Answers1

0

Old answer but worth updating

  • SitePrism by default mirrors capybara's logic of only finding visible elements. Which can be configured using SitePrism on a per-element basis (visible: false) or globally Capybara.ignore_hidden_elements = false

Also given the original question eventually wanted to find alternatives to Poltergeist / detro - There is now headless chrome/firefox

Luke Hill
  • 460
  • 2
  • 10