Cypress string variable condition - javascript

I'm write Cypress script is say js, but I have some issue about variable checking
the code is like this
var baseUrl = Cypress.config('baseUrl');
// baseUrl can return http://developmachine1.localhost/test
I want to do simple condition
if (baseUrl.contains('localhost'))
{
do something ...
}
else
{
do something ..
}
but it will throw TypeError : baseUrl.contains is not a function
can I know to do it ?
Thank you

The JS function for checking if a string contains another string is includes.
if (baseUrl.includes('localhost')) {
// code
} ...

Related

Webpack plugin parser hook doesn't tap for expression

I'm writing a plugin for webpack for extracting values from MyObject.myProperty = 'myValue'; expressions, and my hook doesn't tap for my expression. Here is the example code:
var zzz = (function () {
function zzz() {
return this;
}
zzz.MY_VAR = "My value";
return zzz;
});
Here is how my code for the hook looks like:
parser.hooks.expression.for("zzz.MY_VAR").tap("MyPlugin", expr => {
console.log(expr);
}
I also tried:
parser.hooks.evaluate.for("AssignmentExpression").tap("MyPlugin", expr => {
console.log(expr);
}
Also without success.
I did some debugging, and find out that for some reason when JavascriptParser is calling getFreeInfoFromVariable() in getMemberExpressionInfo() it returns undefined. Because getVariableInfo() method return some scope details instead of VariableInfo instance or 'string'.
Am I missing something ? Is it possible to get value of the object's property via parser somehow ? Or maybe there is another way to do it ?

Not changing image src attribute through javascript

The following is my current JavaScript code which is not working. I'm trying to change the image.
function imgchange(a)
{
var e=document.getElementById(a);
if(e.src == "plus.png")
{
e.src = "minus.png";
}
else
{
e.src="plus.png";
}
}
When you are using img.src it returns whole path to img src, not only plus.png
You have to make comparison like http://localhost/images/plus.png (whatever your path is)
or use getAttribute method like is mentioned in undefined's post
src property includes the full url of the image, try using getAttribute method, which returns the specified value in the HTML.
if ( e.getAttribute("src") === "plus.png" )
Note that means that you should also set the new value using .setAttribute() for future comparisons. If you want to use the .src property you should either compare the full paths or use other methods like regular expression or split method:
if ( e.src.split('/').pop() === "plus.png" )
the function itself should be working. there must be something wrong at the place you are calling imgchange("xyz").
so maybe you can show us the code where the function is actually called.
function imgchange (a) {
var e=document.getElementById(a);
if (e.src.replace(/.+\//, '') === 'plus.png') {
e.src = "minus.png";
} else {
e.src="plus.png";
}
}
Should work.

Javascript running code once

I only want my JavaScript to run once, but I cannot control how many times the javascript file is executed. Basically I'm writing a tiny JS snippet into a CMS, and the CMS is actually calling it 5-10 times. So solutions like this:
function never_called_again(args) {
// do some stuff
never_called_again = function (new_args) {
// do nothing
}
}
never_called_again();
Don't seem to work because as soon as my snippet is run again from the top the function is re-declared, and 'do some stuff' is re-evaluated. Perhaps I'm just not doing it properly, I'm not great with JS. I'm considering using something like try-catch on a global variable, something like
if (code_happened == undefined) {
\\ run code
code_happened = true;
}
EDIT: There is a consistent state e.g. if I set a variable I can see when my snippet is run again. But having to declare it before I access it, I don't know how to say 'does this variable exist yet'
Try this:
var doneTheStuff;
function whatever() {
if (!doneTheStuff) {
doneTheStuff = true;
// do the stuff
}
}
Redundant variable declarations don't affect the value of the variable. Once one of the functions has set the variable to true, the others won't do anything.
if (typeof code_happened === 'undefined') {
window.code_happened = true;
// Your code here.
}
The typeof check gets you around the fact that the global hasn't been declared. You could also just do if (!window.code_happened) since property access isn't banned for undefined properties.
Use a closure, and set a flag. If the flag is true, just return:
if ( ! window.never_called_again ) {
window.never_called_again = (function () {
var ran = false;
return function (args) {
if ( ran ) return;
ran = true;
// Do stuff
};
}());
}
Here's the fiddle: http://jsfiddle.net/U2NCs/
With jQuery, the function .one() may be useful : http://api.jquery.com/one/
W3School exemple here : http://www.w3schools.com/jquery/event_one.asp
In this way, the code is executed only once.
if(typeof onceRun == "undefined") window.onceRun=(
()=>{
//your codes...
console.log("runing...")
return true
}).call()

How to execute a JavaScript function when I send its name as string dynamically without parameters

I want execute JavaScript function which the name is coming as a string dynamically.
I don't need to pass any parameters while executing the function.
Please can any one guide me how to achieve this?
one simple way
eval("SomeFunction()");
or
var funcName = "SomeFunction";
var func == window[funcName];
func();
dangerous but you could use eval(method_name+"()")
are you talking about ´eval()´??
var foo = "alert('bar')";
eval(foo);
Hope this helps;
function a() {
console.log('yeah!');
}
var funcName = 'a'; // get function name
this[funcName]();
If the function is global, you should do window[funcName]() in browser.
Using eval is the worst way imaginable. Avoid that at all costs.
You can use window[functionname]() like this:
function myfunction() {
alert('test');
}
var functionname = 'myfunction';
window[functionname]();
This way you can optionally add arguments as well
Perhaps a safer way is to do something like this (pseudo code only here):
function executer(functionName)
{
if (functionName === "blammo")
{
blammo();
}
else if (functionName === "kapow")
{
kapow();
}
else // unrecognized function name
{
// do something.
}
You might use a switch statement for this (it seems like a better construct):
switch (functionName)
{
case "blammo":
blammo();
break;
case "kapow":
kapow();
break;
default:
// unrecognized function name.
}
You can optimize this by creating an array of function names, searching the array for the desired function name, then executing the value in the array.

javaScript jQuery function, how do i pass variables

What is wrong with my code. How do i pass attrfull to the inside. The way i have it done, if i run the function editsubmit($selected, size), $selected is inserted properly but i'm getting attrfull instead of size.
function editsubmit(attr, attrfull) {
if (attr.length) {
attr.val().length ? $selectedinput.attr({
attrfull: attr.val()
}) : $selectedinput.removeAttr(attrfull);
}
}
$selected is a variable and attrfull i a string. Do i need double qoutes around the string when i run the function like editsubmit($selected,'size').
Try
function editsubmit(attr, attrfull) {
if (attr.length) {
attr.val().length ? $selectedinput.attr(attrfull, attr.val()) : $selectedinput.removeAttr(attrfull);
}
}
Yes, you do need it to be a string (in double quotes), or else it will think you're trying to pass a variable reference.
The problem is this:
{attrfull: attr.val()}
I think you want it to be
{size: (whatever attr.val() is)}
So:
function editsubmit(attr, attrfull) {
if (attr.length) {
if (attr.val().length) {
var myObj = {};
myObj[attrfull] = attr.val();
$selectedinput.attr(myObj);
} else {
$selectedinput.removeAttr(attrfull);
}
}
}

Categories

Resources