Module: WebConditions

Defined in:
lib/web-object/conditions/url.rb,
lib/web-object/conditions/text.rb,
lib/web-object/conditions/alert.rb,
lib/web-object/conditions/title.rb,
lib/web-object/conditions/waiting.rb,
lib/web-object/conditions/elements_count.rb,
lib/web-object/conditions/element_property.rb,
lib/web-object/conditions/element_interaction.rb

Instance Method Summary collapse

Instance Method Details

#alert_present?Boolean true, Boolean false

Returns:

  • (Boolean true)

    – if alert is present on page

  • (Boolean false)

    – if alert is absent from page



7
8
9
10
11
12
# File 'lib/web-object/conditions/alert.rb', line 7

def alert_present?
  @driver.switch_to.alert
  true
rescue
  false
end

#attribute_is_absent(ele, attr) ⇒ Boolean true, Boolean false Also known as: attribute_is_empty

Parameters:

  • ele (WebElement object)
  • ele (locator Hash)

    – eg => ‘some_id’]

  • attr (String)

    – attribute that need to be checked for absence

Returns:

  • (Boolean true)

    – if element attribute is not found in the html tag in page source

  • (Boolean false)

    – if element attribute is found in the html tag in page source



29
30
31
32
33
34
35
36
# File 'lib/web-object/conditions/element_property.rb', line 29

def attribute_is_absent(ele,attr)
  element = element_or_locator(ele)
  if element.attribute(attr).empty?
    true
  else
    false
  end
end

#attribute_is_present(ele, attr) ⇒ Boolean true, Boolean false Also known as: attribute_is_not_empty

Parameters:

  • ele (WebElement object)
  • ele (locator Hash)

    – eg => ‘some_id’]

  • attr (String)

    – attribute that need to be checked for presence

Returns:

  • (Boolean true)

    – if element attribute is found in the html tag in page source

  • (Boolean false)

    – if element attribute is not found in the html tag in page source



11
12
13
14
15
16
17
18
# File 'lib/web-object/conditions/element_property.rb', line 11

def attribute_is_present(ele,attr)
  element = element_or_locator(ele)
  if element.attribute(attr).empty?
    false
  else
    true
  end
end

#attribute_to_include(ele, attr, item) ⇒ Boolean true, Boolean false Also known as: attribute_to_contain

Parameters:

  • ele (WebElement object)
  • ele (locator Hash)

    – eg => ‘some_id’]

  • attr (String)

    – attribute that need to be checked for actual value in page source

  • item (String)

    – substring value of attribute that needs to be contained within actual in page source

Returns:

  • (Boolean true)

    – if element attribute substring is present within actual value in the html tag in page source

  • (Boolean false)

    – if actual attribute value does not contain passed substring



66
67
68
69
70
71
72
73
# File 'lib/web-object/conditions/element_property.rb', line 66

def attribute_to_include(ele,attr,item)
  element = element_or_locator(ele)
  if element.attribute(attr).to_s.include?(item.to_s)
    true
  else
    false
  end
end

#attribute_to_match(ele, attr) ⇒ Boolean true, Boolean false

Parameters:

  • ele (WebElement object)
  • ele (locator Hash)

    – eg => ‘some_id’]

  • attr (String)

    – attribute that need to be checked for actual value in page source

  • item (String)

    – substring value of attribute that needs to be contained within actual in page source

Returns:

  • (Boolean true)

    – if element attribute substring is present within actual value in the html tag in page source

  • (Boolean false)

    – if actual attribute value does not contain passed substring



48
49
50
51
52
53
54
55
# File 'lib/web-object/conditions/element_property.rb', line 48

def attribute_to_match(ele,attr,item)
  element = element_or_locator(ele)
  if element.attribute(attr).to_s == (item.to_s)
    true
  else
    false
  end
end

#element_is_absent(ele) ⇒ Boolean true, Boolean false

Parameters:

  • locator (locator Hash)

    – eg => ‘some_id’]

Returns:

  • (Boolean true)

    – if element is absent in DOM of page

  • (Boolean false)

    – if element is present from DOM of page



102
103
104
105
106
107
108
109
# File 'lib/web-object/conditions/element_interaction.rb', line 102

def element_is_absent(locator)
  begin
    @driver.find_element(locator)
    false
  rescue
    true
  end
end

#element_is_clickable(ele) ⇒ Boolean true, Boolean false

Parameters:

  • ele (WebElement object)
  • ele (locator Hash)

    – eg => ‘some_id’]

Returns:

  • (Boolean true)

    – if element is clickable(visible and enabled) on page

  • (Boolean false)

    – if element is not clickable(visible and enabled) on page



9
10
11
12
13
14
15
16
# File 'lib/web-object/conditions/element_interaction.rb', line 9

def element_is_clickable(ele)
  element = element_or_locator(ele)
  if element_is_visible(element) and element_is_enabled(element)
    true
  else
    false
  end
end

#element_is_disabled(ele) ⇒ Boolean true, Boolean false

Parameters:

  • ele (WebElement object)
  • ele (locator Hash)

    – eg => ‘some_id’]

Returns:

  • (Boolean true)

    – if element is disabled on page

  • (Boolean false)

    – if element is enabled on page



41
42
43
44
45
46
47
48
# File 'lib/web-object/conditions/element_interaction.rb', line 41

def element_is_disabled(ele)
  element = element_or_locator(ele)
  if element.enabled?
    false
  else
    true
  end
end

#element_is_enabled(ele) ⇒ Boolean true, Boolean false

Parameters:

  • ele (WebElement object)
  • ele (locator Hash)

    – eg => ‘some_id’]

Returns:

  • (Boolean true)

    – if element is enabled on page

  • (Boolean false)

    – if element is disabled on page



25
26
27
28
29
30
31
32
# File 'lib/web-object/conditions/element_interaction.rb', line 25

def element_is_enabled(ele)
  element = element_or_locator(ele)
  if element.enabled?
    true
  else
    false
  end
end

#element_is_invisible(ele) ⇒ Boolean true, Boolean false

Parameters:

  • ele (WebElement object)
  • ele (locator Hash)

    – eg => ‘some_id’]

Returns:

  • (Boolean true)

    – if element is invisible / hidden on page

  • (Boolean false)

    – if element is visible on page



73
74
75
76
77
78
79
80
# File 'lib/web-object/conditions/element_interaction.rb', line 73

def element_is_invisible(ele)
  element = element_or_locator(ele)
  if element.displayed?
    false
  else
    true
  end
end

#element_is_present(ele) ⇒ Boolean true, Boolean false

Parameters:

  • locator (locator Hash)

    – eg => ‘some_id’]

Returns:

  • (Boolean true)

    – if element is present in DOM of page

  • (Boolean false)

    – if element is absent from DOM of page



88
89
90
91
92
93
94
95
# File 'lib/web-object/conditions/element_interaction.rb', line 88

def element_is_present(locator)
  begin
    @driver.find_element(locator)
    true
  rescue
    false
  end
end

#element_is_visible(ele) ⇒ Boolean true, Boolean false

Parameters:

  • ele (WebElement object)
  • ele (locator Hash)

    – eg => ‘some_id’]

Returns:

  • (Boolean true)

    – if element is visible on page

  • (Boolean false)

    – if element is invisible / hidden on page



57
58
59
60
61
62
63
64
# File 'lib/web-object/conditions/element_interaction.rb', line 57

def element_is_visible(ele)
  element = element_or_locator(ele)
  if element.displayed?
    true
  else
    false
  end
end

#element_or_locator(ele) ⇒ Object

internal method



112
113
114
115
116
117
118
119
120
121
122
# File 'lib/web-object/conditions/element_interaction.rb', line 112

def element_or_locator(ele)
  if ele.class == Hash
    begin
      @driver.find_element(ele)
    rescue
      raise 'Looks like the element itself is not present on the page, Please wait for element to be present'
    end
  else
    ele
  end
end

#elements_count_to_be_equal_to(ele, count) ⇒ Boolean true, Boolean false

Parameters:

  • ele (Array of WebElement object)
  • ele (locator Hash)

    – eg => ‘some_id’]

  • count (Fixnum)

    – expected count of elements present on page

Returns:

  • (Boolean true)

    – if actual count found on page is less than expected count passed in param

  • (Boolean false)

    – if actual count found on page is greater than expected count passed in param



43
44
45
46
47
48
49
50
# File 'lib/web-object/conditions/elements_count.rb', line 43

def elements_count_to_be_equal_to(loc,count)
  elements = @driver.find_elements(loc)
  if elements.size == count.to_i
    true
  else
    false
  end
end

#elements_count_to_be_less_than(ele, count) ⇒ Boolean true, Boolean false

Parameters:

  • ele (Array of WebElement object)
  • ele (locator Hash)

    – eg => ‘some_id’]

  • count (Fixnum)

    – expected count of elements present on page

Returns:

  • (Boolean true)

    – if actual count found on page is less than expected count passed in param

  • (Boolean false)

    – if actual count found on page is greater than expected count passed in param



27
28
29
30
31
32
33
34
# File 'lib/web-object/conditions/elements_count.rb', line 27

def elements_count_to_be_less_than(loc,count)
  elements = @driver.find_elements(loc)
  if elements.size < count.to_i
    true
  else
    false
  end
end

#elements_count_to_be_more_than(ele, count) ⇒ Boolean true, Boolean false

Parameters:

  • ele (Array of WebElement object)
  • ele (locator Hash)

    – eg => ‘some_id’]

  • count (Fixnum)

    – expected count of elements present on page

Returns:

  • (Boolean true)

    – if actual count found on page is greater than expected count passed in param

  • (Boolean false)

    – if actual count found on page is less than expected count passed in param



10
11
12
13
14
15
16
17
# File 'lib/web-object/conditions/elements_count.rb', line 10

def elements_count_to_be_more_than(loc,count)
  elements = @driver.find_elements(loc)
  if elements.size > count.to_i
    true
  else
    false
  end
end

#text_in_element_to_include(ele, txt) ⇒ Boolean true, Boolean false Also known as: text_in_element_to_contain

Parameters:

  • ele (WebElement object)
  • ele (locator Hash)

    – eg => ‘some_id’]

  • txt (String)

    – text that will be checked in the body of the page

Returns:

  • (Boolean true)

    – if text is found inside element and is exact match of the passed text in parameter

  • (Boolean false)

    – if text is found inside element and is not an exact match of the passed text in parameter



40
41
42
43
44
45
46
# File 'lib/web-object/conditions/text.rb', line 40

def text_in_element_to_include(ele, txt)
  if element_or_locator(ele).text.include?(txt)
    true
  else
    false
  end
end

#text_in_element_to_match(ele, attr) ⇒ Boolean true, Boolean false

Parameters:

  • ele (WebElement object)
  • ele (locator Hash)

    – eg => ‘some_id’]

  • txt (String)

    – text that will be checked in the body of the page

Returns:

  • (Boolean true)

    – if text is found inside element and is exact match of the passed text in parameter

  • (Boolean false)

    – if text is found inside element and is not an exact match of the passed text in parameter



24
25
26
27
28
29
30
# File 'lib/web-object/conditions/text.rb', line 24

def text_in_element_to_match(ele, txt)
  if element_or_locator(ele).text == txt
    true
  else
    false
  end
end

#text_to_be_on_page(txt) ⇒ Boolean true, Boolean false

Parameters:

  • txt (String)

Returns:

  • (Boolean true)

    – if text is found in the body of page

  • (Boolean false)

    – if text is not found in the body of page



8
9
10
11
12
13
14
15
# File 'lib/web-object/conditions/text.rb', line 8

def text_to_be_on_page(txt)
  # puts element_or_locator({:tag_name => 'body'}).text
  if element_or_locator({:tag_name => 'body'}).text.include?(txt)
    true
  else
    false
  end
end

#title_to_includeBoolean true, Boolean false Also known as: title_to_contain

Parameters:

  • -- (title)

    expected title to match the actual title of the web page

Returns:

  • (Boolean true)

    – if expected title is included in the actual title on the web page

  • (Boolean false)

    – if expected title is not included in the actual title on the web page



22
23
24
25
26
27
28
29
# File 'lib/web-object/conditions/title.rb', line 22

def title_to_include(title)
  actual = @driver.title
  if actual.include?(title)
    true
  else
    false
  end
end

#title_to_matchBoolean true, Boolean false

Parameters:

  • -- (title)

    expected title to match the actual title of the web page

Returns:

  • (Boolean true)

    – if expected title matches the actual title on the web page

  • (Boolean false)

    – if expected title does not match the actual title on the web page



8
9
10
11
12
13
14
15
# File 'lib/web-object/conditions/title.rb', line 8

def title_to_match(title)
  actual = @driver.title
  if actual == title
    true
  else
    false
  end
end

#url_to_includeBoolean true, Boolean false Also known as: url_to_contain

Parameters:

  • -- (url)

    part of the expected url to be present in actual url in the browser

Returns:

  • (Boolean true)

    – if expected url in parameter in present in actual url

  • (Boolean false)

    – if expected url in parameter in not present in actual url



22
23
24
25
26
27
28
29
# File 'lib/web-object/conditions/url.rb', line 22

def url_to_include(url)
  actual = @driver.current_url
  if actual.include?(url)
    true
  else
    false
  end
end

#url_to_matchBoolean true, Boolean false

Parameters:

  • -- (url)

    expected url to match the actual url on the browser

Returns:

  • (Boolean true)

    – if expected url matches the actual url on the browser

  • (Boolean false)

    – if expected url does not match the actual url on the browser



8
9
10
11
12
13
14
15
# File 'lib/web-object/conditions/url.rb', line 8

def url_to_match(url)
  actual = @driver.current_url
  if actual == url
    true
  else
    false
  end
end

#waiting(time_in_secs) ⇒ object of WebDriver::Wait class

Parameters:

  • time_in_secs (Fixnum)

    – Maximum amount of time a condition needs to wait

Returns:

  • (object of WebDriver::Wait class)


7
8
9
# File 'lib/web-object/conditions/waiting.rb', line 7

def waiting(time_in_secs)
  Selenium::WebDriver::Wait.new(:timeout => time_in_secs.to_i)
end