Looking for a "legit" way to be able to log a Selector's function chain - testing

I find that I often want to be able to log out what a Selector was looking for at various times during execution. I couldn't find a "legit" way to do this so I worked around it by creating the following function:
function printSelector(selector) {
console.log(selector[Object.getOwnPropertySymbols(selector)[0]].options.apiFnChain.join(""));
}
// And with this, if you have something like:
let mySelector = Selector('button').withText('foo').with({ visibilityCheck: true });
// You can run:
printSelector(mySelector);
// and get: Selector('button').withText('foo').with({ visibilityCheck: true })
My question is: is there some better way that doesn't require using the internal apiFnChain to find / log this info?
Thanks

TestCafe does not allow you to log Selectors if there were no Selector errors. However, the approach we use is similar to yours.
You can continue using your approach. However, please note that this is private API and it can be changed in the future.

Related

Problem with Prefix in Discord.js (including .toUpperCase())

Basically, I've been developing a bot for several weeks now using the discord.js library and recently encountered a small but crucial issue. Essentially when I declare my argument, I also made it so that the message content (message.content) would be capitalized using .toUpperCase(). Essentially in doing so regardless of the type of prefix you would enter (symbol wise) it would all be read by the program as valid.
For example only: !help - should work, however if I would enter .help, it would also be read as valid.
In any case, here's the code. I appreciate all the help!
bot.on('message', message =>{
let args = message.content.toUpperCase().substring(PREFIX.length).split(" ");
const sender = message.member;
switch(args[0])
{
case 'HELP':
message.reply("I've sent you some documentation on all the commands that you can use...").then(d_msg => {d_msg.delete(3000); });
message.delete(3000);
const attachment = new Attachment('./UtilityBot_Documentation.txt')
message.author.send('[Education] Bot - Documentation');
message.author.send(attachment);
break;
}
})
The discord.js tutorial covers an extremely similar problem to what you're trying to do. I recommend you check it out. The page I linked in specific is doing a very similar thing to you, but it's worth giving the whole thing a read through if you haven't done so already. In general, I would include the following line just above where you established args.
if (!message.content.startsWith(PREFIX)) return;
What I'm doing here is saying if the message does not start with the prefix, stop running until a new message is sent. I might be missing something, but certainly check out the tutorial. It's really well written.
https://discordjs.guide/creating-your-bot/commands-with-user-input.html#basic-arguments

Cypress contains and cy.wait()

I've got some problem with Cypress and wait command:
I am using similar to this code:
const counter = cy.get('something')
counter.contains('0') //OK
const container = cy.xpath('something multiple').children()
container.click({multiple:true})
//cy.wait(200)
counter.contains('3') //NOK
Only when I am using cy.wait() this code works. I've tried to use the internaltimeout for this code and
it's not working. Only works when using cy.wait.
It is not recommended to save Elements in variables, please use Alise instead.
cy.get('something').as('counter');
cy.get('#counter') .....
Read the documentation for your reference:
https://docs.cypress.io/guides/core-concepts/variables-and-aliases.html
#HatemHatamleh is correct about not using the return value.
With cy.*() commands you are defining command execution steps, which will run separately from the JS in the tests.
Inserting cy.wait() kind-of works because it allows time for the commands to execute, but it's not at all the correct thing to do as it can fail depending on cpu load, async calls, etc.
Think of commands as a separate "thread", try to define them with chaining, but when that's not possible use alias' as #HatemHatamleh suggests.

How to use module.exports and requireJS?

im quite a noob in html and js, so forgive me if this is a dumb question but, im trying to use requireJs to export modules in node and i can't get the function work right.
here is the code extracted from example.
first i have this main.js, as the note in the documentation says http://requirejs.org/docs/node.html#2
var sayHi = require(['./greetings.js'], function(){});
console.log(sayHi);
and a greetings.js who export the answer
module.exports= 'Hello';
});
and get nothing as result, so i define the exports and modules
define( function(exports,module){
module.exports= 'Hello';
});
and get as result:
function localRequire()
what am i doing wrong? i read the documentation and examples, but somehow i can't make this works.
I'm assuming the require call you are using is RequireJS's require call, not Node's require. (Otherwise, you'd get a very different result.)
You are using the asynchronous form of the require call. With the asynchronous form, there is no return value for you to use, you have to use the callback to get module values, like this:
require(['./greetings.js'], function(sayHi){
console.log(sayHi);
});
However, because you are running in Node, you can do this:
var sayHi = require('./greetings.js');
Note how the first argument is a string, not an array of dependencies. This is the synchronous form of the require call. The returned value is the module you required. When you are in Node, RequireJS allows you to call this synchronous form anywhere. When you are running the browser, it is only available inside a define call.

How to add modernizr build so that I can properly check Modernizr.capture? (currently always undefined)

I need to check if the user's device can input from a camera on my site. To do this I am attempting to use modernizr. I have followed the steps/example code provided on their site but when I test the capture attribute, I always get undefined, regardless of if I am on a device that supports capture.
Steps I followed:
I browsed for the input[capture] attribute and added it to the build
I copied the demo code to check this feature and added it to my project
I downloaded the build, added the js file to my project, and included the appropriate reference in my page
However after all of this, when inspecting Modernizr.capture in the chrome inspector, it always shows up as undefined.
My basic check function is as follows:
$scope.hasCamera = function() {
if (Modernizr.capture) {
// supported
return true;
} else {
// not-supported
return false;
}
}
This is my first time using Modernizr. Am I missing a step or doing something incorrectly? I also installed modernizr using npm install and tried adding the reference to a json config file from the command line.
Alternatively, how might I check if my device has a camera?
Thank you very much for your time. Please let me know if I am being unclear or if you need any additional information from me.
A few things
while photos are helpful, actual code hosted in a public website (either your own project, or on something like jsbin.com) is 10x as useful. As a result, I am not sure why it is coming back as undefined.
The actual capture detect is quite simple. It all comes down to this
var capture = 'capture' in document.createElement('input')`
Your code is a lot more complicated than it needs to be. Lets break it down. You trying to set $scope.hasCamera to equal the result of Modernizr.capture, and you are using a function to check the value of Modernizr.capture, and if it is true, return true. If it is false, return false. There is a fair bit of duplicated logic, so we can break it down from the inside out.
Firstly, your testing for a true/false value, and then returning the same value. That means you could simplify the code by just returning the value of Modernizr.capture
$scope.hasCamera = function() {
return Modernizr.capture
}
While Modernizr will always be giving you a boolean value (when it is functioning - without seeing your actual code I can't say why it is coming back as undefined), if you are unsure of the value you can add !! before it to coerce it into a boolean. In your case, it would make undefined into false
$scope.hasCamera = function() {
return !!Modernizr.capture
}
At this point, you can see that we are setting up a function just to return a static value. That means we can just set assign that static value directly to the variable rather than setting up a function to do that
$scope.hasCamera = !!Modernizr.capture
Now, the final thing you may be able to do something better is if you are only using Modernizr for this one feature. Since it is such a simple feature detection, it is overkill to be using all of Modernizr.
$scope.hasCamera = 'capture' in document.createElement('input')`

durandal event not working properly

I am using durandal to pass messages between view models. So i used below code to send message
return (datacontext.getData("Test, testData))
.then(app.trigger('FireEvent', `dataObsArray`))
.fail(queryFailed);
Then i use below code to retrieve message
app.on('FireEvent').then(function (data) {
testObsArray(data);
});
But when i put breakpoint in the app.on on this line testObsArray(data);
it doesnt stop there. The debugger stops on line app.on('FireEvent').then(function (data)
I dont get data. Why is it so? When i pass data to dataObsArray , there are 10 records.
I am not sure why i am not getting any data. Where i am wrong? I am really new to Durandal so extremely sorry if i am not able to explain this properly and do let me know if you need more clarification.
Your problem is here;
.then(app.trigger('FireEvent', `dataObsArray`))
The way that will resolve is to call app.trigger, get the result, and pass that as the next step in the chain to then(), which is unlikely to be what you want. You need to wrap that in an anonymous function so that then() can call it after the dataContext call.
return (datacontext.getData("Test, testData))
.then(function(data) {
app.trigger('FireEvent', data?) //This depends on what getData returns
})
.fail(queryFailed);