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

clicking on an openlayers canvas doesn't trigger any events #1016

Closed
BasieP opened this issue Dec 6, 2017 · 10 comments · Fixed by #3030
Closed

clicking on an openlayers canvas doesn't trigger any events #1016

BasieP opened this issue Dec 6, 2017 · 10 comments · Fixed by #3030
Assignees

Comments

@BasieP
Copy link

BasieP commented Dec 6, 2017

Hi,

When i have a test like this:

it('should draw little circle on a map', function () {
	cy.visit('http://openlayers.org/en/latest/examples/draw-features.html');
	cy.get('canvas').click();
	cy.get('canvas').click(100, 100);
});

it doesn't trigger any events (the openlayers example should draw dots on the map)
Perhaps this is because the mousemove and mouseover events are not triggered... i don't know the inner workings of openlayers on this... (although i do know they do stuff a little different)

the problem we now have i can't test any geo applications based on openlayers (which are most of them)

@brian-mann
Copy link
Member

brian-mann commented Dec 6, 2017

The simplest thing for you to do here is simply fire the underlying events you want / care about

cy.get('canvas').trigger('someEvent', props)

You'll have to understand what the canvas is wired up to. You can use the dev tools to see which DOM event listeners its bound to.

@jennifer-shehane jennifer-shehane added the stage: needs information Not enough info to reproduce the issue label Dec 7, 2017
@hansagames
Copy link

Any news here? As I want to test my canvas based app and I need to trigger clicks on it bit right now, no clicks are registered on canvas

@BasieP
Copy link
Author

BasieP commented Feb 20, 2018 via email

@jennifer-shehane jennifer-shehane removed the stage: needs information Not enough info to reproduce the issue label Feb 20, 2018
@Renaud009
Copy link

Renaud009 commented May 10, 2018

cy.visit('http://openlayers.org/en/latest/examples/draw-features.html')
cy.get('canvas')
            .trigger('pointerdown')
            .trigger('pointerup')

@Renaud009
Copy link

Renaud009 commented May 10, 2018

What you also can do is intercept ol3 global object and your map instance to capture informations, calculate things like layers features or trigger some other events

cy.window().then((window) => {
      cy.simulateOpenLayersEvent (window.ol, window.map, 'pointerdown', 50, 50)
      cy.simulateOpenLayersEvent (window.ol, window.map, 'pointerup', 50, 50)
})

My command file:

Cypress.Commands.add('simulateOpenLayersEvent', (ol, map, type, x, y, opt_shiftKey = undefined) => {
    var viewport = map.getViewport();
    let position = viewport.getBoundingClientRect();
    cy.log(`left: ${position.left}, top: ${position.top}, width: ${position.width}, height: ${position.height}`)
    cy.get('canvas').trigger(type, {
        clientX: position.left + x + (position.width / 2),
        clientY: position.top + y + (position.height / 2),
    })
})

Example code that makes me able to count how many polygons (selected regions) are loaded in a layer (won't work on draw-feature.htm tho...l)

cy.window().then((window) => {
      cy.hasOpenLayersSelectedRegion(window.ol, window.cyCurrentMap, 1) // 1 selected region attended
})

Cypress.Commands.add('hasOpenLayersSelectedRegion', (ol, map, count) => {
    console.log("Current ol3 global object: %o", ol)
    console.log("Current OLComponent.map instance: %o", map)
  
    let layers = map.getLayers().getArray()
    cy.wrap(layers).each ((layer)=> {
        if (layer instanceof ol.layer.Vector && layer.get('nameId') && layer.get('nameId') === LAYER_SELECTED_REGIONS_NAME) {
          cy.log('Found Vector Layer LAYER_SELECTED_REGIONS')
          cy.wrap(layer.getSource().getFeatures().length).should('eq', count)
        } 
    });
  })

@tascioglu
Copy link

tascioglu commented May 30, 2018

Hi @Renaud009 . This (cy.get('canvas').trigger('pointerdown').trigger('pointerup')) worked for me fine. Thanx for sharing this.. Now I can draw on maps with openlayer. One more question with it. I need to draw/trigger an extent on the map with ctrl-key. I tried something like below, but that isn't correctly I figured out.. :)

     cy.get('canvas')
        .trigger('{ctrl}mousedown', 250, 250)
        .trigger('{ctrl}mousemove', 500, 500)
        .trigger('{ctrl}mouseup', 500, 500)

Can you help me with this one?

@tascioglu
Copy link

@Renaud009 any ideas..?

@kuceb kuceb self-assigned this Dec 17, 2018
@kuceb
Copy link
Contributor

kuceb commented Dec 17, 2018

Currently we do not fire pointerdown or pointerup events during cy.click(), this will be fixed with #2956

@cypress-bot cypress-bot bot added stage: needs review The PR code is done & tested, needs review stage: work in progress and removed stage: needs review The PR code is done & tested, needs review labels Aug 8, 2019
@cypress-bot cypress-bot bot added stage: needs review The PR code is done & tested, needs review and removed stage: work in progress labels Sep 6, 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: needs review The PR code is done & tested, needs review stage: work in progress labels Sep 25, 2019
@cypress-bot cypress-bot bot added stage: needs review The PR code is done & tested, needs review and removed stage: work in progress labels Oct 7, 2019
@cypress-bot cypress-bot bot added stage: pending release and removed stage: needs review The PR code is done & tested, needs review labels Oct 11, 2019
@cypress-bot
Copy link
Contributor

cypress-bot bot commented Oct 11, 2019

The code for this is done in cypress-io/cypress#3030, 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 subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
7 participants