Code examples of several tests, 1 script

I think that perhaps I am mistaken in the concept or do not think about something right. I am looking for a way to connect to db and then run a selenium test (in phantomjs) for each row of the table. The test is to check for broken images on a custom CMS and can be applied to any CMS.

I basically want to run a reception test for each page (of a certain type) by loading their identifiers from the database and then doing a separate test for each identifier.

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 runs until the first failure (because it works like 1 test with many statements).

How do I make this run as individual tests?

+6
source share
3 answers

I ended the loop and saved the key that failed in the comma-separated line, and installed bool to say that there were failures.

 $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); 

Followed 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 answers provided.

+3
source

This will not work. Please avoid loops and conventions in your tests. You must put the key manually. And do not get them from the database. As this introduces additional complexity.

+1
source

This may not be the best design choice, but if you really want to follow this approach, you can use the code pointing tool to keep your test running even if one statement fails: https://github.com/Codeception/Specify

0
source

Source: https://habr.com/ru/post/955496/


All Articles