tanaka's Programming Memo

プログラミングについてのメモ。

PHPUnit+Seleniumで、ページに要素があるかを確認する方法

(サンプルで " が抜けていたのを修正 2017/2/27)

以下に、画面に要素があるかどうかを判断するサンプルコードを示します。

<?php
class EntryTest extends PHPUnit_Extensions_Selenium2TestCase {
    protected function setUp()
    {
        $this->setBrowser('firefox');
        $this->setBrowserUrl("http://www.google.co.jp");
    }

    public function testValidElement() {
        // ページ開始
        $this->url("http://www.google.co.jp");

        // IDがlst-ibの要素があるかを確認
        if (count($this->elements($this->using("id")->value("lst-ib"))) > 0) {
            echo "lst-ibはある\n";
        }
        // IDがnashiの要素がないかを確認
        if (count($this->elements($this->using("id")->value("nashi"))) === 0) {
            echo "nashiはない\n";
        }
    }
}
?>

usingで様々な要素を検索するときの指示の仕方は http://am1tanaka.hatenablog.com/entry/2015/04/03/000126