selenium IDE javascript replace: Throw an exception: missing ; before statement - javascript

I wrote a test using selenium IDE, I need to compare two text that MAY contain the
character, if the strings are equal, it skips to a label. the comparison command
<td>gotoIf</td>
<td>'${var1}'=='${var2}'</td>
<td>skip</td>
works if the character above is not there, but fails with
[error] Threw an exception: missing ; before statement` otherwise
I tried to write a replace statement supposed to replace ' with a blank space (since I don't care if is there or not):
<td>storeEval</td>
<td>javascript{storedVars.var1.replace("\'"," ");}</td>
<td>var1</td>
but it always fails with the same error as above.
what am I missing? can anyone help me?

I took user extension js from here. Can you try with double quote instead of single quote in gotoIf command? It works for me. Please refer my screenshot below.
<td>gotoIf</td>
<td>"${var1}"=="${var2}"</td>
<td>skip</td>

Related

Regex returns nothing to repeat [duplicate]

I'm new to Regex and I'm trying to work it into one of my new projects to see if I can learn it and add it to my repitoire of skills. However, I'm hitting a roadblock here.
I'm trying to see if the user's input has illegal characters in it by using the .search function as so:
if (name.search("[\[\]\?\*\+\|\{\}\\\(\)\#\.\n\r]") != -1) {
...
}
However, when I try to execute the function this line is contained it, it throws the following error for that specific line:
Uncaught SyntaxError: Invalid regular expression: /[[]?*+|{}\()#.
]/: Nothing to repeat
I can't for the life of me see what's wrong with my code. Can anyone point me in the right direction?
You need to double the backslashes used to escape the regular expression special characters. However, as #Bohemian points out, most of those backslashes aren't needed. Unfortunately, his answer suffers from the same problem as yours. What you actually want is:
The backslash is being interpreted by the code that reads the string, rather than passed to the regular expression parser. You want:
"[\\[\\]?*+|{}\\\\()#.\n\r]"
Note the quadrupled backslash. That is definitely needed. The string passed to the regular expression compiler is then identical to #Bohemian's string, and works correctly.
Building off of #Bohemian, I think the easiest approach would be to just use a regex literal, e.g.:
if (name.search(/[\[\]?*+|{}\\()#.\n\r]/) != -1) {
// ... stuff ...
}
Regex literals are nice because you don't have to escape the escape character, and some IDE's will highlight invalid regex (very helpful for me as I constantly screw them up).
For Google travelers: this stupidly unhelpful error message is also presented when you make a typo and double up the + regex operator:
Okay:
\w+
Not okay:
\w++
Firstly, in a character class [...] most characters don't need escaping - they are just literals.
So, your regex should be:
"[\[\]?*+|{}\\()#.\n\r]"
This compiles for me.
Well, in my case I had to test a Phone Number with the help of regex, and I was getting the same error,
Invalid regular expression: /+923[0-9]{2}-(?!1234567)(?!1111111)(?!7654321)[0-9]{7}/: Nothing to repeat'
So, what was the error in my case was that + operator after the / in the start of the regex. So enclosing the + operator with square brackets [+], and again sending the request, worked like a charm.
Following will work:
/[+]923[0-9]{2}-(?!1234567)(?!1111111)(?!7654321)[0-9]{7}/
This answer may be helpful for those, who got the same type of error, but their chances of getting the error from this point of view, as mine! Cheers :)
for example I faced this in express node.js when trying to create route for paths not starting with /internal
app.get(`\/(?!internal).*`, (req, res)=>{
and after long trying it just worked when passing it as a RegExp Object using new RegExp()
app.get(new RegExp("\/(?!internal).*"), (req, res)=>{
this may help if you are getting this common issue in routing
This can also happen if you begin a regex with ?.
? may function as a quantifier -- so ? may expect something else to come before it, thus the "nothing to repeat" error. Nothing preceded it in the regex string so it didn't get to quantify anything; there was nothing to repeat / nothing to quantify.
? also has another role -- if the ? is preceded by ( it may indicate the beginning of a lookaround assertion or some other special construct. See example below.
If one forgets to write the () parentheses around the following lookbehind assertion ?<=x, this will cause the OP's error:
Incorrect: const xThenFive = /?<=x5/;
Correct:
const xThenFive = /(?<=x)5/;
This /(?<=x)5/ is a positive lookbehind: we're looking for a 5 that is preceded by an x e.g. it would match the 5 in x563 but not the 5 in x652.

Illegal character in javascript

I've been debugging this for hours already but really can't find the culprit of this illegal character. My javascript looks fine. This is my code.
this.PrintApplication = function Test$PrintApplication(ApplicationID, callback) {
$.post("/Application/PrintApplication/" + ApplicationID,
function (data) {
var result = eval(data);
if (result.error) {
DisplayPrompt("Error", result.message);
return;
}
else {
callback(result.data);
}
});
};
In firebug it shows.
In inspect in chrome and in console it redirects me in this line.
Any idea where is that illegal character is in my function?
It looks like you've got some unprintable characters in your source. Do you have a way of displaying those in your editor and deleting them? Deleting and retyping the line might fix it as well.
If that's not the case, maybe what you're trying to evaluate isn't JavaScript at all. You could be running that on an image or some kind of binary data.
Remember to be extra super careful when using eval on data that comes from an external source. If you can avoid doing it, avoid it.
This might be due to the reason that you have copied the code from web and simply pasted in your file. Try typing the same code to the file.
This error occurs due to UTF-8 characters.
This could happen if you normally type with different alphabets. For example the Γreek question mark ; is a different ASCII character from the English semi colon ;. If you use the first, you'll get exactly this error.
One solution is to copy paste your method to notepad and then back to your IDE.
This will often normalise and eliminate weird characters that might be hidden or undecipherable.

jscs error : validateLineBreaks: Invalid line break at filename.js

After grunt-jscs it gives following errors for base/index.js file.
Running "jscs:src" (jscs) task
validateLineBreaks: Invalid line break at api/v1/base/index.js :
1 |var errors = require('restify-errors');
-----------------------------------------------^
2 |function Base(Model) {
After I remove var errors = require('restify-errors'); this line, it starts giving below error
Running "jscs:src" (jscs) task
validateLineBreaks: Invalid line break at api/v1/base/index.js :
1 |function Base(Model) {
------------------------------^
How to resolve this?
Workaround found : Created new file and copied all content to it resolves the problem.
Still want to know any specific reason why this is happening?
this is probably an issue with line breaks
You might want to put "validateLineBreaks": null into your .jscsrc file
If your .jscsrs is with the rule
"validateLineBreaks":"LF",
It means you must use LF as line breaks, if you are using other line break symbol (such as CRLF), JSCS will give you an error like:
validateLineBreaks: Invalid line break at api/v1/base/index.js :
There're two ways to resolve the problem, one is to change the jscs' rule, and the other is to always use LF as line breaks.
You can find the different between LF and CR in this link:
https://en.wikipedia.org/wiki/Newline
tldr;
Change between LF an CRLF, if you are using VScode you can do this by clicking the option at the bottom right:
Detailed: As tomato said, the issue is probably that the line break format of your IDE isn't compatible with jscs, from the eslint documentation:
The linebreaks (new lines) used in windows operating system are
usually carriage returns (CR) followed by a line feed (LF) making it a
carriage return line feed (CRLF) whereas Linux and Unix use a simple
line feed (LF). The corresponding control sequences are "\n" (for LF)
and "\r\n" for (CRLF)
You can also try adding *.js text eol=lf to your .gitattributes file if you know this won't affect the project in any meaningful way.
You can read more about the issue here: https://eslint.org/docs/rules/linebreak-style

Sublime Text parse error default(windows).sublime-keymap.2:52

Sublime Text 2 wouldn't open. I kept getting this error:
(Error trying to parse file: Unexpected character, expected a comma or closing
bracket in c:\Users\adeleon\AppData\Roaming\Sublime Text 2\Packages\
Default (Windows).sublime-keymap.2:52)
I found the file and put it into JSON Lint. I got this error.
Expecting 'EOF', got ','
The validator says that the problem is on line 2 of the code. I do not see any missing curly brackets. Would somebody please tell me what I am overlooking?
The link to the code is below.
https://github.com/alexwebcoder/jsonFile/blob/master/ff.txt
I think you are missing an open bracket ([) at the very beginning of the code.

Why does string.match(...)[0] throws an exception?

I'm trying to pull the first occurence of a regex pattern from a string all in one statement to make my code look cleaner. This is what I want to do:
var matchedString = somestring.match(/some regex/g)[0];
I would expect this to be legal but it throws an exception:
Exception: somestring.match(...) is null
It seems like JS is trying to index the array before match is finsihed, as the array does provide atleast one match, so I don't expect it to be null.
I would like some insight in why it happens. Could it be a bug?
My machine is a PC running Arch Linux x86_64. The code is being executed within the scratchpad of firefox 32.0.3.
Thanks for your interest.
If somestring.match() finds no match, then it returns null.
And, null[0] throws an exception.
Since you are getting this exact exception, your regex is not being found in the content. Be very careful using the g flag on a match option in this way as it does not always do what you expect when you have submatches specified in the regex. Since it looks like you just want the first match anyway, you should probably remove the g option.
A safer way to code is:
var matches = somestring.match(/some regex/);
if (matches) {
// do something here with matches[0]
}
If you want to do it in one statement (and there's no particularly good reason why that is a good idea, see jfriend000's answer), then:
var firstMatchOrFalse = /pattern/.test(somestring) && somestring.match(/pattern/)[0];
and if you only want the first match, why the g flag?

Categories

Resources