Example #1
0
def uniq_el_by_position_abs(pos, els):
	if len(els)==1:
		return els[0]
	sorted_by_x = sorted(els, key = lambda e: e.location['x'])
	sorted_by_y = sorted(els, key = lambda e: e.location['y'])
	toppest     = webobject_to_rectangle(sorted_by_y[ 0])
	rightest    = webobject_to_rectangle(sorted_by_x[-1])
	bottest     = webobject_to_rectangle(sorted_by_y[-1])
	leftest     = webobject_to_rectangle(sorted_by_x[ 0])
	dist_horiz  = leftest.distance_to_rect(rightest.align_with_top_edge_of(leftest))
	dist_verti  = toppest.distance_to_rect(bottest.align_with_left_edge_of(toppest))
	
	# Is position_abs ok?
	if (pos=="at_top" or pos=="at_bottom") and dist_horiz>dist_verti:
		fatal_error("The elements found (%d) on page can only be uniquely "\
					"identified with 'at_left' or 'at_right'." % len(els))
	elif (pos=="at_left" or pos=="at_right") and dist_horiz<dist_verti:
		fatal_error("The elements found on page can only be uniquely "\
					"identified with 'at_top' or 'at_bottom'.")
					# fix this with HINT so that we don't have to write all this
					# ("position_abs", {"correct":[at_top, at_bottom]})

	# Return web element
	if pos=="at_top"   : return sorted_by_y[ 0]
	if pos=="at_bottom": return sorted_by_y[-1]
	if pos=="at_left"  : return sorted_by_x[ 0]
	if pos=="at_right" : return sorted_by_x[-1]
Example #2
0
	def type(self, input):

		uniq_el=None

		# Find all typeable fields
		#self.itree.grow_branch(0, ["typeable"], self.last_successful_identifiers)
		#print itree
		descrs=(
				{
					"tag"		: "input",
					"attributes" : [("type", "text")]
				},
				{
					"tag"		: "input",
					"attributes" : [("type", "password")]
				},
				{
					"tag"		: "input",
					"attributes" : [("type", "search")]
				},
		)
		els_list=[]
		els = []
		for desc in descrs:
			els_list.append(self.webdriver.find_elements_by_xpath(ItreeBuilder().xpath_builder.make_xpath(desc)))
		singletons = [els for els in els_list if len(els)==1]
		els = [el[0] for el in singletons]
		els=remove_duplicate_webobjects(els)
		#print els
		
		
		# Choose field closest to last seen
		anchor_el = None
		if self.last_seen_uniq_el:
			anchor_el = webobject_to_rectangle(self.last_seen_uniq_el)

		if len(els)>0:
			uniq_el=els[0]
			for input_field in els:#self.itree.webobjects(0, "typeable"):
				dist_old=anchor_el.distance_to_rect(webobject_to_rectangle(uniq_el))
				dist_new=anchor_el.distance_to_rect(webobject_to_rectangle(input_field))
				if dist_new<dist_old:
					uniq_el=input_field
		#NEED REFACTOR get_unique_webobject FIRST

		#el=self.webpage_analyzer.get_webobject_
		#self.element_on_focus.
		if uniq_el:
			uniq_el.send_keys(input)
			return True
		return False