BehatPageObjectExtension icon indicating copy to clipboard operation
BehatPageObjectExtension copied to clipboard

Opening a page URL optionally with query string parameters

Open walterdolce opened this issue 9 years ago • 6 comments

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?

walterdolce avatar Oct 05 '16 22:10 walterdolce

We could make it real simple, and append all parameters that were not part of the $path as query parameters.

jakzal avatar Oct 06 '16 08:10 jakzal

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

walterdolce avatar Oct 06 '16 08:10 walterdolce

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.

DonCallisto avatar Dec 11 '19 11:12 DonCallisto

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?

DonCallisto avatar Dec 12 '19 08:12 DonCallisto

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']);
   }
}

jakzal avatar Dec 12 '19 08:12 jakzal

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.

DonCallisto avatar Dec 12 '19 08:12 DonCallisto