Checking for presence of an item without using expect - angularjs

In my web application, the Sign Out link is only shown once the user has logged in - it's not present on the login page.
In my Protractor tests, I want to make sure the user is logged in - which means that in my first test, I have to log in. To check whether the user is logged in or needs to log in, I want to check for the presence of the Sign Out link.
I don't want to do this using expect, since not having the Sign Out link present on the page is perfectly acceptable, it just means that I have to log in.
What I'm looking for is a way to implement an if scenario in my test (in a beforeEach function) - pseudo code:
if (!signOut.isPresent()) {
login();
}
So far, I haven't found an easy way to do this - all of the checks for presence of an item in the page's DOM seem to rely on expect, which throws an exception when the item is not present on the page.
Is there an easy way to do that?

You may just catch the exception.
Or use findElements and check if the returned list of elements is empty, which means the element is not present.
(Not sure about the second solution cause I don't know Protractor, but findElements is what you would use in Java)

The answer by #alb-i986 got me on the right track. After looking at the findElement API, I found two references on how to do the error handling for a this function:
https://github.com/angular/protractor/blob/master/docs/api.md#webdriverwebelement
https://github.com/angular/protractor/issues/485#issuecomment-33951648
Protractor is returning a promise from the findElement function, and the promise's then function (which is called once the promise is resolved) takes two callback arguments:
The first one is called when the promise is resolved successfully, i.e. the element was found on the page.
The second one is optional. When it's not provided, the standard error handling takes place in case the element was not found. This means that the No Such Element exception is thrown. When providing the callback, it's your own responsibility to handle the error there. The error object is provided as a parameter to that callback.
Making use of the second (optional) callback, you can decide to handle or ignore the error:
protractor.getInstance().findElement(by.css(".signout-button"))
.then(function(element) {
// The element was found on the page - click it.
element.click();
}, function(error) {
// Sign out button is not present - handle the error or ignore it
});

Related

#stomp/stompjs latest, subscription() getting in unexpected responses

I am using the latest #stomp/stompjs package in react and it appears to be not working as described so I was wondering if anyone know what was going on.
When I do stompClient.subscribe() I expect only 1 message from the backend. But I normally get an empty object first "{}" and then then the callback goes off again I then get the expect json string. Sometimes I get a 3rd callback with the same json string. I know the page is being redrawn with the new data by react, but I would not think this would cause the callback to go off a second time. Also, this behavior seems to be only for large json string responses. Small response status json objects never returned multiple times in the callback (just the {} object the first time). My workaround was to have a flag that I sett when I do the send command and then is set to false when I get a valid response back. Any extract responses are skipped not saved in the useState hook so react doesn't redraw again.
The second thing I see is sometimes the wrong response is received by the callback. The bookmark paths would be like /location/read and /location/readBL but the response for /location/read would be read by the readBL subscription callback sometimes. This happens 1 out of 10, but I don't understand why it would doing that. The workaround I did is to have the main object key have different words like {camera: {}) and {cameraBL: {}} and see if the object key matches in the expected key in the callback, otherwise skip it.
I assume react is somehow responsible for all this. So, has anyone seen this in their stomp code and know what's going on?
I have inserted flags or filtering to workaround the problem. I have not seen any description of these problems on the web and the stomp home page doesn't talk about any of this.

What is the difference between .all() and .one() in Restangular?

What is the difference between these two? Both seems to make a GET to /users and retrieve them.
Restangular.one('users').getList().then(function(users) {
// do something with users
});
Restangular.all('users').getList().then(function(users) {
// do something with users
});
I understand that you can do one('users', 123) and it will retrieve /users/123 but without the second argument it seems to be the same thing. Why not just have one method in that case?
The one() function has a second argument that accepts an id e.g. .one('users', 1).
one('users', 1).get() translates to /users/1
all('users').getList() translates to /users
Unlike all(), one() is not generally used with .getList() without argument. However, if you were to call .one('users', 1).getList('emails') or .one('users', 1).all('emails').getList(), then you would make a GET request to /users/1/emails.
My guess is that they are there for expressing an intention of what you are going to do. I would understand those as a way to build the url, expressing if you are accessing to the whole resource or to a specific one.
In the end, they are gonna build and do a GET request but because you do a GET and retrieve some data it does not mean that it should be used in that way.
Example extracted from https://github.com/mgonto/restangular/issues/450
getList can be called both ways. If it's called in an element one,
then it needs a subelement to get to a Collection. Otherwise, it
fetches the collection. So the following is the same:
Restangular.one('places', 123).getList('venues') // GET /places/123/venues
Restangular.one('places', 123).all('venues').getList() // GET /places/123/venues
As you can see, it is more expressive to call one('places', 123).all('venues') to understand that you just want the venues located in the area/place 123.
Maybe the following url will help you:
https://github.com/mgonto/restangular/issues/450
I've recently discovered a difference between these methods. Yes, both of them make the same get requests, but the results you get might surprise you (as they surprised me).
Let's assume we have an API method /users which returns not strictly an array, but something like this:
{
"result": [{...}]
}
So an array is returned as a value of some prop of the response object. In this case get() and getList() work differently. This code works well:
Restangular.get('users').then(function (response) {...});
Your response handler gets invoked after response has been received. But this code doesn't seem to work:
Restangular.all('users').getList().then(function (response) {...});
Response handler is not invoked, despite that request completed with status code 200 and non-empty response. Browser console doesn't show any errors and network monitor shows successful request.
I've tested this with Restangular 1.5.2 so probably this is already fixed in newer versions.

<f:view> beforePhase method called twice when page loads

I am trying to capture url parameters when page is loaded. I am using the beforePhase attribute inside the tag to call the backing bean method which handles the PhaseEvent. I am able to read the parameters but there seems to be some problem. The backing bean method is executed twice. First time I am getting all the url paramters but second time I am getting null.
This is the output in the console I get (The line "BeforePhase ONLOAD" is my SOP statement inside the method. As you can see the SOP is printed twice (below is the console output) -
* <_checkTimestamp> Apache Trinidad is running with time-stamp checking enabled. This should not be used in a production environment. See the org.apache.myfaces.trinidad.CHECK_FILE_MODIFICATION property in WEB-INF/web.xml
beforePhase ONLOAD
<_isBeanValidationAvailable> A Bean Validation provider is not present, therefore bean validation is disabled
beforePhase ONLOAD***
I have to use these url parameters to bind it to the view object and retrieve the data for the page. But if the method is called twice like it has been called here then the second will cause some database error or ultimately i will not see any records on screen.
Is there a way to handle this ?
I actually followed the Approach 1 example seen on this page - http://jneelmani.blogspot.com/2013/01/adf-how-to-call-method-on-page-load.html. In this example, he shows how to avoid beforePhase being called multiple times, I have done that but still the method is called twice.
Any suggestions here will be helpful.
Thanks.
Of course you know ADF supports a mechanism already for bookmarkable pages & URL parameters?
http://myadfnotebook.blogspot.pt/2010/11/bookmarking-with-adf.html

What's is the right way to trigger an error callback in Backbone if my response has a certain flag?

This is my use case: I call fetch on a collection and receive a JSON from my server yet I have an error flag. This will always trigger the success flow, and I can detect the error by 2 means:
In my parse method - which is ugly.
By not using the success option, and using the Deferred's Done callback to check for the error. This is ugly as well since I have to call parse myself afterwards.
This would be solveable if Backbone had a validate function on collection but it doesn't...
Any suggestions?
EDIT: I know there's a way to do it by supplying my own Sync method but I got a bit lost there...
good question.. I'm not sure it's so bad to work with the parse method. It's name doesn't fit but it's all you've got in the natural path of the code and I guess you can just return an empty list without breaking anything.
The question to me is what is the cause of the error? If it's, say, a permissions thing (or some other error covered by the http protocol), you could return an error code from the server which should trigger your error callback..

Nancy RequiresClaims failure returns a 403. How do I use this?

If a module requires a claim, and the user does not have the claim a 403 response is returned.
eg:
this.RequiresClaims(new[] { "SuperSecure" });
or
this.RequiresValidatedClaims(c => c.Contains("SuperSecure"));
but that just returns a blank page to the user.
How do I deal with a user not having the required claim?
Can I 'catch' the 403 and redirect?
The RequiresClaims method returns void or uses the pre-request hook to throw back a HttpStatusCode.Forbidden. What should I do so the user knows what has happened?
Many Thanks,
Neil
You can catch it either by writing your own post request hook (either at the app level, or the module level) or by implementing your own IErrorHandler, probably wrapping the default one.
The error handler stuff is going to change so you will be able to register multiple ones (for different error codes), it's setup to do that (with the "can/do" interface) but for some reason my brain didn't add it as a collection :-)

Resources