Proplem with url using in Casperjs - javascript

I have problem with the url of my website that I'm doing on it with casperjs.I want to get title and test the class or id that have in this page,and set the value to textbox in this website by using casperjs.
//var url = 'https://www.google.com.kh/';
//this url below it is not work with my code if the url above it work normal
var url = 'https://teleservices.paris.fr/etatcivil/jsp/site/RunStandaloneApp.jsp?page=formengine&form=naissance';
var casper = require('casper').create();
casper.start(url, function() {
this.echo(url);
this.echo("start page...");
});
casper.then(function(){
this.wait(1000,function(){
this.echo('Page: ' + this.getTitle());
});
}),
casper.then(function() {
if(this.exists("div.job-search-box-text")){
this.echo("this is id found");
}
else{
this.echo("no ID found");
}
}),
casper.then(function(){
this.wait(1000,function(){
this.fillSelectors('form', {
'input[name="title"]' : "12"
});
this.capture('result.png');
});
});
casper.run();
I have the problem with this url when I set the var url = 'https://teleservices.paris.fr/etatcivil/jsp/site/RunStandaloneApp.jsp?page=formengine&form=naissance'; my result code can not display the title of the web page,can not get the each id or class in this webpage,can not capture....I'm stuck with this anyone have solution , I have tried it for long time,please help me, thanks.

Wow - I can't figure out that is going on here. I got the code to work but I wasn't able to figure out exactly what fixed it.
There are a few oddities with the site, sometimes an image will 404 for me and I noticed an odd url structure. Note the //css/
https://teleservices.paris.fr/etatcivil//css/plugins/images/local/skin/plugins/etatcivil/left-first-srub.gif
I wish there was some key lesson to give you here - I am sure there is but I don't know it :)
var url = "https://teleservices.paris.fr/etatcivil/jsp/site/RunStandaloneApp.jsp?page=formengine&form=naissance";
var casper = require('casper').create({
logLevel:"debug",
verbose: true,
waitTimeout: 6000
});
casper.userAgent('Mozilla/4.0 (compatible; MSIE 10.0; Windows NT 6.1)');
casper.start(url, function() {
this.echo(url);
this.echo("start page...");
});
casper.waitForSelector('#formengine-menu', function() {
this.echo('Page: ' + this.getTitle());
});
casper.then(function() {
if(this.exists("#naissancelieuEtDate")){ this.echo("Form naissancelieuEtDate exists"); }
else{ this.echo("no ID found"); }
});
casper.then(function(){
this.fill('#naissancelieuEtDate', {
'dateActeJour' : "12"
});
this.capture('result.png');
});
casper.run();

I'm using casperjs 1.0.2 on OSX mavericks and I don't have problem to display the title.
You check if div.job-search-box-text exists but it does not exists in the two cases.
And the fillSelectors code which output error : there is no input field with a name title.
here is the output for the url https://www.google.com.kh/
https://www.google.com.kh/
start page...
Page: Google
no ID found
TypeError: 'undefined' is not a function (evaluating 'this.fillSelectors('form', {'input[name="title"]' : "12"})')
/Users/jeff/Downloads/code/automation/casperjs-1.0.3/scripts/test.js:24
/Users/jeff/Downloads/code/automation/casperjs-1.0.3:1677 in _check
here is the output for the url https://www.google.com.kh/
https://teleservices.paris.fr/etatcivil/jsp/site/RunStandaloneApp.jsp?page=formengine&form=naissance
start page...
Page: Acte de naissance
no ID found
TypeError: 'undefined' is not a function (evaluating 'this.fillSelectors('form', {'input[name="title"]' : "12"})')
/Users/jeff/Downloads/code/automation/casperjs-1.0.3/scripts/test.js:24
/Users/jeff/Downloads/code/automation/casperjs-1.0.3:1677 in _check

Related

Phantomjs cannot open a page that browser opens

I have a Phantomjs script that tries to open a url. phantomjs returns this error:
Unable to load resource (request ID:undefinedURL:http://foo.bar/tree/nav_value/27)
Error code: 203. Description: Error downloading http://foo.bar/tree/nav_value/27 - server replied: Not Found
But when I open the url http://foo.bar/tree/nav_value/27 with chrome browser, there's no problem and the page is loaded correctly!
This is the script:
// Read the Phantom webpage '#intro' element text using jQuery and "includeJs"
"use strict";
var page = require('webpage').create();
var system = require('system');
if (system.args.length != 2) {
console.log("please pass 2 argument")
}
var company_id = system.args[1]
console.log("c", company_id)
page.onConsoleMessage = function(msg) {
console.log("message", msg);
};
page.onResourceError = function(resourceError) {
console.log('Unable to load resource (request ID:' + resourceError.id + 'URL:' + resourceError.url + ')');
console.log('Error code: ' + resourceError.errorCode + '. Description: ' + resourceError.errorString);
};
page.onError = function(msg, trace) {
console.log("error", msg)
}
var nav_value;
page.open("http://foo.bar/tree/nav_value/27", 'post', 'username=navid&password=test', function(status) {
if (status === "success") {
page.includeJs("http://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js", function() {
page.evaluate(function() {
nav_value = parseInt($("#value").text());
});
phantom.exit(0);
});
} else {
phantom.exit(1);
}
});
EDIT:
Something odd happens. When I run this code with phantomjs on windows on another machine it works. But on Ubuntu it returns the error!
The url that phantomjs is trying to open is on the same server. (Ubuntu)
What is the problem?
Not sure this will help, but I have some ideas that helped me figure out problems with PhantomJS in the past.
First, as you say it works on another machine, you may want to test other versions of PhantomJS, by downloading the executable and specifying the path on your Python script. Version 1.9.8 helped me with bypassing some security restrictions in the past (I also left some settings in case it may interest).
driver = webdriver.PhantomJS(
executable_path='/path/to/the/downloaded/phantomjs19',
# you can specify args, such as:
service_args=[
'--ignore-ssl-errors=true',
'--ssl-protocol=any',
'--web-security=false',
],
# and also other capabilities:
desired_capabilities={
'phantomjs.page.settings.resourceTimeout': '5000',
'phantomjs.page.settings.userAgent': (
"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/53 "
"(KHTML, like Gecko) Chrome/15.0.87"
),
},
)
You may also try to see if upgrading Selenium helps.
pip install selenium --upgrade
Another idea that might help to understand what is happening is to try to print a screenshot and log the page source before the error happens. You can do it like:
# Set the window size to something appropriate for your tests.
driver.set_window_size(900, 800)
driver.save_screenshot('screen.png')
# Check if the page source matches your expectations.
with open('temp.html', 'w') as f:
f.write(driver.page_source)
Please, let me know if this helps!

CasperJS; opening dynamic url

I am using casperJS to got ta a page and collect an id from the URL, this is what my start function is doing. I would then like to put the id in to a new url and go there.
This works for the user var, this is coming in from the terminal. However sid is undefined and not the value I updated it to.
Any help would be appreciated. I am wondering if I found a limitation with casperJS. please not that this is not the full code (as that is much longer), if you message me I can provide you the whole script.
var user = decodeURIComponent(casper.cli.get(0).replace(/\+/g, ' '))
var sid
casper.start('https://editor.storify.com/', function() {
sid = this.getCurrentUrl()
sid = sid.split('/')[3]
})
casper.thenOpen('https://storify.com/' + sid, function() {
console.log('lets view the posts')
console.log(token)
console.log(sid)
if (dev)
this.capture('storeheading.png')
})
you can solve this problem by warping the thenOpen inside of a then
casper.then(function() {
casper.thenOpen('https://storify.com/' + sid, function() {
console.log('lets view the posts')
console.log(token)
console.log(sid)
if (dev)
this.capture('storeheading.png')
})
})

CasperJS/PhantomJS chokes on loading a page

I'm running the following script with phantomjs:
var casper = require('casper').create();
var url = 'https://itunesconnect.apple.com/itc/static/login?view=1&path=%2FWebObjects%2FiTunesConnect.woa%3F'
casper.on("page.error", function(msg, trace) {
this.echo("Error: " + msg, "ERROR");
this.echo("file: " + trace[0].file, "WARNING");
this.echo("line: " + trace[0].line, "WARNING");
this.echo("function: " + trace[0]["function"], "WARNING");
errors.push(msg);
});
casper.start(url, function(){
casper.wait(7000, function(){
// casper.echo(casper.getHTML());
})
})
casper.run(function() {
if (errors.length > 0) {
this.echo(errors.length + ' Javascript errors found', "WARNING");
} else {
this.echo(errors.length + ' Javascript errors found', "INFO");
}
casper.exit();
});
Until a few days ago I could access the page which loads an iframe that contains 2 form fields, to allow user login.
Now I get the following error:
Error: Error: undefined is not a constructor (evaluating '$stateParams.path.startsWith('/')')
at setupDSiFrame (https://itunesconnect.apple.com/itc/static-resources/controllers/login_cntrl.js?cache=111920151100:99:46)
at https://itunesconnect.apple.com/itc/static-resources/controllers/login_cntrl.js?cache=111920151100:19:37
at $digest (https://itunesconnect.apple.com/itc/js/compiled/lib/vendor.js?cache=111920151100:13:11750)
at $apply (https://itunesconnect.apple.com/itc/js/compiled/lib/vendor.js?cache=111920151100:13:13237)
at f (https://itunesconnect.apple.com/itc/js/compiled/lib/vendor.js?cache=111920151100:12:56414)
at r (https://itunesconnect.apple.com/itc/js/compiled/lib/vendor.js?cache=111920151100:12:65848)
at onreadystatechange (https://itunesconnect.apple.com/itc/js/compiled/lib/vendor.js?cache=111920151100:12:66409)
file: https://itunesconnect.apple.com/itc/js/compiled/lib/vendor.js?cache=111920151100
line: 12
The page loads fine using slimerjs as the engine, but when using slimerjs the login form does not get filled in because the window is not in focus.
I believe this is an issue where casper is using an old version of WebKit and chokes on loading the page. How would I fix this?
Too late to solve the problem but maybe useful for future reference if somebody finds the question searching for a problem with startsWith in PhantomJS (as I did): startsWith method was added on the ECMAScript 6 specification, which is not supported by PhantomJS.
A good polyfill for this is mathiasbynens/String.prototype.startsWith

casperjs login form error

I am using this code to login to form. I get error.
var casper = require('casper').create();
var url = "www.example.com";
casper.start(url,function(){
this.echo(this.getTitle());
});
casper.waitForResource(url,function() {
this.fillSelectors("form[method='post']", {
'input[name="userName"]' : 'usr1',
'input[name="password"]': 'pswed1'
}, true);
});
casper.run();
http://prntscr.com/7jkia1
I pretty much copied examples I found on stack but still I get error... what to do to fix this?
It seems that casper is not able to do a selector for form[method='post'] ( using fillSelectors ), its not working for me either. But if you specify any other atribute like name, id, class, enctype, action, etc, it works.
var casper = require('casper').create();
var url = "https://careershub.bbc.co.uk/members/";
casper.start(url,function(){
this.echo(this.getTitle());
});
casper.waitForResource(url,function() {
this.fillSelectors('form#registration-form', {
'input[name="name_first"]' : 'firstname',
'input[name="name_last"]': 'lastname'
}, true);
this.captureSelector('test.png', 'form');
});
casper.run();

Is it possible to ping a server from Javascript?

I'm making a web app that requires that I check to see if remote servers are online or not. When I run it from the command line, my page load goes up to a full 60s (for 8 entries, it will scale linearly with more).
I decided to go the route of pinging on the user's end. This way, I can load the page and just have them wait for the "server is online" data while browsing my content.
If anyone has the answer to the above question, or if they know a solution to keep my page loads fast, I'd definitely appreciate it.
I have found someone that accomplishes this with a very clever usage of the native Image object.
From their source, this is the main function (it has dependences on other parts of the source but you get the idea).
function Pinger_ping(ip, callback) {
if(!this.inUse) {
this.inUse = true;
this.callback = callback
this.ip = ip;
var _that = this;
this.img = new Image();
this.img.onload = function() {_that.good();};
this.img.onerror = function() {_that.good();};
this.start = new Date().getTime();
this.img.src = "http://" + ip;
this.timer = setTimeout(function() { _that.bad();}, 1500);
}
}
This works on all types of servers that I've tested (web servers, ftp servers, and game servers). It also works with ports. If anyone encounters a use case that fails, please post in the comments and I will update my answer.
Update: Previous link has been removed. If anyone finds or implements the above, please comment and I'll add it into the answer.
Update 2: #trante was nice enough to provide a jsFiddle.
http://jsfiddle.net/GSSCD/203/
Update 3: #Jonathon created a GitHub repo with the implementation.
https://github.com/jdfreder/pingjs
Update 4: It looks as if this implementation is no longer reliable. People are also reporting that Chrome no longer supports it all, throwing a net::ERR_NAME_NOT_RESOLVED error. If someone can verify an alternate solution I will put that as the accepted answer.
Ping is ICMP, but if there is any open TCP port on the remote server it could be achieved like this:
function ping(host, port, pong) {
var started = new Date().getTime();
var http = new XMLHttpRequest();
http.open("GET", "http://" + host + ":" + port, /*async*/true);
http.onreadystatechange = function() {
if (http.readyState == 4) {
var ended = new Date().getTime();
var milliseconds = ended - started;
if (pong != null) {
pong(milliseconds);
}
}
};
try {
http.send(null);
} catch(exception) {
// this is expected
}
}
you can try this:
put ping.html on the server with or without any content, on the javascript do same as below:
<script>
function ping(){
$.ajax({
url: 'ping.html',
success: function(result){
alert('reply');
},
error: function(result){
alert('timeout/error');
}
});
}
</script>
You can't directly "ping" in javascript.
There may be a few other ways:
Ajax
Using a java applet with isReachable
Writing a serverside script which pings and using AJAX to communicate to your serversidescript
You might also be able to ping in flash (actionscript)
You can't do regular ping in browser Javascript, but you can find out if remote server is alive by for example loading an image from the remote server. If loading fails -> server down.
You can even calculate the loading time by using onload-event. Here's an example how to use onload event.
Pitching in with a websocket solution...
function ping(ip, isUp, isDown) {
var ws = new WebSocket("ws://" + ip);
ws.onerror = function(e){
isUp();
ws = null;
};
setTimeout(function() {
if(ws != null) {
ws.close();
ws = null;
isDown();
}
},2000);
}
Update: this solution does not work anymore on major browsers, since the onerror callback is executed even if the host is a non-existent IP address.
To keep your requests fast, cache the server side results of the ping and update the ping file or database every couple of minutes(or however accurate you want it to be). You can use cron to run a shell command with your 8 pings and write the output into a file, the webserver will include this file into your view.
The problem with standard pings is they're ICMP, which a lot of places don't let through for security and traffic reasons. That might explain the failure.
Ruby prior to 1.9 had a TCP-based ping.rb, which will run with Ruby 1.9+. All you have to do is copy it from the 1.8.7 installation to somewhere else. I just confirmed that it would run by pinging my home router.
There are many crazy answers here and especially about CORS -
You could do an http HEAD request (like GET but without payload).
See https://ochronus.com/http-head-request-good-uses/
It does NOT need a preflight check, the confusion is because of an old version of the specification, see
Why does a cross-origin HEAD request need a preflight check?
So you could use the answer above which is using the jQuery library (didn't say it) but with
type: 'HEAD'
--->
<script>
function ping(){
$.ajax({
url: 'ping.html',
type: 'HEAD',
success: function(result){
alert('reply');
},
error: function(result){
alert('timeout/error');
}
});
}
</script>
Off course you can also use vanilla js or dojo or whatever ...
If what you are trying to see is whether the server "exists", you can use the following:
function isValidURL(url) {
var encodedURL = encodeURIComponent(url);
var isValid = false;
$.ajax({
url: "http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20html%20where%20url%3D%22" + encodedURL + "%22&format=json",
type: "get",
async: false,
dataType: "json",
success: function(data) {
isValid = data.query.results != null;
},
error: function(){
isValid = false;
}
});
return isValid;
}
This will return a true/false indication whether the server exists.
If you want response time, a slight modification will do:
function ping(url) {
var encodedURL = encodeURIComponent(url);
var startDate = new Date();
var endDate = null;
$.ajax({
url: "http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20html%20where%20url%3D%22" + encodedURL + "%22&format=json",
type: "get",
async: false,
dataType: "json",
success: function(data) {
if (data.query.results != null) {
endDate = new Date();
} else {
endDate = null;
}
},
error: function(){
endDate = null;
}
});
if (endDate == null) {
throw "Not responsive...";
}
return endDate.getTime() - startDate.getTime();
}
The usage is then trivial:
var isValid = isValidURL("http://example.com");
alert(isValid ? "Valid URL!!!" : "Damn...");
Or:
var responseInMillis = ping("example.com");
alert(responseInMillis);
const ping = (url, timeout = 6000) => {
return new Promise((resolve, reject) => {
const urlRule = new RegExp('(https?|ftp|file)://[-A-Za-z0-9+&##/%?=~_|!:,.;]+[-A-Za-z0-9+&##/%=~_|]');
if (!urlRule.test(url)) reject('invalid url');
try {
fetch(url)
.then(() => resolve(true))
.catch(() => resolve(false));
setTimeout(() => {
resolve(false);
}, timeout);
} catch (e) {
reject(e);
}
});
};
use like this:
ping('https://stackoverflow.com/')
.then(res=>console.log(res))
.catch(e=>console.log(e))
I don't know what version of Ruby you're running, but have you tried implementing ping for ruby instead of javascript? http://raa.ruby-lang.org/project/net-ping/
let webSite = 'https://google.com/'
https.get(webSite, function (res) {
// If you get here, you have a response.
// If you want, you can check the status code here to verify that it's `200` or some other `2xx`.
console.log(webSite + ' ' + res.statusCode)
}).on('error', function(e) {
// Here, an error occurred. Check `e` for the error.
console.log(e.code)
});;
if you run this with node it would console log 200 as long as google is not down.
You can run the DOS ping.exe command from javaScript using the folowing:
function ping(ip)
{
var input = "";
var WshShell = new ActiveXObject("WScript.Shell");
var oExec = WshShell.Exec("c:/windows/system32/ping.exe " + ip);
while (!oExec.StdOut.AtEndOfStream)
{
input += oExec.StdOut.ReadLine() + "<br />";
}
return input;
}
Is this what was asked for, or am i missing something?
just replace
file_get_contents
with
$ip = $_SERVER['xxx.xxx.xxx.xxx'];
exec("ping -n 4 $ip 2>&1", $output, $retval);
if ($retval != 0) {
echo "no!";
}
else{
echo "yes!";
}
It might be a lot easier than all that. If you want your page to load then check on the availability or content of some foreign page to trigger other web page activity, you could do it using only javascript and php like this.
yourpage.php
<?php
if (isset($_GET['urlget'])){
if ($_GET['urlget']!=''){
$foreignpage= file_get_contents('http://www.foreignpage.html');
// you could also use curl for more fancy internet queries or if http wrappers aren't active in your php.ini
// parse $foreignpage for data that indicates your page should proceed
echo $foreignpage; // or a portion of it as you parsed
exit(); // this is very important otherwise you'll get the contents of your own page returned back to you on each call
}
}
?>
<html>
mypage html content
...
<script>
var stopmelater= setInterval("getforeignurl('?urlget=doesntmatter')", 2000);
function getforeignurl(url){
var handle= browserspec();
handle.open('GET', url, false);
handle.send();
var returnedPageContents= handle.responseText;
// parse page contents for what your looking and trigger javascript events accordingly.
// use handle.open('GET', url, true) to allow javascript to continue executing. must provide a callback function to accept the page contents with handle.onreadystatechange()
}
function browserspec(){
if (window.XMLHttpRequest){
return new XMLHttpRequest();
}else{
return new ActiveXObject("Microsoft.XMLHTTP");
}
}
</script>
That should do it.
The triggered javascript should include clearInterval(stopmelater)
Let me know if that works for you
Jerry
You could try using PHP in your web page...something like this:
<html><body>
<form method="post" name="pingform" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<h1>Host to ping:</h1>
<input type="text" name="tgt_host" value='<?php echo $_POST['tgt_host']; ?>'><br>
<input type="submit" name="submit" value="Submit" >
</form></body>
</html>
<?php
$tgt_host = $_POST['tgt_host'];
$output = shell_exec('ping -c 10 '. $tgt_host.');
echo "<html><body style=\"background-color:#0080c0\">
<script type=\"text/javascript\" language=\"javascript\">alert(\"Ping Results: " . $output . ".\");</script>
</body></html>";
?>
This is not tested so it may have typos etc...but I am confident it would work. Could be improved too...

Categories

Resources