clin407's avatar

crawling site as a test to check responses?

I'd like to be able to create a simple test or a type of test to have a user login (I have a helper for that) as a certain role (right now I have an admin and normal user role) then check all possible URLs (probably 5-7 controllers, each with it's own new/edit methods) starting from the root page and just make sure the http response is correct or that it is not giving off any errors.

What's the best way to implement this without having to write a function/test for every one of the pages? Or is there not one?

0 likes
8 replies
davorminchorov's avatar
Level 53

You can probably write a functional test which just surfs around the website.

/** @test */
public function it_lets_the_users_with_a_role_of_member_to_visit_only_specific_pages()
{
    // call your login helper here before visiting the pages.
    $this->visit('/pageone')
        ->see('Some text on the page')
        ->visit('/pagetwo')
        ->see('Some text on the page')      
        ->visit('/pagethree')
        ->see('Some text on the page')
        ->visit('/pagefour')
        ->see('Some text on the page');
}
clin407's avatar

using visit(), does it error out or return false if the visit cannot be completed?

davorminchorov's avatar

I haven't tried if it will fail on instant redirect. I forgot to add see() after each page visit. That will help you with the true false part.

clin407's avatar

got it. that sounds doable, probably end up doing that and save an array of urls at the top of the function.

clin407's avatar

@Ruffles Any ideas on what I can do to randomly pick an url from a list? If I have PostController, I would have something like /posts/ but I'd like to be able to view a post like /posts/12 but I wouldn't be able to know if the ID exists and I would prefer not to hard code it.

davorminchorov's avatar

You can use TestDummy for that (which is included by default with the model factories). $faker->randomElement($arrayOfIds) will return you a random ID.

clin407's avatar

@Ruffles Well, couldn't I use rand() as well for that? I guess I was wondering if I could either query the database for a list of IDs or to just check the current page and see which IDs are showing.

davorminchorov's avatar

Sure, you can do that but it may be different than querying the DB for a list of IDs with lists('id') and just passing that to randomElement(). I usually do it with faker whenever I need a random existing ID from the DB.

Please or to participate in this conversation.