<commit_before>require 'spec_helper'

describe Watir::Element do
  describe "#when_dom_changed" do
    context "when DOM is changed" do
      context "when block is not given" do
        it "waits using event handler" do
          @browser.button(:id => "quick").click
          @browser.div.when_dom_changed.should have(20).spans
        end

        it "may be run more than one time" do
          3.times do |i|
            @browser.button(:id => "quick").click
            @browser.div.when_dom_changed.should have(20 * (i + 1)).spans
          end
        end

        it "waits using custom interval" do
          @browser.button(:id => "long").click
          @browser.div.when_dom_changed(:interval => 1.1).should have(5).spans
        end

        it "raises timeout error" do
          @browser.button(:id => "quick").click
          lambda { @browser.div.when_dom_changed(:timeout => 2).spans }.should raise_error(Watir::Wait::TimeoutError)
        end
      end

      context "when block given" do
        it "waits using event handler" do
          @browser.button(:id => "quick").click
          @browser.div.when_dom_changed do |div|
            div.should have(20).spans
          end
        end

        it "waits using custom interval" do
          @browser.button(:id => "long").click
          @browser.div.when_dom_changed(:interval => 1.1) do |div|
            div.should have(5).spans
          end
        end

        it "raises timeout error" do
          @browser.button(:id => "quick").click
          lambda do
            @browser.div.when_dom_changed(:timeout => 2) { |div| div.spans }
          end.should raise_error(Watir::Wait::TimeoutError)
        end

        it "returns block evaluation" do
          @browser.button(:id => "quick").click
          @browser.div.when_dom_changed do |div|
            div.spans.size
          end.should == 20
        end
      end
    end

    context "when DOM is not changed" do
      it "doesn't raise any exception" do
        @browser.div.when_dom_changed.should have(0).spans
      end
    end

    context "when element goes stale" do
      before(:all) do
        Watir.always_locate = false
      end

      after(:all) do
        Watir.always_locate = true
      end

      it "relocates element" do
        div = @browser.div(:id => 'container2')
        div.exists?
        @browser.refresh
        lambda do
          div.when_dom_changed.text
        end.should_not raise_error(Selenium::WebDriver::Error::StaleElementReferenceError)
      end
    end
  end

  describe "#wait_until_dom_changed" do
    it "calls #when_dom_changed" do
      div = @browser.div
      opts = { timeout: 1, interval: 2, delay: 3 }
      div.should_receive(:when_dom_changed).with(opts)
      div.wait_until_dom_changed(opts)
    end

    it "returns nil" do
      @browser.div.wait_until_dom_changed.should == nil
    end
  end
end<commit_msg>use new rspec expect syntax<commit_after>require 'spec_helper'

describe Watir::Element do
  describe "#when_dom_changed" do
    context "when DOM is changed" do
      context "when block is not given" do
        it "waits using event handler" do
          @browser.button(:id => "quick").click
          expect(@browser.div.when_dom_changed).to have(20).spans
        end

        it "may be run more than one time" do
          3.times do |i|
            @browser.button(:id => "quick").click
            expect(@browser.div.when_dom_changed).to have(20 * (i + 1)).spans
          end
        end

        it "waits using custom interval" do
          @browser.button(:id => "long").click
          expect(@browser.div.when_dom_changed(:interval => 1.1)).to have(5).spans
        end

        it "raises timeout error" do
          @browser.button(:id => "quick").click
          expect { @browser.div.when_dom_changed(:timeout => 2).spans }.to raise_error(Watir::Wait::TimeoutError)
        end
      end

      context "when block given" do
        it "waits using event handler" do
          @browser.button(:id => "quick").click
          @browser.div.when_dom_changed do |div|
            expect(div).to have(20).spans
          end
        end

        it "waits using custom interval" do
          @browser.button(:id => "long").click
          @browser.div.when_dom_changed(:interval => 1.1) do |div|
            expect(div).to have(5).spans
          end
        end

        it "raises timeout error" do
          @browser.button(:id => "quick").click
          expect {
            @browser.div.when_dom_changed(:timeout => 2) { |div| div.spans }
          }.to raise_error(Watir::Wait::TimeoutError)
        end

        it "returns block evaluation" do
          @browser.button(:id => "quick").click
          size = @browser.div.when_dom_changed do |div|
            div.spans.size
          end
          expect(size).to eq(20)
        end
      end
    end

    context "when DOM is not changed" do
      it "doesn't raise any exception" do
        expect(@browser.div.when_dom_changed).to have(0).spans
      end
    end

    context "when element goes stale" do
      before(:all) do
        Watir.always_locate = false
      end

      after(:all) do
        Watir.always_locate = true
      end

      it "relocates element" do
        div = @browser.div(:id => 'container2')
        div.exists?
        @browser.refresh
        expect {
          div.when_dom_changed.text
        }.not_to raise_error(Selenium::WebDriver::Error::StaleElementReferenceError)
      end
    end
  end

  describe "#wait_until_dom_changed" do
    it "calls #when_dom_changed" do
      div = @browser.div
      opts = { timeout: 1, interval: 2, delay: 3 }
      expect(div).to receive(:when_dom_changed).with(opts)
      div.wait_until_dom_changed(opts)
    end

    it "returns nil" do
      expect(@browser.div.wait_until_dom_changed).to eq(nil)
    end
  end
end
{'fraction_non_alphanumeric': 0.10731292517006803, 'fraction_numerical': 0.007823129251700681, 'mean_word_length': 2.039276485788114, 'pattern_counts': {'":': 0, '<': 3, '<?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': '3086641', 'n_tokens_mistral': 1805, 'n_tokens_neox': 1740, 'n_words': 452}