Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check external redirection with Codeception

I want to check something like this:

<?php

$I->amOnPage('/go/google');
$I->seeCurrentUrlEquals('http://google.com');

But I get error:

Failed asserting that two strings are equal.
--- Expected
+++ Actual
@@ @@
-'http://google.com'
+''

Scenario Steps:
2. I see current url equals "http://google.com"
1. I am on page "/go/google"

Idea is just to check if users was redirected to external resource.

like image 723
2tunnels Avatar asked Oct 05 '13 21:10

2tunnels


People also ask

How to test your redirects?

Just select the browser user-agent to test your redirect. Check your URL redirect for accuracy. Do you use search engine friendly redirections like to many redirects or do you loose link juice for seo by redirects using HTTP Statuscode 301 vs. 302. Check now! This Redirect Checker supports several features like:

What is codeception and how does it work?

Codeception allows you to execute actions in concurrent sessions. The most obvious case for this is testing realtime messaging between users on a site. In order to do it, you will need to launch two browser windows at the same time for the same test. Codeception has a very smart concept for doing this. It is called Friends:

What are the different types of web redirects?

But thanks to the web redirection, those reach the updated web address regardless of typing in the old URLs. Based on the use case, here are various types of redirections: 301 Redirect: This is a permanent redirection meaning the old web property is no longer available.

How to use 301 redirects for URL shortening to boost Seo?

This can be useful to see the entire path and the final server to which a short URL points. And you can check the status code of the redirects and opt for the URL shortening service using 301 redirects for maximum SEO advantage.


2 Answers

Codeception's seeCurrentUrlEquals() and seeCurrentUrlMatches() methods are, unfortunately, misnamed as they do not allow assertions to be made against the entire URL but rather only against the URI parts.

I solved this by declaring the following method in my Helper class:

public function seeFullCurrentURLEquals($value)
{
    return ($this->getModule('PhpBrowser')->client->getHistory()->current()->getUri() == $value);
}

Then, in the scenario, you can simply do something like:

$I->seeFullCurrentUrlEquals('google.com');
like image 60
confirmator Avatar answered Sep 23 '22 20:09

confirmator


I would improve @rickroyce´s comment by swapping out line 2

$I->wait(2);

with something like

$I->waitForElement('#gbqfq', 30);

which waits for the google search input field and is more robust then just assuming 2 seconds is enough that the page is loaded.

The full example would be then

<?php

$I->amOnPage('/go/google');
$I->waitForElement('#gbqfq', 30);
$I->seeCurrentUrlEquals('http://google.com');

(Normally it would put the addition as comment but since I do not have enough reputation it is an answer :))

like image 22
Paul Vincent Beigang Avatar answered Sep 23 '22 20:09

Paul Vincent Beigang