From 7c735aa077084072b78bab02d82e4a81a8bb4508 Mon Sep 17 00:00:00 2001 From: Alex Rodionov Date: Mon, 11 Jan 2016 15:14:41 +0600 Subject: [PATCH] Initial commit --- .gitignore | 3 + .gitmodules | 3 + .rspec | 2 + .travis.yml | 3 + CODE_OF_CONDUCT.md | 13 + Gemfile | 3 + LICENSE.txt | 22 ++ README.md | 60 ++++ Rakefile | 6 + lib/watizzle.rb | 18 ++ lib/watizzle/locators/element/locator.rb | 108 +++++++ .../locators/element/selector_builder.rb | 25 ++ .../element/selector_builder/sizzle.rb | 74 +++++ lib/watizzle/sizzle/loader.js.erb | 5 + lib/watizzle/sizzle/regexp.pseudo.js | 26 ++ lib/watizzle/version.rb | 3 + spec/implementation.rb | 16 + spec/spec_helper.rb | 4 + spec/support/locator_spec_helper.rb | 56 ++++ spec/watirspec | 1 + spec/watizzle/element_locator_spec.rb | 306 ++++++++++++++++++ watizzle.gemspec | 27 ++ 22 files changed, 784 insertions(+) create mode 100644 .gitignore create mode 100644 .gitmodules create mode 100644 .rspec create mode 100644 .travis.yml create mode 100644 CODE_OF_CONDUCT.md create mode 100644 Gemfile create mode 100644 LICENSE.txt create mode 100644 README.md create mode 100644 Rakefile create mode 100644 lib/watizzle.rb create mode 100644 lib/watizzle/locators/element/locator.rb create mode 100644 lib/watizzle/locators/element/selector_builder.rb create mode 100644 lib/watizzle/locators/element/selector_builder/sizzle.rb create mode 100644 lib/watizzle/sizzle/loader.js.erb create mode 100644 lib/watizzle/sizzle/regexp.pseudo.js create mode 100644 lib/watizzle/version.rb create mode 100644 spec/implementation.rb create mode 100644 spec/spec_helper.rb create mode 100644 spec/support/locator_spec_helper.rb create mode 160000 spec/watirspec create mode 100644 spec/watizzle/element_locator_spec.rb create mode 100644 watizzle.gemspec diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..580c07d --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +.bundle/ +Gemfile.lock +pkg/ diff --git a/.gitmodules b/.gitmodules new file mode 100644 index 0000000..98206b6 --- /dev/null +++ b/.gitmodules @@ -0,0 +1,3 @@ +[submodule "spec/watirspec"] + path = spec/watirspec + url = git://github.com/watir/watirspec.git diff --git a/.rspec b/.rspec new file mode 100644 index 0000000..3687797 --- /dev/null +++ b/.rspec @@ -0,0 +1,2 @@ +--require spec_helper +--color diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..a36c7ff --- /dev/null +++ b/.travis.yml @@ -0,0 +1,3 @@ +language: ruby +rvm: + - 2.2.3 diff --git a/CODE_OF_CONDUCT.md b/CODE_OF_CONDUCT.md new file mode 100644 index 0000000..ce9bee7 --- /dev/null +++ b/CODE_OF_CONDUCT.md @@ -0,0 +1,13 @@ +# Contributor Code of Conduct + +As contributors and maintainers of this project, we pledge to respect all people who contribute through reporting issues, posting feature requests, updating documentation, submitting pull requests or patches, and other activities. + +We are committed to making participation in this project a harassment-free experience for everyone, regardless of level of experience, gender, gender identity and expression, sexual orientation, disability, personal appearance, body size, race, ethnicity, age, or religion. + +Examples of unacceptable behavior by participants include the use of sexual language or imagery, derogatory comments or personal attacks, trolling, public or private harassment, insults, or other unprofessional conduct. + +Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct. Project maintainers who do not follow the Code of Conduct may be removed from the project team. + +Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by opening an issue or contacting one or more of the project maintainers. + +This Code of Conduct is adapted from the [Contributor Covenant](http://contributor-covenant.org), version 1.0.0, available at [http://contributor-covenant.org/version/1/0/0/](http://contributor-covenant.org/version/1/0/0/) diff --git a/Gemfile b/Gemfile new file mode 100644 index 0000000..fa75df1 --- /dev/null +++ b/Gemfile @@ -0,0 +1,3 @@ +source 'https://rubygems.org' + +gemspec diff --git a/LICENSE.txt b/LICENSE.txt new file mode 100644 index 0000000..bf0c9af --- /dev/null +++ b/LICENSE.txt @@ -0,0 +1,22 @@ +Copyright (c) 2016 Alex Rodionov + +MIT License + +Permission is hereby granted, free of charge, to any person obtaining +a copy of this software and associated documentation files (the +"Software"), to deal in the Software without restriction, including +without limitation the rights to use, copy, modify, merge, publish, +distribute, sublicense, and/or sell copies of the Software, and to +permit persons to whom the Software is furnished to do so, subject to +the following conditions: + +The above copyright notice and this permission notice shall be +included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND +NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE +LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION +OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION +WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. diff --git a/README.md b/README.md new file mode 100644 index 0000000..7932034 --- /dev/null +++ b/README.md @@ -0,0 +1,60 @@ +# Watizzle + +[Sizzle](http://sizzlejs.com)-based locator engine for [watir-webdriver](https://github.com/watir/watir-webdriver). + +## Installation + +Add this line to your application's Gemfile: + +```ruby +gem 'watizzle' +``` + +And then execute: + +```bash +$ bundle +``` + +Or install it yourself as: + +```bash +$ gem install watizzle +``` + +## Usage + +Require after watir-webdriver, the rest should just work. + +```ruby +require 'watir-webdriver' +require 'watizzle' +``` + +## Development + +To install this gem onto your local machine, run `bundle exec rake install`. +To release a new version, update the version number in `version.rb`, +and then run `bundle exec rake release`, which will create a git tag for the version, +push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org). + +## Specs + +Watizzle uses [watirspec](https://github.com/watir/watirspec) for testing, so +you should first fetch it: + +```bash +$ git submodule init && git submodule update +``` + +Now, you can run all specs: + +```bash +$ bundle exec rake spec +``` + +## Contributing + +Bug reports and pull requests are welcome on GitHub at https://github.com/p0deje/watizzle. +This project is intended to be a safe, welcoming space for collaboration, +and contributors are expected to adhere to the [Contributor Covenant](contributor-covenant.org) code of conduct. diff --git a/Rakefile b/Rakefile new file mode 100644 index 0000000..4c774a2 --- /dev/null +++ b/Rakefile @@ -0,0 +1,6 @@ +require 'bundler/gem_tasks' +require 'rspec/core/rake_task' + +RSpec::Core::RakeTask.new(:spec) + +task default: :spec diff --git a/lib/watizzle.rb b/lib/watizzle.rb new file mode 100644 index 0000000..8151a45 --- /dev/null +++ b/lib/watizzle.rb @@ -0,0 +1,18 @@ +require 'watir-webdriver' + +require 'watizzle/locators/element/locator' +require 'watizzle/locators/element/selector_builder' +require 'watizzle/locators/element/selector_builder/sizzle' +require 'watizzle/version' + +# monkey patch watir-webdriver locators +begin + # silence constant defined warnings + old_verbose, $VERBOSE = $VERBOSE, nil + + Watir::Element::Locator = Watizzle::Element::Locator + Watir::Element::SelectorBuilder = Watizzle::Element::SelectorBuilder + Watizzle::Element::Validator = Watir::Element::Validator +ensure + $VERBOSE = old_verbose +end diff --git a/lib/watizzle/locators/element/locator.rb b/lib/watizzle/locators/element/locator.rb new file mode 100644 index 0000000..715cff4 --- /dev/null +++ b/lib/watizzle/locators/element/locator.rb @@ -0,0 +1,108 @@ +require 'erb' + +module Watizzle + class Element + class Locator < Watir::Element::Locator + SIZZLE_LOADER_PATH = File.expand_path('../../../sizzle/loader.js.erb', __FILE__) + + def initialize(*) + super + inject_sizzle + end + + def locate + e = by_id and return e # short-circuit if :id is given + element = find_first_by_multiple + element_validator.validate(element, @selector) if element + rescue Selenium::WebDriver::Error::NoSuchElementError, Selenium::WebDriver::Error::StaleElementReferenceError + nil + end + + def locate_all + find_all_by_multiple + end + + private + + def by_id + return unless id = @selector[:id] and id.is_a? String + + selector = @selector.dup + selector.delete(:id) + + tag_name = selector.delete(:tag_name) + return unless selector.empty? # multiple attributes + + element = retrieve_element(sizzle_locator("##{id}")) + return if element && tag_name && !element_validator.validate(element, selector) + + element + end + + def find_first_by_multiple + selector = selector_builder.normalized_selector + how, what = selector_builder.build(selector) + + if how == :sizzle + retrieve_element(sizzle_locator(what)) + else + super + end + end + + def find_all_by_multiple + selector = selector_builder.normalized_selector + + if selector.key? :index + raise ArgumentError, "can't locate all elements by :index" + end + + how, what = selector_builder.build(selector) + + if how == :sizzle + retrieve_elements(sizzle_locator(what)) + else + super + end + end + + def inject_sizzle + loader = ERB.new(File.read(SIZZLE_LOADER_PATH)) + execute_script(loader.result) + end + + def retrieve_element(locator) + retrieve_elements("#{locator}[0]") + end + + def retrieve_elements(locator) + args = [locator] + args << @parent.wd if locate_inside_parent? + execute_script(*args) + end + + def execute_script(*args) + browser = case @parent + when Watir::IFrame + # TODO: we should not do that + @parent.tap(&:switch_to!) + when Watir::Element + @parent.browser + else + @parent + end + browser.driver.execute_script(*args) + end + + def sizzle_locator(selector) + loc = "return Sizzle('#{selector}'" + loc << ', arguments[0]' if locate_inside_parent? + loc << ')' + end + + def locate_inside_parent? + @parent.is_a?(Watir::Element) && !@parent.is_a?(Watir::IFrame) + end + end + end +end diff --git a/lib/watizzle/locators/element/selector_builder.rb b/lib/watizzle/locators/element/selector_builder.rb new file mode 100644 index 0000000..9ea00d4 --- /dev/null +++ b/lib/watizzle/locators/element/selector_builder.rb @@ -0,0 +1,25 @@ +module Watizzle + class Element + class SelectorBuilder < Watir::Element::SelectorBuilder + def build(selector) + given_xpath_or_css(selector.dup) || build_sizzle_selector(selector) + end + + private + + def given_xpath_or_css(selector) + # index should not be present for given_xpath_or_css + selector.delete(:index) + super + end + + def build_sizzle_selector(selectors) + sizzle_builder.build(selectors) + end + + def sizzle_builder + @sizzle_builder ||= Sizzle.new + end + end + end +end diff --git a/lib/watizzle/locators/element/selector_builder/sizzle.rb b/lib/watizzle/locators/element/selector_builder/sizzle.rb new file mode 100644 index 0000000..6405e8f --- /dev/null +++ b/lib/watizzle/locators/element/selector_builder/sizzle.rb @@ -0,0 +1,74 @@ +module Watizzle + class Element + class SelectorBuilder + class Sizzle + def build(selectors) + if selectors.empty? + sizzle = '*' + else + sizzle = '' + sizzle << (selectors.delete(:tag_name) || '') + end + + klass = selectors.delete(:class) + if klass + if klass.is_a?(String) + if klass.include? ' ' + sizzle << %([class="#{escape_quotes(klass)}"]) + else + sizzle << ".#{klass}" + end + else + sizzle << %(:regexp(class, #{klass.inspect})) + end + end + + href = selectors.delete(:href) + if href + if href.is_a?(String) + sizzle << %([href~="#{escape_quotes(href)}"]) + else + sizzle << %(:regexp(href, #{href.inspect})) + end + end + + text = selectors.delete(:text) + if text + if text.is_a?(String) + sizzle << %(:contains("#{escape_quotes(text)}")) + else + sizzle << %(:regexp(text, #{text.inspect})) + end + end + + index = selectors.delete(:index) + selectors.each do |key, value| + key = key.to_s.tr("_", "-") + + case value + when String, Fixnum + sizzle << %([#{key}="#{escape_quotes(value)}"]) + when Regexp + sizzle << %(:regexp(#{key}, #{value.inspect})) + end + end + + if index + sizzle << %(:eq(#{index})) + end + + p sizzle: sizzle, selectors: selectors if $DEBUG + + [:sizzle, sizzle] + end + + private + + def escape_quotes(str) + # OMG, what's going on here? + str.to_s.gsub('"', '\\\\\\\\\"') + end + end + end + end +end diff --git a/lib/watizzle/sizzle/loader.js.erb b/lib/watizzle/sizzle/loader.js.erb new file mode 100644 index 0000000..6329406 --- /dev/null +++ b/lib/watizzle/sizzle/loader.js.erb @@ -0,0 +1,5 @@ +if (typeof Sizzle == 'undefined') { + <%= File.read(File.expand_path('vendor/sizzle.min.js')) %> +} + +<%= File.read(File.expand_path('lib/watizzle/sizzle/regexp.pseudo.js')) %> diff --git a/lib/watizzle/sizzle/regexp.pseudo.js b/lib/watizzle/sizzle/regexp.pseudo.js new file mode 100644 index 0000000..f4ad46f --- /dev/null +++ b/lib/watizzle/sizzle/regexp.pseudo.js @@ -0,0 +1,26 @@ +if (typeof Sizzle.selectors.pseudos.regexp == 'undefined') { + Sizzle.selectors.pseudos.regexp = + Sizzle.selectors.createPseudo(function(selector) { + var selectors = selector.match(/^([^,]+), (.+)$/); + var attr = selectors[1]; + var flags = selectors[2].replace(/.*\/([gimy]*)$/, '$1'); + var pattern = selectors[2].replace(new RegExp('^/(.*?)/' + flags + '$'), '$1'); + var regexp = new RegExp(pattern, flags); + return function(el) { + switch (attr) { + case "text": + var value = el.textContent; + break; + case "href": + var value = el.getAttribute('href'); + if (value) { + value = value.replace(/^\s+|\s+$/g, ''); // strip spaces + } + break; + default: + var value = el.getAttribute(attr); + } + return regexp.test(value); + } + }); +} diff --git a/lib/watizzle/version.rb b/lib/watizzle/version.rb new file mode 100644 index 0000000..27370fe --- /dev/null +++ b/lib/watizzle/version.rb @@ -0,0 +1,3 @@ +module Watizzle + VERSION = '0.0.1' +end diff --git a/spec/implementation.rb b/spec/implementation.rb new file mode 100644 index 0000000..a27c13a --- /dev/null +++ b/spec/implementation.rb @@ -0,0 +1,16 @@ +include Watir + +WatirSpec.implementation do |watirspec| + watirspec.name = :watizzle + watirspec.browser_class = Watir::Browser + watirspec.browser_args = [:firefox, {}] + watirspec.guard_proc = lambda do |args| + args.any? do |arg| + [ + :webdriver, + :firefox, + %i[webdriver firefox] + ].include?(arg) + end + end +end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb new file mode 100644 index 0000000..e9c84eb --- /dev/null +++ b/spec/spec_helper.rb @@ -0,0 +1,4 @@ +require 'pry' +require 'watizzle' + +require 'support/locator_spec_helper' diff --git a/spec/support/locator_spec_helper.rb b/spec/support/locator_spec_helper.rb new file mode 100644 index 0000000..579550c --- /dev/null +++ b/spec/support/locator_spec_helper.rb @@ -0,0 +1,56 @@ +require 'active_support' + +module LocatorSpecHelper + def browser + @browser ||= double(Watir::Browser, driver: driver) + end + + def driver + @driver ||= double(Selenium::WebDriver::Driver) + allow(@driver).to receive(:execute_script) + + @driver + end + + def locator(selector, attrs) + attrs ||= Watir::HTMLElement.attributes + element_validator = Watizzle::Element::Validator.new + selector_builder = Watizzle::Element::SelectorBuilder.new(driver, selector, attrs) + Watizzle::Element::Locator.new(browser, selector, selector_builder, element_validator) + end + + def expect_one(selector) + expect(driver).to receive(:execute_script).with(%{return Sizzle('#{selector}')[0]}).and_return(element) + end + + def expect_all(selector) + expect(driver).to receive(:execute_script).with(%{return Sizzle('#{selector}')}).and_return(elements) + end + + def locate_one(selector, attrs = nil) + locator(ordered_hash(selector), attrs).locate + end + + def locate_all(selector, attrs = nil) + locator(ordered_hash(selector), attrs).locate_all + end + + def element + double(Watir::Element).as_null_object + end + + def elements + double(Watir::ElementCollection).as_null_object + end + + def ordered_hash(selector) + case selector + when Hash + selector + when Array + ActiveSupport::OrderedHash[*selector] + else + raise ArgumentError, "couldn't create hash for #{selector.inspect}" + end + end +end diff --git a/spec/watirspec b/spec/watirspec new file mode 160000 index 0000000..60fec63 --- /dev/null +++ b/spec/watirspec @@ -0,0 +1 @@ +Subproject commit 60fec637859552372e4a5a215ac23318ba66827c diff --git a/spec/watizzle/element_locator_spec.rb b/spec/watizzle/element_locator_spec.rb new file mode 100644 index 0000000..bafc74b --- /dev/null +++ b/spec/watizzle/element_locator_spec.rb @@ -0,0 +1,306 @@ +describe Watizzle::Element::Locator do + include LocatorSpecHelper + + describe "finds a single element" do + describe 'with class' do + it 'finds by class name' do + expect_one '.foo' + locate_one class: 'foo' + end + + it 'finds by class name with spaces' do + expect_one '[class="foo bar"]' + locate_one class: 'foo bar' + end + end + + describe 'with id' do + it 'finds by identifier' do + expect_one '#foo' + locate_one id: 'foo' + end + + xit 'finds by identifier with spaces' do + expect_one '[id="foo bar"]' + locate_one id: 'foo bar' + end + end + + describe 'with tag name' do + it 'finds by tag name' do + expect_one 'foo' + locate_one tag_name: 'foo' + end + end + + describe "with selectors not supported by webdriver" do + it "handles selector with tag name and a single attribute" do + expect_one 'div[title="foo"]' + locate_one tag_name: "div", title: "foo" + end + + it "handles selector with no tag name and and a single attribute" do + expect_one '[title="foo"]' + locate_one title: "foo" + end + + it "handles single quotes in the attribute string" do + expect_one %{[title="foo and 'bar'"]} + locate_one title: "foo and 'bar'" + end + + it "handles selector with tag name and multiple attributes" do + expect_one 'div[title="foo"][dir="bar"]' + locate_one [:tag_name, "div", + :title , "foo", + :dir , 'bar'] + end + + it "handles selector with no tag name and multiple attributes" do + expect_one '[dir="foo"][title="bar"]' + locate_one [:dir, "foo", + :title, "bar"] + end + end + + describe "with special cased selectors" do + it "normalizes space for :text" do + expect_one 'div:contains("foo")' + locate_one tag_name: "div", text: "foo" + end + + it "translates :caption to :text" do + expect_one 'div:contains("foo")' + locate_one tag_name: "div", caption: "foo" + end + + it "translates :class_name to :class" do + expect_one "div.foo" + locate_one tag_name: "div", class_name: "foo" + end + + it "handles data-* attributes" do + expect_one 'div[data-name="foo"]' + locate_one tag_name: "div", data_name: "foo" + end + + it "handles aria-* attributes" do + expect_one 'div[aria-label="foo"]' + locate_one tag_name: "div", aria_label: "foo" + end + + it "normalizes space for the :href attribute" do + expect_one 'a[href~="foo"]' + + selector = { + tag_name: "a", + href: "foo" + } + locate_one selector, Watir::Anchor.attributes + end + + xit "wraps :type attribute with translate() for upper case values" do + translated_type = "translate(@type,'ABCDEFGHIJKLMNOPQRSTUVWXYZ','abcdefghijklmnopqrstuvwxyz')" + expect_one :xpath, ".//input[#{translated_type}='file']" + + selector = [ + :tag_name, "input", + :type , "file", + ] + + locate_one selector, Watir::Input.attributes + end + + xit "uses the corresponding