Creating refreshRoutes Command0:00
Now that we've tinkered around with a routes endpoint, let's now make the request. So in Laravel commands, yeah, what if we copied all of this and we'll create a new one here? Once again, just to clear the space like that, we'll do it here. Okay, we're going to call this, hmm, refreshRoutes. How do we want to use it? Cypress.refreshRoutes, maybe something like that. And then fetch and store all named routes, something like that. Okay, so you can see from this other example, Cypress can make a POST request using the request method. So here, was it a POST request?
request method. So here, was it a POST request? I'm sorry, we have GET request. That's probably fine. So we're going to make a GET request to routes, is that right? Cypress slash routes. I don't want to log it to the console. And then Cypress offers kind of a handy way to, it's just shorthand, for grabbing the response.body. Okay, so let's create a new example.
Testing the Route Fetch0:59
response.body. Okay, so let's create a new example. I'm just going to call it testSpec. And we'll say, it fetches all named routes. Okay, so I should be able to say Cypress.refreshRoutes. All right, so think about it. When I call this method, it's immediately going to make a GET request to this endpoint, then grab the body of the response, and we don't do anything with it. Why don't we say refreshRoutes, and then with those routes, we could log it to the console, or Cypress has a cypress.log command that'll make it show up in the UI.
Why don't we say refreshRoutes, and then with those routes, we could log it to the console, or Cypress has a cypress.log command that'll make it show up in the UI. All right, let's have a look. npx cypress open. Again, in Electron, let's do testSpec. Okay, and it fails. I must have screwed up. Let's see, cypress.request failed with a 500, and when you get this, you can just scroll down to the body. Call the undefined method route.getRoute.
Fixing the 500 Error1:54
down to the body. Call the undefined method route.getRoute. Ah, I see it right there. I used the wrong namespace. Let's fix that by going back to CypressController, and let's see. Yeah, that's not right. Use route, and we want the facade. Okay, one more time. Give it a refresh, and there we go. So let's have a look.
Give it a refresh, and there we go. So let's have a look. This'll print it to our console, and there we go. Let's expand that, and we've now made a request to fetch all of our routes keyed according to the name. Have a look. If we go back to my routes file, let's give the welcome page a name called home, and then if we run this again, let's check out the log, where is it? There it is. Here's our home route.
Writing Routes to JSON2:41
There it is. Here's our home route. The action is just a closure, but the URI is the home page. Okay, so now it might be nice if we could write all of these to a file. Here's how we might do that. We'll come on back to our php artisan command, and let's say then, once we have our routes, write it to a file, and maybe we'll put it in the support directory or something like that. Cypress offers a method, what is it called, writeFile, and let's see the syntax here. We need the file path and the contents.
Cypress offers a method, what is it called, writeFile, and let's see the syntax here. We need the file path and the contents. So the file path should be from the document root, which would mean we want cypress/support/routes.json, and the contents would be the routes. Okay, let's try again. We might need to stringify that, but we give it a run, and we're seeing null for log. I'll need to fix that, but if we switch to the editor, and there we go. Here's the routes.json file. Exactly what we want. So now what I'm thinking is, whenever you run the suite, at the very beginning, let's
Running Refresh Before Suite3:40
Exactly what we want. So now what I'm thinking is, whenever you run the suite, at the very beginning, let's run this command a single time to refresh all of the routes. So for example, let's delete that, and then right here we'll comment that out. And in support/index, yeah, we could maybe do it right here. So something like this, cypress.refreshRoutes(). One time when the suite runs. Call this command, that command will hit the endpoint, which returns a named list of all of the routes, where we then grab that and write it to a file. So come on back one more time, and notice before all, writeFile, which means, yep,
Hiding writeFile Logs4:11
of the routes, where we then grab that and write it to a file. So come on back one more time, and notice before all, write file, which means, yep, there it is again. Okay, but now real quick, notice, things like this, do I need to see that every time I run my test suite? Eh, if you want, but I'm not sure it's necessary, so why don't we hide it? We can do that here, by saying refresh routes, let's go in here, and when I write the file, I think, can I just do this? log false. Come on back, refresh, there we go.
Log false. Come on back, refresh, there we go. We don't need to log that particular thing. But now, let's say, in between running your test, you set up a new page for about, that will load the about page, and we give it a name of about. Yeah, the next time you run your suite, that will be reflected. Have a look, we come on back, and about, there it is. Okay, so we're making pretty good progress. In the next episode, I think we still need an easy way to say, when this page loads, cypress.visit the route name called home.
In the next episode, I think we still need an easy way to say, when this page loads, cypress.visit the route name called home. That might be the last piece of the puzzle.
