Monday, September 6, 2010

Checking multiple links on page using Selenium RC

A simple script for checking multiple links on page using selenium RC and taking a screenshot of the page. This script will get all the links then throw it to another php file in an array form.

<?php

set_include_path(get_include_path() . PATH_SEPARATOR . './PEAR/');
require_once 'Testing/Selenium.php';
require_once 'PHPUnit/Framework/TestCase.php';

class check_links extends PHPUnit_Framework_TestCase {
    private $sel;

    function setUp() {
        $this->sel = new Testing_Selenium("*firefox", "www.my-test-url.com/");
        $this->sel->start();
        $this->sel->setTimeOut(60000);
    }

    function tearDown() {
        $this->sel->stop();
    }

    function testLinks() {
        //delete screenshot files
        foreach (glob("C:\\screenshot_repo\\*.png") as $filename) {
            unlink($filename);
        }
        $this->sel->open("/");
        $this->linkval = $this->sel->getAllLinks('id=links-div');
        //write all the links found on a separate php file in an array form
        $txtfile = fopen('dump.php', 'w+');
        fwrite($txtfile, "<?php\n\$links_content = array(\n");
        for($i = 0, $size = sizeof($this->linkval); $i < $size; $i++) {
                $txt = $this->sel->getText("id=".$this->linkval[$i]);
                fwrite($txtfile, "array(href=>".'"'.$this->sel->getAttribute(
                'id='.$this->linkval[$i].'@href').'"'.','."text=>".'"'.$txt.'")
                ,'."\t\n");
        }
        fwrite($txtfile, ");\n?>");
        //read the file and get file length
        $txtfile = fopen('dump.php', 'r');
        $count = 0;
        while(fgets($txtfile)) {
            $count++;
        }
        fclose($txtfile);
        include 'dump.php';
        for ($links=0;$links<=$count-5;$links++) {
            $this->sel->open($links_content[$links]['href']);
            $this->sel->waitForPageToLoad(30000);
            //take a screenshot of the page
            $this->sel->windowFocus();
            $this->sel->windowMaximize();
            $SSPath = 'C:\\screenshot_repo\\';
            $this->sel->captureEntirePageScreenshot($SSPath.str_replace(
            " ","_",(strtolower($links_content[$links]['text'])))
            .".png","");
        }
    }
}
?>

No comments:

Post a Comment