Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
add rubocop
  • Loading branch information
titusfortner committed Aug 15, 2018
1 parent 3b46023 commit 94d5523
Show file tree
Hide file tree
Showing 11 changed files with 121 additions and 78 deletions.
36 changes: 36 additions & 0 deletions .rubocop.yml
@@ -0,0 +1,36 @@
# Cop supports --auto-correct.
# Configuration parameters: EnforcedStyle.
# SupportedStyles: when_needed, always, never
Style/FrozenStringLiteralComment:
Enabled: false

# Cop supports --auto-correct.
# Configuration parameters: EnforcedStyle, EnforcedStyleForEmptyBraces.
# SupportedStyles: space, no_space, compact
# SupportedStylesForEmptyBraces: space, no_space
Layout/SpaceInsideHashLiteralBraces:
EnforcedStyle: no_space

Style/CommentedKeyword:
Enabled: false

# Configuration parameters: AllowHeredoc, AllowURI, URISchemes, IgnoreCopDirectives, IgnoredPatterns.
# URISchemes: http, https
Metrics/LineLength:
Max: 120

# Configuration parameters: CountComments, ExcludedMethods.
# ExcludedMethods: refine
Metrics/BlockLength:
Exclude:
- 'spec/**/*'

Style/Documentation:
Enabled: false

# Configuration parameters: CountComments.
Metrics/MethodLength:
Max: 13

Metrics/CyclomaticComplexity:
Max: 10
7 changes: 6 additions & 1 deletion .travis.yml
@@ -1,8 +1,13 @@
sudo: false
language: ruby
rvm:
- 2.4.1
- 2.3.7
before_install:
- export DISPLAY=:99
- sh -e /etc/init.d/xvfb start
- export PATH=~/.webdrivers:$PATH
script: bundle exec rake $RAKE_TASK
env:
- RAKE_TASK=spec
- RAKE_TASK=watirspec:run
- RAKE_TASK=rubocop
4 changes: 2 additions & 2 deletions Gemfile
@@ -1,6 +1,6 @@
source "https://rubygems.org"
source 'https://rubygems.org'

git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
git_source(:github) { |repo_name| "https://github.com/#{repo_name}" }

# Specify your gem's dependencies in watigiri.gemspec
gemspec
9 changes: 7 additions & 2 deletions Rakefile
@@ -1,9 +1,14 @@
require "bundler/gem_tasks"
require "rspec/core/rake_task"
require 'bundler/gem_tasks'
require 'rspec/core/rake_task'

RSpec::Core::RakeTask.new(:spec)

require 'watirspec/rake_tasks'
WatirSpec::RakeTasks.new

task default: %i[spec watirspec:run]

require 'rubocop/rake_task'
RuboCop::RakeTask.new(:rubocop) do |t|
t.options = ['--display-cop-names']
end
2 changes: 0 additions & 2 deletions lib/extensions/watir/browser.rb
@@ -1,5 +1,4 @@
module Watir

class Browser
attr_reader :doc

Expand Down Expand Up @@ -33,6 +32,5 @@ def text!
def locator_namespace
@locator_namespace ||= Watigiri::Locators
end

end # Browser
end # Watir
5 changes: 1 addition & 4 deletions lib/extensions/watir/element.rb
@@ -1,7 +1,5 @@
module Watir

class Element

attr_reader :doc

#
Expand Down Expand Up @@ -34,10 +32,9 @@ def text!
text.strip
end

alias_method :el_stale?, :stale?
alias el_stale? stale?
def stale?
@doc.nil? && el_stale?
end

end # Element
end # Watir
5 changes: 1 addition & 4 deletions lib/extensions/watir/iframe.rb
Expand Up @@ -12,15 +12,12 @@ def text!
end

class FramedDriver

private

alias_method :watir_switch!, :switch!
alias watir_switch! switch!
def switch!
@browser.doc = nil
watir_switch!
end

end # FramedDriver

end # Watir
53 changes: 30 additions & 23 deletions lib/watigiri/locators/element/locator.rb
Expand Up @@ -16,16 +16,13 @@ module Locators
module LocatorHelpers
def locate
@nokogiri = @selector.delete(:nokogiri)
@regex = regex?
return super unless @nokogiri || regex?

return super unless @nokogiri || @regex
@query_scope.doc ||= if @query_scope.is_a?(Watir::Browser)
Nokogiri::HTML(@query_scope.html).tap { |d| d.css('script').remove }
else
# This should be using `#fragment`, but can't because of
# https://github.com/sparklemotion/nokogiri/issues/572
Nokogiri::HTML(@query_scope.inner_html)
end
# :inner_html should be using `#fragment`, but can't because of
# https://github.com/sparklemotion/nokogiri/issues/572
method = @query_scope.is_a?(Watir::Browser) ? :html : :inner_html

@query_scope.doc ||= Nokogiri::HTML(@query_scope.send(method)).tap { |d| d.css('script').remove }

element = using_watir(:first)
return if element.nil?
Expand All @@ -42,26 +39,26 @@ def locate_element(how, what, _driver_scope = @query_scope.wd)

# "how" can only be :css or :xpath
def locate_elements(how, what, _scope = @query_scope.wd)
return super unless @nokogiri || @regex
return super unless @nokogiri || regex?

@query_scope.doc.send(how, what).map do |el|
Watigiri::Element.new element: el, selector: {how => what}
end
end

def fetch_value(element, how)
return super unless @nokogiri || @regex
return super if element.is_a?(Selenium::WebDriver::Element)
element = element.element if element.is_a?(Watigiri::Element)
element = update_element(element)
return if element.nil?

case how
when :text
element.inner_text
when :tag_name
element.name.to_s.downcase
when :href
(href = element.attribute('href')) && href.to_s.strip
element.attribute('href')&.to_s
else
element.attribute(how.to_s.tr("_", "-")).to_s
element.attribute(how.to_s.tr('_', '-')).to_s
end
end

Expand All @@ -73,11 +70,21 @@ def nokogiri_to_selenium(element)
end

def regex?
return @regex unless @regex.nil?
return false unless (@selector.keys & %i[adjacent visible label text visible_text]).empty?
@selector.values.any? { |v| v.is_a?(Regexp) }
@regex = @selector.values.any? { |v| v.is_a?(Regexp) }
end
end

def update_element(element)
if !(@nokogiri || regex?) || element.is_a?(Selenium::WebDriver::Element)
nil
elsif element.is_a?(Watigiri::Element)
element.element
else
element
end
end
end

class Element
class Locator < Watir::Locators::Element::Locator
Expand Down Expand Up @@ -134,15 +141,15 @@ def matches_selector?(element, rx_selector)

tag_name = element.tag_name

[:text, :value, :label].each do |key|
if rx_selector.key?(key)
correct_key = tag_name == 'input' ? :value : :text
rx_selector[correct_key] = rx_selector.delete(key)
end
%i[text value label].each do |key|
next unless rx_selector.key?(key)
correct_key = tag_name == 'input' ? :value : :text
rx_selector[correct_key] = rx_selector.delete(key)
end

rx_selector.all? do |how, what|
what === fetch_value(element, how)
val = fetch_value(element, how)
what == val || val =~ /#{what}/
end
end

Expand Down
39 changes: 19 additions & 20 deletions spec/watigiri_spec.rb
@@ -1,71 +1,70 @@
require "watirspec_helper"
require 'watirspec_helper'

describe Watigiri do
require 'watirspec_helper'

describe '#text!' do
before do
browser.goto(WatirSpec.url_for("non_control_elements.html"))
browser.goto(WatirSpec.url_for('non_control_elements.html'))
end

it "locates with page_source driver call" do
it 'locates with page_source driver call' do
expect(browser.driver).to_not receive(:find_element)
expect(browser.driver).to_not receive(:find_elements)

expect(browser.li(id: "non_link_1").text!).to eq 'Non-link 1'
expect(browser.li(id: 'non_link_1').text!).to eq 'Non-link 1'
expect(browser.li(id: /non_link_1/).text!).to eq 'Non-link 1'
expect(browser.li(title: "This is not a link!").text!).to eq 'Non-link 1'
expect(browser.li(title: 'This is not a link!').text!).to eq 'Non-link 1'
expect(browser.li(title: /This is not a link!/).text!).to eq 'Non-link 1'
expect(browser.li(class: "nonlink").text!).to eq 'Non-link 1'
expect(browser.li(class: 'nonlink').text!).to eq 'Non-link 1'
expect(browser.li(class: /nonlink/).text!).to eq 'Non-link 1'
expect(browser.li(id: /non_link/, index: 1).text!).to eq 'Non-link 2'
expect(browser.li(xpath: "//li[@id='non_link_1']").text!).to eq 'Non-link 1'
expect(browser.li(css: "li#non_link_1").text!).to eq 'Non-link 1'
expect(browser.li(css: 'li#non_link_1').text!).to eq 'Non-link 1'
end

it "locates with inner html driver call" do
it 'locates with inner html driver call' do
div = browser.div(id: 'header').tap(&:exist?)
expect(browser.driver).to_not receive(:find_element)
expect(browser.driver).to_not receive(:find_elements)

expect(div.li(id: "non_link_1").text!).to eq 'Non-link 1'
expect(div.li(id: 'non_link_1').text!).to eq 'Non-link 1'
expect(div.li(id: /non_link_1/).text!).to eq 'Non-link 1'
expect(div.li(title: "This is not a link!").text!).to eq 'Non-link 1'
expect(div.li(title: 'This is not a link!').text!).to eq 'Non-link 1'
expect(div.li(title: /This is not a link!/).text!).to eq 'Non-link 1'
expect(div.li(class: "nonlink").text!).to eq 'Non-link 1'
expect(div.li(class: 'nonlink').text!).to eq 'Non-link 1'
expect(div.li(class: /nonlink/).text!).to eq 'Non-link 1'
expect(div.li(id: /non_link/, index: 1).text!).to eq 'Non-link 2'
expect(div.li(xpath: "//li[@id='non_link_1']").text!).to eq 'Non-link 1'
expect(div.li(css: "li#non_link_1").text!).to eq 'Non-link 1'
expect(div.li(css: 'li#non_link_1').text!).to eq 'Non-link 1'
end

it "reloads the cached document from after hooks" do
it 'reloads the cached document from after hooks' do
expect(browser.driver).to receive(:page_source).exactly(3).times.and_return(browser.driver.page_source)

expect(browser.li(id: "non_link_1").text!).to eq 'Non-link 1'
expect(browser.li(id: 'non_link_1').text!).to eq 'Non-link 1'
browser.refresh
expect(browser.li(id: /non_link_1/).text!).to eq 'Non-link 1'
browser.li.click
expect(browser.li(title: "This is not a link!").text!).to eq 'Non-link 1'
expect(browser.li(title: 'This is not a link!').text!).to eq 'Non-link 1'
browser.li.click
end

it "locates by sub-element" do
it 'locates by sub-element' do
navbar = browser.ul(id: 'navbar').tap(&:exist?)
expect(browser.driver).to_not receive(:find_element)
expect(browser.driver).to_not receive(:find_elements)
expect(navbar.li(id: "non_link_1").text!).to eq "Non-link 1"
expect(navbar.li(id: 'non_link_1').text!).to eq 'Non-link 1'
end

describe "#exists?" do
it "finds Watir::Element when selector uses regular expression" do
describe '#exists?' do
it 'finds Watir::Element when selector uses regular expression' do
expect_any_instance_of(Selenium::WebDriver::Element).to_not receive(:attribute)

li = browser.li(id: /link/, index: 1)
expect(li).to exist
expect(li).to be_a(Watir::Element)
end
end

end
end
2 changes: 1 addition & 1 deletion spec/watirspec_helper.rb
Expand Up @@ -10,7 +10,7 @@
watirspec.browser_args = [:chrome, opts]

watirspec.guard_proc = lambda do |watirspec_guards|
watigiri_guards = %i(chrome watigiri relaxed_locate)
watigiri_guards = %i[chrome watigiri relaxed_locate]
watirspec_guards.any? { |guard| watigiri_guards.include?(guard) }
end
end
Expand Down
37 changes: 18 additions & 19 deletions watigiri.gemspec
@@ -1,34 +1,33 @@
# coding: utf-8
lib = File.expand_path("../lib", __FILE__)
lib = File.expand_path('lib', __dir__)
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)

Gem::Specification.new do |spec|
spec.name = "watigiri"
spec.version = "0.4.0"
spec.authors = ["Titus Fortner"]
spec.email = ["titusfortner@gmail.com"]
spec.name = 'watigiri'
spec.version = '0.4.0'
spec.authors = ['Titus Fortner']
spec.email = ['titusfortner@gmail.com']

spec.summary = %q{Nokogiri locator engine for Watir}
spec.description = <<-DESCRIPTION_MESSAGE
By default Watir locates elements with Selenium; this gem will replace Selenium calls
with Nokogiri calls where designated.
spec.summary = 'Nokogiri locator engine for Watir'
spec.description = <<~DESCRIPTION_MESSAGE
By default Watir locates elements with Selenium; this gem will replace Selenium calls
with Nokogiri calls where designated.
DESCRIPTION_MESSAGE
spec.homepage = "http://github.com/titusfortner/watigiri"
spec.license = "MIT"
spec.homepage = 'http://github.com/titusfortner/watigiri'
spec.license = 'MIT'

spec.files = `git ls-files -z`.split("\x0").reject do |f|
f.match(%r{^(test|spec|features)/})
end
spec.bindir = "exe"
spec.bindir = 'exe'
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
spec.require_paths = ["lib"]
spec.require_paths = ['lib']

spec.add_development_dependency "bundler", "~> 1.15"
spec.add_development_dependency "rake", "~> 10.0"
spec.add_development_dependency "rspec", "~> 3.0"
spec.add_development_dependency 'bundler', '~> 1.15'
spec.add_development_dependency 'rake', '~> 10.0'
spec.add_development_dependency 'rspec', '~> 3.0'
spec.add_development_dependency 'rubocop'
spec.add_development_dependency 'webdrivers', '~> 3.3', '>= 3.3.3'

spec.add_runtime_dependency "watir", "~> 6.12"
spec.add_runtime_dependency "nokogiri"
spec.add_runtime_dependency 'nokogiri'
spec.add_runtime_dependency 'watir', '~> 6.12'
end

0 comments on commit 94d5523

Please sign in to comment.