Summer Sale! All accounts are 50% off this week.

okorpheus's avatar

checking for html tags with assertSeeInOrder

In pest tests in a laravel project, I'd like to do something like

$response->assertSeeInOrder(['<option', 'value=', $var->id, $var->name, ''])

However, it will fail if the <> and / are included. Adding the second parameter to AssertSeeInOrder makes no difference. Escaping the characters seems to make no difference. Is there something I'm missing?

0 likes
6 replies
LaryAI's avatar
Level 58

To check for HTML tags in a specific order using assertSeeInOrder in your Pest tests for a Laravel project, you need to ensure that the HTML tags are treated as plain text. The assertSeeInOrder method checks for the presence of strings in the given order, but it does not parse HTML. Therefore, you need to provide the exact strings as they appear in the response.

Here's how you can achieve this:

  1. Ensure that the HTML tags and their attributes are included as plain text in the array.
  2. Use the assertSeeInOrder method to check for the presence of these strings in the specified order.

Here's an example of how you can do this:

$response->assertSeeInOrder([
    '<option', 
    'value="' . $var->id . '"', 
    $var->name, 
    '</option>'
]);

In this example:

  • '<option' checks for the opening tag of the option.
  • 'value="' . $var->id . '"' checks for the value attribute with the dynamic ID.
  • $var->name checks for the name inside the option tag.
  • '</option>' checks for the closing option tag.

Make sure that the strings you are checking for match exactly with the HTML output in the response.

If you are still facing issues, you might want to inspect the actual HTML output in the response to ensure that the strings you are checking for are correct. You can do this by dumping the response content:

dd($response->getContent());

This will help you verify the exact structure of the HTML and adjust your assertSeeInOrder accordingly.

By following these steps, you should be able to check for the presence of HTML tags and their attributes in the specified order using assertSeeInOrder.

okorpheus's avatar

I have tried Larry's response with no luck.

okorpheus's avatar

@martinbean thanks. I ran across the package sinnbeck/laravel-dom-assertions and have mades some progress.

I'm fairly new to writing tests and just need to ensure that only the appropriate options are included. Would it be more typical to just test the content of the array fed to the view by the controller for this type of test?

Please or to participate in this conversation.