How to change browser settings in a Selenium test

Magium tests are designed to be as cross browser compatible as possible. For that reason the Remote WebDriver component is used in all circumstances.

However, there might be some times when you need to test browser specific configurations. One example is setting the browser language. Each browser has different ways of doing it. So given that Magium is intended to be browser-independent, how do you make browser specific changes?

The key to this problem is the Initializer. It manages all of the configuration for an individual test and most of the time Magium will configure itself properly. But you can easily make changes in this process.

Start in your test class, or, preferably, a generic abstract test class that you have written for your site that is globally used for your tests (this allows for easy global changes). Override the setup() method, and prior to calling parent::setUp() provide the test class with a new Initializer.

class WebDriverArgumentsTest extends AbstractTestCase
{

    protected function setUp()
    {
        $this->initializer = new EspanolChromeInitializer();
        parent::setUp();
    }
}

It is in the code of the EspanolChromeInitialiser class where some of the magic occurs.

class EspanolChromeInitializer extends Initializer
{

    protected function getDefaultConfiguration()
    {
        $config = parent::getDefaultConfiguration();
        $capabilitities = $config['definition']['class']['Magium\WebDriver\WebDriverFactory']['create']['desired_capabilities']['default'];
        if ($capabilitities instanceof DesiredCapabilities) {
            $options = new ChromeOptions();
            $options->addArguments(['--lang=es']);
            $capabilitities->setCapability(ChromeOptions::CAPABILITY, $options);
        }
        return $config;
    }
}

This could also be done via DIC configuration but the DIC configuration is 100% global, so you would not be able to switch between one option and another. This approach allows you to change configuration on a per-test-class basis.