Behat / Mink / Selenium testing broken (macOS)

2020-03-31 07:01发布

问题:

I have been using Selenium for quite some time for automated browser testing. I never did figure out how to get it to run in anything other than Firefox. Well now FireFox will launch but won't get pulled to the front and it just sits there and does nothing. I have a feeling it's because FireFox updated itself.

I'm running macOS Sierra. I have installed and enabled the WebDriver 2.48 extension into Safari.

I have downloaded and tried both selenium-server-standalone-2.53.1.jar and selenium-server-standalone-3.0.0-beta4.jar but I get different results from each. With 2.53.1 and Firefox 49, it launches Firefox but as mentioned it just sits there and does nothing, it doesn't even load a URL. Eventually it times out with the following error:

Could not open connection: Unable to connect to host 127.0.0.1 on port 7055 after 45000 ms. Firefox console output: 08198ce6fd}","syncGUID":"mT5F_TRS51sP","location":"app-global","version":"49.0.1","type":"theme","internalName":"classic/1.0","updateURL":null,"updateKey":null,"optionsURL":null,"optionsType":null,"aboutURL":null,"icons":{"32":"icon.png","48":"icon.png"},"iconURL":null,"icon64URL":null,"defaultLocale":{"name":"Default","description":"The default theme.","creator":"Mozilla","homepageURL":null,"contributors":["Mozilla Contributors"]},"visible":true,"active":true,"userDisabled":false,"appDisabled":false,"descriptor":"/Applications/Firefox.app/Contents/Resources/browser/extensions/{972ce4c6-7e08-4474-a285-3208198ce6fd}.xpi","installDate":1475253044000,"updateDate":1475253044000,"applyBackgroundUpdates":1,"skinnable":true,"size":7140,"sourceURI":null,"releaseNotesURI":null,"softDisabled":false,"foreignInstall":false,"hasBinaryComponents":false,"strictCompatibility":true,"locales":[],"targetApplications":[{"id":"{ec8030f7-c20a-464f-9b0e-13a3a9e97384}","minVersion":"49.0.1","maxVersion":"49.0.1"}],"targetPlatforms":[],"seen":true} 1475260611275 addons.xpi DEBUG getModTime: Recursive scan of {972ce4c6-7e08-4474-a285-3208198ce6fd} 1475260611276 DeferredSave.extensions.json DEBUG Save changes 1475260611277 addons.xpi DEBUG Updating database with changes to installed add-ons 1475260611277 addons.xpi-utils DEBUG Updating add-on states 1475260611277 addons.xpi-utils DEBUG Writing add-ons list 1475260611279 addons.xpi DEBUG Registering manifest for /Applications/Firefox.app/Contents/Resources/browser/features/e10srollout@mozilla.org.xpi 1475260611279 addons.xpi DEBUG Calling bootstrap method startup on e10srollout@mozilla.org version 1.2 1475260611279 addons.xpi DEBUG Registering manifest for /Applications/Firefox.app/Contents/Resources/browser/features/firefox@getpocket.com.xpi 1475260611280 addons.xpi DEBUG Calling bootstrap method startup on firefox@getpocket.com version 1.0.4 1475260611280 addons.xpi DEBUG Registering manifest for /Applications/Firefox.app/Contents/Resources/browser/features/webcompat@mozilla.org.xpi 1475260611281 addons.xpi DEBUG Calling bootstrap method startup on webcompat@mozilla.org version 1.0 1475260611284 addons.manager DEBUG Registering shutdown blocker for XPIProvider 1475260611284 addons.manager DEBUG Provider finished startup: XPIProvider 1475260611284 addons.manager DEBUG Starting provider: LightweightThemeManager 1475260611284 addons.manager DEBUG Registering shutdown blocker for LightweightThemeManager 1475260611285 addons.manager DEBUG Provider finished startup: LightweightThemeManager 1475260611285 addons.manager DEBUG Starting provider: GMPProvider 1475260611294 addons.manager DEBUG Registering shutdown blocker for GMPProvider 1475260611294 addons.manager DEBUG Provider finished startup: GMPProvider 1475260611294 addons.manager DEBUG Starting provider: PluginProvider 1475260611295 addons.manager DEBUG Registering shutdown blocker for PluginProvider 1475260611295 addons.manager DEBUG Provider finished startup: PluginProvider 1475260611295 addons.manager DEBUG Completed startup sequence 1475260611717 addons.manager DEBUG Starting provider: 1475260611717 addons.manager DEBUG Registering shutdown blocker for 1475260611718 addons.manager DEBUG Provider finished startup: 1475260611721 DeferredSave.extensions.json DEBUG Starting write 1475260611881 addons.repository DEBUG No addons.json found. 1475260611881 DeferredSave.addons.json DEBUG Save changes 1475260611884 DeferredSave.addons.json DEBUG Starting timer 1475260611917 addons.manager DEBUG Starting provider: PreviousExperimentProvider 1475260611918 addons.manager DEBUG Registering shutdown blocker for PreviousExperimentProvider 1475260611919 addons.manager DEBUG Provider finished startup: PreviousExperimentProvider 1475260611924 DeferredSave.extensions.json DEBUG Write succeeded 1475260611924 addons.xpi-utils DEBUG XPI Database saved, setting schema version preference to 17 1475260611942 DeferredSave.addons.json DEBUG Starting write 1475260611957 DeferredSave.addons.json DEBUG Write succeeded (Behat\Mink\Exception\DriverException)

With 3.0.0-beta-4 it doesn't even launch Firefox, it just throws the following error:

Could not open connection: The path to the driver executable must be set by the webdriver.gecko.driver system property; for more information, see https://github.com/mozilla/geckodriver. The latest version can be downloaded from https://github.com/mozilla/geckodriver/releases (Behat\Mink\Exception\DriverException)

If I switch my behat.yml config to use Safari (browser_name: safari), with 2.53.1 it launches an instance of Safari which also does not come to the front or do anything (like Firefox) and I get the following error:

Unknown command: {"id":"f3xi7h2t2u16","name":"mouseMoveTo","parameters":{"element":":wdc:1475260954832"}} (WARNING: The server did not provide any stacktrace information) Command duration or timeout: 5 milliseconds Build info: version: '2.53.1', revision: 'a36b8b1', time: '2016-06-30 17:37:03' System info: host: 'flightbyte.local', ip: '127.0.0.1', os.name: 'Mac OS X', os.arch: 'x86_64', os.version: '10.12', java.version: '1.8.0_101' Driver info: org.openqa.selenium.safari.SafariDriver Capabilities [{browserName=safari, takesScreenshot=true, javascriptEnabled=true, version=10.0, cssSelectorsEnabled=true, platform=MAC, secureSsl=true}] Session ID: null (WebDriver\Exception\UnknownError)

With 3.0.0-beta4 I get ...

Could not open connection: null (WARNING: The server did not provide any stacktrace information) Command duration or timeout: 0 milliseconds Build info: version: '3.0.0-beta4', revision: '3169782', time: '2016-09-29 10:29:23 -0700' System info: host: 'myapp.local', ip: '127.0.0.1', os.name: 'Mac OS X', os.arch: 'x86_64', os.version: '10.12', java.version: '1.8.0_101' Driver info: driver.version: SafariDriver (Behat\Mink\Exception\DriverException)

In all the cases above, I simply open the standalone server .jar file then run my behat tests as I have always done. Today I think I figured out how to get the chomedriver work so when attempting to use it with chrome I launch it this way ...

java -jar -Dwebdriver.chrome.driver='/Users/me/bin/selenium/drivers/chromedriver' /Users/me/bin/selenium/selenium-server-standalone-2.53.1.jar

... and I change the browser_name key in my behat.yml to googlechrome. When I run it this way I get the following error...

Could not open connection: The best matching driver provider org.openqa.selenium.safari.SafariDriver can't create a new driver instance for Capabilities [{browser=firefox, name=Behat feature suite, browserName=googlechrome, ignoreZoomSetting=false, tags=[myapp.local, PHP 7.0.10]}] Build info: version: '2.53.1', revision: 'a36b8b1', time: '2016-06-30 17:37:03' System info: host: 'myapp.local', ip: '127.0.0.1', os.name: 'Mac OS X', os.arch: 'x86_64', os.version: '10.12', java.version: '1.8.0_101' Driver info: driver.version: unknown (Behat\Mink\Exception\DriverException)

And with 3.0.0-beta4 I get this:

Could not open connection: The best matching driver provider org.openqa.selenium.safari.SafariDriver can't create a new driver instance for Capabilities [{browser=firefox, name=Behat feature suite, browserName=googlechrome, ignoreZoomSetting=false, tags=[flightbyte.local, PHP 7.0.10]}] Build info: version: '3.0.0-beta4', revision: '3169782', time: '2016-09-29 10:29:23 -0700' System info: host: 'flightbyte.local', ip: '127.0.0.1', os.name: 'Mac OS X', os.arch: 'x86_64', os.version: '10.12', java.version: '1.8.0_101' Driver info: driver.version: unknown (Behat\Mink\Exception\DriverException)

My behat.yml starts off like this:

default:
  extensions:
    Laracasts\Behat:
      env_path: .env.testing
    Behat\MinkExtension:
      default_session: laravel
      base_url: http://myapp.local:8080/
      browser_name: googlechrome (this is where I switch browsers)
      laravel: ~
      selenium2: ~

My composer.json contains this:

"require-dev": {
    "fzaninotto/faker": "~1.4",
    "mockery/mockery": "0.9.*",
    "phpunit/phpunit": "~5.4",
    "phpspec/phpspec": "~2.1",
    "behat/behat": "^3.2",
    "behat/mink": "^1.7",
    "behat/mink-extension": "^2.2",
    "laracasts/behat-laravel-extension": "dev-master",
    "behat/mink-selenium2-driver": "^1.3@dev",
    "symfony/dom-crawler": "~3.0",
    "symfony/css-selector": "~3.0"
  },

I did try adjusting up the versions of the behat and mink stuff to what I saw on packagist, followed by composer update.

I just need to get these tests working again. I really don't care which browser it is, would prefer it not be Firefox but that's what I have lived with up until it all broke.

Any help would be greatly appreciated!

回答1:

Try with chromedriver, from my experience it works better.

Add chromedriver and selenium server in the same folder and start selenium with:

java -Dwebdriver.chrome.driver=chromedriver -jar selenium-server_file

In behat.yml set chrome as browser name.



回答2:

Selenium does not work with firefox without a driver anymore. Since Firefox 47 you need to download the marionette/geckodriver so you can run selenium automation tests.

If you need to run Chrome and Firefox(Until v46) in Selenium 2.53.X (You dont need to run geckodriver):

java -jar selenium-server-standalone-2.53.1.jar -role node -host XXX.XXX.XXX.XXX -hub http://XXX.XXX.XXX.XXX:4444/grid/register -nodeTimeout 1200 maxSession 4 -browser browserName=chrome,maxInstances=4,platform=WINDOWS, -Dwebdriver.chrome.driver=chromedriver.exe -browser browserName=firefox,maxInstances=4,platform=WINDOWS

And the behat for chrome

Behat\MinkExtension:
        base_url: "https://www.yourpage.com"
        sessions:
            chrome_mac:
                selenium2:
                    browser: "chrome"
                    wd_host: http://XXX.XXX.XXX.XXX:4444/wd/hub
                    capabilities:
                        platform: MAC
                        browser: chrome
                        version: ""

And the behat for firefox

Behat\MinkExtension:
        base_url: "https://www.yourpage.com"
        sessions:
            firefox_mac:
                selenium2:
                    browser: "firefox"
                    wd_host: http://XXX.XXX.XXX.XXX:4444/wd/hub
                    capabilities: { "platform": "MAC", "browser": "firefox", "version": "" }

If you need to run Chrome and Firefox(After v47) you will need Selenium 3-betaN (You NEED to run geckodriver): https://github.com/mozilla/geckodriver/releases

java -Dwebdriver.chrome.driver=chromedriver.exe -Dwebdriver.gecko.driver=geckodriver.exe -jar selenium-server-standalone-3.0.0-beta4.jar