Window is not a constructor error? - javascript

I have this code in a JavaScript file:
this.tooltipWindow = new Window("__tooltip__", TooltipManager.options);
This gives me the TypeError: Window is not a constructor error in Firefox. Is there something wrong with this code and yes, how can I rewrite it, so it works?
Thanks!

If you have a type defined by the word "Window", it's likely interfering with the actual 'window' object that exists on all pages.
If you're actually trying to create a new 'Window', as in the browser-typed object, that way, then I think that you're entering into some unfamiliar areas to me...are you just trying to create a popup window?
https://developer.mozilla.org/en-US/docs/Web/API/Window.open

A quick search on that line of code pointed me to a library called ATK, specifically the tooltip.js file. At first glance it's quite a complete library, but it seems like you are only using tooltip.js. Since the Window class is defined in window.js you'll need to include that script as well to make it work.
I don't know the framework myself, so it might be wise to check if it even supports cherry-picking specific pieces of code like that.

Related

Extending TestComplete: How to change cursor?

I wrote a TestComplete extension to update Keyword-Test signature from TestComplete in to an external tool.
However, this action takes very long time. That's why I need to change the cursor from arrow to hour glass, and back to arrow after action is done.
The module doing the opperation is writen in js.
If I try to use the following code, suggested by TestComplete code completition
Win32API.SetCursor(Win32API.IDC_WAIT);
I got the error "Object expected". I.e., the js in the TestComplete extension does not know About Win32API object, despite the code completition suggestion.
Ommiting the Win32API. prefix has the same effect. Trying to create appropiate object via
new ActiveXObject("SomeKindClass")
fails, because I am not able to find appropiate name for the class containing some methode to change cursor. (I tryed, Microsoft.Win32, Microsoft.Win32API, Win32, Win32API and some other non-sence names...)
SmartBears description on writing extentions seems to contain no hint about changing the cursor in a js ScriptExtension.
Please appologize, if I overlook it.
Any suggestions are appreciated. Thanx in advice for your F1!
Edit:
A possible way to solve this is described bellow. However, I am not able to follow it to the end, because of lack of time. Perhaps someone can confirm or deny the correctness. That' would be great.
Steps:
Create a new vbs module in the ScriptExtension (or extend an existing one if any).
In
the init method of vbs module, load user32.dll, create prototypes for
the LoadCursor and CreateCursor methods of user32.dll. (See Tutorial)
You call those methods in your setCursor2* methods.
Declare the setCursor2* methods in the Description.xml as method in RuntimeObject of your namespace (See Script Extension Description file)
Call the methods in the js module YourNameSpace.setCursor2Hourglass(); and YourNameSpace.setCursor2Arrow(); respectively.
It is impossible to show an hour glass from a TestComplete extension.
The reason is, following quote, that comes from https://support.smartbear.com/testcomplete/docs/working-with/extending/script/creating/specifics.htm.
"If your design-time action performs actions against the TestComplete
main window (for example, minimizes the window, changes the window
size and position, and so on), an error will occur. This happens due
to specifics of thread organization in TestComplete."
I guess, the "on so on" part includes changing the cursor…

Javascript Error: Function is not Defined when called through onclick

I've been attempting to write a basic website for a school project, and have decided to create it using a hybrid of html and basic javascript. Currently, I have the following html code for part of the site:
HTML Code
With the javascript "ScriptWindow.js", called in line 6 as:
Javascript "ScriptWindow.js"
The main issue I have seems to be lines 11-15, where I attempt to call the javascript functions GetNext(), GetPrev(), etc. My Google Chrome developer tools tell me that the error on the site is "Uncaught ReferenceError: GetNext is not defined ", even though I thought I had defined it in the javascript file perfectly clear.
I've tried putting the javascript file through a bug tested, and there seems to be no error that I or it can find, which leads me to the presumption that the javascript file is not being properly called. I've tried pasting the javascript directly into the html code, but it still doesn't recognize that the functions are there.
This leads me to believe that it's some issue with how the onclick tries to find the javascript function, or that somehow I have placed the javascript in the wrong spot or perhaps it is not loading in time for the button to find it. I've tried moving the line where I reference the javascript to different places: At the end of the body section, before the body section, with the other two javascript declarations at the very beginning, but nothing seems to work.
If anyone could shed a little light on my issue, it would be wonderful. I am completely new to javascript, so I realize that there's probably a silly mistake (Or a big one) that I'm overlooking. Thanks for your time.

Timing issues with overriding js functions across files

In my rails project, I've written a javascript function to override the default behaviour of an object in another defined in another file. The line that did this looked something like:
window.someObject.methodToOverride = function...
Initially this would get me a cannot set property 'methodToOverride' of undefined error, which I'm assuming was related to the a timing issue (with someObject not being setup yet). I was able to resolve the issue by chucking it in a jQuery $(document).ready function, but while that works, it seems a bit hacky to me.
Is there a better way to do this?
This sounds like you're looking for require.js. What it does is let you set up your code modularly, defining which module depends on which. In your case, once it's set up, by putting a wrapper such as
require(["someObject"], function (someObject) {
someObject.methodToOverride = function...
}
will make it so when you got to this function, require.js would dynamically load your someObject file, and when it was fully loaded, pass someObject as a parameter into the function you provided. It can work with much more complicated examples, with any level and any number of dependencies, loading them no more than once on an as-needed basis.
You can find a lot more information on require.js all over SO, e.g.:
simple example for using require.js
Require.js nested requires

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

Is there a good way to prevent jQuery from being redefined?

I encountered a problem that took me some time to debug where a plug-in that I was using for jQuery (in this case jFeed) was not working. The problem ended up being because we also used Amazon Associates product previews. The product previews code ends up including a number of other JS files through document.write(), including another copy of jQuery. Because the product previews code appeared below the jFeed script, jQuery was redefined without the getFeed function.
Is there a best practice to ensure that certain objects like jQuery only get defined once on a page? I'm thinking of something like #ifndef with C/C++, but I don't know how it would work in this case where I didn't write the code that dynamically pulled in jQuery again.
I think in your situation, it would probably be best to redefine the jQuery variable as something else. The other jQuery code might use a different version so you might want to define a new variable which would indicate which jQuery you're using.
You could so something like this:
<script>
var $jMain = jQuery;
</script>
You would then just use the $jMain instead of jQuery or $. It'll be up to you to you to ensure you have the correct jQuery object when you do this. Here's the documentation.
Unfortunately the environment inside one JS sandbox (like within a window or frame of a browser) was not really designed to support the modern world of pulling in scripts from various places; there's no way you can say "define this object and make it resistant to redefinition". (You can even redefine most of the Javascript built-ins if you try!)
Your best shot is to make sure that your code is eval'd last, which gives you final say over the state of the environment when it runs. That doesn't mean other code can't come along later and clobber your definitions, but that's generally really bad form. You can do this by having your script tag be the last element in the body of the document, for example.
See also this jQuery method, which won't help you directly, but gets you thinking about some solutions to page sharing: http://api.jquery.com/jQuery.noConflict/

Categories

Resources