Karate assert - I'm trying to extract a value from HTML - karate

I'm making an API call using karate that gives an HTML response (snippet below). I'm trying to extract the value='HotelTestLondonHotel'. I tried to use the Karate.extract but I couldn't find an example of it anywhere. I know I can use JS for this in some way but I was wondering if there was an easier way?
'''
<input type='hidden' name='security_emerchant_id value='HotelTestLondonHotel'/><input type='hidden' name='XXX_IPGTRXNO_XXX' '''

Here's an example from the Karate unit-tests:
Feature: karate.extract()
Background:
* def text = karate.readAsString('extract.html')
Scenario: extract first regex
* def token = karate.extract(text, 'login_form_token.+value=\\"([^\\"]+)', 1)
* match token == 'secret1'
Scenario: extract all regexes
* def tokens = karate.extractAll(text, 'login_form.?_token.+value=\\"([^\\"]+)', 1)
* match tokens == ['secret1', 'secret2']
And here is the HTML:
<html>
<form name="login_form" method="post" action="/login">
<input type="hidden" id="login_form_token" value="secret1">
</form>
<form name="login_form2" method="post" action="/login">
<input type="hidden" id="login_form2_token" value="secret2">
</form>
</html>

Related

Trying to connect a HTML5 date input with date model

I have an HTML5 date input element like this:
<input type="date" />
if you choose a date in this input a string will be the value, for example:
"2018-12-31"
In my model I want the date to be presented as the following string:
"2018-12-31T00:00:00.000Z" and not the simplified string.
How do I make sure that the date in my model keeps the correct format? I tried using a computed but since I am in a loop this time I cannot use them.
{{eventDate.date}}
<b-form-input
v-bind:value="eventDate.date"
v-on:input="eventDate.date = $event.target.value"
v-bind:id="'event-day-date-' + index"
size="sm"
type="date"
placeholder="2018-12-31"
>
</b-form-input>
As you can see here the eventDate.date is a long string but the input needs the format YYYY-MM-DD. I need to convert it to and from this format some way.
You could use a filter:
filter: {
dateToString(date) {
return date.toString().substr(0,10)
}
}
and then update your template
:value="eventDate.date | dateToString"
This is what I ended up using:
<input
v-validate="'required'"
v-bind:name="'eventdate-' + index"
v-bind:value="eventDate.date | dateToString"
v-on:input="eventDate.date = $event.target.value + 'T00:00:00.000Z'"
v-bind:id="'event-day-date-' + index"
class="form-control form-control-sm"
type="date"
placeholder="2018-12-31"
/>

Issue parsing variable from HTML with bs4

Im trying to parse the "value" of variable ( __VIEWSTATEGENERATOR ), here's the HTML code ::
<div>
<input id="__VIEWSTATEGENERATOR" name="__VIEWSTATEGENERATOR" type="hidden" value="1434571F"/>
</div>
Here's the code I am attempting to do that with ::
viewstategenerator = soup.findAll("input", {"type": "hidden", "name": "__VIEWSTATEGENERATOR"})
I then execute:: print(viewstategenerator), and I get the following string for my variable:
>>> print(viewstategenerator)
[<input id="__VIEWSTATEGENERATOR" name="__VIEWSTATEGENERATOR" type="hidden" value="1434571F"/>]
I was expecting to grab just the value of "1434571F", not sure why that is... Any help would be highly appreciated!!
It looks like you're close but just a tad confused about the BeautifulSoup API.
soup.findAll returns a list of all of the DOM elements that match the query you gave it. Seeing as only one element on the page can match your query, you should use soup.find instead. To get the value of the value attribute of your input element, use ['value'].
from bs4 import BeautifulSoup as Soup
html = """
<div>
<input id="__VIEWSTATEGENERATOR" name="__VIEWSTATEGENERATOR" type="hidden" value="1434571F"/>
</div>
"""
soup = Soup(html, 'lxml') # Use whatever parser you're already using.
viewstategenerator = soup.find("input", {"type": "hidden", "name": "__VIEWSTATEGENERATOR"})
print(viewstategenerator['value'])
# Prints 1434571F

ValueError at /sup/ invalid literal for int() with base 10: ''

i'm new on stackoverflow and in django
I know this has been asked before, but for my circumstance, I can't seem to figure out why this is being thrown
When I try to delete a user, I am given this error from my console:
ValueError: invalid literal for int() with base 10: ''
Here is my view.py where that code is located:
class Delete_user(DeleteView):
model = AuthUser
template_name = 'delUser.html'
def get_success_url(self):
return reverse('ListUserAdmin')
the templete :
<form action="{% url 'Sup-User' pk=AuthUser.id %}" method="POST">
{% csrf_token %}
<input type="submit" value="Yes, delete." />
No, cancel.
</form>
and this is the url :
url(r'^sup/(?P<pk>.*)', log.views.Delete_user.as_view(),
name='Sup-User',),

sql mulitple checkbox OR query statement

How would I use this in a query with multiple selections it would have to be something like
SELECT * FROM venue_event where venue = $check or $check ??
Form
<form action="test.php" method="post">
<input type="checkbox" name="check_list[]" value="value 1">
<input type="submit" />
</form>
<?php
if(!empty($_POST['check_list'])) {
foreach($_POST['check_list'] as $check) {
echo $check;
}
}
?>
I don't know php well enough to post that part, but your SQL query should end up looking like:
SELECT *
FROM venue_event
WHERE venue IN ('venue1','venue2');
Or you can use OR:
SELECT *
FROM venue_event
WHERE venue = 'venue1' OR
venue = 'venue2';

How do I conditionally add an id attribute in TAL (PHPTAL)?

I'm creating a form elements template file in PHPTAL. I would like to be able to OPTIONALLY pass in an id attribute for a field...
So far the code looks like this:
<xml>
<tal:block metal:define-macro="text">
<label tal:condition="php: !isset(hideLabel) || isset(hideLabel) && !hideLabel">${field/label}</label>
<input name="${name}" type="text" value="${field/value}" />
<p tal:condition="exists:field/error">${field/error}</p>
</tal:block>
</xml>
This works as advertised. What I'd like to add is something, like
<input name="${name}" tal:attributes="id exists: id $id | $name" value="${field/value}" />
to allow me to optionally pass in an id from the METAL call...
Should I be doing it differently? I've tried using PHP: isset(id) ? $id : NULL and variations thereof, but just end up with an id="0" in the resultant HTML.
Any ideas?
In case anyone else needs it, one working answer is:
<xml>
<tal:block metal:define-macro="text">
<label tal:condition="not: exists:hideLabel">${field/label}</label>
<input name="${name}" tal:attributes="id id | nothing" type="text" value="${field/value}" />
<p tal:condition="exists:field/error">${field/error}</p>
</tal:block>
</xml>
Where passed in variables are id, name, an array named field, and hideLabel .
Note, that I've also managed to simplify the label test to something which I believe is more idiomatically TAL.
Set VAR at a DIV containing the soon to be used element:
div class="" tal:define="VAR context.property"
div class="" tal:attributes="class python:'grid_8 omega' if VAR else 'grid_8 alpha'"
in PHP:
<div id="contentCenter" tal:attributes="id
php:isset(variable)&&isset(variable.property)?'IDVALUE':NULL">