I have a problem at work that looks like I'll need to use both some javascript and a shell script to solve.
What I need to know is - how can I pass the value of a javascript variable to a shell script?
My particular use-case is getting the selected value from an HTML dropdown list. That's fine - I can get that value and assign it to a Javascript variable. I now just need to know how to take the next step and get that variable into a bash variable.
So, let's say I have this bit of Javascript -
var foo = "some text" ;
How do I get "foo" into bash?
The answer is you can't with a regular web page. Browser Javascript code is not allowed to effect the local computer's environment for security reasons.
This would take some sort of browser add-on the end-user installed running at higher privileges in order to set an environment variable in the local computer's environment.
You can Uses Global Variable
//base.js
var foo;
And Child Js
foo="some text"
Must Be Load Js file Base First After Second
You can debug Statement usins
console.log(foo);
Related
I have a bunch of old test scripts that were written for the old Selenium IDE. I'm trying to update them to run with the new Selenium, but I'm having a really hard time figuring out what to do with bits of javascript and the new syntax.
For example, I have something like:
(command)STORE (Target) javascript{Math.floor(Math.random()*100000)} (Value) ReportNumber
But all Selenium does is store the javascript expression as a variable if I use that old Syntax. I saw that the new IDE wants us to instead use Run Script but I need to run the javascript and save it as a variable and none of the attempts I've made thus far have worked. Has anyone figure out HOW to use and run javascript successfully, and to save it as a variable. Examples if you have them, please!!
You now need to use the execute script command instead, like this:
execute script | return Math.floor(Math.random()*100000) | ReportNumber
The "target" of the execute script command is the JavaScript to execute, and the "value" is the name of variable you want Selenium to put the result into. The return in the JavaScript is not optional!
I'm trying to log a local variable in an external script that is called on the page. If I place a breakpoint I can see its values, but it becomes inaccessible if I make the code start again. Is there any way to do this?
that's how you do it console.log(variablename);
I have the following problem:
In file (let a.js be) I have:
var kindofdisplay ;
In an other file ( let b.js be)
I get the information to set kindofdisplay.
Now, I would like to set kindofdisplay from file b.js so that when a.js is executed it will be able to process the variable in a correct way.
Many thanks
As Raja pointed out. If you can access the kindofdisplay variable on b.js, you can change it. You just need to take care not to declare it again.
You can try give a default value like:
var kindofdisplay='none';
And check if that's the value the variable has when on b.js. If it's not, you are probably declaring it again.
If you're using the two javascript files on different web pages, you could always set the variable as a cookie (providing you don't need it to be secure).
Have a look at this tutorial about cookies.
Another way to do this would be to put the script that defines the function for setting the variable in one file, link it to both pages that you need the variable to exist in and call the function on each page.
Of course, as a couple of people have already explained, if you're using the two javascript files on the same page, there's no need to do this - just ensure that the variable has the appropriate scope.
I'm trying to find a way to programatically get the last value returned by the Javascript interpreter. Ruby's interpreter, to name an example, has the "_":
1 + 2 #=> 3
_ #=> 3
I would like to know if the same thing exists in Javascript.
EDIT:
Another way to maybe achieve this. Is there any syntax that supports the continuation of an expression in a newline? Something like this:
var a = \&
1 + 2;
a #=> 3
Some sort of combination of characters that tell the interpreter the expression continues in a newline (like the + for string concatenation).
PURPOSE:
Purpose of this research is to find if I can load a JSON data structure using a script tag and successfully assign it from outside of its scope, something like this:
<script> var json_struct = </script>
<script src="http://domain.com/myjsonfile.json" type='application/json' ></script>
which, by the way, doesn't work. Surprisingly :)
PURPOSE:
Purpose of this research is to find if I can load a JSON data structure
using a script tag and successfully assign it from outside of its scope
There is no construct in browser based javascript that can do this.
The reason is that browsers, since the earliest Netscape days, have always initiated the script compiler upon the closing of the script tag. Regardless if it's javascript, VBscript (IE only) or Tcl (with the appropriate plugin).
Which means that any statement that is incomplete will simply be treated as a syntax error. Each <script> tag is basically treated as a single file.
What you're trying to do is similar to this in Ruby:
a = require 'one_plus_two.rb'
which I don't think works in Ruby.
However, in non-browser environments that support modules like Node.js, the method that imports module does in fact return a value (usually an object). So you can do something like this in node.js:
var a = require('my_data_file.js');
Unfortunately, the require function only works on local files. But Node.js is open source so you can always fork it and modify require to be able to source from http:// like PHP.
Alas, if what you're trying to do is browser scripting then the above point is moot.
So, I'm running these Selenium IDE tests against a site I'm working on. Everything about the tests themselves is running fine, except I would like to do a bit of clean-up once I'm done. In my MVC3 Razor based site, I have a JavaScript file with a function that gets a JsonResult from a Controller of mine. That Controller handles the database clean-up that Selenium IDE otherwise couldn't handle.
However, I'm having a hard time finding any sort of documentation on how to do this. I know I can do JavaScript{ myJavascriptGoesHere } as one of the Values for a line in the test, but I can't seem to find a way to tell it to go find my clean-up function.
Is it even possible for Selenium IDE to do this sort of thing?
If it comes down to it, I can just make a separate View to handle the clean-up, but I'd really like to avoid that if possible.
Thanks!
If you want to execute your own JavaScript function that exists in your test page from Selenium IDE, you need to make sure you access it via the window object. If you look at the reference for storeEval for instance, it says:
Note that, by default, the snippet will run in the context of the
"selenium" object itself, so this will refer to the Selenium object.
Use window to refer to the window of your application, e.g.
window.document.getElementById('foo')
So if you have your own function e.g. myFunc(). You need to refer to it as window.myFunc().
This can be very handy for exercising client-side validation without actually submitting the form, e.g. if you want to test a variety of invalid and valid form field values.
If you use runScript, that should already run in the window's context.
This works for me.
IJavaScriptExecutor js = driver as IJavaScriptExecutor;
string title = (string)js.ExecuteScript("myJavascriptGoesHere");
Make sure your javascript works first before using it here!
Actually to access your page javascript space, you need to get the real window of your page : this.browserbot.getUserWindow()
See this statement to get the jQuery entry point in your page (if it has jQuery of course ^^ )
https://stackoverflow.com/a/54887281/2143734