2

We run our Codeception tests (as smoke tests) against our production environment through Browserstack. Is there an easy way to identify Codeception? Through setting custom headers, user-agent or cookies?

The reason being, we constantly run A/B tests on our site and want to always show the control segment to Codeception.

I am not able to find any documentation about it.

daydreamer
  • 153
  • 2
  • 10
  • I find it weird that the documentation doesn't try to address this, it seems like a really common need. I want it just for the sake of loading a different database on my local WordPress install depending on whether it's a normal browser or a WebDriver visiting. – jerclarke Jul 29 '21 at 03:43

3 Answers3

1

This is what I ended up doing. In acceptance.test.yml, I used the chrome browser (did not matter to me) overwrote the user-agent using args and looked for that in my prod environment.

capabilities:
   chromeOptions:
     args: ["--user-agent=SmokeTesting"]
daydreamer
  • 153
  • 2
  • 10
  • Correct answer for me. You can then test it anywhere with `false !== strpos($_SERVER['HTTP_USER_AGENT'], 'SmokeTesting')` or whatever you set as the UA. – jerclarke Jul 29 '21 at 04:03
0

It depends on how you're using it. You're probably talking about doing acceptance test, although that concept can be used for any action that drives a browser interaction from the first person. So you might do something like :

$I->amOnPage('/homepage');
$I->loginAsAdmin();

So depending on what you were doing, you could identify Codeception through whatever normal login actions you were doing. For instance, if you were testing WordPress, you could create a username "CodeceptTester" and just watch for that login. It's tough to set actual headers. You can do it with PHPbrowser, but not with Selenium. In fact, I'm pretty sure you cannot set the Selenium headers through Codeception, but there are plugins for the browsers that can do this. You CAN do it with PhantomJS but I don't know how.

setting request headers in selenium

http://phptest.club/t/useragent-in-codeception/186/3

John Dee
  • 539
  • 4
  • 14
0

Perhaps you can identify it by looking at user agent string. For example, we're using Chrome headless (+WebDriver) and it's nicely introducing itself:

Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Ubuntu Chromium/63.0.3239.84 HeadlessChrome/63.0.3239.84 Safari/537.36

...but you can't be 100% sure it's you because, in theory, someone else could be running it against your website as well.

Alternatively, you can override amOnPage() method in your acceptance tester to set a header before calling parent::amOnPage():

public function amOnPage($page)
{
    $this->haveHttpHeader('x-codeception', '1');
    parent::amOnPage($page);
}

Just don't forget overriding amOnUrl() too, because it will hit the page in the same way as amOnPage(), while amOnSubdomain() only switches the subdomain without visiting it.

Oliver Maksimovic
  • 3,204
  • 3
  • 28
  • 44