Nodejs ES6 class not returning methods [duplicate] - node.js

I know that if I type:
$('body');
I get a jQuery object. However on chrome's console I'll only see the internal array of the jQuery object, despite the fact that jQuery methods are accessible like
$('body').hide();
Why the console don't show me all the accessible methods and how did jQuery manage to do this magic?
If it's just because these methods are defined on a prototype, then how come when I write these lines:
function Person(){this.myProp = 'test'};
var person = new Person();
Person.prototype.proto = 'test2';
and then I inspect person in chrome I will see:
__proto__: Person
constructor: Person()
proto: "test2"
but when Inspecting $('body'); then no proto is shown on dev tools?

The methods are on the prototype of the object. Both the console and console.log() do not, by default, show items on the prototype.
If you examine a jQuery object in the Chrome debugger, you can expand the prototype and can then see all the methods there.
So, what you are seeing is just the chosen implementation of the console. It shows direct instance properties, not items on the prototype.
When I put this code into a page:
function Person(){this.myProp = 'test'};
Person.prototype.proto = 'test2';
var person = new Person();
var jq = $("body");
And, then examine both person and jq in the Chrome debugger, I see this:
which shows the `proto property on both objects. And, if I expand that property for the jQuery object, it does indeed show all the methods.

Related

Using Custom Functions with "I" in Page Object and Custom Helper in CodeceptJS

Hi CodeceptJS Community,
Is there a way to use custom defined functions (under steps_file.js) as I. customFunction() in page object files.
Is there a way to use native codeceptjs functions (like I.click()) in my custom helper files
I couldn't find any help in the documentation and in other sources. Is there any way to achieve this?
And is there any way to use xpath locators in puppeteer helper?
this.helpers['Puppeteer'].page.click(xpath);
I had the same problem to use custom steps in pageObjects.
To avoid it, i passed the actor (I) as parameter to my pageObject function.
page object:
const I = actor();
module.exports = {
doSomething(I){
I.login();
}
};
Test scenario:
Scenario('Test something' (I,pageObject)=>{
pageObject.doSomething(I)
})
In this case, pageObjects will has access to all custom steps from I :)
Thank you for your sharing Matheus. I have used a different solution. Instead of writing "I" object in every page object method (which was also one option for me), I have created a custom helper file and written all methods using puppeteer helper like below;
async method() {
await this.helpers['Puppeteer'].click(xpath);
}
I can call this method both in tests and page objects
I.method();
I was facing the same issue and when I looked into the typescripts definitions I noticed that actor() which is required in every page object etc. has custom steps arguments.
So this worked for me to extend the const I = actor(); witht the custom steps form steps_file.js;
const customSteps = require('./steps_file');
const I = actor(customSteps());
After that, I can use all methods in page objects like in tests scenarios which are accessing the methods from steps_file.js

Call SSJS from CSJS in XPages

I need to call a java bean method from a client side javascript library. Is there a way to call ssjs from csjs library?
something like this that works in csjs
var test = #{javascript:getConfig.getKeyValuesList("param")};
You want to use the Remote Service tool in the Ext. Library. That lets you define a function in SSJS and call it from CSJS.
There's discussion of it here:
http://www.notesin9.com/2014/05/21/tim-explains-json-rpc-codefortim/
There's an old NotesIn9 Video should should still be very valid:
http://www.notesin9.com/2011/08/25/notesin9-033-introduction-to-remote-services-in-xpages/
You can do it the way you showed in your example like
var test = #{javascript:yourBean.getSomething()};
The SSJS code gets executed first, the result gets inserted into CSJS code and send to client.
It depends on your use case if that can be a solution for you.
Your a bit modified example
var test = ['#{javascript:getConfig.getKeyValuesList("param").join("', '")}'];
would execute the methode getKeyValuesList(), return a List of strings, .join() would convert it to a string like "aaa', 'bbb', 'ccc" and send the following resulting code to client:
var test = ['aaa', 'bbb', 'ccc'];
I do this with a combination of CSJS libraries where I define objects with properties and methods and then on a custom control (usually the one with the resource for the library) I add a scriptBlock for getting the data into my client side objects. As frank says this only happens when the page is loaded but for configuration data like what you seem to be getting that works just fine.
Here is a sample csjs class for your library:
var appConfig = {
param1 : "",
param2 : ""
}
Then this is the scriptBlock code:
<xp:scriptBlock>
<xp:this.value><![CDATA[
// setup config parameters
appConfig.param1 = '#{javascript:getConfig.getKeyValuesList("param1")}';
appConfig.param2 = '#{javascript:getConfig.getKeyValuesList("param2")}';
})
]]></xp:this.value>
</xp:scriptBlock>
Happy coding.

What are the scopes and/or persistence of JavaScript Code Modules?

Experimenting with a bootstrapped extension, I'm trying to understand the scopes and/or persistence of jsm modules by setting a property, called baseUri, on a module object from bootstrap.js and reading it again from javascript in my options.xul (which is opened from the Add-ons Manager).
My current understanding is that JavaScript Code Modules are persisted, once loaded. However, when I try to access baseUri from options.xul, its value is undefined.
install.rdf:
<!-- just the relevant XML (this works as expected, by the way): -->
<em:optionsURL>chrome://test/content/options.xul</em:optionsURL>
/modules/Test.jsm:
var EXPORTED_SYMBOLS = [ 'Test' ];
Test = {
baseUri: undefined
}
/bootstrap.js:
// this is done in global scope,
// not inside install() or startup() for instance, if that matters
let test = Components.utils.import( 'file:///absolute/path/to/Test.jsm', {} ).Test;
test.baseUri = someBaseUriIExtracted;
/chrome/content/options.js (included in /chrome/content/options.xul):
let test = Components.utils.import( 'file:///absolute/path/to/Test.jsm', {} ).Test;
console.log( test.baseUri ); // undefined
So, I guess what I'm failing to fully understand is what the exact scopes are from which I should be able to access object properties from exported jsm symbols and/or how and when exactly these objects are persisted.
Does my problem have anything to do with sand-boxing, perhaps? Does Firefox consider options.xul, when opened from the Add-ons Manager, to be a different security scope than bootstrap.js, perhaps?
Can you shed a thorough light on the actual scopes of jsm modules and when and where I should be able to access persisted properties on jsm modules?
The documentation is pretty straightforward about what and how is shared
Each scope that imports a module receives a by-value copy of the
exported symbols in that module. Changes to the symbol's value will
not propagate to other scopes (though an object's properties will be
manipulated by reference).
I think the accompanying examples are clear.
Maybe you should use getters/setters.
From what I know:
Other jsm modules
Browser window
Content window
bootstrap addon scope

Unit test rest service without specifying URL

Using servicestack, there are examples of unit testing using types, etc. Here is an example:
GetFactorial
I would like to test my REST style service with a test similar to the above.
Here is an example REST unit test FileService
Notice how in the PUT unit test, the Path argument has to be specified in the URL text instead of in the class argument. Another example is here, where we have perfectly good request models that have to be translated into the URL. For testing, I would like to get away from having to build the arguments in the url and use a system similar to the one above like this:
var response = restClient.Put<FilesResponse>(new Files { TextContents = ReplacedFileContents, Path = "README.txt" });
or
var singleCustomer = restClient.Get<Customer>(new Customer {Id=1};
Is this possible?
Then there is the DirectServiceClient. Would that help? In the end, with servicestack, we get to write services and they can be called from many different type clients - I would like to write my unit test like that.
Is this possible?
If you decorate your DTOs with the route variable and use ServiceStack's "New API" then it can discover the routes automatically. You can get away with writing very minimal code and still get a strong typed rest API.
Your code could look something like this:
Customer singleCustomer = restClient.Get(new Customer {Id=1});
See https://github.com/ServiceStack/ServiceStack/wiki/New-Api
In response to your comments, your DTO needs to adhere to the IReturn interface:
[Route("/customer/{Id}")]
public Customer : IReturn<Customer> {
public int Id {get;set;}
}
The IRestClient interface below will now be able to work with your DTO without specify the type since it is expecting an IReturn object.
public interface IRestClient
{
TResponse Get<TResponse>(IReturn<TResponse> request);
...
}

How do I add a globally available MetaMethod on Object in Groovy?

(this is a generalized example)
I'd like to create a utility method that can be called on any object, it'll have a signature like:
class StringMetaData {
Object value
String meta
}
Object.metaClass.withStringMetaData = { meta ->
new StringMetaData(delegate, meta)
}
With the idea that then anywhere in my program I could do something like:
def foo = 1.withStringMetaData("bar")
And now I can grab foo.value for the value or foo.meta for the attached String.
Within a local context, I'm able to define this meta method on Object, but I'd like to make it available globally within my application, what's the right way to make this metamethod available everywhere?
Perhaps a groovy extension module could help you. I never tried it myself, but the documentation states, that you can add custom methods to JDK classes.

Resources