Best Site_prism code snippet using ClassMethods.element_exists
dsl.rb
Source:dsl.rb  
...22      kwargs = find_args.pop23      page.all(*find_args, **kwargs)24    end25    # Call `has_selector?` inside context set on page/section26    def element_exists?(*find_args)27      kwargs = find_args.pop28      page.has_selector?(*find_args, **kwargs)29    end30    # Call `has_no_selector?` inside context set on page/section31    def element_does_not_exist?(*find_args)32      kwargs = find_args.pop33      page.has_no_selector?(*find_args, **kwargs)34    end35    # Prevent users from calling methods with blocks when they shouldn't be.36    #37    # Example (Triggering error):38    #39    #       class MyPage40    #         element :sample, '.css-locator' do41    #           puts "This won't be output"42    #         end43    #       end44    #45    # At runtime this will generate a `SitePrism::UnsupportedBlockError`46    #47    # The only DSL keywords that can use blocks are :section and :iframe48    def raise_if_block(obj, name, has_block, type)49      return unless has_block50      SitePrism.logger.debug("Type passed in: #{type}")51      SitePrism.logger.warn('section / iFrame can only accept blocks.')52      SitePrism.logger.error("#{obj.class}##{name} does not accept blocks")53      raise SitePrism::UnsupportedBlockError54    end55    # Warn users from naming the elements starting with no_56    def warn_if_dsl_collision(obj, name)57      return unless name.to_s.start_with?('no_')58      SitePrism.logger.warn("#{obj.class}##{name} should not start with no_")59      SitePrism::Deprecator.deprecate('Using no_ prefix in DSL definition')60    end61    # Sanitize method called before calling any SitePrism DSL method or62    # meta-programmed method. This ensures that the Capybara query is correct.63    #64    # Accepts any combination of arguments sent at DSL definition or runtime65    # and combines them in such a way that Capybara can operate with them.66    def merge_args(find_args, runtime_args, visibility_args = {})67      find_args = find_args.dup68      runtime_args = runtime_args.dup69      options = visibility_args.dup70      SitePrism.logger.debug("Initial args: #{find_args}, #{runtime_args}.")71      recombine_args(find_args, runtime_args, options)72      return [*find_args, *runtime_args, {}] if options.empty?73      [*find_args, *runtime_args, options]74    end75    # Options re-combiner. This takes the original inputs and combines76    # them such that there is only one hash passed as a final argument77    # to Capybara.78    #79    # If the hash is empty, then the hash is omitted from the payload sent80    # to Capybara, and the find / runtime arguments are sent alone.81    #82    # NB: If the +wait+ key is present in the options hash, even as false or 0, It will83    # be set as the user-supplied value (So user error can be the cause for issues).84    def recombine_args(find_args, runtime_args, options)85      options.merge!(find_args.pop) if find_args.last.is_a? Hash86      options.merge!(runtime_args.pop) if runtime_args.last.is_a? Hash87      options[:wait] = Capybara.default_max_wait_time unless options.key?(:wait)88    end89    # SitePrism::DSL::ClassMethods90    # This exposes all of the DSL definitions users will use when generating91    # their POM classes.92    #93    # Many of these methods will be used in-line to allow users to generate a multitude of94    # methods and locators for finding elements / sections on a page or section of a page95    module ClassMethods96      attr_reader :expected_items97      # Creates an instance of a SitePrism Element - This will create several methods designed to98      # Locate the element -> @return [Capybara::Node::Element]99      # Check the elements presence or non-presence -> @return [Boolean]100      # Wait for the elements to be present or not -> @return [TrueClass, SitePrism::Error]101      # Validate certain properties about the element102      def element(name, *find_args)103        SitePrism::Deprecator.deprecate('Passing a block to :element') if block_given?104        build(:element, name, *find_args) do105          define_method(name) do |*runtime_args, &element_block|106            warn_if_dsl_collision(self, name)107            raise_if_block(self, name, !element_block.nil?, :element)108            _find(*merge_args(find_args, runtime_args))109          end110        end111      end112      # Creates a enumerable instance of a SitePrism Element - This will create several methods designed to113      # Locate the enumerable element -> @return [Capybara::Result]114      # Check the elements presence or non-presence -> @return [Boolean]115      # Wait for the elements to be present or not -> @return [TrueClass, SitePrism::Error]116      # Validate certain properties about the elements117      def elements(name, *find_args)118        SitePrism::Deprecator.deprecate('Passing a block to :elements') if block_given?119        build(:elements, name, *find_args) do120          define_method(name) do |*runtime_args, &element_block|121            warn_if_dsl_collision(self, name)122            raise_if_block(self, name, !element_block.nil?, :elements)123            _all(*merge_args(find_args, runtime_args))124          end125        end126      end127      # Sets the `expected_items` iVar on a class. This property is used in conjunction with128      # `all_there?` to provide a way of granularising the check made to only interrogate a sub-set129      # of DSL defined items130      def expected_elements(*elements)131        @expected_items = elements132      end133      # Creates an instance of a SitePrism Section - This will create several methods designed to134      # Locate the section -> @return [SitePrism::Section]135      # Check the section presence or non-presence -> @return [Boolean]136      # Wait for the section to be present or not -> @return [TrueClass, SitePrism::Error]137      # Validate certain properties about the section138      def section(name, *args, &block)139        section_class, find_args = extract_section_options(args, &block)140        build(:section, name, *find_args) do141          define_method(name) do |*runtime_args, &runtime_block|142            warn_if_dsl_collision(self, name)143            section_element = _find(*merge_args(find_args, runtime_args))144            section_class.new(self, section_element, &runtime_block)145          end146        end147      end148      # Creates an enumerable instance of a SitePrism Section - This will create several methods designed to149      # Locate the sections -> @return [Array]150      # Check the sections presence or non-presence -> @return [Boolean]151      # Wait for the sections to be present or not -> @return [TrueClass, SitePrism::Error]152      # Validate certain properties about the section153      def sections(name, *args, &block)154        section_class, find_args = extract_section_options(args, &block)155        build(:sections, name, *find_args) do156          define_method(name) do |*runtime_args, &element_block|157            raise_if_block(self, name, !element_block.nil?, :sections)158            _all(*merge_args(find_args, runtime_args)).map do |element|159              section_class.new(self, element)160            end161          end162        end163      end164      def iframe(name, klass, *args)165        SitePrism.logger.debug('Block passed into iFrame construct at build time') if block_given?166        element_find_args = deduce_iframe_element_find_args(args)167        scope_find_args = deduce_iframe_scope_find_args(args)168        build(:iframe, name, *element_find_args) do169          define_method(name) do |&block|170            raise MissingBlockError unless block171            within_frame(*scope_find_args) { block.call(klass.new) }172          end173        end174      end175      def mapped_items(legacy: true)176        return old_mapped_items if legacy177        new_mapped_items178      end179      private180      def old_mapped_items181        SitePrism::Deprecator.soft_deprecate(182          '.mapped_items on a class',183          'To allow easier recursion through the items in conjunction with #all_there?',184          '.mapped_items(legacy: false)'185        )186        @old_mapped_items ||= []187      end188      def new_mapped_items189        @new_mapped_items ||= { element: [], elements: [], section: [], sections: [], iframe: [] }190      end191      def build(type, name, *find_args)192        if find_args.empty?193          create_error_method(name)194        else195          map_item(type, name)196          yield197        end198        add_helper_methods(name, *find_args)199      end200      def map_item(type, name)201        old_mapped_items << { type => name }202        new_mapped_items[type] << name.to_sym203      end204      def add_helper_methods(name, *find_args)205        create_existence_checker(name, *find_args)206        create_nonexistence_checker(name, *find_args)207        SitePrism::RspecMatchers.new(name)._create_rspec_existence_matchers if defined?(RSpec)208        create_visibility_waiter(name, *find_args)209        create_invisibility_waiter(name, *find_args)210      end211      def create_helper_method(proposed_method_name, *find_args)212        return create_error_method(proposed_method_name) if find_args.empty?213        yield214      end215      def create_existence_checker(element_name, *find_args)216        method_name = "has_#{element_name}?"217        create_helper_method(method_name, *find_args) do218          define_method(method_name) do |*runtime_args|219            args = merge_args(find_args, runtime_args)220            element_exists?(*args)221          end222        end223      end224      def create_nonexistence_checker(element_name, *find_args)225        method_name = "has_no_#{element_name}?"226        create_helper_method(method_name, *find_args) do227          define_method(method_name) do |*runtime_args|228            args = merge_args(find_args, runtime_args)229            element_does_not_exist?(*args)230          end231        end232      end233      def create_visibility_waiter(element_name, *find_args)234        method_name = "wait_until_#{element_name}_visible"235        create_helper_method(method_name, *find_args) do236          define_method(method_name) do |*runtime_args|237            args = merge_args(find_args, runtime_args, visible: true)238            return true if element_exists?(*args)239            raise SitePrism::ElementVisibilityTimeoutError240          end241        end242      end243      def create_invisibility_waiter(element_name, *find_args)244        method_name = "wait_until_#{element_name}_invisible"245        create_helper_method(method_name, *find_args) do246          define_method(method_name) do |*runtime_args|247            args = merge_args(find_args, runtime_args, visible: true)248            return true if element_does_not_exist?(*args)249            raise SitePrism::ElementInvisibilityTimeoutError250          end251        end252      end...element_container.rb
Source:element_container.rb  
...141        create_helper_method(method_name, *find_args) do142          define_method(method_name) do |*runtime_args|143            wait_time = SitePrism.use_implicit_waits ? max_wait_time : 0144            visibility_args = { wait: wait_time }145            element_exists?(*merge_args(find_args, runtime_args, **visibility_args))146          end147        end148      end149      def create_nonexistence_checker(element_name, *find_args)150        method_name = "has_no_#{element_name}?"151        create_helper_method(method_name, *find_args) do152          define_method(method_name) do |*runtime_args|153            wait_time = SitePrism.use_implicit_waits ? max_wait_time : 0154            visibility_args = { wait: wait_time }155            element_does_not_exist?(156              *merge_args(find_args, runtime_args, **visibility_args)157            )158          end159        end160      end161      def create_waiter(element_name, *find_args)162        method_name = "wait_for_#{element_name}"163        create_helper_method(method_name, *find_args) do164          define_method(method_name) do |timeout = max_wait_time, *runtime_args|165            visibility_args = { wait: timeout }166            result = element_exists?(*merge_args(find_args, runtime_args, **visibility_args))167            raise_wait_for_if_failed(self, element_name.to_s, timeout, !result)168            result169          end170        end171      end172      def create_nonexistence_waiter(element_name, *find_args)173        method_name = "wait_for_no_#{element_name}"174        create_helper_method(method_name, *find_args) do175          define_method(method_name) do |timeout = max_wait_time, *runtime_args|176            visibility_args = { wait: timeout }177            res = element_does_not_exist?(*merge_args(find_args, runtime_args, **visibility_args))178            raise_wait_for_no_if_failed(self, element_name.to_s, timeout, !res)179            res180          end181        end182      end183      def create_visibility_waiter(element_name, *find_args)184        method_name = "wait_until_#{element_name}_visible"185        create_helper_method(method_name, *find_args) do186          define_method(method_name) do |timeout = max_wait_time, *runtime_args|187            visibility_args = { visible: true, wait: timeout }188            args = merge_args(find_args, runtime_args, **visibility_args)189            return true if element_exists?(*args)190            raise SitePrism::TimeOutWaitingForElementVisibility191          end192        end193      end194      def create_invisibility_waiter(element_name, *find_args)195        method_name = "wait_until_#{element_name}_invisible"196        create_helper_method(method_name, *find_args) do197          define_method(method_name) do |timeout = max_wait_time, *runtime_args|198            visibility_args = { visible: true, wait: timeout }199            args = merge_args(find_args, runtime_args, **visibility_args)200            return true if element_does_not_exist?(*args)201            raise SitePrism::TimeOutWaitingForElementInvisibility202          end203        end...element_exists
Using AI Code Generation
1Then(/^I see the button$/) do2  wait_for_element_exists("* id:'button1'")3Then(/^I see the text$/) do4  wait_for_element_exists("* id:'text1'")5Then(/^I see the text field$/) do6  wait_for_element_exists("* id:'editText1'")7Then(/^I see the image$/) do8  wait_for_element_exists("* id:'imageView1'")9Then(/^I see the tab$/) do10  wait_for_element_exists("* id:'tab1'")11Then(/^I see the list$/) do12  wait_for_element_exists("* id:'list1'")13Then(/^I see the check box$/) do14  wait_for_element_exists("* id:'checkBox1'")15Then(/^I see the radio button$/) do16  wait_for_element_exists("* id:'radioButton1'")17Then(/^I see the spinner$/) do18  wait_for_element_exists("* id:'spinner1'")19Then(/^I see the progress bar$/) do20  wait_for_element_exists("* id:'progressBar1'")21Then(/^I see the seek bar$/) do22  wait_for_element_exists("* id:'seekBar1'")23Then(/^I see the toggle button$/) do24  wait_for_element_exists("* id:'toggleButton1'")25Then(/^I see the rating bar$/) do26  wait_for_element_exists("* id:'ratingBar1'")27Then(/^I see the switch$/) do28  wait_for_element_exists("* id:'switch1'")29Then(/^I see the text view$/) do30  wait_for_element_exists("* id:'textView1'")31Then(/^I see the web view$/) do32  wait_for_element_exists("* id:'webView1'")33Then(/^I see the date picker$/) do34  wait_for_element_exists("* id:'datePicker1'")35Then(/^I see the time picker$/) do36  wait_for_element_exists("* id:'timePicker1'")37Then(/^I see the toast$/) do38  wait_for_element_exists("* id:'toast1'")39Then(/^I see the alert dialog$/) do40  wait_for_element_exists("* id:'alertDialog1'")41Then(/^I see the progress dialog$/) do42  wait_for_element_exists("* id:'progressDialog1'")43Then(/^element_exists
Using AI Code Generation
1  def element_exists(element)2Page.element_exists('element')3  def element_exists(element)4  def element_exists(element)5Page.element_exists('element')6  def element_exists(element)7Page.element_exists('element')8  def element_exists(element)9Page.element_exists('element')10  def element_exists(element)11Page.element_exists('element')12  def element_exists(element)13Page.element_exists('element')14  def element_exists(element)15Page.element_exists('element')16  def element_exists(element)element_exists
Using AI Code Generation
1if element_exists('my_element_id')2if element_exists('my_element_id')3if element_exists('my_element_id')4if element_exists('my_element_id')5if element_exists('my_element_id')6if element_exists('my_element_id')7if element_exists('my_element_id')8if element_exists('my_element_id')9if element_exists('my_element_id')element_exists
Using AI Code Generation
1puts ClassMethods.element_exists?(array, element)2puts ClassMethods.new.element_exists?(array, element)3puts ClassMethods.new.element_exists?(array, element)4puts ClassMethods.new.element_exists?(array, element)5puts ClassMethods.new.element_exists?(array, element)6puts ClassMethods.new.element_exists?(array, element)7puts ClassMethods.new.element_exists?(array, element)element_exists
Using AI Code Generation
1def element_exists(element)2def element_exists(element)3def element_exists(element)4def element_exists(element)5def element_exists(element)6def element_exists(element)7def element_exists(element)8def element_exists(element)9def element_exists(element)10def element_exists(element)element_exists
Using AI Code Generation
1if RAutomation::Window.new(:title => "Notepad").exists?2  puts RAutomation::Window.new(:title => "Notepad").text3if RAutomation::Window.new(:title => "Notepad").exists?4  puts RAutomation::Window.new(:title => "Notepad").text5if RAutomation::Window.new(:title => "Notepad").exists?6  puts RAutomation::Window.new(:title => "Notepad").text7if RAutomation::Window.new(:title => "Notepad").exists?8  puts RAutomation::Window.new(:title => "Notepad").textelement_exists
Using AI Code Generation
1Then(/^I see the button$/) do2  wait_for_element_exists("* id:'button1'")3Then(/^I see the text$/) do4  wait_for_element_exists("* id:'text1'")5Then(/^I see the text field$/) do6  wait_for_element_exists("* id:'editText1'")7Then(/^I see the image$/) do8  wait_for_element_exists("* id:'imageView1'")9Then(/^I see the tab$/) do10  wait_for_element_exists("* id:'tab1'")11Then(/^I see the list$/) do12  wait_for_element_exists("* id:'list1'")13Then(/^I see the check box$/) do14  wait_for_element_exists("* id:'checkBox1'")15Then(/^I see the radio button$/) do16  wait_for_element_exists("* id:'radioButton1'")17Then(/^I see the spinner$/) do18  wait_for_element_exists("* id:'spinner1'")19Then(/^I see the progress bar$/) do20  wait_for_element_exists("* id:'progressBar1'")21Then(/^I see the seek bar$/) do22  wait_for_element_exists("* id:'seekBar1'")23Then(/^I see the toggle button$/) do24  wait_for_element_exists("* id:'toggleButton1'")25Then(/^I see the rating bar$/) do26  wait_for_element_exists("* id:'ratingBar1'")27Then(/^I see the switch$/) do28  wait_for_element_exists("* id:'switch1'")29Then(/^I see the text view$/) do30  wait_for_element_exists("* id:'textView1'")31Then(/^I see the web view$/) do32  wait_for_element_exists("* id:'webView1'")33Then(/^I see the date picker$/) do34  wait_for_element_exists("* id:'datePicker1'")35Then(/^I see the time picker$/) do36  wait_for_element_exists("* id:'timePicker1'")37Then(/^I see the toast$/) do38  wait_for_element_exists("* id:'toast1'")39Then(/^I see the alert dialog$/) do40  wait_for_element_exists("* id:'alertDialog1'")41Then(/^I see the progress dialog$/) do42  wait_for_element_exists("* id:'progressDialog1'")43Then(/^element_exists
Using AI Code Generation
1  def element_exists(element)2Page.element_exists('element')3  def element_exists(element)4  def element_exists(element)5Page.element_exists('element')6  def element_exists(element)7Page.element_exists('element')8  def element_exists(element)9Page.element_exists('element')10  def element_exists(element)11Page.element_exists('element')12  def element_exists(element)13Page.element_exists('element')14  def element_exists(element)15Page.element_exists('element')16  def element_exists(element)element_exists
Using AI Code Generation
1if element_exists('my_element_id')2if element_exists('my_element_id')3if element_exists('my_element_id')4if element_exists('my_element_id')5if element_exists('my_element_id')6if element_exists('my_element_id')7if element_exists('my_element_id')8if element_exists('my_element_id')9if element_exists('my_element_id')element_exists
Using AI Code Generation
1def element_exists(element)2def element_exists(element)3def element_exists(element)4def element_exists(element)5def element_exists(element)6def element_exists(element)7def element_exists(element)8def element_exists(element)9def element_exists(element)10def element_exists(element)Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!
