Module: Async::WebDriver::Scope::Fields

Included in:
Element, Async::WebDriver::Session
Defined in:
lib/async/webdriver/scope/fields.rb

Overview

Helpers for working with forms and form fields.

Instance Method Summary collapse

Instance Method Details

#check(field_name, value = true) ⇒ Object

Check a checkbox with the given name.

Does not modify the checkbox if it is already in the desired state.



59
60
61
62
63
64
65
# File 'lib/async/webdriver/scope/fields.rb', line 59

def check(field_name, value = true)
	element = current_scope.find_element(xpath: "//input[@type='checkbox' and @name='#{field_name}']")
	
	if element.checked? != value
		element.click
	end
end

#click_button(label) ⇒ Object

Click a button with the given label.



46
47
48
49
50
# File 'lib/async/webdriver/scope/fields.rb', line 46

def click_button(label)
	element = current_scope.find_element_by_xpath("//button[text()=#{XPath::escape(label)}] | //input[@type='submit' and @value=#{XPath::escape(label)}] | //input[@type='button' and @value=#{XPath::escape(label)}]")
	
	element.click
end

#fill_in(name, value) ⇒ Object

Fill in a field with the given name.

Clears the field before filling it in.



28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/async/webdriver/scope/fields.rb', line 28

def fill_in(name, value)
	element = find_field(name)
	
	if element.tag_name == "input" || element.tag_name == "textarea"
		element.clear
	end
	
	if element.tag_name == "select"
		option = element.find_element_by_xpath(".//option[text()=#{XPath::escape(value)}]")
		option.click unless option.selected?
	else
		element.send_keys(value)
	end
end

#find_field(name) ⇒ Object

Find a field with the given name.



17
18
19
# File 'lib/async/webdriver/scope/fields.rb', line 17

def find_field(name)
	current_scope.find_element_by_xpath("//*[@name=#{XPath::escape(name)}]")
end