不,我的问题在那里没有得到回答。我确实看过这个问题,但这并不能解决我的问题。我知道错误的含义,但我不知道为什么在这种情况下,脚本本身可以正常运行。
我做了一个小脚本,可以打开一个文本文件,用一行删除它。
$file = fopen("trainees.txt", "r+") or die("Unable to open file!");
$lines = file('trainees.txt', FILE_IGNORE_NEW_LINES);
$nl = count($lines);
$randIndex = rand(0,$nl-1);
$creds = $lines[$randIndex];
foreach ($lines as $line){
if ($line == $creds){
unset($lines[array_search($creds,$lines)]);
file_put_contents("trainees.txt", implode("\r\n",
$lines));
}
echo $line.'<br>';
}
fclose($file);
echo '<br><br><br><br><br><br>';
echo $creds;
这在完全独立的php文件中可以正常工作。但是,一旦我将其粘贴到测试中,它就会给我这个错误:
1) FirstCest: Login successfully
Test tests\acceptance\FirstCest.php:loginSuccessfully
[PHPUnit\Framework\Exception] Undefined offset: 0
#1 D:\Programas\xampp\htdocs\robo2\tests\acceptance\FirstCest.php:50
#2 FirstCest->loginSuccessfully
Artifacts:
Png: D:\Programas\xampp\htdocs\robo2\tests/_output\FirstCest.loginSuccessfully.fail.png
Html: D:\Programas\xampp\htdocs\robo2\tests/_output\FirstCest.loginSuccessfully.fail.html
ERRORS!
Tests: 1, Assertions: 0, Errors: 1.
您可以看到错误来自$creds = $lines[$randIndex];
行
该索引应该存在,所以我不知道为什么会收到此错误。
这是文本文件:
008C108:8706405
108C408:1776723
008B108:9315051
008BD08:4357300
008BF08:4235645
008C308:8571541
008B008:7139762
008BB08:8942468
008BC08:6648592
6648592:8504197
008C608:6219682
008AE08:2728576
008AF08:9407850
这是我的整个测试文件:
// test
public function loginSuccessfully(AcceptanceTester $I)
{
//Times to answer questions
$nq = 8;
$lexam = 10; //minutes
$lexamVariation = 0.07;
$lexam1 = round($this->__rand_float($lexam - ($lexam * $lexamVariation), $lexam * ($lexamVariation + 1)),4);
$tquest = $lexam1 / $nq;
$times = [];
for ($i=$nq; $i > 0; $i--) {
$tquest1 = $this->__rand_float($tquest * 0.5,$tquest*1.5,50) *60;
array_push($times,$tquest1);
}
//echo 'total time (sec)= '.array_sum($times).'<br>';
//echo 'total time (min)= '.array_sum($times) /60;
//picks an user
$file = fopen("trainees.txt", "r+") or die("Unable to open file!");
$lines = file('trainees.txt', FILE_IGNORE_NEW_LINES);
$nl = count($lines);
$randIndex = rand(0,$nl - 1);
$creds = $lines[$randIndex];
foreach ($lines as $line){
if ($line == $creds){
unset($lines[array_search($creds,$lines)]);
file_put_contents("trainees.txt", implode("\r\n", $lines));
}
}
fclose($file);
$user = $trainees[rand(0,count($trainees))];
$credentials = explode(':',$user,2);
unset($trainees[$user]);
$I->amOnPage('');
$I->waitForElementClickable('//*[@id="submitLogin"]');
$I->fillField('username',$credentials[0]);
$I->fillField('password',$credentials[1]);
$I->click('//*[@id="submitLogin"]');
//preliminaries
$I->waitForElementClickable('//*[@id="Proceed"]');
$I->click('//*[@id="Proceed"]');
$I->waitForElementClickable('//*[@id="pCheckbox"]/span');
$I->click('//*[@id="pCheckbox"]/span');
$I->waitForElementClickable('//*[@id="Proceed"]');
$I->click('//*[@id="Proceed"]');
$I->waitForElementClickable('//*[@id="Proceed"]');
$I->click('//*[@id="Proceed"]');
$I->waitForElementClickable('//*[@id="pCheckbox"]/span');
$I->click('//*[@id="pCheckbox"]/span');
$I->waitForElementClickable('//*[@id="Proceed"]');
$I->click('//*[@id="Proceed"]');
$I->waitForElementClickable('//*[@id="Proceed"]');
$I->click('//*[@id="Proceed"]');
$I->waitForElementClickable('//*[@id="pCheckbox"]/span');
$I->click('//*[@id="pCheckbox"]/span');
$I->waitForElementClickable('//*[@id="Proceed"]');
$I->click('//*[@id="Proceed"]');
//start exam
$I->waitForElementNotVisible('//*[@id="oleWaitMessage"]', 200);
for ($q=count($times) - 1; $q > 0 ; $q--) {
$I->waitForElementClickable('//*[@id="rb_0"]');
$I->wait($times[$q]);
$I->click('//*[@id="rb_'.rand(0,2).'"]');
$I->waitForElementClickable('//*[@id="oleNextItemBtn"]');
$I->click('//*[@id="oleNextItemBtn"]');
}
//last question
$I->waitForElementClickable('//*[@id="rb_0"]');
$I->wait($times[$q]);
$I->click('//*[@id="rb_'.rand(0,2).'"]');
//submits
$I->waitForElementClickable('//*[@id="oleSubmitBtn"]');
$I->wait(2);
$I->click('//*[@id="oleSubmitBtn"]');
$I->waitForElementClickable('//*[@id="yui-gen0-button"]');
$I->wait(2);
$I->click('//*[@id="yui-gen0-button"]');
}
}