Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: Align Bioportal and AgroPortal - part 2.1 - Add UI tests #24

Merged
merged 2 commits into from
Oct 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
200 changes: 200 additions & 0 deletions test/application_system_test_case.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,200 @@
require "test_helper"
require_relative 'helpers/application_test_helpers'

class ApplicationSystemTestCase < ActionDispatch::SystemTestCase
include ApplicationTestHelpers::Ontologies
include ApplicationTestHelpers::Users
include ApplicationTestHelpers::Users
include ApplicationTestHelpers::Categories
include ApplicationTestHelpers::Groups
include ApplicationTestHelpers::Agents

driven_by :selenium, using: ENV['CI'].present? ? :headless_chrome : :chrome, screen_size: [1400, 1400], options: {
browser: :remote,
url: "http://localhost:4444"
}

def wait_for(selector, tries = 60)
tries.times.each do
puts "waiting for #{selector}"
break if page.has_selector?(selector)
sleep 1
end
end

def wait_for_text(text, tries = 60)
tries.times.each do
sleep 1
puts "waiting for #{text}"
break if page.has_text?(text)
end
assert_text text
end

def login_in_as(user, admin: false)
create_user(user, admin: admin)

visit login_index_url

# Fill in the login form
fill_in 'user_username', with: user.username
fill_in 'user_password', with: user.password

# Click the login button
click_button 'Login'
end

def assert_date(date)
assert_text I18n.l(DateTime.parse(date), format: '%B %-d, %Y')
end

def search_input(selector, value)
within "#{selector}" do
find(".search-inputs .input-field-component").last.set(value)
page.execute_script("document.querySelector('#{selector} > .search-inputs .input-field-component').dispatchEvent(new Event('input'))")
sleep 1
find(".search-inputs .search-content", text: value).click
sleep 1
find("input", text: 'Save').click
end
end

def list_checks(selected_values, all_values = [])
all_values.each do |val|
uncheck val, allow_label_click: true
end

selected_values.each do |val|
check val, allow_label_click: true
end
end

def list_inputs(parent_selector, selector, values, &block)
within parent_selector do
all('.delete').each { |x| x.click }
find('.add-another-object', text: 'Add another').click
last_index = values.size - 1
values.each_with_index do |value, index|
if value.is_a?(Hash)
value.each do |key, val|
all("[name^='#{selector}'][name$='[#{key}]']").last.set(val)
end
else
if block_given?
block.call(selector, value, index)
else
all("[name^='#{selector}']").last.set(value)
end
end
find('.add-another-object', text: 'Add another').click unless index.eql?(last_index)
end
end
end

def tom_select(selector, values, open_to_add: false)

multiple = values.is_a?(Array)

real_select = "[name='#{selector}']"

ts_wrapper_selector = "#{real_select} + div.ts-wrapper"
assert_selector ts_wrapper_selector

# Click on the Tom Select input to open the dropdown
element = find(ts_wrapper_selector)
element.click

return unless page.has_selector?("#{ts_wrapper_selector} > .ts-dropdown")

if multiple
# reset the input to empty
all("#{ts_wrapper_selector} > .ts-control > .item .remove").each do |element|
element.click
end
else
values = Array(values)
end

values.each do |value|
find("#{ts_wrapper_selector} input").set(value) if open_to_add
within "#{ts_wrapper_selector} > .ts-dropdown" do
if page.has_selector?('.option', text: value)
find('.option', text: value).click
elsif open_to_add
find('.create').click
end
end
end

if multiple
find("#{ts_wrapper_selector} input").click
end
end

def date_picker_fill_in(selector, value, index = 0)
page.execute_script("document.querySelectorAll(\"[name^='#{selector}']\")[#{index}].flatpickr().setDate('#{value}')")
end

def agent_search(name)
within(".search-inputs:last-of-type") do
input = find("input[name^='agent']")
agent_id = input[:name].split('agent').last
input.set(name)
sleep 2
links = all('a', text: name)
links_size = links.size
sleep 1
first(:link, name).click
return links_size.eql?(1) ? agent_id : nil
end

end

def agent_fill(agent, parent_id: nil, is_affiliation: false)
id = agent.id ? "/#{agent.id}" : ''
form = all("form[action=\"/agents#{id}\"]").first
within form do
choose "", option: agent.agentType, allow_label_click: true unless is_affiliation
fill_in 'name', with: agent.name

if agent.agentType.eql?('organization')
refute_selector('input[name="email"]')
fill_in 'acronym', with: agent.acronym
fill_in 'homepage', with: agent.homepage
else
refute_selector('input[name="acronym"]')
refute_selector('input[name="homepage"]')
fill_in 'email', with: agent.email
end

identifier = agent.identifiers.first

if agent.agentType.eql?('organization')
fill_in "_identifiers_0_notation", with: identifier['notation']
else
fill_in "_identifiers_1_notation", with: identifier['notation']
end

if is_affiliation || agent.agentType.eql?('organization')
refute_selector ".agents-affiliations"
click_on "Save" if agent.agentType.eql?('organization')
return
end

within '.agents-affiliations' do
all('.delete').each { |x| x.click }
Array(agent.affiliations).each do |aff|
aff = OpenStruct.new(aff)
find('.add-another-object', text: 'Add another').click
agent_id = agent_search(aff.name)
id = parent_id && !parent_id.eql?('NEW_RECORD') ? "#{parent_id}_#{agent_id}" : agent_id
within "turbo-frame[id=\"#{id}\"]" do
agent_fill(aff, is_affiliation: true)
sleep 1
end
end
end
click_on "Save"
end
end
end
35 changes: 35 additions & 0 deletions test/controllers/application_controller_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# frozen_string_literal: true

require 'test_helper'

class ApplicationControllerTest < ActionDispatch::IntegrationTest
test 'should show home page' do
get ''
assert_response :success
end

test 'should show projects page' do
get '/projects'
assert_response :success
end

test 'should show annotator page' do
get '/annotator'
assert_response :success
end

test 'should show recommender page' do
get '/recommender'
assert_response :success
end

test 'should show mapping page' do
get '/mappings'
assert_response :success
end

test 'should show feedback page' do
get '/feedback'
assert_response :success
end
end
11 changes: 11 additions & 0 deletions test/controllers/landscape_controller_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# frozen_string_literal: true

require 'test_helper'

class LandscapeControllerTest < ActionController::TestCase
test 'should get index' do
skip('take too much time')
get :index
assert_response :success
end
end
107 changes: 107 additions & 0 deletions test/controllers/ontologies_controller_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
# frozen_string_literal: true

require 'test_helper'

class OntologiesControllerTest < ActionDispatch::IntegrationTest
ONTOLOGIES = LinkedData::Client::Models::Ontology.all(include: 'acronym')
PAGES = %w[summary classes properties notes mappings schemes collections widgets].freeze

test 'should return all the ontologies' do
get ontologies_path
assert_response :success
end

ONTOLOGIES.each do |ont|
PAGES.each do |page|
test "should get page #{page} of #{ont.acronym} ontology" do
path = "#{ontologies_path}/#{ont.acronym}?p=#{page}"
get path
if response.redirect?
follow_redirect!
end
assert_response :success, "GET #{path} returned #{response.status}"
end
end

test "should open the tree views of #{ont.acronym} ontology" do
paths = [
ajax_classes_treeview_path(ontology: ont.acronym),
"/ontologies/#{ont.acronym}/properties"
]
paths.each do |path|
begin
get path
assert_includes [404, 200], response.status, "GET #{path} returned #{response.status}"
rescue StandardError => e
assert_equal ActiveRecord::RecordNotFound, e.class
end
end

end
end

test 'test get STY in html format' do
get '/ontologies/STY', headers: { 'Accept' => 'text/html' }
assert_response :success
assert_equal 'text/html; charset=utf-8', response.content_type
end

test 'test get STY in json format' do
get '/ontologies/STY', headers: { 'Accept' => 'application/json' }
assert_response :success
assert_equal 'application/json', response.content_type

end

test 'test get STY in xml format' do
get '/ontologies/STY', headers: { 'Accept' => 'application/xml' }
assert_equal 500, response.status # STY has only Turtle
end

test 'test get STY in csv format' do
get '/ontologies/STY', headers: { 'Accept' => 'text/csv' }
assert_response :success
end

test 'test get STY in turtle format' do
get '/ontologies/STY', headers: { 'Accept' => 'text/turtle' }
assert_response :success
end

test 'test get STY in ntriples format' do
get '/ontologies/STY', headers: { 'Accept' => 'application/ntriples' }
assert_response :not_acceptable
end


test 'test get STY resource in html format' do
get '/ontologies/STY/T071', headers: { 'Accept' => 'text/html' }
assert_includes ["http://www.example.com/ontologies/STY?conceptid=http%3A%2F%2Fpurl.bioontology.org%2Fontology%2FSTY%2FT071&p=classes", "http://www.example.com/ontologies/STY?conceptid=http%3A%2F%2Fpurl.lirmm.fr%2Fontology%2FSTY%2FT071&p=classes"], response.location
assert_response :redirect
assert_equal "text/html; charset=utf-8" , response.content_type
end

test 'test get STY resource in json format' do
get '/ontologies/STY/T071', headers: { 'Accept' => 'application/json' }
assert_response :success
assert_equal "application/ld+json; charset=utf-8" , response.content_type
end

test 'test get STY resource in xml format' do
get '/ontologies/STY/T071', headers: { 'Accept' => 'application/xml' }
assert_response :success
assert_equal "application/rdf+xml; charset=utf-8" , response.content_type
end

test 'test get STY resource in ntriples format' do
get '/ontologies/STY/T071', headers: { 'Accept' => 'application/n-triples' }
assert_response :success
assert_equal "application/n-triples; charset=utf-8" , response.content_type
end

test 'test get STY resource in turtle format' do
get '/ontologies/STY/T071', headers: { 'Accept' => 'text/turtle' }
assert_response :success
assert_equal "text/turtle; charset=utf-8" , response.content_type
end
end
Loading
Loading