tomzi
tomzi

Reputation: 1235

How to send email with selenium php

Hi people,

I would like to know if somebody knows how to send an email from a test in selenium/php I'm trying to do this :

<?php

class Example extends PHPUnit_Extensions_SeleniumTestCase
{
  protected function setUp()
  {
    $this->setBrowser("chrome");
    $this->setBrowserUrl("http://recette2011.thalys.com/");
    $this->setSpeed(500);
  }

  public function testMyTestCase()
  {
    $this->open("/be/en");
    $this->click("link=Help");
    $this->waitForPageToLoad("30000");
    $this->click("id=multi_block_title_span_element_2");
    $this->click("id=demande_information");
    $this->click("id=type_billet_information_aucun");
    $this->click("id=btn_valider");
    $this->waitForPageToLoad("30000");
    $url = $this->getLocation();
    echo $url;
    $cpt = substr_count ($url, "&");
    if($cpt >3){
        if(mail('[email protected]','Test',"Le test a foiré"))
        {
            echo "message sent";
        }
        else
        {
            echo "sent message failed";
        }
        echo "test failed";
    }
    else
    {
        echo "Test Granted";
    }
  }

}

When i go in the " if($cpt >3)" my server go out telling me : Argument 5 passed to PHPUnit_Framework_Error::__construct() must be an instance of Exception, array given, called in C:\wamp\bin\php\php5.3.5\PEAR\PHPUnit\Exten sions\SeleniumTestCase.php on line 1152 and defined

Upvotes: 0

Views: 597

Answers (2)

tomzi
tomzi

Reputation: 1235

I found how to by writing a log file when an error accurs and i created a Cron to send an email by checking if the file exists or not

This is my code :

<?php

class Example extends PHPUnit_Extensions_SeleniumTestCase
{
  protected function setUp()
  {
    $this->setBrowser("*firefox");
    $this->setBrowserUrl("http://recette2011.thalys.com/");
    $this->setSpeed(500);
  }

  public function testMyTestCase()
  {
      $this->open("/be/en");
      $this->click("link=Help");
      $this->waitForPageToLoad("30000");
      $this->click("id=multi_block_title_span_element_2");
      $this->click("id=demande_information");
      $this->click("id=type_billet_information_aucun");
      $this->click("id=btn_valider");
      $this->waitForPageToLoad("30000");
      $url = $this->getLocation();
      echo $url;
      $cpt = substr_count ($url, "&");
      if($cpt >3){
         $date = date("d-m-Y");
         $heure = date("H:i");
         $monfichier = fopen('C:\Users\intégrateur\Desktop\testSelenium\logSelenium.txt', 'a+');
         fputs($monfichier, '-- Probleme dans le fichier PasBillet.php le '.$date.' a '.$heure.' -- ');
         fclose($monfichier);
      }
      else
      {
          echo "Test Granted";
      }
   }

}
?>

And the Cron is :

<?php
$file = "C:\Users\intégrateur\Desktop\testSelenium\logSelenium.txt";
if(file_exists($file))
{
    if(filesize($file)>0)
    {
        mail('[email protected]','Probleme selenium','Une erreur est survenue dans un test de formaulaire. Pour plus d\'info consulter le fichier logSelenium.txt');
    } 
}
?>

Upvotes: 1

Davide Bellettini
Davide Bellettini

Reputation: 63

I think this is due to backward compatibily break of PHPUnit 3.7.1.
I solved it by downloading phpunit/phpunit-selenium using composer.

Upvotes: 0

Related Questions