Opening a page URL optionally with query string parameters
Currently, the only way I can see to open a page with query string parameters it seems to be the following:
class Homepage extends Page {
protected $path = '/?{QS}';
}
class MyContext ... {
public function aStep()
{
$this->homepage->open(array('QS' => 'utm_source=1&utm_campaign=foo'));
}
}
This produces the expected result:
<schema>://<hostname>/?utm_source=1&utm_campaign=foo
But this way one is always forced to pass something. By leaving the array parameter passed to the open method, this is the resulting absolute URL:
<schema>://<hostname>/?{QS}
There might be cases where the behaviour of a page (or element) might vary depending on whether something's included in the URL as a query string parameter. And both cases to be verified. Creating the same page object twice (i.e. Homepage and ParameterisedHomepage) feels wrong (even if it just extend the non-parameterised page object class).
How should one deal with optional query string parameters?
We could make it real simple, and append all parameters that were not part of the $path as query parameters.
Sounds like a good start. What about URL fragments? Semantically speaking, those would be two different things.
I.e.
<schema>://<hostname>/?utm_source=1&utm_campaign=foo#the-anchor
I came across this today in one of my projects. @jakzal are you still interested in this feature? Maybe I can try an implementation in the next weeks.
We can add two more (optional) parameters to open: array $query = [], string $fragment in order to not introduct BC.
Another solution could be to use one array (as it is atm) but prepend to the param keys someting like ?<key> and #<key>.
Personally I feel that first solution is better than latter one (no validation for multiple fragment, just to mention the first issue I see).
Any thoughts?
Thinking about it again what's wrong with the following?
class Homepage extends Page {
protected $path = '/?utm_source={utm_source}&utm_campaign={utm_campaign}';
}
class MyContext ... {
public function aStep()
{
$this->homepage->open(['utm_source' => 1, 'utm_campaign' => 'foo']);
}
}
I have some cases where I would like to open the page without some parameters (maybe not mandatory?) and in those cases I would land in the page with placeholders instead of real parameters or without parameters at all.