Before do
	Capybara.register_driver :selenium_chrome do |app|   
    	Capybara::Selenium::Driver.new(app, :browser => :chrome)
	end
	Capybara.current_driver = :selenium_chrome
  Capybara.default_selector = :xpath
end

Given /^I am on "([^"]*)"$/ do |page|
  visit(page)
end

When /^click on "([^"]*)"$/ do |text|
  find("a", :text => text).click
end

Given /^fill form entry "([^"]*)" with "([^"]*)"$/ do |name, value|
  fill_in name, with: value
end

Given /^click on "(.*?)" Button$/ do |button|
  click_button button
end

Given /^click on "(.*?)" Button if exists$/ do |button|
  begin
    click_button button
  rescue

  end
end

Given /^click on input with src like "(.*?)"$/ do |src|
  find(:xpath, "//input[contains(@src,'#{src}')]").click
end

Given /^click on "(.*?)" Text$/ do |text|
  click_on text
end

Given /^click on first "(.*?)" Text$/ do |text|
  all(:xpath, "//a[contains(text(),'#{text}')]").each do |element|
    begin
      element.click
      break
    rescue

    end
  end
end

Given /^click on "(.*?)" Text if exists$/ do |text|
  begin
    click_on text
  rescue

  end
end

Given /^click on "(.*?)" element with text "(.*?)"$/ do |element, text|
  element = find(:xpath, "//#{element}[contains(text(),'#{text}')]")
  element.click if element
end

Given /^Wait for "(.*?)" seconds$/ do |seconds|
  sleep seconds.to_i
end

Given /^choose option "(.*?)" with "(.*?)"$/ do |select, text|
  select text, from: select
end

Given /^check "(.*?)"$/ do |check_name|
  check check_name
end

Given /^click on id "(.*?)"$/ do |id|
  begin
    find(:xpath, "//*[contains(@id,'#{id}')]").click
  rescue
    #Kernel.puts "failed"
  end
end


Then /^I should see the text "(.*?)"$/ do |text|
  page.should have_content(text)
end

Then /^I should not see the text "(.*?)"$/ do |text|
  page.should_not have_content(text)
end

Given /^Destroy all$/ do

  all(:xpath, "//td/a[contains(@href,'/issues/')]").each do |element|
    element.click
    
    all(:xpath, "//a[contains(text(),'Excluir')]").each do |link|
      retries = 2;
      begin
        link.clic
        page.driver.browser.switch_to.alert.accept  
        click_on "Tarefas"
      rescue
        retries = retries-1
        retry unless retries==0
      end
    end
    
    
  end
  #page.should_not have_content("Destroy")
  #sleep 30
end


