Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cy.get('button').type() only works if button has tabindex specified #2166

Closed
kyleoliveiro opened this issue Jul 18, 2018 · 11 comments · Fixed by #4870
Closed

cy.get('button').type() only works if button has tabindex specified #2166

kyleoliveiro opened this issue Jul 18, 2018 · 11 comments · Fixed by #4870

Comments

@kyleoliveiro
Copy link

Current behavior:

When calling cy.type() on a <button> element, Cypress throws:

CypressError: cy.type() failed because it requires a valid typeable element.

However if the <button> has a tabindex attribute of any value specified, then calling cy.type() will succeed.

Desired behavior:

cy.type() should succeed in any case, since <buttons> are naturally focusable.

Steps to reproduce:

Call cy.type() on <button> (will throw).
Call cy.type() on <button tabindex="-1"> (will succeed).
Call cy.type() on <button tabindex="0"> (will succeed).
Call cy.type() on <button tabindex="1"> (will succeed).

Versions

Cypress 3.0.2

@kuceb
Copy link
Contributor

kuceb commented Jul 18, 2018

Hi @kyleoliveiro,
type is for typing text into the element. If you want to focus the element you should use .focus

@kyleoliveiro
Copy link
Author

kyleoliveiro commented Jul 18, 2018

I know. I'm trying to simulate a keypress on a button with .type(). In my actual code I'm doing .get().focus().type().

@jennifer-shehane
Copy link
Member

Hi @kyleoliveiro, the error should have also given you information about what we consider typeable including:

Cypress considers the 'body', 'textarea', any 'element' with a 'tabindex' or 'contenteditable' attribute, or any 'input' with a 'type' attribute of 'text', 'password', 'email', 'number', 'date', 'week', 'month', 'time', 'datetime', 'datetime-local', 'search', 'url', or 'tel' to be valid typeable elements.

If you want to simulate a specific event on the button, I'd suggest using trigger to trigger that event like:

cy.get('button').trigger('keypress', {
      keyCode: 35,  // whatever code for the key you wanted to press
      which: 35 // whatever which for the key you wanted to press
}) 

Let me know if this works for you.

@jennifer-shehane jennifer-shehane added the stage: awaiting response Potential fix was proposed; awaiting response label Jul 18, 2018
@kyleoliveiro
Copy link
Author

Yes it will work; but are there plans to include a keypress helper? Cypress already has stuff like .click() etc, I assume that .type() is intended as a shorthand for triggering a keypress event.

@kuceb
Copy link
Contributor

kuceb commented Jul 19, 2018

@kyleoliveiro have you tried cy.type('foobar', {force:true}) ?

@GandalfM
Copy link

I tried cy.type('something', {force:true}) - unfortunately the result is the same.

@mendax02
Copy link

mendax02 commented Sep 23, 2018

@jennifer-shehane I am still not able to click the button on my page using the above.
"Cypress considers the 'body', 'textarea', any 'element' with a 'tabindex' or 'contenteditable' attribute, or any 'input' with a 'type' attribute of 'text', 'password', 'email', 'number', 'date', 'week', 'month', 'time', 'datetime', 'datetime-local', 'search', 'url', or 'tel' to be valid typeable elements."

If there is any work around. What if we can introduce the attribute while it is running and then we try to simulate the key press event???

I am not able to click on the button no matter what I try :(

@kuceb
Copy link
Contributor

kuceb commented Oct 10, 2018

@mendax02 if you're trying to click a button, you should refer to our docs on cy.click

@jennifer-shehane jennifer-shehane removed stage: awaiting response Potential fix was proposed; awaiting response labels Jan 16, 2019
@kuceb kuceb mentioned this issue Aug 30, 2019
17 tasks
@kuceb kuceb reopened this Aug 30, 2019
@kuceb
Copy link
Contributor

kuceb commented Aug 30, 2019

Re-opened this since we want to allow typing into any element that is focusable. This is fixed in #4870

@cypress-bot cypress-bot bot added stage: ready for work The issue is reproducible and in scope and removed stage: work in progress labels Aug 30, 2019
@cypress-bot cypress-bot bot added stage: work in progress stage: needs review The PR code is done & tested, needs review and removed stage: ready for work The issue is reproducible and in scope stage: work in progress labels Sep 24, 2019
@cypress-bot cypress-bot bot added stage: work in progress and removed stage: needs review The PR code is done & tested, needs review labels Oct 22, 2019
kuceb added a commit that referenced this issue Oct 22, 2019
brian-mann added a commit that referenced this issue Oct 23, 2019
* cleanup type_spec, allow unused-vars for args

* fix missing return

* rename mouse/keyboard

* apply changes on this branch with rename

* temp 07/18/19 [skip ci] type_spec minor changes

* temp 07/19/19 [skip ci]

* temp 07/22/19 [skip ci]

* temp 07/22/19 [skip ci]

* remove accidental spec, use beforeEach visit in click_spec

* add tests for cy.trigger changes

* re-run build

* extract simulated type from native events

* most type_spec passing until tab

* add typedefs

* temp 08/08/19 [skip ci]

* fix type_spec, click_spec

* fix console table event table logging

* fix spec utils

* temp 08/12/19 [skip ci]

* large changes to selection.ts, working type_spec, eslint changes

* fix elements util, cleanup debugging code

* fix linting rules

* fix type_spec in electron by using plain Event contructor

* fix invalid clicking-into-iframe spec

* address review, cleanup

* use webpack-preprocessor (required for typescript files)

* fix invalid connectors_spec

* chore(lint): fix linting, since changed rules

* fix not borrowing property getter for maxlength

* temp 09/25/19 [skip ci]

* add test for clicking checkbox, cleanup click_spec

* document mouse click and mouse move algo, adjust mouse move, add test for recursive movement

* cleanup / formatting

* scope the getCommandLogWithText to the active runnable to prevent leakage

* cleanup tests, make values static so tests pass consistently

- use attach listener alias APIs
- remove magic number constants
- fix some incoherent html / script tag stuff

* begin implementation for returning fromAutWindow

- restore original fromWindow algorithm
- begin writing fromAutWindow for calculating red hitbox coordinates
which are layered on the AUT

* move describe block with the others

* refactor actions to DRY up duplicated logic between click/dblclick/rightclick

- tighten up the mouse.js method names, name them consistently

* remove comment

* fix iframe coords and test

* temp 10/06/19 [skip ci]

* rename fromDocCoords->fromWindowCoords, fix type_spec ux test

* fix after merge from mouse-fixes branch / develop

* temp 10/08/19 [skip ci]

* Revert "temp 10/08/19 [skip ci]"

This reverts commit 92b73f6.

* remove only, fix contenteditable test

* add test for datetime-local inputs

* add webpack devdep

* fix debugging code

* fix force:true on hidden input

* up timeout for test in ci

* tighten up method, remove unnecessary arg

* add debug logic for retries to console.error() non cypress errors

* renamed fromWindow -> fromElWindow, and fromViewport -> fromElViewport

* handle calculating the fromElWindow coordinates inline

* extract out function for clarity, receive consistent arg names

* cleanup, add lots of comments

* fix display specific test

* fix type follow focus when redirect on focus

* cleanup new follow focus test

* upgrade eslint-plugin-dev to 5.0.0

* more [lint fixes]

* use typescript in driver

* cleanup minor

* remove unneeded file

* mild refactor webpack configs

* fix elements.ts

* fix isTextLike signature change

* fix isType check, fix type_spec

* actually use webpack-preprocessor

* fix isType domUtil

* fix connectors_spec

* cleanup connectors_spec

* address feedback- cleanup, add comments, refac

* answer: no, need index.js

* cleanup after driver typescript PR

* fix keyboard getActiveModifiers

* fix(driver:keyboard): fix typing negative numbers with existing text selection

* remove debugging code, cleanup

* link fixed issues in tests, actually fix one issue

* cleanup remove dead code

* remove unused helper

* add test for #2166

* pay no attention to the code and implementation changing... add tests and note for changing focus into date input

* mark fix for issue #3316

* mark fix for issue #3722

* rename elements.isType -> elements.isInputType for clarity

* restructure keyboard a bit, don't export default, rework static class properties to regular exports

- attach $Mouse + $Keyboard to Cypress

* tighten up

* coffeescript fixes, consistent regexp const naming

* capitalize interface, tighten up several modifier functions

* misc cleanup and tightening up

* rework impl to use stubs instead of numbers

* some more cleanup

* fix logic with when function is called


Co-authored-by: Brian Mann <brian.mann86@gmail.com>
Co-authored-by: Jennifer Shehane <jennifer@cypress.io>
@cypress-bot
Copy link
Contributor

cypress-bot bot commented Oct 23, 2019

The code for this is done in cypress-io/cypress#4870, but has yet to be released.
We'll update this issue and reference the changelog when it's released.

@cypress-bot
Copy link
Contributor

cypress-bot bot commented Oct 23, 2019

Released in 3.5.0.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
5 participants