15
15

More than 5 years have passed since last update.

CakePHPのアプリをSeleniumでテストする。

Last updated at Posted at 2014-08-23

Target

Prepare

vagrant ssh
cd /vagrant_data/
/usr/bin/Xvfb :1 -screen 0 1024x768x8 > /tmp/xvfb.log 2> /tmp/xvfb.error &
export DISPLAY=:1.0
java -jar /var/chef/cache/selenium-server-standalone-2.39.0.jar > /tmp/selenium.log 2> /tmp/selenium.error &
mysql -u root -e "drop database if exists test_candycane;create database test_candycane;"
./vendor/bin/phpunit app/Test/Case/Selenium/InstallerTest.php

First Step

<?php
/**
 * Created by PhpStorm.
 * User: yandod
 * Date: 2014/08/22
 * Time: 11:01
 */

class IssueTest extends PHPUnit_Extensions_Selenium2TestCase {

    protected function setUp()
    {
        $this->setHost('127.0.0.1');
        $this->setBrowser('firefox');
        $this->setBrowserUrl('http://127.0.0.1/');
        //$this->setPort(80);
    }

    public function testCreateShowDestroy()
    {

        $this->url('http://127.0.0.1/projects/sampleproject');
        $this->assertEquals('Overview - Sample Project - CandyCane', $this->title());
        $link = $this->byLinkText('New Issue');
    }

    public function onNotSuccessfulTest(Exception $e)
    {
        file_put_contents('app/tmp/logs/screenshot.png',$this->currentScreenshot());
        throw $e;
    }
} 
./vendor/bin/phpunit app/Test/Case/Selenium/IssueTest.php

screenshot.png

Login

  public function testCreateShowDestroy()
    {

        //login
        $this->url('http://127.0.0.1/account/login');
        $input = $this->byName('data[User][username]');
        $input->clear();
        $input->value('admin');
        $input = $this->byName('data[User][password]');
        $input->clear();
        $input->value('admin');
        $button = $this->byName('login');
        $this->moveto($button);
        $this->byId('UserLoginForm')->submit();

        //navigate
        $this->url('http://127.0.0.1/projects/sampleproject');
        $this->assertEquals('Overview - Sample Project - CandyCane', $this->title());
        $link = $this->byLinkText('New issue');
        $this->moveto($link);
        $this->click();

        //new issue
        $this->assertEquals('e', $this->title());
        file_put_contents('app/tmp/logs/screenshot.png',$this->currentScreenshot());
    }

screenshot.png

Create Issue

    public function testCreateShowDestroy()
    {

        //login
        $this->url('http://127.0.0.1/account/login');
        $input = $this->byName('data[User][username]');
        $input->clear();
        $input->value('admin');
        $input = $this->byName('data[User][password]');
        $input->clear();
        $input->value('admin');
        $button = $this->byName('login');
        $this->moveto($button);
        $this->byId('UserLoginForm')->submit();

        //navigate
        $this->url('http://127.0.0.1/projects/sampleproject');
        $this->assertEquals('Overview - Sample Project - CandyCane', $this->title());
        $link = $this->byLinkText('New issue');
        $this->moveto($link);
        $this->click();

        //new issue
        $input = $this->byId('IssueSubject');
        $input->value('We are in Spain');
        $input = $this->byId('description');
        $input->value('Hola.');
        $this->byId('IssueAddForm')->submit();

        file_put_contents('app/tmp/logs/screenshot.png',$this->currentScreenshot());
    }

screenshot.png

Add Relate Issue

    public function testCreateShowDestroy()
    {

        //login
        $this->url('http://127.0.0.1/account/login');
        $input = $this->byName('data[User][username]');
        $input->clear();
        $input->value('admin');
        $input = $this->byName('data[User][password]');
        $input->clear();
        $input->value('admin');
        $button = $this->byName('login');
        $this->moveto($button);
        $this->byId('UserLoginForm')->submit();

        //navigate
        $this->url('http://127.0.0.1/projects/sampleproject');
        $this->assertEquals('Overview - Sample Project - CandyCane', $this->title());
        $link = $this->byLinkText('New issue');
        $this->moveto($link);
        $this->click();

        //new issue
        $input = $this->byId('IssueSubject');
        $input->value('We are in Spain');
        $input = $this->byId('description');
        $input->value('Hola.');
        $this->byId('IssueAddForm')->submit();

        //add more issue
        $link = $this->byLinkText('New issue');
        $this->moveto($link);
        $this->click();
        $input = $this->byId('IssueSubject');
        $input->value('We are in Madrid');
        $input = $this->byId('description');
        $input->value('Madrid is center of Spain.');
        $this->byId('IssueAddForm')->submit();

        //link issue
        $link = $this->byLinkText('Add');
        $this->moveto($link);
        $this->click();
        $input = $this->byId('IssueRelationIssueToId');
        $current_id = array_pop(explode('/',$this->url())) - 1;
        $input->value($current_id);
        $this->byId('new-relation-form')->submit();
        $this->timeouts()->implicitWait(800);

        file_put_contents('app/tmp/logs/screenshot.png',$this->currentScreenshot());
    }

screenshot.png

Click delete relation.

XPath!

    public function testCreateShowDestroy()
    {

        //login
        $this->url('http://127.0.0.1/account/login');
        $input = $this->byName('data[User][username]');
        $input->clear();
        $input->value('admin');
        $input = $this->byName('data[User][password]');
        $input->clear();
        $input->value('admin');
        $button = $this->byName('login');
        $this->moveto($button);
        $this->byId('UserLoginForm')->submit();

        //navigate
        $this->url('http://127.0.0.1/projects/sampleproject');
        $this->assertEquals('Overview - Sample Project - CandyCane', $this->title());
        $link = $this->byLinkText('New issue');
        $this->moveto($link);
        $this->click();
        $this->timeouts()->implicitWait(300);

        //new issue
        $input = $this->byId('IssueSubject');
        $input->value('We are in Spain');
        $input = $this->byId('description');
        $input->value('Hola.');
        $this->byId('IssueAddForm')->submit();

        //add more issue
        $link = $this->byLinkText('New issue');
        $this->moveto($link);
        $this->click();
        $input = $this->byId('IssueSubject');
        $input->value('We are in Madrid');
        $input = $this->byId('description');
        $input->value('Madrid is center of Spain.');
        $this->byId('IssueAddForm')->submit();

        //link issue
        $link = $this->byLinkText('Add');
        $this->moveto($link);
        $this->click();
        $input = $this->byId('IssueRelationIssueToId');
        $current_id = array_pop(explode('/',$this->url())) - 1;
        $input->value($current_id);
        $this->byId('new-relation-form')->submit();
        $this->timeouts()->implicitWait(800);

        //delete relation
        $link = $this->byXPath("//a[@title='Delete relation']");
        $this->moveto($link);
        $this->click();
        $this->timeouts()->implicitWait(300);

        file_put_contents('app/tmp/logs/screenshot.png',$this->currentScreenshot());
    }

screenshot.png

Bug Fix!

IssueReleationsController.php
    function destroy($relation_id)
    {
        if ($this->request->is('ajax')) {
            Configure::write('debug', 0);
        }

        $relation = $this->IssueRelation->read(null, $relation_id);
        if ($relation) {
            $this->IssueRelation->delete();
        }

        $this->redirect(array('controller' => 'issues', 'action' => 'show', $this->_issue['Issue']['id']));
    }
View/Elements/issues/relations.ctp
  <?php if($this->Candy->authorize_for(array('controller'=>'issue_relations', 'action'=>'destroy'))) {
    echo $this->Js->link($this->Html->image('delete.png'), 
      array('controller'=>'issue_relations', 'action'=>'destroy', 'issue_id'=>$issue['Issue']['id'], $relation['IssueRelation']['id']),
      array('method'=>'post', 'title'=> __('Delete relation'), 'update'=>'relations', 'escape' => false));
  }?>

screenshot.png

15
15
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
15
15