<commit_before># Created on 20 Sept 2017
# @author: <NAME>
#
# Versions:
# 1.0 - Baseline
#
# web_functions.rb - all web functions for the TAF.
module WebFuncs
  require './taf_config.rb'
   # Ping test Function.
  def self.ping_test(url)
    while pingtest == Net::Ping::HTTP.new(url)
      pingtest.ping?
      sleep 5
        if (pingtest.ping? == true)
          # website alive
          $results_file.write("pinged #{url}")
          $PDF.text("pinged #{url}")
          $results_file.puts ''
          return true
        else
          # website not responding
          $results_file.write("Failed to ping #{url}")
          $PDF.text("Failed to ping #{url}")
          $results_file.puts ''
          return false
        end
    end
  end # pingTest

  # Open URL function. - the browser object is created via the main program
  def self.open_url(url)
    Utils.open_browser()
    $browser.goto(url)
    sleep 2
    url_name = $browser.url
      if (url_name == url)
        $results_file.write("opened URL: #{url}")
        $PDF.text("opened URL: #{url}")
        $results_file.puts ''
        return true
      else
        $results_file.write("URL not open: #{url} - opened #{url_name} instead")
        $PDF.text("URL not open: #{url} - opened #{url_name} instead")
        $results_file.puts ''
        return false
      end
  end # openUrl

  # Check URL function.
  def self.check_url(url)
    if ($browser.url == url)
      $results_file.write("URL: #{url} is correct")
      $PDF.text("URL: #{url} is correct")
      $results_file.puts ''
      return true
    else
      $results_file.write("URL: #{url} is incorrect")
      $PDF.text("URL: #{url} is incorrect")
      $results_file.puts ''
      return false
    end
  end # CheckURL

  # Click button function.
  def self.click_button(button, locate)
    $browser.button(:"#{locate}" => button).wait_until_present.click
    $results_file.write("Button: #{button} has been selected")
    $PDF.text("Button: #{button} has been selected")
    $results_file.puts ''
    return true
  rescue
    $results_file.write("Button: #{button} does not exist")
    $PDF.text("Button: #{button} does not exist")
    $results_file.puts ''
    return false
  end # clickButton

  # Select Link Function.
  def self.select_link(link, locate)
    $browser.link(:"#{locate}" => link).exist?
    $browser.link(:"#{locate}" => link).wait_until_present.click
    $results_file.write("Link: #{link} has been selected")
    $PDF.text("Link: #{link} has been selected")
    $results_file.puts ''
    return true
  rescue
    $results_file.write("Link: #{link} does not exist")
    $PDF.text("Link: #{link} does not exist")
    $results_file.puts ''
    return false
  end # select link.

  # Select Image Function.
  def self.select_image(image, locate)
    $browser.image(:"#{locate}" => image).exist?
    $browser.image(:"#{locate}" => image).wait_until_present.click
    $results_file.write("Image: #{image} has been selected")
    $PDF.text("Image: #{image} has been selected")
    $results_file.puts ''
    return true
  rescue
    $results_file.write("Image: #{image} does not exist")
    $PDF.text("Image: #{image} does not exist")
    $results_file.puts ''
    return false
  end # select image.

  # Select Item Function.
  def self.select_item(item, locate)
    $browser.a(:"#{locate}" => item).exist?
    $browser.a(:"#{locate}" => item).wait_until_present.click
    $results_file.write("Item: #{item} has been selected")
    $PDF.text("Item: #{item} has been selected")
    $results_file.puts ''
    return true
  rescue
    $results_file.write("Item: #{item} does not exist")
    $PDF.text("Item: #{item} does not exist")
    $results_file.puts ''
   return false
  end # select item.

  # click on a "h<font size>" tag.
  def self.click_h_tag(item, locate)
    found_tags = [
      $browser.h1(:"#{locate}" => item).exist?,
      $browser.h2(:"#{locate}" => item).exist?,
      $browser.h3(:"#{locate}" => item).exist?,
      $browser.h4(:"#{locate}" => item).exist?,
      $browser.h5(:"#{locate}" => item).exist?,
      $browser.h6(:"#{locate}" => item).exist?
    ]

    raise "Multiple matches" if found_tags.select { |i| i }.size > 1
    index = found_tags.index(true)
    return unless index
    $browser.send("h#{index + 1}", :"#{locate}" => item).click
    $results_file.write("Item: #{item} has been selected")
    $PDF.text("Item: #{item} has been selected")
    $results_file.puts ''
    return true
    else
    $results_file.write("Item: #{item} does not exist")
    $PDF.text("Item: #{item} does not exist")
    $results_file.puts ''
    return false
    #end
  end # select item.

  # Capture alerts status Function.
  def self.capture_alert
    $browser.div(:class => "alert").exist?
    alertmsg = $browser.div(:class => "alert").text
    $results_file.write("Alert shown: #{alertmsg}")
    $PDF.text("Alert shown: #{alertmsg}")
    $results_file.puts ''
    return true
  rescue
    $results_file.write("No Alert Found")
    $PDF.text("No Alert Found")
    $results_file.puts ''
    return false
  end # capture alerts status

  # write to editor Function.
  def self.write_to_editor(iframe, locate, value)
    $browser.iframe(:"#{locate}" => iframe).wait_until_present<commit_msg>Update web_functions.rb<commit_after># Created on 20 Sept 2017
# @author: <NAME>
#
# Versions:
# 1.0 - Baseline
#
# web_functions.rb - all web functions for the TAF.
module WebFuncs
  require './taf_config.rb'
   # Ping test Function.
  def self.ping_test(url)
    while pingtest == Net::Ping::HTTP.new(url)
      pingtest.ping?
      sleep 5
        if (pingtest.ping? == true)
          # website alive
          $results_file.write("pinged #{url}")
          $PDF.text("pinged #{url}")
          $results_file.puts ''
          return true
        else
          # website not responding
          $results_file.write("Failed to ping #{url}")
          $PDF.text("Failed to ping #{url}")
          $results_file.puts ''
          return false
        end
    end
  end # pingTest

  # Open URL function. - the browser object is created via the main program
  def self.open_url(url)
    Utils.open_browser()
    $browser.goto(url)
    sleep 2
    url_name = $browser.url
      if (url_name == url)
        $results_file.write("opened URL: #{url}")
        $PDF.text("opened URL: #{url}")
        $results_file.puts ''
        return true
      else
        $results_file.write("URL not open: #{url} - opened #{url_name} instead")
        $PDF.text("URL not open: #{url} - opened #{url_name} instead")
        $results_file.puts ''
        return false
      end
  end # openUrl

  # Check URL function.
  def self.check_url(url)
    if ($browser.url == url)
      $results_file.write("URL: #{url} is correct")
      $PDF.text("URL: #{url} is correct")
      $results_file.puts ''
      return true
    else
      $results_file.write("URL: #{url} is incorrect")
      $PDF.text("URL: #{url} is incorrect")
      $results_file.puts ''
      return false
    end
  end # CheckURL

  # Click button function.
  def self.click_button(button, locate)
    $browser.button(:"#{locate}" => button).wait_until_present.click
    $results_file.write("Button: #{button} has been selected")
    $PDF.text("Button: #{button} has been selected")
    $results_file.puts ''
    return true
  rescue
    $results_file.write("Button: #{button} does not exist")
    $PDF.text("Button: #{button} does not exist")
    $results_file.puts ''
    return false
  end # clickButton

  # Select Link Function.
  def self.select_link(link, locate)
    $browser.link(:"#{locate}" => link).exist?
    $browser.link(:"#{locate}" => link).wait_until_present.click
    $results_file.write("Link: #{link} has been selected")
    $PDF.text("Link: #{link} has been selected")
    $results_file.puts ''
    return true
  rescue
    $results_file.write("Link: #{link} does not exist")
    $PDF.text("Link: #{link} does not exist")
    $results_file.puts ''
    return false
  end # select link.

  # Select Image Function.
  def self.select_image(image, locate)
    $browser.image(:"#{locate}" => image).exist?
    $browser.image(:"#{locate}" => image).wait_until_present.click
    $results_file.write("Image: #{image} has been selected")
    $PDF.text("Image: #{image} has been selected")
    $results_file.puts ''
    return true
  rescue
    $results_file.write("Image: #{image} does not exist")
    $PDF.text("Image: #{image} does not exist")
    $results_file.puts ''
    return false
  end # select image.

  # Select Item Function.
  def self.select_item(item, locate)
    $browser.a(:"#{locate}" => item).exist?
    $browser.a(:"#{locate}" => item).wait_until_present.click
    $results_file.write("Item: #{item} has been selected")
    $PDF.text("Item: #{item} has been selected")
    $results_file.puts ''
    return true
  rescue
    $results_file.write("Item: #{item} does not exist")
    $PDF.text("Item: #{item} does not exist")
    $results_file.puts ''
   return false
  end # select item.

  # click on a "h<font size>" tag.
  def self.click_h_tag(item, locate)
    found_tags = [
      $browser.h1(:"#{locate}" => item).exist?,
      $browser.h2(:"#{locate}" => item).exist?,
      $browser.h3(:"#{locate}" => item).exist?,
      $browser.h4(:"#{locate}" => item).exist?,
      $browser.h5(:"#{locate}" => item).exist?,
      $browser.h6(:"#{locate}" => item).exist?
    ]

    raise "Multiple matches" if found_tags.select { |i| i }.size > 1
    index = found_tags.index(true)
    return unless index
    $browser.send("h#{index + 1}", :"#{locate}" => item).click
    $results_file.write("Item: #{item} has been selected")
    $PDF.text("Item: #{item} has been selected")
    $results_file.puts ''
    return true
    else
    $results_file.write("Item: #{item} does not exist")
    $PDF.text("Item: #{item} does not exist")
    $results_file.puts ''
    return false
  end # click on a "h<font size>" tag.

  # Capture alerts status Function.
  def self.capture_alert
    $browser.div(:class => "alert").exist?
    alertmsg = $browser.div(:class => "alert").text
    $results_file.write("Alert shown: #{alertmsg}")
    $PDF.text("Alert shown: #{alertmsg}")
    $results_file.puts ''
    return true
  rescue
    $results_file.write("No Alert Found")
    $PDF.text("No Alert Found")
    $results_file.puts ''
    return false
  end # capture alerts status

  # write to editor Function.
  def self.write_to_editor(iframe, locate, value)
    $browser.iframe(:"#{locate}" => iframe).wait_until_present
{'fraction_non_alphanumeric': 0.15672137368371122, 'fraction_numerical': 0.0034152357461341427, 'mean_word_length': 2.9871406959152798, 'pattern_counts': {'":': 0, '<': 8, '<?xml version=': 0, '>': 44, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2931766', 'n_tokens_mistral': 3486, 'n_tokens_neox': 3469, 'n_words': 849}