{"record":{"id":"2ab807fe1b1b4a9b","repo":"SeleniumHQ/selenium","slug":"button-number-cannot-be-negative","errorCode":null,"errorMessage":"Button number cannot be negative!","messagePattern":"Button number cannot be negative!","errorType":"exception","errorClass":"ArgumentError","httpStatus":null,"severity":"error","filePath":"rb/lib/selenium/webdriver/common/interactions/pointer_press.rb","lineNumber":69,"sourceCode":"\n        def encode\n          process_opts.merge('type' => type.to_s, 'button' => @button)\n        end\n\n        private\n\n        def assert_source(source)\n          raise TypeError, \"#{source.type} is not a valid input type\" unless source.is_a? PointerInput\n        end\n\n        def assert_button(button)\n          case button\n          when Symbol\n            raise ArgumentError, \"#{button} is not a valid button!\" unless BUTTONS.key? button\n\n            BUTTONS[button]\n          when Integer\n            raise ArgumentError, 'Button number cannot be negative!' if button.negative?\n\n            button\n          else\n            raise TypeError, \"button must be a positive integer or one of #{BUTTONS.keys}, not #{button.class}\"\n          end\n        end\n\n        def assert_direction(direction)\n          raise ArgumentError, \"#{direction.inspect} is not a valid button direction\" unless DIRECTIONS.key? direction\n\n          DIRECTIONS[direction]\n        end\n      end # PointerPress\n    end # Interactions\n  end # WebDriver\nend # Selenium\n","sourceCodeStart":51,"sourceCodeEnd":86,"githubUrl":"https://github.com/SeleniumHQ/selenium/blob/aa36b38e696a0909e973bdf5e2f9031ffe842c4b/rb/lib/selenium/webdriver/common/interactions/pointer_press.rb#L51-L86","documentation":"Raised as ArgumentError by PointerPress#assert_button when button is an Integer but negative (button.negative? is true). Non-negative integers are accepted verbatim as the W3C button code (0=left/primary, 1=middle/auxiliary, 2=right/secondary, 3=back, 4=forward).","triggerScenarios":"Calling pointer_down/pointer_up (or PointerPress.new) with a negative integer such as pointer_down(-1), or computed button = some_index - 1 that can go below zero.","commonSituations":"Off-by-one in computed button indices; passing a sentinel like -1 to mean 'no button'; decrementing an index without a floor check.","solutions":["Pass a non-negative integer (0..4 are the standard W3C codes).","Guard computed values: button = [computed, 0].max before passing.","Prefer named Symbols (:left/:middle/:right) when the button is one of the standard ones."],"exampleFix":"# before\ndriver.action.pointer_down(idx - 1).perform # idx == 0 => ArgumentError: Button number cannot be negative!\n\n# after\ndriver.action.pointer_down([idx - 1, 0].max).perform","handlingStrategy":"validation","validationCode":"button = [button, 0].max if button.is_a?(Integer)\ndriver.action.pointer_down(button).perform","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Never pass a sentinel like -1 for 'no button'; use a named Symbol or 0.","Floor computed button indices at 0 before passing."],"tags":["interactions","pointer-press","argument-validation","ruby"],"backgroundTag":null,"analyzedSha":"aa36b38e696a0909e973bdf5e2f9031ffe842c4b","analyzedAt":"2026-08-14T02:32:32.244Z","schemaVersion":2},"datasetVersion":"2026-08-14T05:17:29.042Z"}