Rectangle 27 2

var page = new WebPage(),
     url = 'http://localhost/a-search-form',
     stepIndex = 0;

 /**
  * From PhantomJS documentation:
  * This callback is invoked when there is a JavaScript console. The callback may accept up to three arguments: 
  * the string for the message, the line number, and the source identifier.
  */
 page.onConsoleMessage = function (msg, line, source) {
     console.log('console> ' + msg);
 };

 /**
  * From PhantomJS documentation:
  * This callback is invoked when there is a JavaScript alert. The only argument passed to the callback is the string for the message.
  */
 page.onAlert = function (msg) {
     console.log('alert!!> ' + msg);
 };

 // Callback is executed each time a page is loaded...
 page.open(url, function (status) {
   if (status === 'success') {
     // State is initially empty. State is persisted between page loads and can be used for identifying which page we're on.
     console.log('============================================');
     console.log('Step "' + stepIndex + '"');
     console.log('============================================');

     // Inject jQuery for scraping (you need to save jquery-1.6.1.min.js in the same folder as this file)
     page.injectJs('jquery-1.6.1.min.js');

     // Our "event loop"
     if(!phantom.state){
       initialize();
     } else {
       phantom.state();
     } 

     // Save screenshot for debugging purposes
     page.render("step" + stepIndex++ + ".png");
   }
 });

 // Step 1
 function initialize() {
   page.evaluate(function() {
     $('form#search input.query').val('Jebus saves');
     $('form#search').submit();
     console.log('Searching...');
   });
   // Phantom state doesn't change between page reloads
   // We use the state to store the search result handler, ie. the next step
   phantom.state = parseResults; 
 }

 // Step 2
 function parseResults() {
   page.evaluate(function() {
     $('#search-result a').each(function(index, link) {
       console.log($(link).attr('href'));
     })
     console.log('Parsed results');
   });
   // If there was a 3rd step we could point to another function
   // but we would have to reload the page for the callback to be called again
   phantom.exit(); 
 }

Thanks a lot for your time and help. I have made minimal changes to make it executable on a real site, maybe i break doing it, but when i run the code it dont return, stay hours and dont return, only one screenshot (the first) is taken. anyway thanks a lot.

javascript - On PhantomJS I can't include jQuery and without jQuery I ...

javascript phantomjs evaluate
Rectangle 27 2

var page = new WebPage(),
     url = 'http://localhost/a-search-form',
     stepIndex = 0;

 /**
  * From PhantomJS documentation:
  * This callback is invoked when there is a JavaScript console. The callback may accept up to three arguments: 
  * the string for the message, the line number, and the source identifier.
  */
 page.onConsoleMessage = function (msg, line, source) {
     console.log('console> ' + msg);
 };

 /**
  * From PhantomJS documentation:
  * This callback is invoked when there is a JavaScript alert. The only argument passed to the callback is the string for the message.
  */
 page.onAlert = function (msg) {
     console.log('alert!!> ' + msg);
 };

 // Callback is executed each time a page is loaded...
 page.open(url, function (status) {
   if (status === 'success') {
     // State is initially empty. State is persisted between page loads and can be used for identifying which page we're on.
     console.log('============================================');
     console.log('Step "' + stepIndex + '"');
     console.log('============================================');

     // Inject jQuery for scraping (you need to save jquery-1.6.1.min.js in the same folder as this file)
     page.injectJs('jquery-1.6.1.min.js');

     // Our "event loop"
     if(!phantom.state){
       initialize();
     } else {
       phantom.state();
     } 

     // Save screenshot for debugging purposes
     page.render("step" + stepIndex++ + ".png");
   }
 });

 // Step 1
 function initialize() {
   page.evaluate(function() {
     $('form#search input.query').val('Jebus saves');
     $('form#search').submit();
     console.log('Searching...');
   });
   // Phantom state doesn't change between page reloads
   // We use the state to store the search result handler, ie. the next step
   phantom.state = parseResults; 
 }

 // Step 2
 function parseResults() {
   page.evaluate(function() {
     $('#search-result a').each(function(index, link) {
       console.log($(link).attr('href'));
     })
     console.log('Parsed results');
   });
   // If there was a 3rd step we could point to another function
   // but we would have to reload the page for the callback to be called again
   phantom.exit(); 
 }

Thanks a lot for your time and help. I have made minimal changes to make it executable on a real site, maybe i break doing it, but when i run the code it dont return, stay hours and dont return, only one screenshot (the first) is taken. anyway thanks a lot.

javascript - On PhantomJS I can't include jQuery and without jQuery I ...

javascript phantomjs evaluate
Rectangle 27 62

var fs = require('fs');
var cookies = JSON.stringify(phantom.cookies);
fs.write(cookieFilename, cookies, 644);
var fs = require('fs');
var data = fs.read(cookieFilename);
phantom.cookies = JSON.parse(data);

The phantom is global variable in PhantomJS. More information you can get in wiki

ugh, why the F doesn't this work by passing the damn flag. THANK YOU for this solution.

phantom
fs

thanks, very useful

Sign up for our newsletter and get our top new questions delivered to your inbox (see an example).

node.js - how to persist cookies between different casperjs processes ...

node.js session cookies phantomjs casperjs
Rectangle 27 62

var fs = require('fs');
var cookies = JSON.stringify(phantom.cookies);
fs.write(cookieFilename, cookies, 644);
var fs = require('fs');
var data = fs.read(cookieFilename);
phantom.cookies = JSON.parse(data);

The phantom is global variable in PhantomJS. More information you can get in wiki

ugh, why the F doesn't this work by passing the damn flag. THANK YOU for this solution.

phantom
fs

thanks, very useful

node.js - how to persist cookies between different casperjs processes ...

node.js session cookies phantomjs casperjs
Rectangle 27 11

@imos Great answer! It just helped me out and I was hoping I could add some more.

You can also add individual cookies to a page using:

var fs = require('fs')
var data = fs.read(file)
var cookies = JSON.parse(data)

for(var i = 0; i < cookies.length; i++) {
    phantom.addCookie(cookies[i]);
}

This might help if there are multiple cookie files.

node.js - how to persist cookies between different casperjs processes ...

node.js session cookies phantomjs casperjs
Rectangle 27 11

@imos Great answer! It just helped me out and I was hoping I could add some more.

You can also add individual cookies to a page using:

var fs = require('fs')
var data = fs.read(file)
var cookies = JSON.parse(data)

for(var i = 0; i < cookies.length; i++) {
    phantom.addCookie(cookies[i]);
}

This might help if there are multiple cookie files.

node.js - how to persist cookies between different casperjs processes ...

node.js session cookies phantomjs casperjs
Rectangle 27 0

It may be timing-related. If you exit phantom too quickly the changes to localStorage may not be persisted. Changing phantom.exit() to setTimeout(phantom.exit, 1500) seemed to work.

PhantomJS not persisting localStorage consistently - Stack Overflow

phantomjs
Rectangle 27 0

The phantom is global variable in PhantomJS. More information you can get in wiki

node.js - how to persist cookies between different casperjs processes ...

node.js session cookies phantomjs casperjs
Rectangle 27 0

It may be timing-related. If you exit phantom too quickly the changes to localStorage may not be persisted. Changing phantom.exit() to setTimeout(phantom.exit, 1500) seemed to work.

PhantomJS not persisting localStorage consistently - Stack Overflow

phantomjs
Rectangle 27 0

@imos Great answer! It just helped me out and I was hoping I could add some more.

You can also add individual cookies to a page using:

This might help if there are multiple cookie files.

node.js - how to persist cookies between different casperjs processes ...

node.js session cookies phantomjs casperjs
Rectangle 27 0

var page = new WebPage(),
     url = 'http://localhost/a-search-form',
     stepIndex = 0;

 /**
  * From PhantomJS documentation:
  * This callback is invoked when there is a JavaScript console. The callback may accept up to three arguments: 
  * the string for the message, the line number, and the source identifier.
  */
 page.onConsoleMessage = function (msg, line, source) {
     console.log('console> ' + msg);
 };

 /**
  * From PhantomJS documentation:
  * This callback is invoked when there is a JavaScript alert. The only argument passed to the callback is the string for the message.
  */
 page.onAlert = function (msg) {
     console.log('alert!!> ' + msg);
 };

 // Callback is executed each time a page is loaded...
 page.open(url, function (status) {
   if (status === 'success') {
     // State is initially empty. State is persisted between page loads and can be used for identifying which page we're on.
     console.log('============================================');
     console.log('Step "' + stepIndex + '"');
     console.log('============================================');

     // Inject jQuery for scraping (you need to save jquery-1.6.1.min.js in the same folder as this file)
     page.injectJs('jquery-1.6.1.min.js');

     // Our "event loop"
     if(!phantom.state){
       initialize();
     } else {
       phantom.state();
     } 

     // Save screenshot for debugging purposes
     page.render("step" + stepIndex++ + ".png");
   }
 });

 // Step 1
 function initialize() {
   page.evaluate(function() {
     $('form#search input.query').val('Jebus saves');
     $('form#search').submit();
     console.log('Searching...');
   });
   // Phantom state doesn't change between page reloads
   // We use the state to store the search result handler, ie. the next step
   phantom.state = parseResults; 
 }

 // Step 2
 function parseResults() {
   page.evaluate(function() {
     $('#search-result a').each(function(index, link) {
       console.log($(link).attr('href'));
     })
     console.log('Parsed results');
   });
   // If there was a 3rd step we could point to another function
   // but we would have to reload the page for the callback to be called again
   phantom.exit(); 
 }

Thanks a lot for your time and help. I have made minimal changes to make it executable on a real site, maybe i break doing it, but when i run the code it dont return, stay hours and dont return, only one screenshot (the first) is taken. anyway thanks a lot.

javascript - On PhantomJS I can't include jQuery and without jQuery I ...

javascript phantomjs evaluate
Rectangle 27 0

In plain phantomjs there is the command line flag --cookies-file=/path/to/cookies.txt which can be used to persist the session cookie and use it in the next invocation.

The .net API provides the same functionality through the PhantomJSDriverService class. This is taken from this answer.

DriverService service = PhantomJSDriverService.CreateDefaultService(driverpath);
service.CookiesFile = "path/to/cookies.txt";
IWebDriver driver = new PhantomJSDriver(service);

If you want to have a little more control, then you should save the session cookie from window.document.cookie using driver.ExecuteScript into a file and retrieve it later.

does the cookiefile need to be stored in a cookie file? can store in database? its just a string right?

Yes, but I guess, the problem is that using it through service.CookiesFile, you would need a file. With window.document.cookie you can dynamically store and retrieve without falling back to a file.

I'll just use a file, :)

c# - Selenium phantomjs webdriver, how do I use sessions? - Stack Over...

c# selenium selenium-webdriver phantomjs
Rectangle 27 0

1. When filling in forms, ensure AngularJS bindings are complete and C...

Problems arose when the test was using capybara's fill_in DSL. In some test runs the fields would be populated correctly and the form would be submitted. In the other scenario the form would be filled in correctly but the submit button would be hit too quickly. The result here is that a record was created but input fields of name and description were not persisted.

AngularJS' ng-if statements needed to be used not to show form fields until they are ready. This needed to be done in conjunction with the use of Capybara waiting methods to ensure fill_in field are only submitted once form load has completed.

<div  ng-if="tableParams.data">
  <table id="costings_table ng-table="tableParams" class="table">
    <td id="field1">{{table.field1}}</td>
    <td id="field2">{{table.field2}}</td>
   </table>
</div>

This seemed to enable tests to run without as many Capybara waiting methods in order to achieve reliable tests.

require "rails_helper"

RSpec.describe "Show a new costing in the listing,", :type => :feature do

  before :each do
    admin_sign_in
    create_costing("test1")
  end

  it "shows the costing after creation" do
    within "#costings_table" do
      expect(page.find("#code2")).to have_content("2")
      expect(page.find("#name2")).to have_content("test1")
    end
  end

  it "shows the details of the new costing after creation" do
    within "#costings_table" do
      click_on "show2"
    end

    expect(page.find("#page_title")).to have_content("Costing Details")
    expect(page.find("#code")).to have_content("2")
    expect(page.find("#name")).to have_content("test1") 
    expect(page.find("#description")).to have_content("test description")
  end
end
# This file is copied to spec/ when you run 'rails generate rspec:install'
ENV["RAILS_ENV"] ||= 'test'
require File.expand_path("../../config/environment", __FILE__)

# Add library functions here so we can test them.
require File.expand_path(File.dirname(__FILE__) + "/../lib/general")

require 'rspec/rails'
require 'devise'

RSpec.configure do |config|
  config.before(:suite) do
    # Requires supporting ruby files with custom matchers and macros, etc,
    # in spec/support/ and its subdirectories.
    require File.expand_path(File.dirname(__FILE__) + "/support/blueprints")
    Dir[Rails.root.join("spec/support/**/*.rb")].each {|f| require f}

    # Setup Devise before it is used in rails_helper
    config.include Devise::TestHelpers, :type => :controller
    Devise.stretches = 1 # Improves speed.
end

config.include Capybara::DSL, :type => :feature
  config.mock_with :rspec

# Remove this line if you're not using ActiveRecord or ActiveRecord fixtures
config.fixture_path = "#{::Rails.root}/spec/fixtures"

# Allow a 'focus' tag so that we can run just a few tests which we are currently working on
config.filter_run focus: true
config.run_all_when_everything_filtered = true
config.filter_run_excluding :slow unless ENV["SLOW_SPECS"]

# Defer Garbage Collection
config.before(:all) { DeferredGarbageCollection.start }
config.after(:all)  { DeferredGarbageCollection.reconsider }

# Integration Testing
require 'capybara/rspec'
require 'capybara/poltergeist'

Capybara.register_driver :poltergeist_debug do |app|
  Capybara::Poltergeist::Driver.new(app, {:inspector => true, js_errors: false })  
end

Capybara.javascript_driver = :poltergeist_debug
Capybara.default_driver = :poltergeist_debug

# Debugging tools
def debugit
  puts current_url
  require 'pry'
  binding.pry
end

# If you're not using ActiveRecord, or you'd prefer not to run each of your
# examples within a transaction, remove the following line or assign false
# instead of true.
config.use_transactional_fixtures = false

#Show Deprications As Errors with full backtracing
config.raise_errors_for_deprecations!

#rest of the file....
# Final part of Configure Database Cleaner

Capybara.default_max_wait_time = 5
config.use_transactional_fixtures = false

config.before(:suite) do
  if config.use_transactional_fixtures?
    raise(<<-MSG)
      Delete line `config.use_transactional_fixtures = true` from
      rails_helper.rb (or set it to false) to prevent uncommitted
      transactions being used in JavaScript-dependent specs. During 
      testing, the app-under-test that the browser driver connects to 
      uses a different database connection to the database connection 
      used by the spec. The app's database connection would not be 
      able to access uncommitted transaction data setup over the 
      spec's database connection.
     MSG
  end
  DatabaseCleaner.clean_with(:truncation)
end  

config.before(:each) do
  DatabaseCleaner.strategy = :transaction
end

config.before(:each, type: :feature) do
  # :rack_test driver's Rack app under test shares database connection
  # with the specs, so continue to use transaction strategy for speed.
  driver_shares_db_connection_with_specs = Capybara.current_driver == :rack_test

    if !driver_shares_db_connection_with_specs
      # Driver is probably for an external browser with an app
      # under test that does *not* share a database connection with the
      # specs, so use truncation strategy.
      DatabaseCleaner.strategy = :truncation
    end
  end

  config.before(:each) do
    DatabaseCleaner.start
  end

  config.append_after(:each) do
    DatabaseCleaner.clean
  end
end


def admin_sign_in
  visit "/login"

  #Create staff member in database
  Staff.make!(:admin)

  #Log In
  fill_in "staff_username", with: "adminstaff"
  fill_in "staff_password", with: "password"
  click_button "login"

  expect(page).to have_text('Logout')
end

def create_costing(item)
  @item = item
  visit "/api#/costings"

  expect(page).to have_selector("#new_btn")
  click_on "new_btn"

  expect(page).to have_text("New Costing")
  within "#form_costing" do
    fill_in "name", with: "#{@item}"
    fill_in "description", with: "test description"
    fill_in "from_date1", with: "15/02/2015" 
    fill_in "cost_hourly_cents1", with: "12.00"

    expect(page).to have_selector("#create_btn")
    click_on "create_btn"
  end
  expect(page.find("#page_title")).to have_content("Costings")
end

rspec - Unreliable/Flakey Capybara/AngularJS Integration Tests With Ti...

rspec phantomjs capybara integration-testing poltergeist