<commit_before>
module FireWatir
  include Watir::Exception

  class Firefox
    include Watir::WaitHelper
    include FireWatir::Container

    # XPath Result type. Return only first node that matches the xpath expression.
    # More details: "http://developer.mozilla.org/en/docs/DOM:document.evaluate"
    FIRST_ORDERED_NODE_TYPE = 9

    # Description:
    #   Starts the firefox browser.
    #   On windows this starts the first version listed in the registry.
    #
    # Input:
    #   options  - Hash of any of the following options:
    #     :profile  - The Firefox profile to use. If none is specified, Firefox will use
    #                 the last used profile.
    #     :suppress_launch_process - do not create a new firefox process. Connect to an existing one.

    # TODO: Start the firefox version given by user.

    def initialize(options = {})
      if current_os == :macosx && !%x{ps x | grep firefox-bin | grep -v grep}.empty?
        # check for jssh not running, firefox may be open but not with -jssh
        # if its not open at all, regardless of the :suppress_launch_process option start it
        # error if running without jssh, we don't want to kill their current window (mac only)
        jssh_down = false
        begin
          set_defaults()
        rescue Watir::Exception::UnableToStartJSShException
          jssh_down = true
        end

        raise "Firefox is running without -jssh" if jssh_down
        open_window unless options[:suppress_launch_process]
      elsif not options[:suppress_launch_process]
        launch_browser(options)
      end

      set_defaults()
      get_window_number()
      set_browser_document()
    end

    def inspect
      '#<%s:0x%x url=%s title=%s>' % [self.class, hash*2, url.inspect, title.inspect]
    end


    # Launches firebox browser
    # options as .new

    def launch_browser(options = {})

      if(options[:profile])
        profile_opt = "-no-remote -P #{options[:profile]}"
      else
        profile_opt = ""
      end

      bin = path_to_bin()
      @t = Thread.new { system("#{bin} -jssh #{profile_opt}") }
    end
    private :launch_browser

    # Creates a new instance of Firefox. Loads the URL and return the instance.
    # Input:
    #   url - url of the page to be loaded.
    def self.start(url)
      ff = Firefox.new
      ff.goto(url)
      return ff
    end

    # Gets the window number opened.
    # Currently, this returns the most recently opened window, which may or may
    # not be the current window.
    def get_window_number()
      # If at any time a non-browser window like the "Downloads" window
      #   pops up, it will become the topmost window, so make sure we
      #   ignore it.
      window_count = js_eval("getWindows().length").to_i - 1
      while js_eval("getWindows()[#{window_count}].getBrowser") == ''
        window_count -= 1;
      end

      # now correctly handles instances where only browserless windows are open
      # opens one we can use if count is 0

      if window_count < 0
        open_window
        window_count = 1
      end
      @window_index = window_count
    end
    private :get_window_number

    # Loads the given url in the browser. Waits for the page to get loaded.
    def goto(url)
      get_window_number()
      set_browser_document()
      js_eval "#{browser_var}.loadURI(\"#{url}\")"
      wait()
    end

    # Loads the previous page (if there is any) in the browser. Waits for the page to get loaded.
    def back()<commit_msg>add method #exists? with alias #exist? to FireWatir::Firefox<commit_after>
module FireWatir
  include Watir::Exception

  class Firefox
    include Watir::WaitHelper
    include FireWatir::Container

    # XPath Result type. Return only first node that matches the xpath expression.
    # More details: "http://developer.mozilla.org/en/docs/DOM:document.evaluate"
    FIRST_ORDERED_NODE_TYPE = 9

    # Description:
    #   Starts the firefox browser.
    #   On windows this starts the first version listed in the registry.
    #
    # Input:
    #   options  - Hash of any of the following options:
    #     :profile  - The Firefox profile to use. If none is specified, Firefox will use
    #                 the last used profile.
    #     :suppress_launch_process - do not create a new firefox process. Connect to an existing one.

    # TODO: Start the firefox version given by user.

    def initialize(options = {})
      if current_os == :macosx && !%x{ps x | grep firefox-bin | grep -v grep}.empty?
        # check for jssh not running, firefox may be open but not with -jssh
        # if its not open at all, regardless of the :suppress_launch_process option start it
        # error if running without jssh, we don't want to kill their current window (mac only)
        jssh_down = false
        begin
          set_defaults()
        rescue Watir::Exception::UnableToStartJSShException
          jssh_down = true
        end

        raise "Firefox is running without -jssh" if jssh_down
        open_window unless options[:suppress_launch_process]
      elsif not options[:suppress_launch_process]
        launch_browser(options)
      end

      set_defaults()
      get_window_number()
      set_browser_document()
    end

    def inspect
      '#<%s:0x%x url=%s title=%s>' % [self.class, hash*2, url.inspect, title.inspect]
    end


    # Launches firebox browser
    # options as .new

    def launch_browser(options = {})

      if(options[:profile])
        profile_opt = "-no-remote -P #{options[:profile]}"
      else
        profile_opt = ""
      end

      bin = path_to_bin()
      @t = Thread.new { system("#{bin} -jssh #{profile_opt}") }
    end
    private :launch_browser

    # Creates a new instance of Firefox. Loads the URL and return the instance.
    # Input:
    #   url - url of the page to be loaded.
    def self.start(url)
      ff = Firefox.new
      ff.goto(url)
      return ff
    end

    # Gets the window number opened.
    # Currently, this returns the most recently opened window, which may or may
    # not be the current window.
    def get_window_number()
      # If at any time a non-browser window like the "Downloads" window
      #   pops up, it will become the topmost window, so make sure we
      #   ignore it.
      window_count = js_eval("getWindows().length").to_i - 1
      while js_eval("getWindows()[#{window_count}].getBrowser") == ''
        window_count -= 1;
      end

      # now correctly handles instances where only browserless windows are open
      # opens one we can use if count is 0

      if window_count < 0
        open_window
        window_count = 1
      end
      @window_index = window_count
    end
    private :get_window_number

    # Loads the given url in the browser. Waits for the page to get loaded.
    def goto(url)
      get_window_number()
      set_browser_document()
      js_eval "#{browser_var}.loadURI(\"#{url}\")"
      wait()
    end

    # Returns true if Firefox window is opened.
    def exists?
      js_eval("getWindows().length").to_i == 1 || ((window = find_window(:url, @window_url)) && window > 0)
    end

    alias_method :exist?, :exists?

    # Loads the previous page (if there is any) in the browser. Waits for the page to get loaded.
    def back()
{'fraction_non_alphanumeric': 0.0830359593741422, 'fraction_numerical': 0.0024704913532802636, 'mean_word_length': 2.4421350968351443, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 6, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6520454', 'n_tokens_mistral': 2175, 'n_tokens_neox': 2072, 'n_words': 764}