Mohaletic tests: how to get input value using css selectors
I am writing a test for a Mojolicious Application that sends some data in HTML form and then checks to see if it appears in a subsequent get request.
$t->post_ok('/intern/offer/edit' => form => {
'angebotsdatum' => '2014-02-22',
'gastname' => 'Hansi Hinterseer',
'einmalkosten' => '128.00',
'tage' => '9',
'ankunft' => '2014-01-01',
'gesamtpreis' => '764.00',
'abfahrt' => '2014-01-10',
'tagespreis' => '70.71',
'apartments_id' => 'HAG28',
'id' => '1',
'aufenthaltspreis' => '636.39'
}
)
->status_is(200)
->content_like( qr/Angbot bearbeiten/ )
;
$t->get_ok('/intern/offer/edit/1')
->status_is(200)
->text_like('html body h2' => qr/Intern - Angebote/ )
->text_like('html body h3' => qr/Angbot bearbeiten/ )
->text_is('html body form div#adatum label' => 'Angebotsdatum:' )
->text_is('html body form div#adatum input#angebotsdatum value' => '2014-02-22' )
;
Unfortunately, the last test for the value of the input element fails because the return value is always empty. Label test (here "Angebotsdatum:") is doing well.
How can I select and return input elements from an HTML form?
Form HTML:
<div id="adatum">
<label>Angebotsdatum:</label>
<input type="text" name="angebotsdatum" value="2014-02-22" id="angebotsdatum">
</div>
And this is the test output:
# Failed test 'exact match for selector "html body form div\#adatum input[name=angebotsdatum] value"'
# at t/1210-workflow_booking.t line 80.
# got: ''
# expected: '2014-02-22'
So, we can clearly see that the CSS selector is returning empty .
source to share
From Test :: Mojo
text_is . First checks the text content of the CSS selectors. HTML / XML element mapping for exact match to in in Mojo :: DOM.
Method at
Find the first element in the DOM structure that matches the CSS selector and return it as a Mojo :: DOM object, or return undef if no one can be found. All selectors from "SELECTORS" in Mojo :: DOM :: CSS are supported.
AND
html body form div#adatum input#angebotsdatum value
^^^^^^ - this isn't valid
IMHO not a valid CSS selector.
You can try the following (shorthand) selector:
div#adatum > input[value="2014-02-22"]
find the element <input>
that has an attribute equal to 2014-02-22
.
E [Foo = "bar"] An E element whose foo attribute is equal to bar.
More information Mojo :: DOM :: CSS # SELECTORS
So, it is enough to check the existence of an element with exactly the required value:
->element_exists('input[value="2014-02-22"]', '...');
Ps: I'm not a very experienced Mojo developer, so ...
source to share
input#angebotsdatum
means you are looking for an element <input>
with an attribute id=angebotsdatum
, but what you want to find is an attribute name=angebotsdatum
. So I think the last test should look something like this:
->text_is('html body form div#adatum input[name=angebotsdatum] value' => ...)
source to share