Combine "toHaveBeenCalledWith" and "toBeCloseTo" - jasmine

I need a way to check if a spy was called with an approximate value, like it can be done with expect(value).toBeCloseTo(3,2).
Something like this:
expect(spy).toHaveBeenCalledWith("some string", valueToBeCloseTo(3, 2))

I think you can use the calls and mostRecent helpers.
const secondArgument = spy.calls.mostRecent().args[1]; // args[0] would be the string
expect(secondArgument).toBeLessThanOrEqual(3);
expect(secondArgument).toBeGreaterThanOrEqual(2);
Check out this link for most recent and this link for toBeLessThanOrEqual and toBeGreaterThanOrEqual.

Related

how to access a node when path contains space using node.xpath?

I need to use node.xpath to access a JSON node, but the property name contains space, like "First Name":
empDesc = cts.doc('/employee/employee1.json').xpath('//First Name');
How can I make this work?
Something like this:
empDesc = cts.doc('/employee/employee1.json').xpath('//*[name(.)="First Name"]')
But you are probably better off converting this to a JSON object and using normal access methods.
You can use node() with an argument inside MarkLogic XPath:
empDesc = cts.doc('/employee/employee1.json').xpath('//node("First Name")');
If you need to grab multiple properties, you could also convert to a JSON object first, and access that the regular way, like Mary suggested. Something like this:
let doc = cts.doc('/employee/employee1.json').toObject();
let empDesc = doc.employee['First Name'] + ' ' + doc.employee['Last Name'];
HTH!

Using one variable for multiple items data in descriptive programming

I know that with Descriptive programming you can do something like this:
Browser("StackOverflow").Page("StackOverflow").Link("text:=Go To Next Page ", "html tag:=A").Click
But is it possible to create some kind of string so I can assign more than one data value and pass it as single variable? I've tried many combinations using escape characters and I always get error.
For example in the case above, let's say I have more properties in the Page object, so I'd normally have to do something like this:
Browser("StackOverflow").Page("name:=StackOverflow", "html id:=PageID")...etc...
But I'd like to pass "name:=StackOverflow", "html id:=PageID" as a single variable, so when writing many objects I'd only have to write:
Browser(BrowserString).Page(PageString).WebEdit("name:=asdfgh")
And the first part would remain static, so if the parents' data needs to be modified I'd only have to modify two variables and not all the objects created in all libraries.
Is it possible?
If I was not clear enough please let me know.
Thank you in advance!
I think what you're looking for is UFT's Description object
This allows you finer grained control on the description since in descriptive programming all values are regular expressions but with Description you can turn the regular expression functionality off for a specific property.
Set desc = Description.Create()
desc("html tag").Value = "A"
desc("innertext").Value = "More information..."
desc("innertext").RegularExpression = False
Browser("Example Domain").Navigate "www.example.com"
Browser("Example Domain").Page("Example Domain").WebElement(desc).Click
If you want to represent this with plain string then it's a bit more of a problem, you can write a helper function but I'm not sure I would recommend it.
Function Desc(descString)
Set ret = Description.Create()
values = Split(descString, "::")
For Each value In values
keyVal = Split(value, ":=")
ret(keyVal(0)).Value = keyVal(1)
Next
Set Desc = ret
End Function
' Usage
Browser("StackOverflow").Page("StackOverflow").WebElement(Desc("html tag:=H2::innertext:=some text")).Click
Further reading about descriptive programming.
As an alternative to Motti's excellent answer, you could also Set a variable to match your initial descriptive object and then extend it as required:
Set myPage = Browser("StackOverflow").Page("name:=StackOverflow", "html id:=PageID")
after which you can then use
myPage.WebEdit("name:=asdfgh")
throughout the rest of the code, so long as the myPage object stays in scope...

How to find the definition of an existing method?

I am new to programming and Ruby both. In some existing code, it says something like this:
kid_raising_btn=query("switch marked:'KIDRAISING'",:isOn).first
From this, I understand that a variable kid_raising_btn is defined, which will query and return true or false, then call the method first (but this is confusing).
How can I find what first does?
".first" is kind of convenient method.
Answer for the UI query comes as an array.
So the ".first" represent the first element of the array.
There are few methods like ".count"
Ex:
your Query :
ans = query("button",:accessibilityLabel)
Assume You will get result like this
[
[0] "icon rewards new",
[1] "icon my receipts new",
[2] "icon my account",
[3] "icon order#2x",
[4] "icon check in"
]
if you use ".first" like this
ans2 = query("button",:accessibilityLabel).first
you will get a String with first element as a result instead of an array.
"icon rewards new"
Now you can see you get the first element as the answer
From your description, it sounds like the method chaining here might be contributing to your confusion, so first let's rewrite that line of code like this:
query_response = query("switch marked: 'KIDRAISING'", :isOn)
kid_raising_btn = query_response.first
Now, it's easier to see that the #query method returns an object that has a #first method. You can take a look at this object to see what class it is (query_response.class), and then either look up the appropriate documentation for the class or find the method definition in your codebase. For example, if #query returns an array, you can find the documentation here: http://ruby-doc.org/core-2.0.0/Array.html#method-i-first

magento _redirect with parameters that have + or /

seems like a call to
$this->_redirect('*/*/myaction',$myargs);
does not properly escape the arguments
so if
$myargs=array(p1=>'string that has + or / within it')
the created URL will be something like:
..../myaction/?p1/string%20that%20has%20+%20or%20/%20within%20it
causing the getParams collection on the action to have
p1 with value 'string that has or ' <- plus sign missing and value broken and
' within it' with no value or something similar.
is there any standard way I should handle the arguments before passing them to _redirect ?
Eyal
Yes, there are two standard ways.
Pass all your params as route params, but encode them with php urlencode() func:
foreach ($myargs as $key => $val) {
$myargs[$key] = urlencode($val);
}
$this->_redirect('*/*/myaction', $myargs);
Pass your params as query params
$this->_redirect('*/*/myaction', array('_query', $myargs));
You'd better take second approach, because your params logically are not route but query parameters. Magento is made with a lot of architecture thinking, so it usually points better ways to do stuff - that's why in your case it's easier to send params using second way.
Notice: _redirect() internally uses Mage_Core_Model_Url, so everything said in this answer is true for all other url-forming routines and all usages of Url model.
refer to http://www.blooberry.com/indexdot/html/topics/urlencoding.htm#whatwhy and read the section "Reserved characters"

Prototype: how to dynamically construct selector?

I am having a little bit of difficulty passing a variable into a selector in prototype. I would like to be able to pass a variable into the select string, so that one function can work for many of the same kind.
At the moment, this is what I would basically like to do:
function myFunct(var)
{
$(var + 'add_form').hide() //so inde the brackets would be ('#product1 #add_form') for example.
}
Be able to pass 'var' into the function that would pass it to the selector, so that I can hide a pattern that is the same for many on the page.
Any ideas for a path to follow would be greatly appreciated.
You're on the right track! Couple things:
var is a JavaScript keyword (source), don't use it to name a variable
if you're querying an element by id (such as #add_form) you don't need to add any container element as you're doing
If you're querying an element by class, you need to use the $$ function, not the $ function
You need to iterate over the wrapped set to call your method
whitespace is significant in css selectors, so make sure to include those in your selector construction to tell Prototype to search within your parent container:
function myFunct(parent) {
$$(parent + ' .add_form').invoke('hide')
}
myFunct('#someparent'); // hides .add_form inside #someparent
That should work... just rename var to something else.
function myFunct(yourVar)
{
$$('#' + yourVar + ' .add_form').each(function(s){ s.hide(); }); // yourVar being the id of the container element
}
I've put a '.' in front of add_form because you can't use multiple elements with same ID, make it a class.

Resources