qa-tools / behat-extension

Behat extension for integration with QA-Tools
BSD 3-Clause "New" or "Revised" License
0 stars 1 forks source link

Stale Mink's Session usage in PageFactory #11

Open aik099 opened 10 years ago

aik099 commented 10 years ago

The PageFactory is created only once (when extension is initialized - https://github.com/qa-tools/behat-extension/blob/master/src/QATools/BehatExtension/QATools.php#L78) and therefore it uses Mink's session available at the moment.

Such setup however is can backfire when multiple sessions are configured in MinkExtension. The MinkExtension works like this:

  1. it analyzes scenarios and figures out which sessions needs to be created for them
  2. for example in most simple case there will be 2:
    • one using GoutteDriver for simple pages
    • one using Selenium for pages that require JavaScript interactions
  3. in before scenario hook the name of Mink's default session is set to one, that needs to be used by the scenario

To solve this we need to have separate page factory instance created per session and call ->getPageFactory() (instead of ->pageFactory) that will:

  1. look in ->pageFactories array (key is Mink's session name, value is page factory created for it)
  2. if no page factory found for that session name, then create and return it
  3. if page factory found then return it

Thanks to caching in ->pageFactories array we don't need to create new page factory for each scenario.

aik099 commented 10 years ago

In fact my solution isn't that good if we think again about a problem. Within Mink class instance a sessions can appear/disappear/start/stop at will and fact that session has same name as one we remember doesn't prove that it's the same session at all.

aik099 commented 10 years ago

The PageFactory::setSession method, introduced in qa-tools/qa-tools#105, can be used to do a hot swap of session used by page factory. This however won't replace session within Pages/Elements that you have created before.