Capybara with javascript and factory girl - javascript

I have one factory with inheritance
FactoryGirl.define do
factory :client do
sequence(:first_name) { |n| "John#{n}" }
sequence(:last_name) { |n| "Smith#{n}" }
factory : client_with_dialog do
show_popup true
end
On my react code I am getting from the server the client show popup value.
$.get('/client/get_choise')
.done(function (result) {
if (result.show_popup) {
alert();
}
});
test code:
scenario 'visit page with popup' do
client = create :client_with_dialog
visit_page_with_popup
end
On my capybara testing the popup is not showing,
My question is does the $.get('/client/get_choise') code actually run in tests or not?
How can I write a capybara test that will actually open the popup window?

It sounds like you're using the rack-test driver which doesn't support JavaScript. Start here https://github.com/jnicklas/capybara#selecting-the-driver

Related

Mail Rules with javascript instead of applescript

In a Mac mail rule, I am trying to run a javascript instead of an applescript. This has been asked in Mail Rules using JavaScript for Automation instead of AppleScript but the answer is not working for me!
I have tried to simplify the code as much as I can. So, the following applescript works fine:
on perform mail action with messages theMessages
say "running!"
end perform mail action with messages
but the equivalent javascript is not working.
function performMailActionWithMessages(messages) {
app = Application.currentApplication()
app.includeStandardAdditions = true
app.say ("running")
}
Edit
Here are my rule parameters
I do it without getting app. Try something like…
// ObjC.import('Cocoa')
ObjC.import('stdlib')
var program="/usr/bin/say"
function performMailActionWithMessages(messages) {
say("running")
}
function say(what){
var command = program + " '"+what+"'"
console.log(command)
$.system(command)
}
I’m not sure you need cocoa. You probably need stdlib.

Automate End to End Test of Google.Com through Protractor

I am trying to make Protractor go to google.com and search a term.
I have come to where protractor loads the non angular page Google
and then puts the text in. How do I make it press "enter" or click
the button?
In addition I am not finding any guides on how to write protractor
tests and the functions available? I am also new to JS and Angular.
Should I learn more AngularJS concepts or Protractor Concepts?
spec.js:
browser.waitForAngularEnabled(false);
describe('Enter Search Term', function() {
it('This will insert into the text field in google.com', function() {
browser.get('www.google.com');
element(by.xpath('//*[#id="q"]')).sendKeys('What is Protractor?');
var query = element(by.xpath('//*[#id="q"]'));
expect(query = 'What is Protractor?');
browser.pause();
});
});
conf.js:
exports.config = {
seleniumAddress: 'http://localhost:4444/wd/hub',
specs: ['spec.js']
};
Mapping the page links by xpath does the job.
https://github.com/SDasman/Angular_Protractor_End2End_Tests/tree/master/Protractor_Google

mocha-casperjs headless testing of a multi-client node.js app

I have a node.js application that runs on the Sails.js MVC Framework. The application supports connections from multiple clients using socket.io in real-time. It supports different roles. For example, you can login as a standard user or as a moderator.
For testing, I use mocha-casperjs (and chai and other stuff that is not important for my question) and run the tests from grunt using grunt mocha_phantomjs. The tests are specified in the Gruntfile.js file in the root folder of my project. They are specified like this:
Gruntfile.js:
mocha_casperjs: {
files: {
src: [
'test/single-client.js',
'test/multi-client.js',
]
}
}
Here is a test case I want to verify: Once the Moderator clicks on a specific button, a div.container should appear on both the Moderator's view as well as the standard user's view.
Now, testing the single-client aspect works well. Here is a basic scenario:
test/single-client.js:
describe('Clicking on a Button as a Moderator', function() {
it('results in div.container to appear on the Moderators view', function() {
casper
.start('http://localhost:1337')
.logInAsMod() //Note: This is pseudocode for submitting a login form
.thenClick('button')
.then(function() {
('div.container').should.be.inDOM.and.visible;
})
})
}
which results in:
Clicking on a Button as a Moderator
✓ results in div.container to appear on the Moderator's view
However, I'm struggling in testing the multi-client aspect of this test case: The div.container should not only appear on the Moderator's view, but also on the standard user's view. From what I understand, the casper.run() method, which starts the tests defined earlier in the source code, will be called by the mocha-phantomjs module. So something like this won't work:
test/multi-client.js:
describe('Clicking on a Button as a Moderator', function() {
it('results in div.container to appear on the Moderators view and on the standard users view', function() {
casper
.start('http://localhost:1337')
.logInAsMod() //Note: This is pseudocode for submitting a login form
.thenClick('button')
.then(function() {
('div.container').should.be.inDOM.and.visible;
})
casper
.logInAsUser() //Note: This is pseudocode for submitting a login form
.then(function() {
('div.container').should.be.inDOM.and.visible;
})
})
}
The problem with the code above is that the casper.run() call only creates one instance of casperjs. It would be really great if I could write something like this:
var casperMod = require('casper').create();
var casperUser = require('casper').create();
casperMod
.start('http://localhost:1337')
.logInAsMod() //Note: This is pseudocode for submitting a login form
casperUser
.start('http://localhost:1337')
.logInAsUser() //Note: This is pseudocode for submitting a login form
casperMod
.thenClick('button')
.then(function() {
('div.container').should.be.inDOM.and.visible;
})
casperUser
.then(function() {
('div.container').should.be.inDOM.and.visible;
})
So I would have two instances of casperjs that I can write the routines for. However, this results in Error: Cannot find module 'casper' because the mocha-casperjs framework does not support the inclusion of another casperjs instance.
Permanently logging out and in again is not an option, because I want to test the real-time aspect of the application.
Does anyone have a suggestion in how to achieve multiple instances of casperjs when using mocha-casperjs?
You will have problems with this in any framework as you need two sets of cookies, basically two browsers running. Even if you did create two casper instances, you're still in one phantomjs process that shares the cookies.
What can do is create a new WebPage instance and swap that out on the current casper instance, plus swap the cookies:
casper
.start('http://localhost:1337')
.logInAsMod()
.thenClick('button')
.then(function() {
('div.container').should.be.inDOM.and.visible;
})
.then(function() {
var userPage = require('webpage').create(),
modCookies = phantom.cookies
phantom.clearCookies()
casper
.logInAsUser()
.then(function() {
('div.container').should.be.inDOM.and.visible;
})
})

Server-Sent Event method not handled by Firefox

I am building a rails application using ActionController::Live and and Custom SSE library,
My server is puma, and rails version is 4 this is my code in my controller:
include ActionController::Live
def index
response.headers['Content-Type'] = 'text/event-stream'
ss = Reloader::SSE.new(response.stream)
100.times {
ss.write({ :message => "just checking"}, :event => 'refresh')
sleep 10
}
ensure
ss.close
end
and this is my Library sse.rb file
class SSE
def initialize io
#io = io
end
def write object, options = {}
options.each do |k,v|
#io.write "#{k}: #{v}\n"
end
#io.write "data: #{JSON.dump(object)}\n\n"
end
def close
#io.close
end
end
and this is my application.js file to handle SSE
$(document).ready(function() {
setTimeout(function() {
var source = new EventSource('/');
source.addEventListener('refresh', function(e) {
window.location.reload();
});
}, 1);
});
I am following the tutorial from this site http://tenderlovemaking.com/2012/07/30/is-it-live.html
Whenever I load my Firefox page, it is not handling the stream, instead it is showing the option to download the stream as text file. I don't why this is happening.
My understanding here of the SSE is at fault here, the SSE and the page/controller i am working, i took them as one, i was trying to view the controller of SSE in firefox or chrome. Very poor understanding on my part of the SSE. SSE controller and the page i want to trigger events are completely different. I got this answer at the end of this tutorial
http://tenderlovemaking.com/2012/07/30/is-it-live.html
Hope you guys got it too.

Web site not working as expected with CasperJS

I'm using CasperJS 1.0.2 under PhantomJS 1.8.1 on Windows 8.
Trying to write a test for a web site. The site is heavily reliant on JS and the coding principals are quite unusual, which may be creating some problems but I'm not sure.
Here is the code I'm using to test login and search function:
var url = 'http://www.testsite.com/';
var casper = require('casper').create();
casper.start();
casper.start(url, function() {
this.echo('Page: ' + this.getTitle());
this.capture('start.png');
if (this.exists('input#TxtUserName')) {
this.sendKeys('input#TxtUserName', 'testlogin');
this.sendKeys('input#TxtPassword', 'testpass');
this.click('input#BtnLogin');
this.capture('loggedin.png');
}
});
casper.then(function() {
this.capture('beforesearch.png');
this.sendKeys("input#txtSearch", '1002');
this.click("input#cmdSubmit");
this.echo('Searching');
this.capture('aftersearch.png');
});
casper.run();
When I run this code, every page on the screen capture is the same with the exception that the login information is filled in on login.png. At no point does it actually login (using my real login credentials) after the click event. The search results also don't show after that click is fired.
Any clue what could be causing this?
Here is my waitFor code after submitting the search:
casper.waitForText("Part:", function() {
this.capture('searchresults.png');
});
You should use casper.waitFor to make sure the next page has been loaded. Otherwise phantom will take the screenshot before the form submit has been answered.

Categories

Resources