How to handle error 'console is undefined' in powershell? - javascript

Just a simple .js file in one of my drive which contains code
var x=3;
function numSqaure(x)
{
return(x*x);
}
var sentence="The square of" + x + "is" + numSqaure(x);
console.log(sentence);
I'm trying to run it through Powershell but it shows an error 'console' is undefined .
How to handle this error? I want it through Powershell only.

Javascript files are made for being executed by a browser, not directly in console unless you're using Node.js or something similar.
Create an HTML with a script tag like this:
<script src="c:\whatever\folder\you\choose\yourscript.js"></script>
and enter developer tools in browser. There this error won't happen anymore and console will exist.

Related

How to trace <script> elements/errors with Codeception with phantomjs/Selenium?

I've set up acceptance tests with codeception and phantomjs.
My website contains script elements:
<script src="/bla/test.js" type="text/javascript"></script>
The JavaScript file looks like this:
Bla = {
request: function(var) {
}
};
It looks like the content cannot be found since one of the buttons contains
onclick="return Bla.request('100')"
Running the acceptance test which clicks on this button shows me:
[Selenium browser Logs]
17:10:24.876 WARNING - SyntaxError: Parse error
17:10:25.121 WARNING - ReferenceError: Can't find variable: Bla
onclick (http://localhost/bla)
Screenshot and page source were saved into '/var/www/bla/_output/' dir FAIL
Does the first parse error already mean that the files have been loaded but contain errors? If so, how can I see the exact parse errors?
Requesting the JS file manually in the acceptance test works and it is the correct file (as the output shows):
$I->amOnPage('/bla/test.js');
I run codeception with the option --debug and looked at the output files where everything looked correct. How can I see if the JavaScript file has been imported correctly and if the parse error means that it failed, how do I get more information about the error?
edit:
Even if the parse error is not present I cannot access variables from a different JavaScript file.
When I have multiple script elements:
<script src="/bla/test0.js" type="text/javascript"></script>
<script src="/bla/test1.js" type="text/javascript"></script>
I get an error:
ReferenceError: Can't find variable: Bla
in file test1.js if it was defined in test0.js. This works in the normal browser without errors.

I am getting un-terminated string literal for my userextension.js file in Selenium IDE

I've the following code to add in userextensions.js file whenever I add the following piece of code it gives me following error
Failed to load user-extensions.js: Syntax Error: Unterminated String Literal
Selenium.prototype.doInsertCKEditor = function(locator,word)
{
this.doWaitForCondition("var x =
Selenium.browserbot.findElementOrNull('//td[#id=\"cke_contents_form
\"]');x != null;", "50000");
this.doRunScript("CKEDITOR.instances['"+locator+"'].setData('"+word
+"');");
the best way to remove this ever I found it to open your script in some javascript editor I did it in Adobe Dreamweaver and it pin pointed the line where actually the problem is and I resolved this

Javascript java.lang.runtime.getRuntime.exec() not working; "java is not defined", importPackage not working

I'm trying to execute a java program from within an HTML file using Javascript (specifically, the assignment is to activate an FTP server from an HTML interface, so I need to execute the FTPServer class from a function on my HTML page). I've been trying to use the following code to execute the "program" class which should simply print out "hello." The Chrome console says "Uncaught ReferenceError: java is not defined"
var p1 = java.lang.Runtime.getRuntime().exec("javac program.java");
document.getElementById("test").innerHTML = "working";
var p2 = java.lang.Runtime.getRuntime().exec("cmd /c java program");
var stdInput = new java.io.BufferedReader(new java.io.InputStreamReader(p2.getInputStream()));
var stdError = new java.io.BufferedReader(new java.io.InputStreamReader(p2.getErrorStream()));
I've also tried doing Packages.java.lang.Runtime... to which I get the error "Packages is not defined." I've tried importing the java.lang package:
importPackage(java.lang);
but I get the same error, "Uncaught ReferenceError: importPackage is not defined"
Why isn't importPackage working, and how can I get Runtime to work?
What you are trying to do only makes sense in rhino.
According to Rhino Migration Guide you can try something like this:
var Runtime = Java.type("java.lang.Runtime");
and then run it with
Runtime.getRuntime().exec("javac program.java");

Logging value of a variable in MonkeyTalk IDE Javascript file

I'm using MonkeyTalk IDE Beta2 for testing iPad application. I exported the javascript from the MonkeyTalk IDE and got a new .js file. I am storing the Boolean value of a Verify command in a var and want to see what is its value, and accordingly do custom logic. I tried document.write, console.log and alert used in javascript but got an error that they are not defined. Please help me with this.
Also, is it possible to output the result of a test as XML (as in FoneMonkey) or as an Excel spreadsheet or something like that?
Thank you in advance.
Believe it or not*, but to date there is no way direct way to cause MonkeyTalk to log messages to the console. What you can do, however, is abuse a command like verifyNot which will result in a log message. In a MonkeyTalk .mt this would be done like:
View * VerifyNot Message
I created the following helper script called log.js for this purpose. Timestamps are automatically added by Eclipse, but not elsewhere so I have prepended the time.
load("libs/Executor.js");
function getTimeStamp() {
var now = new Date();
return now.getHours() + ":" + now.getMinutes() + ":" + now.getSeconds();
}
EXECUTOR.defineScript("Log", function(msg) {
this.app.view().verifyNot(getTimeStamp() + ": " + msg);
});
Finally, you don't need the executor boilerplate (only the verifyNot line), but we use that with scripts by Doba in order to be able to organize files in different directories (Doba.js renamed to Executor.js) -- another feature not available out of the box.
* It's almost like GorillaLogic doesn't want you to be able to resolve your own problems. ;)

Javascript wshell.run not working properly

I'm using HTA and in it I have a function that should run a command line with wshell.run , If I'm writing this line in Windows 'Run' util it is working fine, I want it to work also in the HTA with wshell.run.
The line is:
C:\xxxx\xxx\xxx.EXE aaa.psl abc
( The names are xxx just in here - not in the real code.. )
In the javascript code I'm using:
function runCmd()
{
wshShell.exec( "C:\xxxx\xxx\xxx.EXE aaa.psl abc" );
}
The error I got is in the xxx.EXE application says "couldn't open aaa.psl File not found".
Thanks,
Rotem
I'm surprised the xxx.EXE program is running at all. You need to escape those backslashes in the command:
wshShell.Exec( "C:\\xxxx\\xxx\\xxx.EXE aaa.psl abc" );
// ^-----^----^--- here
If you're doing the same thing in the aaa.psl filename, that's your problem.
If you're not passing a full path to the aaa.psl file, then most programs (not all) will expect it to be in the current directory, so you'll want to make sure you've set the current directory correctly (although using absolute paths may be a better option).
Here's an example, for instance, of telling Notepad to edit a file:
shell = WScript.CreateObject("WScript.Shell");
shell.Exec("c:\\windows\\system32\\notepad.exe c:\\temp\\temp.txt");
...or via the current directory:
shell = WScript.CreateObject("WScript.Shell");
shell.CurrentDirectory = "c:\\temp";
shell.Exec("c:\\windows\\system32\\notepad.exe temp.txt");
Okkkk T.J. is the man!! :)
I finnaly made it with your help by replacing exec to run:
This is the final (and working) code:
function runCmd()
{
wshShell.CurrentDirectory = "G:\\xxx\\xxx";
wshShell.run( "xxx.EXE xxx.psl abc" );
}

Categories