Accessing properties of window with Selenium - javascript

The page I am trying to test sets the value of "global variables". From the Firebug, I can access those as properties of the window object (e.g. window.foo).
From Selenium however, typeof selenium.browserbot.getCurrentWindow().foo always return "undefined", say when used in the condition of a waitForCondition. Any idea of what I could be doing wrong?

I think it cant be done, it could be done in greasemonkey using unsafeWindow, however that doesnt work in selenium. i tried to do it using
addLocationStrategy
zzz
return prompt(inWindow.a);
and than
click
zzz=xxx
but i cant access it. if you have any ideas how to do it let me know.
if its your page maybe you could keep that variable in some html control. its a hack, but .....

Related

GTM custom javascript is not working, what to do?

I've created custom js function. It worked on console, however I tried to transfer it to GTM using custom js variable function, it returned undefined. The function transfers a string to integer and removes whitespaces.
function(){
a= parseInt(google_tag_manager["GTM-000000000"].dataLayer.get("gtm.element").querySelectorAll("div>div>div>div")[27].querySelector("div>input").getAttribute("value").replace(/\s/g, ""));
return a}
it returns undefined((
No, the function does a lot more than you indicated. All the dom scraping The reason for it "not firing" in GTM may be due to timing. However, you're not supposed to use this kind of global scope code in GTM.
GTM kindly provides you with a {{Clicked Element}} which you can definitely use in your GTM custom JS code. So your code would become something like:
return {{Clicked Element}}.querySelectorAll("div>div>div>div")[27].querySelector("div>input").getAttribute("value").replace(/\s/g, ""));
Still, you can debug it. You can console.log your google_tag_manager["GTM-000000000"].dataLayer.get("gtm.element") from GTM and then debug it further in your console. The console nowadays allows you to copy a consol logged object, put it in a var and query against it.

Expose variable to the devtools console without making it global on window

Is it possible to make a variable easily accessible by the debug console without actually making it global? For example, something like this in the code:
console.$ = jQuery
And then in the console, I'd like this to work:
$('#foo')
Not while the page isn't paused at a breakpoint, no. But when the page is paused at a breakpoint, the console has access to all variables that are in-scope at the point where it's stopped.
Of course, your console.$ = jQuery example does make it possible to do
console.$("#foo")
...in the console.
Side note: console.$ = jQuery and $("#foo") aren't great examples, as jQuery is almost always global, and even if you don't have jQuery loaded on your page at all, Chrome provides its own $ function in the console, and I think at least one other browser does as well...

How to access controls from a console

I tried to execute the following command from the console.
var subject = Xrm.Page.ui.controls.get("subject");
That's the exact syntax I'm using in the web resource that I'm plugging in to CRM. However, I only got an error message saying that "unable to get property 'controls' of undefined or null reference".
I do understand the message. What I want to know is two-fold.
What syntax will work from the console (F12) to refer to the stuff on the screen?
Why doesn't it work the way I did? Where doesn ui come from?
I've checked that I can refer to both Xrm and Crm.Page but apparently ui is null (it's listed when I print out the contents of Page but sett to null).
I know this is a kinda old thread, but if you still getting that 'object doesn't support property..' error when executing the command from console, IE F12; try calling it from the frame i.e
frames[0].Xrm.Page.getAttribute("controlId").getValue();
In CRM 2013 it is a little different
frames[1].Xrm.Page
It's kind of tough to detect the frames across different browsers, so this little javascript can help you out:
for(var i=0;i<5;i++) //loop through 0 to 4
if(frames[i].Xrm.Page.ui != undefined) //check if undefined
{
Xrm = frames[i].Xrm; //assign Xrm
console.info("~: Xrm updated with frame " + i + " :~"); //show info
break; //breakout the loop
}
What it does ?
What it's basically doing is to loop through 0-5 to find frame where
Xrm.Page.ui is not undefined, once it gets it it assigns it to the Xrm and breaks the loop.
How to use ?
To use it just copy/paste and run in the browser console once per
session then after you can run/test all your Xrm codes form the browser console.
This works for me Xrm.Page.getControl("controlId"). It's just a shortcut for what you have already though...cant-disable-set-to-read-only-protect-gray-out-etc-a-field
In addition to what #Daryl said, I can add that I use different syntax. For some reason, I don't get his to work either. Might have to do with different browser version or something. Instead try to execute this, if you still can't get it to work (although I must admit that his is shorter = better).
Xrm.Page.getAttribute("lastname").getValue();
The lastname parts is tested a minute ago on creation of an instance of entity Contact. I just put in a breakpoint inside a script that is executed onchange and while broken-pointed, I entered the command above to the console.
If neither approach works for you, you've got some weird problem with your CRM or browser.
A reason some people need this information is to access their own code. If you need to access your own methods from the console, in 2011, any global methods (or namespaces) in your javascript were also in forms[0]. Obviously, this is a bad idea, just from a naming standpoint. In forms v6+ any global objects or functions are in an object called customScriptsFrame inside frames[0] (or presumably whichever frame the Xrm is found).
frames[0].customScriptsFrame.myFunctionName();

Calling a function in a JavaScript file with Selenium IDE

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

Get a parameter value in JavaScript

In Cognos 8.4, I have a prompt, "NAME", and its parameter p_name.
How do I get that parameter through JavaScript?
<script>
alert(p_name)
</script>
shows a JavaScript error. Why?
Is my approach correct?
Maybe the reference for the object will be missing, and you have to keep up with the scope of the variables or the parameters. You can use the "Developer Tools" in Internet Explorer 8 by pressing "f12". There in the right hand side pan you can choose the tab "console" to find where you went wrong or you can choose the tab "locals" to find whether the parameter has any values in the scope where you are calling it.
I don't know Cognos, but your problem is that you are calling a variable that is local to an object in the global scope. You have to do whatEverTheObjectIsCalled.p_name (probably). An easy way to find out exactly what you are looking for is to fire up the web page in Chrome and console.log(theObject) and browse through the internals of the object until you find the attribute you want.

Categories

Resources