<commit_before>require "watirspec_helper"

describe Watir::Element do
  before do
    browser.goto WatirSpec.url_for("wait.html")
  end

  # TODO: This is deprecated; remove in future version
  not_compliant_on :relaxed_locate do
    describe "#when_present" do
      it "invokes subsequent method calls when the element becomes present" do
        browser.a(id: 'show_bar').click

        bar = browser.div(id: 'bar')
        bar.when_present(2).click
        expect(bar.text).to eq "changed"
      end

      it "times out when given a block" do
        expect { browser.div(id: 'bar').when_present(1) {} }.to raise_error(Watir::Wait::TimeoutError)
      end

      it "times out when not given a block" do
        message = /^timed out after 1 seconds, waiting for (\{:id=>"bar", :tag_name=>"div"\}|\{:tag_name=>"div", :id=>"bar"\}) to become present$/
        expect { browser.div(id: 'bar').when_present(1).click }.to raise_error(Watir::Wait::TimeoutError, message)
      end

      it "responds to Element methods" do
        decorator = browser.div.when_present

        expect(decorator).to respond_to(:exist?)
        expect(decorator).to respond_to(:present?)
        expect(decorator).to respond_to(:click)
      end

      it "delegates present? to element" do
        Object.class_eval do
          def present?
            false
          end
        end
        element = browser.a(id: "show_bar").when_present(1)
        expect(element).to be_present
      end

      it "processes before calling present?" do
        browser.a(id: 'show_bar').click
        expect(browser.div(id: 'bar').when_present.present?).to be true
      end
    end
  end

  compliant_on :relaxed_locate do
    it "clicking automatically waits until the element appears" do
      browser.a(id: 'show_bar').click
      expect { browser.div(id: 'bar').click }.to_not raise_exception
      expect(browser.div(id: 'bar').text).to eq 'changed'
    end

    it "raises exception if the element doesn't appear" do
      expect { browser.div(id: 'bar').click }.to raise_unknown_object_exception
    end

    it "raises exception if the element doesn't become enabled" do
      expect { browser.button(id: 'btn').click }.to raise_object_disabled_exception
    end

  end

  not_compliant_on :relaxed_locate do
    describe "#wait_until &:enabled?" do
      it "invokes subsequent method calls when the element becomes enabled" do
        browser.a(id: 'enable_btn').click

        btn = browser.button(id: 'btn')
        btn.wait_until(timeout: 2, &:enabled?).click
        Watir::Wait.while { btn.enabled? }
        expect(btn.disabled?).to be true
      end

      it "times out" do
        error = Watir::Wait::TimeoutError
        inspected = '#<Watir::Button: located: false; {:id=>"btn", :tag_name=>"button"}>'
        message = "timed out after 1 seconds, waiting for true condition on #{inspected}"
        element = browser.button(id: 'btn')
        expect { element.wait_until(timeout: 1, &:enabled?).click }.to raise_error(error, message)
      end

      it "responds to Element methods" do
        element = browser.button.wait_until { true }

        expect(element).to respond_to(:exist?)
        expect(element).to respond_to(:present?)
        expect(element).to respond_to(:click)
      end

      it "can be chained with #wait_until &:present?" do
        browser.a(id: 'show_and_enable_btn').click
        browser.button(id: 'btn2').wait_until(&:present?).wait_until(&:enabled?).click

        expect(browser.button(id: 'btn2')).to exist
        expect(browser.button(id: 'btn2')).to be_enabled
      end
    end
  end

  describe "#wait_until_present" do
    it "waits until the element appears" do
      browser.a(id: 'show_bar').click
      expect { browser.div(id: 'bar').wait_until_present(timeout: 5) }.to_not raise_exception
    end

    it "waits until the element re-appears" do
      browser.link(id: 'readd_bar').click
      expect { browser.div(id: 'bar').wait_until_present }.to_not raise_exception
    end

    it "times out if the element doesn't appear" do
      inspected = '#<Watir::Div: located: false; {:id=>"bar", :tag_name=>"div"}>'
      error = Watir::Wait::TimeoutError
      message = "timed out after 1 seconds, waiting for true condition on #{inspected}"

      expect { browser.div(id: 'bar').wait_until_present(timeout: 1) }.to raise_error(error, message)
    end

    it "uses provided interval" do
      element = browser.div(id: 'bar')
      expect(element).to receive(:present?).twice

      begin
        element.wait_until_present(timeout: 0.4, interval: 0.2)
      rescue Watir::Wait::TimeoutError
      end
    end
  end

  describe "#wait_while_present" do
    it "waits until the element disappears" do
      browser.a(id: 'hide_foo').click
      expect { browser.div(id: 'foo').wait_while_present(timeout: 2) }.to_not raise_exception
    end

    it "times out if the element doesn't disappear" do
      error = Watir::Wait::TimeoutError
      inspected = '#<Watir::Div: located: false; {:id=>"foo", :tag_name=>"div"}>'
      message = "timed out after 1 seconds, waiting for false condition on #{inspected}"
      expect { browser.div(id: 'foo').wait_while_present(timeout: 1) }.to raise_error(error, message)
    end

    it "uses provided interval" do
      element = browser.div(id: 'foo')<commit_msg>these elements have both been located at the time of the exception<commit_after>require "watirspec_helper"

describe Watir::Element do
  before do
    browser.goto WatirSpec.url_for("wait.html")
  end

  # TODO: This is deprecated; remove in future version
  not_compliant_on :relaxed_locate do
    describe "#when_present" do
      it "invokes subsequent method calls when the element becomes present" do
        browser.a(id: 'show_bar').click

        bar = browser.div(id: 'bar')
        bar.when_present(2).click
        expect(bar.text).to eq "changed"
      end

      it "times out when given a block" do
        expect { browser.div(id: 'bar').when_present(1) {} }.to raise_error(Watir::Wait::TimeoutError)
      end

      it "times out when not given a block" do
        message = /^timed out after 1 seconds, waiting for (\{:id=>"bar", :tag_name=>"div"\}|\{:tag_name=>"div", :id=>"bar"\}) to become present$/
        expect { browser.div(id: 'bar').when_present(1).click }.to raise_error(Watir::Wait::TimeoutError, message)
      end

      it "responds to Element methods" do
        decorator = browser.div.when_present

        expect(decorator).to respond_to(:exist?)
        expect(decorator).to respond_to(:present?)
        expect(decorator).to respond_to(:click)
      end

      it "delegates present? to element" do
        Object.class_eval do
          def present?
            false
          end
        end
        element = browser.a(id: "show_bar").when_present(1)
        expect(element).to be_present
      end

      it "processes before calling present?" do
        browser.a(id: 'show_bar').click
        expect(browser.div(id: 'bar').when_present.present?).to be true
      end
    end
  end

  compliant_on :relaxed_locate do
    it "clicking automatically waits until the element appears" do
      browser.a(id: 'show_bar').click
      expect { browser.div(id: 'bar').click }.to_not raise_exception
      expect(browser.div(id: 'bar').text).to eq 'changed'
    end

    it "raises exception if the element doesn't appear" do
      expect { browser.div(id: 'bar').click }.to raise_unknown_object_exception
    end

    it "raises exception if the element doesn't become enabled" do
      expect { browser.button(id: 'btn').click }.to raise_object_disabled_exception
    end

  end

  not_compliant_on :relaxed_locate do
    describe "#wait_until &:enabled?" do
      it "invokes subsequent method calls when the element becomes enabled" do
        browser.a(id: 'enable_btn').click

        btn = browser.button(id: 'btn')
        btn.wait_until(timeout: 2, &:enabled?).click
        Watir::Wait.while { btn.enabled? }
        expect(btn.disabled?).to be true
      end

      it "times out" do
        error = Watir::Wait::TimeoutError
        inspected = '#<Watir::Button: located: false; {:id=>"btn", :tag_name=>"button"}>'
        message = "timed out after 1 seconds, waiting for true condition on #{inspected}"
        element = browser.button(id: 'btn')
        expect { element.wait_until(timeout: 1, &:enabled?).click }.to raise_error(error, message)
      end

      it "responds to Element methods" do
        element = browser.button.wait_until { true }

        expect(element).to respond_to(:exist?)
        expect(element).to respond_to(:present?)
        expect(element).to respond_to(:click)
      end

      it "can be chained with #wait_until &:present?" do
        browser.a(id: 'show_and_enable_btn').click
        browser.button(id: 'btn2').wait_until(&:present?).wait_until(&:enabled?).click

        expect(browser.button(id: 'btn2')).to exist
        expect(browser.button(id: 'btn2')).to be_enabled
      end
    end
  end

  describe "#wait_until_present" do
    it "waits until the element appears" do
      browser.a(id: 'show_bar').click
      expect { browser.div(id: 'bar').wait_until_present(timeout: 5) }.to_not raise_exception
    end

    it "waits until the element re-appears" do
      browser.link(id: 'readd_bar').click
      expect { browser.div(id: 'bar').wait_until_present }.to_not raise_exception
    end

    it "times out if the element doesn't appear" do
      inspected = '#<Watir::Div: located: true; {:id=>"bar", :tag_name=>"div"}>'
      error = Watir::Wait::TimeoutError
      message = "timed out after 1 seconds, waiting for true condition on #{inspected}"

      expect { browser.div(id: 'bar').wait_until_present(timeout: 1) }.to raise_error(error, message)
    end

    it "uses provided interval" do
      element = browser.div(id: 'bar')
      expect(element).to receive(:present?).twice

      begin
        element.wait_until_present(timeout: 0.4, interval: 0.2)
      rescue Watir::Wait::TimeoutError
      end
    end
  end

  describe "#wait_while_present" do
    it "waits until the element disappears" do
      browser.a(id: 'hide_foo').click
      expect { browser.div(id: 'foo').wait_while_present(timeout: 2) }.to_not raise_exception
    end

    it "times out if the element doesn't disappear" do
      error = Watir::Wait::TimeoutError
      inspected = '#<Watir::Div: located: true; {:id=>"foo", :tag_name=>"div"}>'
      message = "timed out after 1 seconds, waiting for false condition on #{inspected}"
      expect { browser.div(id: 'foo').wait_while_present(timeout: 1) }.to raise_error(error, message)
    end

    it "uses provided interval" do
      element = browser.div(id: 'foo')
{'fraction_non_alphanumeric': 0.12135877152163797, 'fraction_numerical': 0.003908794788273616, 'mean_word_length': 3.0067114093959733, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 29, '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': '896174', 'n_tokens_mistral': 3383, 'n_tokens_neox': 3243, 'n_words': 916}