I am trying to make .formParam- independent scenario with random values. So far I faced troubles trying to dynamically extract attributes from a request.
So firstly I need to check if the product is having any attributes (needed to be filled). Extract them by
regex("""\b(product_attribute_\d)(?![\s\S]*?\b\1)""").findAll.optional.saveAs("prod_attributes")
prod_attributes -> List(product_attribute_1, product_attribute_2, product_attribute_3, product_attribute_4, product_attribute_5)
Then, if the attributes isn't empty (checkIf(!"prod_attributes".isEmpty)), I try to extract values for every attribute in Seq("prod_attributes"):
(for (i <- 0 to "${prod_attributes}".size - 1) {
css("dd[id='product_attribute_input_" + i + 1 + "'][value]").findAll.saveAs("attribute_input_" + i + 1))
}
Now I want to create new Map and put as key = product_attribute_n, and value List of values for relevant attribute. For example:
val atrib_map= Map(
product_attribute_1-> List(1, 2),
product_attribute_2-> List(3, 4, 5),
...... etc
)
Or just extract random value for every attribute. And then use this list for .formParamSeq.
Please share your ideas about my approach, and help me find out if there any possibility to make it through. (Or I just dig in the wrong direction and it should be performed by simple "hardcoded" transactions)
This part doesn't work, but I'd like to use something like this.
val goToProduct = http("""Go_to_product""")
.get("/product")
.headers(headers_0)
.check(
regex("""\b(product_attribute_\d)(?![\s\S]*\b\1)""").findAll.optional.saveAs("prod_attributes"),
checkIf(!"prod_attributes".isEmpty){
for (i <- 0 to "${prod_attributes}".size - 1) {
css("dd[id='product_attribute_input_" + i + 1 + "'] [value]").findAll.saveAs("attribute_input_" + i + 1)
}
})
I have this part of body (this is item with most attributes to be filled).
Main idea to have not hardcoded values, that can be parsed from any product ( with 2,3,4, any attributes).
<!--attributes-->
<div class="attributes">
<dl>
<dt id="product_attribute_label_1">
<label class="text-prompt">
Processor
</label>
<span class="required">*</span>
</dt>
<dd id="product_attribute_input_1">
<select name="product_attribute_1" id="product_attribute_1">
<option value="1">2.2 GHz Intel Pentium Dual-Core E2200</option>
<option selected="selected" value="2">2.5 GHz Intel Pentium Dual-Core E2200 [+$15.00]</option>
</select>
</dd>
<dt id="product_attribute_label_2">
<label class="text-prompt">
RAM
</label>
<span class="required">*</span>
</dt>
<dd id="product_attribute_input_2">
<select name="product_attribute_2" id="product_attribute_2">
<option value="3">2 GB</option>
<option value="4">4GB [+$20.00]</option>
<option value="5">8GB [+$60.00]</option>
</select>
</dd>
<dt id="product_attribute_label_3">
<label class="text-prompt">
HDD
</label>
<span class="required">*</span>
</dt>
<dd id="product_attribute_input_3">
<ul class="option-list">
<li>
<input id="product_attribute_3_6" type="radio" name="product_attribute_3" value="6">
<label for="product_attribute_3_6">320 GB</label>
</li>
<li>
<input id="product_attribute_3_7" type="radio" name="product_attribute_3" value="7">
<label for="product_attribute_3_7">400 GB [+$100.00]</label>
</li>
</ul>
</dd>
<dt id="product_attribute_label_4">
<label class="text-prompt">
OS
</label>
<span class="required">*</span>
</dt>
<dd id="product_attribute_input_4">
<ul class="option-list">
<li>
<input id="product_attribute_4_8" type="radio" name="product_attribute_4" value="8" checked="checked">
<label for="product_attribute_4_8">Vista Home [+$50.00]</label>
</li>
<li>
<input id="product_attribute_4_9" type="radio" name="product_attribute_4" value="9">
<label for="product_attribute_4_9">Vista Premium [+$60.00]</label>
</li>
</ul>
</dd>
<dt id="product_attribute_label_5">
<label class="text-prompt">
Software
</label>
</dt>
<dd id="product_attribute_input_5">
<ul class="option-list">
<li>
<input id="product_attribute_5_10" type="checkbox" name="product_attribute_5" value="10" checked="checked">
<label for="product_attribute_5_10">Microsoft Office [+$50.00]</label>
</li>
<li>
<input id="product_attribute_5_11" type="checkbox" name="product_attribute_5" value="11">
<label for="product_attribute_5_11">Acrobat Reader [+$10.00]</label>
</li>
<li>
<input id="product_attribute_5_12" type="checkbox" name="product_attribute_5" value="12">
<label for="product_attribute_5_12">Total Commander [+$5.00]</label>
</li>
</ul>
</dd>
</dl>
</div>
It does not seem that you can dynamically add checks.
My solution gets the nodes and transform
s them. Note the ^=
which selects all the nodes starting with "product_attribute_input_".
.check(
css("""dd[id^="product_attribute_input_"]""").ofType[jodd.lagarto.dom.Node].findAll
.transform{ nodes =>
nodes.map { node =>
val name = node.getAttribute("id")
val valuesInSelect = Option(node.getFirstChildElement("select")).map {
_.getChildElements().flatMap { child =>
Option(child.getAttribute("value"))
}
.toList
}
val valuesInList = Option(node.getFirstChildElement("ul")).map {
_.getChildElements()
.filter { _.getNodeName() == "li" }
.flatMap { child =>
Option(child.getFirstChildElement("input")).flatMap { i =>
Option(i.getAttribute("value"))
}
}
.toList
}
name -> valuesInSelect.orElse(valuesInList).getOrElse(List())
}.toMap
}
.saveAs("atrib_map")
)
When you print the session, you can see the attribute to be atrib_map -> Map(product_attribute_input_3 -> List(6, 7), product_attribute_input_5 -> List(10, 11, 12), product_attribute_input_2 -> List(3, 4, 5), product_attribute_input_4 -> List(8, 9), product_attribute_input_1 -> List(1, 2))
This extraction of data from an XML node is not really efficient or elegant, but it feels declarative.