Test only a fragment

classic Classic list List threaded Threaded
8 messages Options
Reply | Threaded
Open this post in threaded view
|

Test only a fragment

Michielb
Hi,

I'm testing my Thymeleaf pages, but since I don't want to test the whole file (layout), I try to test only a fragment. Is this possible?


My .thtest file:

%TEMPLATE_MODE HTML5
# ------------ separator comment -----------
%CONTEXT
success = false
errmessage = {"Message 1", "Message 2"}
# ------------------------------------------

# I try to read only the content fragment
%INPUT (file:src/main/webapp/WEB-INF/spring/views/thymeleafpage.html :: content)

# ------------------------------------------
%OUTPUT
<tr>
	<td>
		<ul>
			<li>Message 1</li>
		</ul>
		<ul>
			<li>Message 2</li>
		</ul>
	</td>
</tr>
Reply | Threaded
Open this post in threaded view
|

Re: Test only a fragment

Emanuel
Administrator
According to the readme for the %INPUT parameter, it should be possible:

Test input, in the form of an HTML template or fragment. A resource name can also be specified between parenthesis, like %INPUT (file:/home/user/myproject/src/main/resources/templates/mytemplate.html). This parameter is required.
Although I'm not sure what the output to match on would be.  I expect it would just be the fragment execution as well.
Reply | Threaded
Open this post in threaded view
|

Re: Test only a fragment

Michielb
Unfortunatly the documentation doesn't cover how to implement the fragment

# This works, but gives me the whole file.
%INPUT (file:src/main/webapp/WEB-INF/spring/views/thymeleafpage.html)

# This doesn't work
%INPUT (file:src/main/webapp/WEB-INF/spring/views/thymeleafpage.html :: content)
Reply | Threaded
Open this post in threaded view
|

Re: Test only a fragment

Michielb
In reply to this post by Michielb
I finally had some progress:

%INPUT 
<span th:include="page :: content" th:remove="tag"></span>

%INPUT[page] (file:src/main/webapp/WEB-INF/EmptyThymeleafPage.html)

%OUTPUT
...

EmptyThymeleafPage.html:

<div th:fragment="content">...</div>


Could I do somthing similair with the %FRAGMENT?
Reply | Threaded
Open this post in threaded view
|

Re: Test only a fragment

danielfernandez
Administrator
You can specify both %INPUT for your input file, and then %FRAGMENT for a DOM selector specifying the specific fragment of your input you want to test.

See https://github.com/thymeleaf/thymeleaf-testing#test-file-format for the complete specification of directives and https://github.com/thymeleaf/thymeleaf-tests/blob/26f41bceffca7d8cf2ff69b65ae4a612a71ed9f4/src/test-21/resources/engine21/domselector/domselector42.thtest for an example (note the %INPUT for the example is defined at the extended test file).
Reply | Threaded
Open this post in threaded view
|

Re: Test only a fragment

Michielb
Ow nice! A DOM selector sounds really need! Is the TestFragmentTestFieldEvaluator somewere in the Thymeleaf testing dependency? I couldn't find it and %FRAGMENT[evaluator] org.thymeleaf.engine21.domselector.TestFragmentTestFieldEvaluator throws a ClassNotFoundException.
Reply | Threaded
Open this post in threaded view
|

Re: Test only a fragment

danielfernandez
Administrator
hmmm... you shouldn't need anything like that to use a DOM selector for a %FRAGMENT directive. Something like:

%FRAGMENT div#content

...should suffice.

Or are you trying to specify your own implementation of the fragment evaluator for some reason?
Reply | Threaded
Open this post in threaded view
|

Re: Test only a fragment

Michielb
Ah, div#content works, but div%content doesn't work. Thanks! This is exactly what I was looking for.