2013-10-05 33 views
4

我想看看这样的事情:检查外部重定向与Codeception

<?php 

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

,但我得到的错误:

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" 

想法只是检查,如果用户被重定向到外部资源。

+0

重定向页面('/ go/google /')的外观如何? –

+0

@Jordan,它只是一个重定向用户使用重定向标题的脚本......现在我想我知道该怎么做:D – 2tunnels

回答

0

你想使用什么样的浏览器?大多数浏览器需要一点时间来解释重定向。在访问网站“/ go/google”后,测试马上运行url检查,这可能会更快,然后浏览器可以进行重定向。多给点时间。

尝试:

<?php 

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

通过一种类似

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

换出2号线

$I->wait(2); 

的东西对于

2

我会提高@ rickroyce's评论这等待谷歌搜索输入字段,更健壮,然后只是假设2秒就足以加载页面。

完整的例子是那么

<?php 

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

(通常它会把另外的评论,但因为我没有足够的声誉是一个答案:))

-1

岂不是更好地检查响应代码?

$I->amOnPage('/go/google'); 
$I->seeResponseCodeIs(302); 
$I->seeHttpHeader('Location', 'http://google.com'); 
+4

我一直在试图做到这一点,但它似乎只是代码的填充客户端 - > getInternalRequest数据与结果即200,而不是301或302. – GaryJ

4

Codeception的seeCurrentUrlEquals()seeCurrentUrlMatches()方法,不幸的是,名不副实的,因为他们没有允许对整个URL的断言,而是只对URI部分。

我解决了这个由我的助手类声明下面的方法:

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

然后,在场景中,你可以简单地这样做:

$I->seeFullCurrentUrlEquals('google.com'); 
+0

谢谢你的解决方案。 注意1.也许事情在一年中发生了变化,但不是返回布尔值,而是辅助方法必须调用断言。 我的方法是这样的: '''PHP 公共职能seeFullCurrentURLEquals($值){ $ = CURRENTURL $这个 - > getModule( 'PhpBrowser') - >客户端 - > getHistory() - >电流() - > getUri(); codecept_debug('当前URL:'。$ currentUrl); $ this-> assertEquals($ currentUrl,$ value,'Current URL'。$ currentUrl。'不等于'。$ value); } ''' – Naktibalda

1

正如@gopherIT提到,Codeception在使用这些功能时不会检查URL的域名部分。

如果您使用phpBrowser,这里有几个选项。


  1. 我已经写了两个函数verifyRedirect()verifyNoRedirect()可用于这一目的。你只需将代码弹出_support/Helper/Acceptance.php即可。

    然后可以像这样测试...

    $I->verifyRedirect('http://www2.ames.net.au/', 'http://www.ames.net.au/', 301); 
    

    查看代码:https://gist.github.com/SimonEast/b550e04300ac56cb5ac8eea704fccdfa


  • GaryJones也放在一起用于测试重定向另一个(稍微复杂)的变化,并张贴上Github:

    https://gist.github.com/GaryJones/284eea905cff882cc7cb

    它允许你运行像这样的测试...

    $I = new RedirectsTester($scenario); 
    $I->wantTo('check 301 redirects are working'); 
    
    // First, test /login/ page gets rewritten to https:// 
    $I->seePermanentRedirectToHttpsFor('login/'); 
    $I->seePermanentRedirectToHttpsFor('login/?redirect_to=foo'); 
    
    // For all other redirects, a Location header is sent, so we stop the redirect 
    // and just check that header instead. 
    $I->followRedirects(false); 
    
    // External 
    $I->sendHead('facebook'); 
    $I->seePermanentRedirectTo('https://www.facebook.com/DanielsTrading'); 
    
    // Internal link 
    $I->sendHead('don-debartolo/access'); 
    $I->seePermanentRedirectTo('ddebartolo/#account'); 
    

    他回顾了源代码,如有必要,可以帮助进一步定制。

  • 0

    这里的代码片段,可以帮助:

    $uri_to_redirect = '/use_stackoverflow'; 
    $redirected_to_uri = '/love_stackoverflow'; 
    
    $I->wantToTest('if rediecting works well from : '.$uri_to_redirect.' to : '.$redirected_to_uri); 
    $I->amOnUrl($mydomain.$uri_to_redirect); 
    $I->seeCurrentUrlEquals($redirected_to_uri); 
    

    你需要保持在头“seeCurrentUrlEquals”验证URI,而不是URL。

    Regards