Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

About this user

Sylvain Le Courtois

« Newer Snippets
Older Snippets »
Showing 1-1 of 1 total  RSS 

Internet Explorer automation using win32::OLE

// Sample code used for one of my client
   1  
   2  #!/usr/bin/perl -w
   3  
   4  use strict;
   5  use Data::Dumper;
   6  use Win32::OLE qw( EVENTS );
   7  
   8  my ($day, $mon, $year, $hour, $min, $sec) = (localtime)[3, 4, 5, 2, 1, 0];
   9  $mon++; # 0-based index
  10  $year = $year + 1900;
  11  my $date = sprintf ("%04i-%02i-%02i %02i\:%02i\:%02i", $year, $mon, $day, $hour, $min, $sec);
  12  
  13  my $Disconnect;
  14  my $Menu;
  15  my $TreeView;
  16  my $WatchDog;
  17  my $MenuClicked=0;
  18  
  19  my $ScenarioCompleted=0;
  20  
  21  my @TreeViewLinks=("Appareillage du B","Branchement Comptage","Branchement individuel");
  22  my $Previouslink=$TreeViewLinks[0];
  23  
  24  my $ie = Win32::OLE->new( 'InternetExplorer.Application' ) or die "error starting IE";
  25  $ie->{visible} = 1;
  26  
  27  Win32::OLE->Option( Warn => 3 );
  28  
  29  $WatchDog=time();
  30  Win32::OLE->WithEvents( $ie, \&Event, 'DWebBrowserEvents2' );
  31  $ie->navigate( 'http://www.xxx.fr' );
  32  Win32::OLE->MessageLoop();
  33  unlink("noemis.err") if -f "noemis.err";
  34  if ( ! $ScenarioCompleted ) {
  35  	open( ERR , ">noemis.err" ); 
  36  	print ERR "Problem executing Noemis scenario, please check www.xxx.fr.\n" ;
  37  	close(ERR);
  38  }
  39  $Disconnect->Click();
  40  Win32::OLE->SpinMessageLoop;
  41  
  42  # Maintenance du fichier historique
  43  open ( STATS , "noemis.txt" );
  44  my @lines=<STATS>;
  45  close (STATS);
  46  open( STATS , ">noemis.txt" ); 
  47  for my $line (@lines) {
  48  	my ($datetime) = split ( /;/ , $line );
  49  	my ($h_year,$h_mon) = $datetime =~ /^([0-9]{4})-([0-9]{2})/;
  50  	print STATS $line if ($year*12+$mon) - ($h_year*12+$h_mon) < 2;
  51  }
  52  print STATS join(";",$date,"Noemis Scenario",( time() - $WatchDog ))."\n";
  53  close( STATS );
  54  
  55  sleep 2;
  56  Win32::OLE->SpinMessageLoop;
  57  sleep 1;
  58  $ie->Quit();
  59  exit 0;
  60  
  61  
  62  sub Event {
  63  	my ($Obj,$Event,@Args) = @_;
  64  	my $IEObject = shift @Args;
  65  	print " Event triggered: $Event\n";    
  66  
  67  	my ($i,$anchor);
  68  	my $anchors;
  69      
  70  	# STEP 1 : Find the main menu, login to the web site, find the treeview
  71  	if ($Event eq "DocumentComplete") {    
  72  		print "URL: " . $IEObject->Document->URL . "\n";
  73  		if ( $IEObject->Document->URL eq "http://www.xxx.fr/ident.aspx" ) {
  74  			my $forms = $IEObject->Document->forms;
  75  			my $form = $forms->item(0);
  76  			if ( defined($form->elements("fldNumCli")) ) {
  77  				print "--------------------------------------------\n";
  78  				print "Found the login box, authenticating ...\n";
  79  				print "--------------------------------------------\n";
  80  			    $form->elements("fldNumCli")->{value} = "xxxx";
  81  			    $form->elements("fldUtil")->{value} = "xxx";
  82  			    $form->elements("fldPwd")->{value} = "xxx";
  83  		    	$form->elements("btIdent")->Click();
  84  	    		}
  85  		}
  86  		if ( $IEObject->Document->URL eq "http://www.xxx.fr/menu.aspx" ) {
  87  			print "Found the menu.\n";
  88  			$Menu = $IEObject->Document;
  89  			$anchors = $IEObject->Document->links;
  90  			for (my $i=0; $i < $anchors->length; $i++) {
  91  				$anchor = $anchors->item($i);
  92  				print $anchor->href."\n";
  93  				$Disconnect = $anchor if $anchor->href eq "http://www.xxx.fr/ident.aspx?qs=deconnecter";
  94  			}
  95  	      	}	    
  96  		if ( $IEObject->Document->URL eq "http://www.xxx.fr/client/frameTreeview.aspx" ) {
  97  			print "Found the TreeView.\n";
  98  			$TreeView = $IEObject->Document;
  99        		}		
 100  	}
 101  
 102  	# STEP 2 : Click on the Menu and TreeView links   
 103  	if ($Event eq "DocumentComplete") {    		
 104  	if ( ! $MenuClicked and defined($Menu) ) {
 105  		my $MenuItem = $Menu->getElementById("SM_CLIE_RECH");
 106  		if ( defined($MenuItem) ) { 
 107  			print $MenuItem->ID."\n";
 108  			$MenuItem->Click;
 109  			$MenuClicked = 1;
 110  		}
 111  	}}
 112  
 113  	if ( $Event eq "CommandStateChange" or $Event eq "StatusTextChange" ) {
 114  		print Dumper($IEObject);
 115  	}
 116  	if ( @TreeViewLinks != 0 and 
 117  	     defined($TreeView) and 
 118  	     $Event eq "DocumentComplete" 
 119  	) {
 120  		my $link = shift(@TreeViewLinks);
 121  		$anchors = $TreeView->links;
 122  		my $found=0;
 123  		print "Looking for '$link' in the TreeView ... \n";
 124  	        for (my $i=0; $i < $anchors->length; $i++) {
 125  		       	$anchor = $anchors->item($i);
 126  	        	#print $anchor->innerHTML."\n";
 127  		       	if ( $anchor->innerHTML =~ /$link/ ) {
 128  				print "Clicking on '$link' ... \n";
 129  	                	$anchor->Click;
 130  				$found=1;
 131  				$Previouslink=$link;
 132  				last;
 133  			}
 134  	        }
 135  		if ( ! $found ) { 
 136  			# Le TreeView a bugge, on reclique
 137  			sleep 1;
 138  			print "Looking for '$Previouslink' in the TreeView ... \n";
 139  		        for (my $i=0; $i < $anchors->length; $i++) {
 140  			       	$anchor = $anchors->item($i);
 141  	        		#print $anchor->innerHTML."\n";
 142  		       		if ( $anchor->innerHTML =~ /$Previouslink/ ) {
 143  					print "Clicking on '$Previouslink' ... \n";
 144  	              		  	$anchor->Click;
 145  					last;
 146  				}
 147  		        }
 148  			unshift @TreeViewLinks,$link;
 149  		}
 150  	} 
 151     
 152  	# STEP 3 : Verify the list displayed 
 153  		
 154  	if ($Event eq "DocumentComplete") {    
 155     		if ( @TreeViewLinks == 0 and $IEObject->Document->URL =~ /listeRefPlof.aspx/ ) {
 156  			print "Scenario completed, exiting ...\n";
 157  			$ScenarioCompleted=1;
 158  	   		Win32::OLE->QuitMessageLoop;
 159  		}
 160  	}
 161      
 162  
 163  	# Exit on errors
 164  	    
 165  	Win32::OLE->QuitMessageLoop() if $Event eq "OnQuit" or time() > $WatchDog + 60;
 166      
 167  }
« Newer Snippets
Older Snippets »
Showing 1-1 of 1 total  RSS