def create_form_helper(form_data): ''' Creates a dc.Form object from a dict container :param form_data: A list containing dicts representing a form's internal structure :return: A dc.Form object from `form_data` ''' new_form = form.Form() for elem_data in form_data: elem_type = elem_data['tagname'] attrs = elem_data.items() if elem_type == 'input': _type = elem_data['type'] if _type == 'radio': new_form.add_radio(attrs) elif _type == 'checkbox': new_form.add_check_box(attrs) elif _type in ('text', 'hidden'): new_form.add_input(attrs) elif elem_type == 'select': new_form.add_select(elem_data['name'], elem_data['options']) return new_form
def test_all_variants(self): # 'all' mode variants bigform_data = form_with_radio + form_select_misc clean_data = get_gruped_data(bigform_data) new_bigform = create_form_helper(bigform_data) total_variants = 2 * 5 * 10 variants_set = set() for i, form_variant in enumerate(new_bigform.get_variants(mode="all")): if i == 0: # First element must be the created `new_bigform` self.assertEquals(id(new_bigform), id(form_variant)) continue for name, all_values in clean_data.items(): self.assertTrue(form_variant[name][0] in all_values) variants_set.add(repr(form_variant)) # Ensure we actually got the expected number of variants f = form.Form() expected = min(total_variants, f.TOP_VARIANTS) self.assertEquals(expected, i) # Variants shouldn't duplicated self.assertEquals(expected, len(variants_set))
def test_tmb_variants(self): # 'top-middle-bottom' mode variants def filter_tmb(values): if len(values) > 3: values = (values[0], values[len(values) / 2], values[-1]) return values bigform_data = form_with_radio + form_select_misc clean_data = get_gruped_data(bigform_data) new_bigform = create_form_helper(bigform_data) total_variants = 2 * 2 * 3 variants_set = set() for i, form_variant in enumerate(new_bigform.get_variants(mode="tmb")): if i == 0: # First element must be the created `new_bigform` self.assertEquals(id(new_bigform), id(form_variant)) continue for name, values in clean_data.items(): tmb_values = filter_tmb(values) msg = 'Failed to find "%s" in "%s"' % (form_variant[name][0], tmb_values) self.assertTrue(form_variant[name][0] in tmb_values, msg) variants_set.add(repr(form_variant)) # Ensure we actually got the expected number of variants f = form.Form() expected = min(total_variants, f.TOP_VARIANTS) self.assertEquals(i, expected) # Variants shouldn't appear duplicated self.assertEquals(len(variants_set), expected)
def test_tmb_variants_large(self): ''' Note that this test has several changes from test_tmb_variants: * It uses form_select_misc_large, which exceeds the form's TOP_VARIANTS = 15 * Doesn't use filter_tmb since variants are based on a "random pick" ''' bigform_data = form_with_radio + form_select_cars + \ form_select_misc_large clean_data = get_gruped_data(bigform_data) new_bigform = create_form_helper(bigform_data) total_variants = 2 * 3 * 3 * 3 variants_set = set() # Please note that this depends completely in form.SEED AND form.TOP_VARIANTS RANDOM_PICKS = { 1: ('volvo', 'black', 'd', 'female'), 2: ('volvo', 'blue', 'i', 'male'), 3: ('volvo', 'blue', 'f', 'female'), 4: ('volvo', 'black', 'g', 'female'), 5: ('volvo', 'black', 'm', 'male'), 6: ('volvo', 'black', 'l', 'male'), 7: ('volvo', 'blue', 'b', 'female'), 8: ('volvo', 'blue', 'e', 'female'), 9: ('volvo', 'black', 'c', 'male'), 10: ('volvo', 'black', 'a', 'female'), 11: ('volvo', 'blue', 'e', 'male'), 12: ('volvo', 'black', 'j', 'male'), 13: ('volvo', 'blue', 'c', 'male'), 14: ('volvo', 'black', 'a', 'male'), 15: ('volvo', 'black', 'i', 'female') } for i, form_variant in enumerate(new_bigform.get_variants(mode="tmb")): if i == 0: # First element must be the created `new_bigform` self.assertEquals(id(new_bigform), id(form_variant)) continue for name, values in clean_data.items(): current_random_values = RANDOM_PICKS[i] msg = 'Failed to find "%s" in "%s"' % (form_variant[name][0], current_random_values) self.assertTrue(form_variant[name][0] in current_random_values, msg) variants_set.add(repr(form_variant)) # Ensure we actually got the expected number of variants f = form.Form() expected = min(total_variants, f.TOP_VARIANTS) self.assertEquals(i, expected) # Variants shouldn't appear duplicated self.assertEquals(len(variants_set), expected)
def _handle_form_tag_start(self, tag, attrs): ''' Handle the form tags. This method also looks if there are "pending inputs" in the self._saved_inputs list and parses them. ''' SGMLParser._handle_form_tag_start(self, tag, attrs) # Get the 'method' method = attrs.get('method', 'GET').upper() # Get the action action = attrs.get('action', None) missing_or_invalid_action = action is None if not missing_or_invalid_action: action = self._decode_url(action) try: action = self._base_url.url_join(action, encoding=self._encoding) except ValueError: missing_or_invalid_action = True if missing_or_invalid_action: msg = ( 'HTMLParser found a form without an action attribute. ' 'Javascript may be used... but another option (mozilla does ' 'this) is that the form is expected to be posted back to the' ' same URL (the one that returned the HTML that we are parsing).' ) om.out.debug(msg) action = self._source_url # Create the form object and store everything for later use form_obj = form.Form(encoding=self._encoding) form_obj.set_method(method) form_obj.set_action(action) self._forms.append(form_obj) # Now I verify if there are any input tags that were found # outside the scope of a form tag for inputattrs in self._saved_inputs: # Parse them just like if they were found AFTER the # form tag opening if isinstance(inputattrs, dict): self._handle_input_tag_inside_form('input', inputattrs) # All parsed, remove them. self._saved_inputs = []
def _handle_go_tag_start(self, tag, attrs): # Find method method = attrs.get('method', 'GET').upper() # Find action action = attrs.get('href', '') if action: self._inside_form = True action = unicode(self._base_url.url_join(action)) action = URL(self._decode_url(action), encoding=self._encoding) # Create the form f = form.Form(encoding=self._encoding) f.set_method(method) f.set_action(action) self._forms.append(f) else: om.out.debug('WMLParser found a form without an action. ' 'Javascript is being used.')