Behat Resize or maximize browser window

Profile picture for user devraj

You will see while executing Behat script you browser does not starts in full screen mode. You can start your browser in full screen mode or resize it using different methods, 2 of them are mentioned below:

Resize or Maximize Browser window using Mink 

First make sure that you have extended MinkContext

class FeatureContext extends MinkContext implements Context

remove MinkContext from your behat.yml Contexts section if you are extended MinkContext. Otherwise you will get error Step is already defined in FeatureContext class.

Then add following lines to before scenario:

/**
* @BeforeScenario
*/
public function before($event)
{
    $this->visitPath('/');
    $this->getSession()->maximizeWindow();
    $this->getSession()->resizeWindow(1500,500);
}

 You would like to go for maximum size or specific size. Comment the other option which you don't want to go for.

Note: If you are using "Given I am on homepage" in your feature file and then executing this, then you will get below error:

┌─ @BeforeScenario # FeatureContext::before()
│
╳  Fatal error: Call to a member function window() on null (Behat\Testwork\Call\Exception\FatalThrowableError)
│

Make sure you are calling visitPath('/') method in Before Suite or Scenario. Good thing about this approach you don't need any browser specific configuration. Whether it is Firefox, Chrome or any other browser, all will open in full screen mode.

Resize or Maximize Browser window using behat.yml 

Method 1: Use size in chrome switches

You can specify the size in switches

selenium2:
    browser: chrome
    wd_host: http://127.0.0.1:4444/wd/hub
    capabilities:
        chrome:
            switches:
              - "--start-maximized"
              - "--start-fullscreen"
              - "--window-size=1200,600"

You can also use the JSON version of above:

capabilities: { "browser": "chrome",  'chrome': {'switches':['--start-maximized','--start-fullscreen']}}

Note: --start-maximized is not working on Mac, so you can specify --start-fullscreen for that. remove or add --window-size depending on your choice.

Method 2: Use size in extra capabilities args

selenium2:
    browser: chrome
    wd_host: http://127.0.0.1:4444/wd/hub
    capabilities:
        extra_capabilities:
            chromeOptions:
                args:
#                 - "--window-size=1200,600"
                 - "--start-maximized"
                 - "--start-fullscreen"

Well this option will work for Chrome only, I spent several hours, I am unable to find any property to start firefox in full screen mode from yml.

Tags