How do I use the 'Evaluate Javascript' function in Robotframework? - javascript

I recently updated some outdated Robotframework libraries and found that the 'Execute Javascript' function from the Browser Library was deprecated and deleted, to be replaced with 'Evaluate Javascript'. After having read the description I tried adjusting the tests that were given to me to work with it, but to no avail.
This was the old, deprecated code:
${value} Evaluate Javascript document.querySelector("score_Id").innerHTML.
This gave the ${value} the '3.9' from this element:
3.9
I attempted to adjust my code to the following:
${value} Evaluate Javascript score_Id document.innerHTML.
This didn't work, as it returned an empty value. so I tried multiple variations:
${value} Evaluate Javascript score_Id document.textContent
${value} Evaluate Javascript score_Id document.getElementById.textContent
${value} Evaluate Javascript score_Id document.querySelector().innerHTML
${value} Evaluate Javascript score_Id innerHTML
All of these do not work.
I'm a bit stuck on what to do next. I don't have colleagues to ask about and I'm relatively new to all of this. I also tried a simple 'Get Text' function from the Browser Library:
${value} Get Text //*[#id="score_Id"]
But this resulted in the following:
FAIL : Error: elementHandle.innerText: Error: Node is not an HTMLElement
Can anyone explain to me what I'm doing wrong? Thanks in advance!

Figured it out:
${value} Evaluate JavaScript id=scoreId (elem) => elem.innerHTML

Related

JavaScript Object Property Includes SPACE Character

I'm working in VS Code on a VueJS 3 project. I have Vetur Extension (version: v0.35.0) installed for error checking and syntax highlighting. I'm getting notified by red tick mark and output in the Problems window about the format of how I'm identifying Object Properties that include a space character in their definition. Here is an example:
this.ncCases = this.store.state.csvJson.data.filter(obj => {
return obj.[`Neighborhood Council`] == this.selectedNC
})
The property "Neighborhood Council" includes a space. I found a reference at mozilla.org that describes enclosing these properties within Square Brackets. However, when I do this, I get the Red Error Market of death.
Identifier expected. Vetur(1003)
Must say the code runs without error so I think the problem is Vetur Extension does not recognize this syntax. Never the less, it would be nice to fix the code to not generate the error or fix Vetur to not display a false positive error.
Any suggestions?
this.ncCases = this.store.state.csvJson.data.filter(obj => {
return obj['Neighborhood Council'] == this.selectedNC
})
This should be fine in order to get access to the property
Maybe this response could help you in order to disable some checks here

Cypress getByTestId, queryByTestId, findByTestId to check if element doesn't exist

I am trying to check if element doesn't exist in a DOM Tree with Cypress and testing-library/cypress.
If I try to do cy.getByTestId("my-button").should("not.exist") test fails because it couldn't find element.
If I do cy.findByTestId("my-button").should("not.exist") it also fails because of time out.
The test does work if I do either cy.queryByTestId("my-button").should("not.exist") or
cy.get('[data-testid="my-button"]').should("not.exist").
Can someone please explain what's the difference between all 4.
Thanks
https://testing-library.com/docs/dom-testing-library/api-queries
getBy will throw errors if it can't find the element
findBy will return and reject a Promise if it doesn't find an element
queryBy will return null if no element is found:
This is useful for asserting an element that is not present.
looks like queryBy is your best choice for this problem
In the latest version of Cypress Testing Library they have removed queryBy.
Cypress Testing Library | Intro
If you want to check if something doesn't exist just use findBy, but put a should() straight afterwards. It won't time out in that case.
cy.findByText('My error message').should('not.exist')
Discussion on GitHub

JavaScript "Packed" Function in CSharp

I have an obfuscated (p,a,c,k,e,d) function from JavaScript.
string value = #"eval(function(p,a,c,k,e,d)
{while(c--)if(k[c])p=p.replace(new RegExp('\\b'+c.toString(a)+'\\b','g'),k[c]);
return p}
('3("3b").3a({39:\"5://1b.1a.19.8:18/38/v.37\",36:\"5://1b.1a.19.8:18/i/35/34/33.32\",31:\"\",30:\"2z\",2y:\"2x\",2w:2v,2u:\"7\",2t:\"2s\",2r:[],2q:{2p:\'#2o\',2n:22,2m:\"2l\",2k:0}});b f;b k=0;b 6=0;3().2j(2(x){a(6>0)k+=x.17-6;6=x.17;a(0!=0&&k>=0){6=-1;3().2i();3().2h(2g);$(\'#2f\').j();$(\'h.g\').j()}});3().2e(2(x){6=-1});3().2d(2(x){16(x)});3().2c(2(){$(\'h.g\').j()});2 16(x){$(\'h.g\').2b();a(f)2a;f=1;$.29(\'5://12.9/15-28/27.15?26=25&24=23&21=20-1z-1y-1x-1w\',2(14){$(\'#1v\').1u(14)})};3().1t(\'1s\',2(){b 13=3().1r();13.1q(\'1p\',2(){11.10(\'z-y\')[0].w[1].1o=\"5://12.9\";11.10(\'z-y\')[0].w[1].1n=\"<u>1m - 1l 1k 1j & 1i</u>\"});a($.c(\'4\')==\"d\"){t.s(\"5://r.q.p/o/7.n\",\"m 9 1h\",e,\"l\")}1g{t.s(\"5://r.q.p/o/d.n\",\"m 9 1f\",e,\"l\")}});2 e(){$.c(\'4\')==\'7\'?4=\'d\':4=\'7\';$.c(\'4\',4);1e.1d.1c()};',36,120,'||function|jwplayer|primaryCookie|http|p02887336|html5||to|if|var|cookie|flash|switchMode|vvplay|video_ad|div||show|tt2887336|button2|Switch|png|images|tv|flashx|static|addButton|this|center||childNodes||featured|jw|getElementsByClassName|document|streamin|container|data|cgi|doPlay|position|8777|213|211|95|reload|location|window|Flash|else|HTML5|Storage|Sharing|Video|Free|Streamin|innerHTML|href|contextmenu|addEventListener|getContainer|ready|on|html|fviews|fb98c55a544241de2464a88086a6b0c9|1486917733|186|182|2887336|hash||2ca0tpqawtre|file_code|view|op|index_dl|bin|get|return|hide|onComplete|onPlay|onSeek|play_limit_box|false|setFullscreen|stop|onTime|backgroundOpacity|Arial|fontFamily|fontSize|FFFFFF|color|captions|tracks|start|startparam|primary|326|height|580|width|2638|duration|skin|jpg|lhl2j9yhfp1s|00085|01|image|mp4|rwi7bsgc5huzcg3h5fpsfen3362uwfp4cyzyo2mavczsvbbx4tnesmwhdteq|file|setup|vplayer'.split('|')))"
I am trying to use jint to interpret and get the value of the above function but when I execute the statement in jint engine it throws an exception for jwplayer is not defined.
I know why the exception is thrown but I only want to get the value of p above. But jint runs the whole function and then ends up with an empty function which doesn't exist and when it tries to run it, an exception is thrown.
I only want it to return the value in a string and it should not run it any further. I have already tried to search it for an answer but so far found nothing.
I am using the code as following:
Jint.Engine jEngine = new Jint.Engine();
var linkString = jEngine.Execute(value).GetCompletionValue().ToString();
var arrayFile = linkString.Split(' ', '\n');
Is something I am missing here? If someone can shed some light on it. That will be much appreciated. Thanks
I think the problem was eval at the start of the packed function. It was running the code after getting the value. So I removed the eval part and now it works as expected.

Google closure compiler javascript parse error

I am trying to use google closure compiler on my javascript files. It works fine except for the following piece of code:
function goto(form) { var index=form.select.selectedIndex
if (form.select.options[index].value != "0") {
location=form.select.options[index].value;}}
The compiler returns:
JSC_PARSE_ERROR: Parse error. missing ( before function parameters. at line 1 character 9
function goto(form) { var index=form.select.selectedIndex
There is a caret (^) pointing to the g in "goto" when it is output on the screen.
I am using just the basic UI version here to test:
http://closure-compiler.appspot.com/home
Any idea what is wrong with the javacript? It seems to work just fine but I am not a javascript person so I have no clue how to fix it. Thanks,
Bill
I am not sure why #Sirko deleted his answer. So I will add it.
You need to change the name of the function goto to something else. Something like gotoUrl, gotoPage, etc.
It was a reserved word in ECMAScript 3, but removed in ECMAScript 5. I am guessing the closure compiler uses that older list still.

Javascript regex match fails on actual page, but regex tests work just fine

I have a very specific problem concerning a regular expression matching in Javascript. I'm trying to match a piece of source code, more specifically a portion here:
<TD WIDTH=100% ALIGN=right>World Boards | Olympa - Trade | <b>Bump when Yasir...</b></TD>
The part I'm trying to match is boardid=106121">Olympa - Trade</a>, the part I actually need is "Olympa". So I use the following line of JS code to get a match and have "Olympa" returned:
var world = document.documentElement.innerHTML.match('/boardid=[0-9]+">([A-Z][a-z]+)( - Trade){0,1}<\/a>/i')[1];
the ( - Trade) part is optional in my problem, hence the {0,1} in the regex.
There's also no easier way to narrow down the code by e.g. getElementsByTagName, so searching the complete source code is my only option.
Now here's the funny thing. I have used two online regex matchers (of which one was for JS-regex specifically) to test my regex against the complete source code. Both times, it had a match and returned "Olympa" exactly as it should have. However, when I have Chrome include the script on the actual page, it gives the following error:
Error in event handler for 'undefined': Cannot read property '1' of null TypeError: Cannot read property '1' of null
Obviously, the first part of my line returns "null" because it does not find a match, and taking [1] of "null" doesn't work.
I figured I might not be doing the match on the source code, but when I let the script output document.documentElement.innerHTML to the console, it outputs the complete source code.
I see no reason why this regex fails, so I must be overlooking something very silly. Does anyone else see the problem?
All help appreciated,
Kenneth
You're putting your regular expression inside a string. It should not be inside a string.
var world = document.documentElement.innerHTML.match(/boardid=[0-9]+">([A-Z][a-z]+)( - Trade){0,1}<\/a>/i)[1];
Another thing — it appears you have a document object, in which case all this HTML is already parsed for you, and you can take advantage of that instead of reinventing a fragile wheel.
var element = document.querySelector('a[href*="boardid="]');
var world = element.textContent;
(This assumes that you don't need <=IE8 support. If you do, there remains a better way, though.)
(P.S. ? is shorthand for {0,1}.)

Categories