<commit_before>require File.expand_path(File.dirname(__FILE__) + '/common')

shared_examples_for "course selenium tests" do
  it_should_behave_like "in-process server selenium tests"

  before do
    account = Account.default
    account.settings = { :open_registration => true, :no_enrollments_can_create_courses => true, :teachers_can_create_courses => true }
    account.save!
  end

  it "should properly hide the wizard and remember its hidden state" do
    course_with_teacher_logged_in

    get "/getting_started?fresh=1"
    driver.find_element(:css, ".save_button").click
    wizard_box = driver.find_element(:id, "wizard_box")
    keep_trying_until { wizard_box.displayed? }
    wizard_box.find_element(:css, ".close_wizard_link").click

    refresh_page
    wait_for_animations# we need to give the wizard a chance to pop up
    wizard_box = driver.find_element(:id, "wizard_box")
    wizard_box.displayed?.should be_false
  end

  it "should open and close wizard after initial close" do
    course_with_teacher_logged_in

    get "/getting_started?fresh=1"
    driver.find_element(:css, ".save_button").click

    checklist_button = driver.find_element(:css, '#course_show_secondary .wizard_popup_link')
    checklist_button.click
    wizard_box = driver.find_element(:id, "wizard_box")
    wait_for_animations
    wizard_box.should be_displayed
    checklist_button.should_not be_displayed
    wizard_box.find_element(:css, ".close_wizard_link").click
    wait_for_animations
    wizard_box.displayed?.should be_false
    checklist_button.displayed?.should be_true

  end

  it "should allow content export downloads" do
    course_with_teacher_logged_in
    get "/courses/#{@course.id}/content_exports"
    driver.find_element(:css, "button.submit_button").click
    job = Delayed::Job.last(:conditions => { :tag => 'ContentExport#export_course_without_send_later' })
    export = keep_trying_until { ContentExport.last }
    export.export_course_without_send_later
    new_download_link = keep_trying_until { driver.find_element(:css, "div#exports a") }
    url = new_download_link.attribute 'href'
    url.should match(%r{/files/\d+/download\?verifier=})
  end

  it "should copy course content" do
    course_with_teacher_logged_in
    @second_course = Course.create!(:name => 'second course')
    @second_course.offer!
    #add teacher as a user
    e = @second_course.enroll_teacher(@user)
    e.workflow_state = 'active'
    e.accept
    e.save!
    @second_course.reload

    new_term = Account.default.enrollment_terms.create(:name => 'Test Term')
    third_course = Course.create!(:name => 'third course', :enrollment_term => new_term)
    e = third_course.enroll_teacher(@user)
    e.workflow_state = 'active'
    e.accept
    e.save!

    get "/courses/#{@course.id}/details"

    driver.find_element(:link, I18n.t('links.import','Import Content into this Course')).click
    driver.find_element(:css, '#content a.button').click

    select_box = driver.find_element(:id, 'copy_from_course')
    select_box.find_elements(:css, 'optgroup').length.should == 2
    second_group = select_box.find_elements(:css, 'optgroup').last
    second_group.find_elements(:css, 'option').length.should == 1
    second_group.attribute('label').should == 'Test Term'

    option_value = find_option_value(:id, 'copy_from_course', 'second course')
    driver.find_element(:css, '#copy_from_course option[value="'+option_value+'"]').click
    driver.find_element(:css, '#content form').submit

    #modify course dates
    driver.find_element(:id, 'copy_shift_dates').click
    #adjust start dates
    driver.find_element(:css, '#copy_old_start_date + img').click
    datepicker_prev
    #adjust end dates
    driver.find_element(:css, '#copy_old_end_date + img').click
    datepicker_next
    #adjust day substitutions
    driver.find_element(:css, '.shift_dates_settings .add_substitution_link').click
    driver.find_element(:css, '.substitutions > .substitution').should be_displayed

    driver.find_element(:id, 'copy_context_form').submit
    wait_for_ajaximations

    # since jobs aren't running
    CourseImport.last.perform

    keep_trying_until{ driver.find_element(:css, '#copy_results > h2').should include_text('Copy Succeeded') }
  end

  it "should copy the course" do
    enable_cache do
      course_with_teacher_logged_in

      get "/courses/#{@course.id}/copy"
      driver.find_element(:css, "div#content form button[type=submit]").click
      wait_for_dom_ready
      driver.find_element(:id, 'copy_context_form').submit
      wait_for_ajaximations

      CourseImport.last.perform

      keep_trying_until{ driver.find_element(:css, '#copy_results > h2').should include_text('Copy Succeeded') }

      @new_course = Course.last(:order => :id)
      get "/courses/#{@new_course.id}"
      driver.find_element(:css, "#no_topics_message span.title").should include_text("No Recent Messages")
    end
  end

  it "should correctly update the course quota" do
    course_with_admin_logged_in

    # first try setting the quota explicitly
    get "/courses/#{@course.id}/details"
    form = driver.find_element(:css, "#course_form")
    keep_trying_until{ driver.find_element(:css, "#course_form .edit_course_link").should be_displayed }
    form.find_element(:css, ".edit_course_link").click
    quota_input = form.find_element(:css, "input#course_storage_quota_mb")
    quota_input.clear
    quota_input.send_keys("10")
    form.find_element(:css, 'button[type="submit"]').click
    keep_trying_until { driver.find_element(:css, ".loading_image_holder").nil? rescue true }
    form.find_element(:css, ".course_info.storage_quota_mb").text.should == "10"

    # then try just saving it (without resetting it)
    get "/courses/#{@course.id}/details"
    form = driver.find_element(:css, "#course_form")
    form.find_element(:css, ".course_info.storage_quota_mb").text.should == "10"
    form.find_element(:css, ".edit_course_link").click
    form.find_element(:css, 'button[type="submit"]').click
    keep_trying_until { driver.find_element(:css, ".loading_image_holder").nil? rescue true }
    form.find_element(:css, ".course_info.storage_quota_mb").text.should == "10"

    # then make sure it's right after a reload
    get "/courses/#{@course.id}/details"
    form = driver.find_element(:css, "#course_form")
    form.find_element(:css, ".course_info.storage_quota_mb").text.should == "10"
    @course.reload
    @course.storage_quota.should == 10.megabytes
  end<commit_msg>fixing inconsistently failing selenium courses spec<commit_after>require File.expand_path(File.dirname(__FILE__) + '/common')

shared_examples_for "course selenium tests" do
  it_should_behave_like "in-process server selenium tests"

  before do
    account = Account.default
    account.settings = { :open_registration => true, :no_enrollments_can_create_courses => true, :teachers_can_create_courses => true }
    account.save!
  end

  it "should properly hide the wizard and remember its hidden state" do
    course_with_teacher_logged_in

    get "/getting_started?fresh=1"
    driver.find_element(:css, ".save_button").click
    wizard_box = driver.find_element(:id, "wizard_box")
    keep_trying_until { wizard_box.displayed? }
    wizard_box.find_element(:css, ".close_wizard_link").click

    refresh_page
    wait_for_animations# we need to give the wizard a chance to pop up
    wizard_box = driver.find_element(:id, "wizard_box")
    wizard_box.displayed?.should be_false
  end

  it "should open and close wizard after initial close" do
    course_with_teacher_logged_in

    get "/getting_started?fresh=1"
    driver.find_element(:css, ".save_button").click

    checklist_button = driver.find_element(:css, '#course_show_secondary .wizard_popup_link')
    checklist_button.click
    wizard_box = driver.find_element(:id, "wizard_box")
    wait_for_animations
    wizard_box.should be_displayed
    checklist_button.should_not be_displayed
    wizard_box.find_element(:css, ".close_wizard_link").click
    wait_for_animations
    wizard_box.displayed?.should be_false
    checklist_button.displayed?.should be_true

  end

  it "should allow content export downloads" do
    course_with_teacher_logged_in
    get "/courses/#{@course.id}/content_exports"
    driver.find_element(:css, "button.submit_button").click
    job = Delayed::Job.last(:conditions => { :tag => 'ContentExport#export_course_without_send_later' })
    export = keep_trying_until { ContentExport.last }
    export.export_course_without_send_later
    new_download_link = keep_trying_until { driver.find_element(:css, "div#exports a") }
    url = new_download_link.attribute 'href'
    url.should match(%r{/files/\d+/download\?verifier=})
  end

  it "should copy course content" do
    course_with_teacher_logged_in
    @second_course = Course.create!(:name => 'second course')
    @second_course.offer!
    #add teacher as a user
    e = @second_course.enroll_teacher(@user)
    e.workflow_state = 'active'
    e.accept
    e.save!
    @second_course.reload

    new_term = Account.default.enrollment_terms.create(:name => 'Test Term')
    third_course = Course.create!(:name => 'third course', :enrollment_term => new_term)
    e = third_course.enroll_teacher(@user)
    e.workflow_state = 'active'
    e.accept
    e.save!

    get "/courses/#{@course.id}/details"

    driver.find_element(:link, I18n.t('links.import','Import Content into this Course')).click
    driver.find_element(:css, '#content a.button').click

    select_box = driver.find_element(:id, 'copy_from_course')
    select_box.find_elements(:css, 'optgroup').length.should == 2
    second_group = select_box.find_elements(:css, 'optgroup').last
    second_group.find_elements(:css, 'option').length.should == 1
    second_group.attribute('label').should == 'Test Term'

    option_value = find_option_value(:id, 'copy_from_course', 'second course')
    driver.find_element(:css, '#copy_from_course option[value="'+option_value+'"]').click
    driver.find_element(:css, '#content form').submit

    #modify course dates
    driver.find_element(:id, 'copy_shift_dates').click
    #adjust start dates
    driver.find_element(:css, '#copy_old_start_date + img').click
    datepicker_prev
    #adjust end dates
    driver.find_element(:css, '#copy_old_end_date + img').click
    datepicker_next
    #adjust day substitutions
    driver.find_element(:css, '.shift_dates_settings .add_substitution_link').click
    driver.find_element(:css, '.substitutions > .substitution').should be_displayed

    driver.find_element(:id, 'copy_context_form').submit
    wait_for_ajaximations

    # since jobs aren't running
    CourseImport.last.perform

    keep_trying_until{ driver.find_element(:css, '#copy_results > h2').should include_text('Copy Succeeded') }
  end

  it "should copy the course" do
    enable_cache do
      course_with_teacher_logged_in

      get "/courses/#{@course.id}/copy"
      driver.find_element(:css, "div#content form button[type=submit]").click
      wait_for_dom_ready
      driver.find_element(:id, 'copy_context_form').submit
      wait_for_ajaximations

      CourseImport.last.perform

      keep_trying_until{ driver.find_element(:css, '#copy_results > h2').should include_text('Copy Succeeded') }

      @new_course = Course.last(:order => :id)
      get "/courses/#{@new_course.id}"
      driver.find_element(:css, "#no_topics_message span.title").should include_text("No Recent Messages")
    end
  end

  it "should correctly update the course quota" do
    course_with_admin_logged_in

    # first try setting the quota explicitly
    get "/courses/#{@course.id}/details"
    driver.find_element(:link, 'Course Details').click
    form = driver.find_element(:css, "#course_form")
    form.find_element(:css, ".edit_course_link").click
    quota_input = form.find_element(:css, "input#course_storage_quota_mb")
    quota_input.clear
    quota_input.send_keys("10")
    form.find_element(:css, 'button[type="submit"]').click
    keep_trying_until { driver.find_element(:css, ".loading_image_holder").nil? rescue true }
    form.find_element(:css, ".course_info.storage_quota_mb").text.should == "10"

    # then try just saving it (without resetting it)
    get "/courses/#{@course.id}/details"
    form = driver.find_element(:css, "#course_form")
    form.find_element(:css, ".course_info.storage_quota_mb").text.should == "10"
    form.find_element(:css, ".edit_course_link").click
    form.find_element(:css, 'button[type="submit"]').click
    keep_trying_until { driver.find_element(:css, ".loading_image_holder").nil? rescue true }
    form.find_element(:css, ".course_info.storage_quota_mb").text.should == "10"

    # then make sure it's right after a reload
    get "/courses/#{@course.id}/details"
    form = driver.find_element(:css, "#course_form")
    form.find_element(:css, ".course_info.storage_quota_mb").text.should == "10"
    @course.reload
    @course.storage_quota.should == 10.megabytes
  end
{'fraction_non_alphanumeric': 0.12134727776068902, 'fraction_numerical': 0.0030759766225776685, 'mean_word_length': 4.436872909698996, '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': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1438508', 'n_tokens_mistral': 4285, 'n_tokens_neox': 4136, 'n_words': 774}