How can I test if a page object yml file has duplicate keys using rspec?



  • Given a yml file with

    thing1: 'thing1'
    thing2: 'thing2'
    thing3: 'first'
    thing3: 'second'
    thing4: 'thing4'
    thing5: 'thing5'
    thing5: 'thing5'
    thing6: 'thing6'
    

    How can I write an rspec test that fails if there are duplicate keys as shown? Duplicate values is ok.



  • Write a test to see if each key (locator name) occurs more than once

    require 'rspec'
    
    describe 'Page Objects locator yml file' do
      it 'does not have duplicates' do
        file = File.open('locators.yml')
        pairs = []
        keys = []
    
        file.each_line do |line|
          words = line.split(': ')
          pairs << {words[0] => words[1]}
          keys << words[0]
        end 
        file.close
    
        if keys.uniq.count != keys.count
          p "Duplicate Page Object key(s) found: #{keys.uniq.count} unique vs #{keys.count} total"
          p 'dupe keys:'
          dupe_keys = keys.select{|n| keys.count(n) > 1}.uniq
          dupe_keys.each do |key|
            pairs.each do |pair|
              p "#{key.to_sym} : #{pair[key]}" if pair[key]
            end 
          end 
        end 
    
        expect(keys.uniq.count).to (eq keys.count),
          lambda {"Duplicate page object keys found! #{dupe_keys}"}
    
      end 
    end
    

Log in to reply
 

Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2