Showing posts with label screenshot. Show all posts
Showing posts with label screenshot. Show all posts

Sunday, December 12, 2010

screen_capture Sample in Watir

This simple watir script will take a screenshot and save the to a random number filename.

require 'watir'
require 'watir/screen_capture'
include Watir::ScreenCapture
require 'win32ole'

def take_screenshot
  randfname = Array.new(6) { (rand(122-97) + 97).chr }.join
  begin
    directory_name = File.dirname(__FILE__).gsub('/','\\') +
    "\\screenshots\\#{$randdir}"
    if FileTest::directory?(directory_name)
      filename = "#{directory_name}\\#{randfname}.png"
      screen_capture(filename,false, false)
    else
      Dir::mkdir(directory_name)
      filename = "#{directory_name}\\#{randfname}.png"
      screen_capture(filename,false, false)
    end
  end
end

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","");
        }
    }
}
?>