0

So I want to run my tests in Chrome instead of Firefox (because Chrome offers mobile emulation capability) when using Selenium driver with Behat's Mink extension.

I'm running Selenium stand alone on a testing server, and running tests on a local machine.

So after running Behat tests, a Chrome session is created but is not used, instead, another parallel Firefox session also get created and gets used to run the test scenarios.

I tried that also in an environment with graphical display abilities, so a new Chrome window opens but only data;; is displayed in the address bar, then afterwards a Firefox window opens and tests are run there.

I'm running the latest version of Chrome (52.0.2743.82), Selenium standalone server (2.53.1 - Java version) and Chromedriver (2.22.397932)

My behat.yml contains the following:

default:
    extensions:
        Behat\MinkExtension:
            browser_name: chrome
            base_url: "<url to website>"
            default_session: selenium_chrome_session
            goutte: ~
            sessions:
                selenium_chrome_session:
                    selenium2:
                        browser: chrome
                        wd_host: "http://<testing server host>:4444/wd/hub"
                        capabilities:
                            extra_capabilities:
                                chromeOptions:
                                    args:
                                        - "--start-maximized"
                                        - "--test_type"

After looking at Selenium standalone server's logfile, I get the following output:

15:44:47.677 INFO [45] org.openqa.selenium.remote.server.DriverServlet - Executing: [new session: Capabilities [{chrome.switches=[--no-sandbox], browser=chrome, name=Behat feature suite, browserName=chrome, chrome.extensions=[], ignoreZoomSetting=false, chromeOptions={args=[--no-sandbox], extensions=[]}, version=, tags=[PHP 5.6.21-1~dotdeb+7.1]}]])
15:44:47.678 INFO [113] org.openqa.selenium.remote.server.DefaultDriverProvider - Creating a new session for Capabilities [{chrome.switches=[--no-sandbox], browser=chrome, name=Behat feature suite, browserName=chrome, chrome.extensions=[], ignoreZoomSetting=false, chromeOptions={args=[--no-sandbox], extensions=[]}, version=, tags=[PHP 5.6.21-1~dotdeb+7.1]}]
15:44:48.021 INFO [45] org.openqa.selenium.remote.server.DriverServlet - Done: [new session: Capabilities [{chrome.switches=[--no-sandbox], browser=chrome, name=Behat feature suite, browserName=chrome, chrome.extensions=[], ignoreZoomSetting=false, chromeOptions={args=[--no-sandbox], extensions=[]}, version=, tags=[PHP 5.6.21-1~dotdeb+7.1]}]]
15:44:48.035 INFO [45] org.openqa.selenium.remote.server.DriverServlet - Executing: [new session: Capabilities [{deviceType=tablet, selenium-version=2.31.0, browserVersion=9, browser=firefox, name=Behat Test, browserName=firefox, deviceOrientation=portrait, version=9, platform=ANY}]])
15:44:48.036 INFO [120] org.openqa.selenium.remote.server.FirefoxDriverProvider - Creating a new session for Capabilities [{deviceType=tablet, selenium-version=2.31.0, browserVersion=9, browser=firefox, name=Behat Test, browserName=firefox, deviceOrientation=portrait, version=9, platform=ANY}]

As you can see, Chrome session is successfully created, and then a new Firefox session is created afterwards and is used to conduct tests.

Shivox
  • 13
  • 1
  • 4
  • Why do you use sessions, do you have setup for multiple sessions? – lauda Aug 03 '16 at 14:42
  • Yes, the idea is to have a mobile session and a desktop session. – Shivox Aug 03 '16 at 14:54
  • Do you have any custom code related to the driver in FeatureContext? have you tried to set different profiles for mobile and desktop? Also try to remove browser_name an see what happens. – lauda Aug 03 '16 at 15:03
  • I tried setting different profiles and also tried without browser_name. Could be due to custom code related to driver in my context class but I noticed one interesting thing though: I started Selenium server with the flag `-forcedBrowserMode chrome` and it didn't help. – Shivox Aug 03 '16 at 15:51
  • Check your Feature context, maybe you added some code that starts a new session with new browser. Also try a simple profile without using sessions and see if is working.Try these on local machine to see if working and after you can run on the testing server and check what is different there. – lauda Aug 04 '16 at 08:03
  • Hmm, indeed it was because of some custom code in my FeatureContext, thanks for the hint, I spent two days debugging, and would've spent more without your suggestion. – Shivox Aug 04 '16 at 10:11
  • Any chance that you could probably write a tutorial on this subject @Shivox? – scruffycoder86 Aug 17 '16 at 12:21
  • @LuyandaSiko I'll try to do that when I get a little bit of free time. – Shivox Aug 26 '16 at 13:57
  • @Shivox yes please do. i'm seeing a similar issue. – Sirex Dec 04 '16 at 20:36

3 Answers3

1

You can pass which browser you want to use to selenium standalone server.

Here you are an example:

java -jar ./bin/selenium-server-standalone-2.53.1.jar -Dwebdriver.chrome.driver="./bin/chromedriver"

Notice you may be using a different selenenium standalone server (the jar file) and a different Chrome bin path (-Dwebdriver.chrome.driver). Also ensure you have Chrome installed if you are testing your web pages in a headless server.

Samuel Vicent
  • 991
  • 10
  • 16
0

If the issues is not from the yml setup then you might have some custom code in the FeatureContext class that overrides your desired session and starts a new one.

lauda
  • 4,153
  • 2
  • 14
  • 28
0
java -Dwebdriver.chrome.driver="C:\bin\chromedriver_win32\chromedriver.exe" -jar selenium-server-standalone-3.3.1.jar 

and also change in setup and setUpBeforeClass method

public static function setUpBeforeClass() {
        if (null === self::$mink) {
//            $app = require_once('../PATH_TO_YOUR/app.php');
//            $app['debug'] = true;
//            $app['session.test'] = true;
//            $app['exception_handler']->disable();

            self::$mink = new Mink(array(
                'selenium' => new Session(new Driver\Selenium2Driver('chrome', 'null', "http://google.com/")),
            ));
            self::$mink->setDefaultSessionName('selenium');
        }
    }

    protected function setUp() {

        $this->setBrowser('chrome');
        $this->setBrowserUrl('http://google.com/');
    }
Suchi Shah
  • 19
  • 3