Search code examples
seleniumspockgebchrome-web-driverwebdrivermanager-java

Selenium commands not working in Chrome web driver (working with firefox)


I'm writing integration/e2e tests and for some reason any selenium driver commands don't see to be working with chromedriver, but they are working flawlessly with firefox driver and the firefox headless driver.

Commands tried: moveByOffset, and doubleClick

Tried both Geb's Interact method

interact {
 doubleClick(centerClickable)
}

and accessing the webdriver directly:

def driver = browser.getDriver()
Actions action = new Actions(driver)
WebElement element= driver.findElement(By.className("vis-drag-center"))
def doubleclick = action.doubleClick(element).build()
doubleclick.perform()

Both methods work with the firefox driver. Neither work with chrome driver.

GebConfig.groovy file is set up as thus:

import io.github.bonigarcia.wdm.WebDriverManager
import org.openqa.selenium.Dimension
import org.openqa.selenium.chrome.ChromeDriver
import org.openqa.selenium.chrome.ChromeOptions
import org.openqa.selenium.firefox.FirefoxDriver
import org.openqa.selenium.firefox.FirefoxOptions

def chromeWebDriverVersion = '70.0.3538.67'

def driverFirefox = {
  WebDriverManager.firefoxdriver().setup()
  def driver = new FirefoxDriver()
  driver.manage().window().setSize(new Dimension(width, height))
  return driver
}

// ChromeDriver reference: https://sites.google.com/a/chromium.org/chromedriver/
// Download and configure ChromeDriver using https://github.com/bonigarcia/webdrivermanager
def driverChrome = {
  WebDriverManager.chromedriver().version(chromeWebDriverVersion).setup()
  def driver = new ChromeDriver()
  driver.manage().window().setSize(new Dimension(width, height))
  return driver
}

environments {
  firefox {
    driver = driverFirefox
  }
  chrome {
    driver = driverChrome
  }
//driver = driverFirefox
driver = driverChrome

I also tried version 2.43 of chrome.

Additional information:

  • Mac Mojave
  • Selenium v 3.7.0
  • geb v 2.2
  • spockcore v 1.1-groovy-2.4
  • groovy v 2.4.5
  • webdrivermanager v 3.0.0

If anyone is interested, what the test is doing: Selecting a vis.js element by clicking on it. Sleeping for a second (code not included here), then opening/activating it by double clicking it. Or dragging it.

Apart from the selenium actions everything works fine with chromedriver and geb. It's only now that I need the doubleClick and moveByOffset (not move to an element!) that I'm getting issues getting things to work properly

I found a similar question on here, might be the same issue. Maybe not. But there's no solution provided: Selenium Web Driver DragAndDropToOffset in Chrome not working?

Any help is hugely appreciated.


Solution

  • Thank you for your response kriegaex. Your tests work for me as well. This leads me to think there's just some lower-level interaction between differences in how selenium's chromedriver and firefox driver have implemented the doubleclick and dragAndDropBy actions + the way our application responds to commands.

    For any other people observing similar behaviour, I use a work-around where I add an additional action for chromedriver. Perhaps it's nicer to actually find out which KEYDOWN events etc. you should be using and fire those, or perhaps find out why application isn't responding to these events. But I feel like enough time is spent on this already :)

      if (browser.getDriver().toString().contains("chrome")) {
    //      Work-around for chromedriver's double-click implementation
        content.click()
      }
    
      interact {
        doubleClick(content)
      }
    

    And for the dragAndDropBy:

      def drag(Navigator content, int xOff, int yOff) {
        //Work-around: move additional time for when chrome driver is used.
        int timesToMove = browser.getDriver().toString().contains("chrome") ? 2 : 1
    
        interact {
          clickAndHold(content)
          timesToMove.times {
            moveByOffset(xOff, yOff)
          }
          release()
        }
      }