douchuang4402 2013-10-08 13:55
浏览 31
已采纳

Codeception多次测试,1个脚本

I think I might be getting the concept wrong or not thinking about something correctly. I'm looking for a way to connect to db, and then run a selenium test (in phantomjs) for every row of a table. The test is to check for broken images on a bespoke CMS, and could be applied to any CMS.

I basically want to run an acceptance test for every page (of a specific type) by loading their IDs from the db and then running a separate test for each ID.

This is what I have so far:

$I = new WebGuy($scenario);
$results = $I->getArrayFromDB('talkthrough', '`key`', array());
foreach ($results as $r) {
    $I->wantTo('Check helpfile '.$r['key'].'for broken images');
    $I->amOnPage('/talkThrough.php?id='.$r['key']);
    $I->seeAllImages();
}

This works to some extent in that it executes until the first failure (because it is running as 1 test with many assertions).

How do I make this run as individual tests?

  • 写回答

3条回答 默认 最新

  • doubi2228 2013-12-18 15:34
    关注

    I ended up looping through and storing the key that failed in a comma delimited string and setting a bool to say failures found.

    $I = new WebGuy($scenario);
    $results = $I->getArrayFromDB('talkthrough', '`key`', array());
    $failures = "Broken help files are: ";
    $failures_found = false;
    foreach ($results as $key => $r) {
       $I->wantTo('Check helpfile '.$r['key'].'for broken images');
       $I->amOnPage('/talkThrough.php?id='.$r['key']);
       $allImagesFine = $I->checkAllImages();
       if($allImagesFine != '1')
       {
           $fail = $r['key'].",";
           $failures.= $fail;
           $failures_found = true;
       }
    }
    $I->seeBrokenImages($failures_found,$failures);
    

    With following as my webhelper

    <?php
    namespace Codeception\Module;
    
    // here you can define custom functions for WebGuy 
    
    class WebHelper extends \Codeception\Module
    {
        function checkAllImages()
        {
            $result = $this->getModule('Selenium2')->session->evaluateScript("return     (function(){ return Array.prototype.slice.call(document.images).every(function (img) {return img.complete && img.naturalWidth > 0;}); })()");
            return $result;
        }
        function getArrayFromDB($table, $column, $criteria = array())
        {
            $dbh = $this->getModule('Db');
            $query = $dbh->driver->select($column, $table, $criteria);
            $dbh->debugSection('Query', $query, json_encode($criteria));
    
            $sth = $dbh->driver->getDbh()->prepare($query);
            if (!$sth) \PHPUnit_Framework_Assert::fail("Query '$query' can't be executed.");
    
            $sth->execute(array_values($criteria));
            return $sth->fetchAll();
        }
        function seeBrokenImages($bool,$failArray)
        {
            $this->assertFalse($bool,$failArray);
        }
    }
    

    Thanks for the submitted answers

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥60 pb数据库修改或者求完整pb库存系统,需为pb自带数据库
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)
  • ¥15 相敏解调 matlab
  • ¥15 求lingo代码和思路