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

情到浓时终转凉″ 提交于 2019-12-11 15:37:53

问题


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.


回答1:


I just had a little bit of time and was curious because I never tried to perform a double-click in any of my tests before. So I used this page as a test case and ran the following test with both Firefox and Chrome drivers:

package de.scrum_master.stackoverflow

import geb.spock.GebReportingSpec
import org.openqa.selenium.By
import org.openqa.selenium.interactions.Actions

class DoubleClickTest extends GebReportingSpec {
  def "double-click via Geb interaction"() {
    given:
    go "https://artoftesting.com/sampleSiteForSelenium.html"
    def doubleClickButton = $("#dblClkBtn")

    expect:
    withAlert {
      interact {
        doubleClick(doubleClickButton)
      }
    } == "Hi! Art Of Testing, Here!"
  }

  def "double-click via Selenium action"() {
    given:
    go "https://artoftesting.com/sampleSiteForSelenium.html"
    def doubleClickButton = driver.findElement(By.id("dblClkBtn"))
    def doubleClick = new Actions(driver).doubleClick(doubleClickButton).build()

    expect:
    withAlert {
      doubleClick.perform()
    } == "Hi! Art Of Testing, Here!"
  }
}

It works flawlessly, both ways of double-clicking trigger the expected Javascript alert.

I am not even using the latest driver version 2.45 but 2.41 against Chrome 71 64-bit on Windows 10. Besides, I also use bonigarcia's Webdriver Manager. I have no idea what is wrong with your setup. My Selenium version is 3.14.0, a bit newer than yours, Geb 2.2, Spock 1.1-groovy-2.4, Groovy 2.4.7.

Maybe it is a MacOS thing? I cannot verify that. Maybe you just run my test first, then maybe upgrade your Selenium and if that also does not help try to downgrade the Chrome driver in order to find out if the problem could be driver version related.


Update: I upgraded to Chrome driver 2.45, the test still works.




回答2:


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()
    }
  }


来源:https://stackoverflow.com/questions/53677237/selenium-commands-not-working-in-chrome-web-driver-working-with-firefox

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!