<commit_before>require File.expand_path(File.dirname(__FILE__) + "/../spec_helper")

module Seleniumrc
describe SeleniumTestCase, :shared => true do
  include SeleniumTestCaseSpec

  before(:each) do
    @test_case = SeleniumTestCaseSpec::MySeleniumTestCase.new
    @base_selenium = "Base Selenium"
    @test_case.base_selenium = @base_selenium
    
    stub_wait_for(@test_case)
    stub.probe(SeleniumElement).new do |element|
      stub_wait_for element
      element
    end
    stub.probe(SeleniumPage).new do |page|
      stub_wait_for page
      page
    end
  end

  def sample_locator
    "sample_locator"
  end

  def sample_text
    "test text"
  end

  def create_sample_configuration
    configuration = Seleniumrc::SeleniumConfiguration.instance
    configuration.external_app_server_host = "test.com"
    configuration.external_app_server_port = 80
    configuration
  end
end

describe SeleniumTestCase, "#setup" do
  it_should_behave_like "Seleniumrc::SeleniumTestCase"

  it "should not allow transactional fixtures" do
    stub(@test_case.class).use_transactional_fixtures.returns true

    expected_message = "Cannot use transactional fixtures if ActiveRecord concurrency is turned on (which is required for Selenium tests to work)."
    proc {@test_case.setup}.should raise_error(RuntimeError, expected_message)
  end
end

describe SeleniumTestCase, "#wait_for" do
  it_should_behave_like "Seleniumrc::SeleniumTestCase"

  it "should pass when the block returns true within time limit" do
    @test_case.wait_for(:timeout => 2) do
      true
    end
  end

  it "should raise a AssertionFailedError when block times out" do
    proc do
      @test_case.wait_for(:timeout => 2) {false}
    end.should raise_error(Test::Unit::AssertionFailedError, "Timeout exceeded (after 2 sec)")
  end
end

describe SeleniumTestCase, "#wait_for_element_to_contain" do
  it_should_behave_like "Seleniumrc::SeleniumTestCase"

  it "should pass when finding text within time limit" do
    is_element_present_results = [false, true]

    stub(base_selenium).is_element_present {is_element_present_results.shift}
    stub(base_selenium).get_eval("this.page().findElement(\"#{sample_locator}\").innerHTML") do
      sample_text
    end

    @test_case.wait_for_element_to_contain(sample_locator, sample_text)
  end

  it "should fail when element not found in time" do
    is_element_present_results = [false, false, false, false]

    stub(base_selenium).is_element_present {is_element_present_results.shift}

    proc do
      @test_case.wait_for_element_to_contain(sample_locator, "")
    end.should raise_error(Test::Unit::AssertionFailedError, "Timeout exceeded (after 5 sec)")
  end

  it "should fail when text does not match in time" do
    is_element_present_results = [false, true, true, true]

    stub(base_selenium).is_element_present {is_element_present_results.shift}
    stub(base_selenium).get_eval.
      with("this.page().findElement(\"#{sample_locator}\").innerHTML").
      returns(sample_text)

    proc do
      @test_case.wait_for_element_to_contain(sample_locator, "wrong text", nil, 1)
    end.should raise_error(Test::Unit::AssertionFailedError, "Timeout exceeded (after 1 sec)")
  end
end

describe SeleniumTestCase, "#default_timeout" do
  it_should_behave_like "Seleniumrc::SeleniumTestCase"

  it "default_timeout should be 20 seconds" do
    @test_case.default_timeout.should == 20000
  end
end

describe SeleniumTestCase, "#open_home_page" do
  it_should_behave_like "Seleniumrc::SeleniumTestCase"

  it "should open home page" do
    configuration = create_sample_configuration

    @test_case.base_selenium = base_selenium

    mock(base_selenium).open("http://test.com:80")
    mock(base_selenium).wait_for_page_to_load(@test_case.default_timeout)
    stub(base_selenium).send {""}

    @test_case.open_home_page
  end
end

describe SeleniumTestCase, "#assert_title" do
  it_should_behave_like "Seleniumrc::SeleniumTestCase"

  before do
    mock.proxy(SeleniumPage).new(base_selenium) do |page|<commit_msg>Refactoring<commit_after>require File.expand_path(File.dirname(__FILE__) + "/../spec_helper")

module Seleniumrc
describe SeleniumTestCase, :shared => true do
  include SeleniumTestCaseSpec

  before(:each) do
    @test_case = SeleniumTestCaseSpec::MySeleniumTestCase.new
    @base_selenium = "Base Selenium"
    @test_case.base_selenium = @base_selenium
    @configuration = create_sample_configuration
    
    stub_wait_for(@test_case)
    stub.probe(SeleniumElement).new do |element|
      stub_wait_for element
      element
    end
    stub.probe(SeleniumPage).new do |page|
      stub_wait_for page
      page
    end
  end

  def sample_locator
    "sample_locator"
  end

  def sample_text
    "test text"
  end

  def create_sample_configuration
    configuration = Seleniumrc::SeleniumConfiguration.instance
    configuration.external_app_server_host = "test.com"
    configuration.external_app_server_port = 80
    configuration
  end
end

describe SeleniumTestCase, "#setup" do
  it_should_behave_like "Seleniumrc::SeleniumTestCase"

  it "should not allow transactional fixtures" do
    stub(@test_case.class).use_transactional_fixtures.returns true

    expected_message = "Cannot use transactional fixtures if ActiveRecord concurrency is turned on (which is required for Selenium tests to work)."
    proc {@test_case.setup}.should raise_error(RuntimeError, expected_message)
  end
end

describe SeleniumTestCase, "#wait_for" do
  it_should_behave_like "Seleniumrc::SeleniumTestCase"

  it "should pass when the block returns true within time limit" do
    @test_case.wait_for(:timeout => 2) do
      true
    end
  end

  it "should raise a AssertionFailedError when block times out" do
    proc do
      @test_case.wait_for(:timeout => 2) {false}
    end.should raise_error(Test::Unit::AssertionFailedError, "Timeout exceeded (after 2 sec)")
  end
end

describe SeleniumTestCase, "#wait_for_element_to_contain" do
  it_should_behave_like "Seleniumrc::SeleniumTestCase"

  it "should pass when finding text within time limit" do
    is_element_present_results = [false, true]

    stub(base_selenium).is_element_present {is_element_present_results.shift}
    stub(base_selenium).get_eval("this.page().findElement(\"#{sample_locator}\").innerHTML") do
      sample_text
    end

    @test_case.wait_for_element_to_contain(sample_locator, sample_text)
  end

  it "should fail when element not found in time" do
    is_element_present_results = [false, false, false, false]

    stub(base_selenium).is_element_present {is_element_present_results.shift}

    proc do
      @test_case.wait_for_element_to_contain(sample_locator, "")
    end.should raise_error(Test::Unit::AssertionFailedError, "Timeout exceeded (after 5 sec)")
  end

  it "should fail when text does not match in time" do
    is_element_present_results = [false, true, true, true]

    stub(base_selenium).is_element_present {is_element_present_results.shift}
    stub(base_selenium).get_eval.
      with("this.page().findElement(\"#{sample_locator}\").innerHTML").
      returns(sample_text)

    proc do
      @test_case.wait_for_element_to_contain(sample_locator, "wrong text", nil, 1)
    end.should raise_error(Test::Unit::AssertionFailedError, "Timeout exceeded (after 1 sec)")
  end
end

describe SeleniumTestCase, "#default_timeout" do
  it_should_behave_like "Seleniumrc::SeleniumTestCase"

  it "default_timeout should be 20 seconds" do
    @test_case.default_timeout.should == 20000
  end
end

describe SeleniumTestCase, "#open_home_page" do
  it_should_behave_like "Seleniumrc::SeleniumTestCase"

  it "opens home page" do
    mock(base_selenium).open("http://test.com:80")
    mock(base_selenium).wait_for_page_to_load(@test_case.default_timeout)
    stub(base_selenium).send {""}

    @test_case.open_home_page
  end
end

describe SeleniumTestCase, "#assert_title" do
  it_should_behave_like "Seleniumrc::SeleniumTestCase"

  before do
    mock.proxy(SeleniumPage).new(base_selenium) do |page|
{'fraction_non_alphanumeric': 0.08642590286425902, 'fraction_numerical': 0.004234122042341221, 'mean_word_length': 4.728245363766049, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4245423', 'n_tokens_mistral': 2677, 'n_tokens_neox': 2547, 'n_words': 574}