Perl script to monitor changes in files under a given directory/folder

The following script should help you monitor the changes happening in the directory:

Perl

#!/usr/bin/perl

use strict;
use warnings;

my $directory = $ARGV[0];

if(!$directory) {
die "Usage: perl monitor.pl <directory>";
}elsif(!(-d $directory)){
die "$directory is not a directory. Kindly provide a valid directory to be monitored";

}

my $first_run = ‘true’;
my @prev_files_list = ();
my %prev_files_hash = ();
my %current_files_hash = ();

sub updateMaps {
my @files = @_;

foreach(@files) {
if($prev_files_hash{$_}){
delete $prev_files_hash{$_};
}elsif(!$prev_files_hash{$_}){
next if($_ eq ‘.’ || $_ eq ‘..’);
print "File: $_ has been added now\n";
}
$current_files_hash{$_} = 1;
}
@prev_files_list = @files;
foreach my $file (keys %prev_files_hash) {
print "File: $file is deleted\n";
delete $prev_files_hash{$file};
}
%prev_files_hash = ();
%prev_files_hash = %current_files_hash;

}

foreach (1..12) {
print "Count: $_\n";
opendir(DIR, $directory);
my @files = readdir(DIR);
if($first_run eq ‘true’){
@prev_files_list = @files;
foreach (@files) {
$prev_files_hash{$_} = 1;
}
$first_run = ‘false’;
sleep(4);
next;
}else{
##Compare prev_files and new_files
if(scalar(@files) > scalar(@prev_files_list)){
print "Files has been added\n";
updateMaps(@files);
}elsif(scalar(@files) < scalar(@prev_files_list)) {
print "Files has been deleted\n";
updateMaps(@files);
}else {
print "Either the file is changed, or same number of file deleted and added, or nothing changes at all\n";
%current_files_hash = ();
updateMaps(@files);
}
}
sleep(4);

}

Hope it will help someone somewhere sometime.

Thanks,
Rajat Jindal

Posted in Perl, Programming

Auto IT script to upload files (Windows XP)

Once your Choose File dialog box is open, just execute the following script:

; Starts here

WinWaitActive(“Choose File”, “”, 10);
Local $file = “C:\Users\QuickSilver\Desktop\filename.fileextesion”
ControlSetText(“Choose File”, “”, “Edit1”, $file )
ControlClick(“Choose File”, “”, “Button2”)
; Ends here

Steps to execute the above script:

1. Download AutoIt from : AutoItScript.com
2. Open Scite editor [comes bundled with downloaded package]
3. Copy paste the above script there
4. save it as “somename.au3”
5. Now make a system call from your automation script once “choose file” dialog box appears as follows
system(“somename.au3”);

The system command may differ on the basis of language you are using to write selenium automation script.

Hope it helps !

Thanks
Rajat Jindal
http://quicksilver1183.com

Posted in AutoIt

Apple buys chomp – search engine for finding apps

Apple Inc. is said to have purchased chomp, the search engine provider mainly used for discovering the apps from ever growing app stores. It appears Apple is trying to maintain the apps store hosting several thousand apps with vastly distributed features and functionality.

Algorithm used by Chomp find out the functions and topic of the apps available on store and thus help user search on the basis of what apps actually do instead of just name.

The deal is said to have materialized at a whopping $50 million as quoted by Bloomberg Business Week.

Apple confirmed the acquisition, however refused to comment on the price or the strategy behind this purchase.

“We buy smaller technology companies from time to time and generally don’t comment on our purposes or plans,” an Apple spokeswoman told Bloomberg Business Week.

Posted in Apple

Google Chrome soon going to have ‘Do not Track’ button.

Google Chrome., has finally made it to the list of browsers that give user power to disable the tracking cookies.

Chrome which is the world’s third highest used browser is soon going to have a button to empower user to disable the tracking usually done by advertising companies to present user ads which are customized per user browsing history.


The move came just hours after Obama administration revealed a bill to respect user privacy which is almost certain to become a law in coming days.

Remember few days back an article that was published on all leading technical blogs claimed Google to have knowingly used a known workaround in Safari to track the user experience however Google claimed to have mistakenly used the workaround and is promptly working to remove all the cookies thus set.

Do not Track has always been an interesting configuration from browsers because it also needs support from the websites to respect the configuration.

This feature is expected to be available sometime by end of the current year. So happy tracking for ads till then.

Tagged with: , ,
Posted in Chrome

getAttribute(‘class’) does not work with IE but works with Firefox

While debugging a error in Selenium get_eval function, I faced a strange problem (at least strange to me and probably couple of other folks might be facing this STRANGE problem as well) where my following piece of code was throwing “Expected identifier” exception:

var class = row.getAttribute(‘class’);
Here row was a table row, for which I wanted to get the class attribute to process further.
As class is a reserved word in Javascript and therefore it throws error when used as an identifier in IE, but it works fine on Firefox. Also getAttribute function works good with class in Firefox, but throws error in IE.
So the code was changed to something like below and it worked perfect for IE:
var class_name = row.getAttribute(‘className’);
But now it gave error with Firefox as getAttribute function does not work good with ‘className’
So I have to modify code to something like below and it worked perfectly for IE and Firefox:
//Works fine for Firefox
var class_name = row.getAttribute(‘class’);
//If browser is IE, this piece of code returns the correct class name
class_name = class_name ? class_name : row.getAttribute(‘className’);
Hope it helps.
Thanks,
QuickSilver1183
QuickSilver1183@gmail.com
Tagged with: , , ,
Posted in Selenium/Webdriver

Permission denied error after clicking on link when using selenium

Selenium throws a Permission Denied error after clicking, if we are not waiting for page to load before accessing the GUI. In most of the cases it works fine for Firefox, but with IE, this fails.

Consider the following piece of code:
//click function here take care of sending click command to selenium RC Server
$object->click(‘link=www.somelink.com’);
//Type command sends USERNAME to the Selenium RC Server to be typed in input box with ID: username
$object->type(‘id=username’, ‘USERNAME’);
Now the above mentioned code works good for Firefox, but it throws Permission denied error with IE.
Reason for that is as we have clicked on the link, and the page has to refresh, therefore when we try to type into some field on GUI that has not loaded yet, selenium throws Permission denied error.
Simple solution to above is to use clickAndWait command instead of just click, which will give some time to GUI to get refreshed or load before executing the next command.
so our new code will look like this:
//It clicks on the link and then waits for page to load dynamically for default time out value of selenium
$object->click_and_wait(‘link=www.somelink.com’);
$object->type(‘id=username’, ‘USERNAME’);
Now it works fine for both IE and Firefox.
However for applications that involve AJAX or changing display properties to block or none on click, this solution does not work.
For these, we need to either use explicit sleep($time_in_seconds) command [ Not suggested though ], or we can wait for some element on the refreshed GUI, which will make the page to wait before actually typing in the field.
Modified code will look something similar to as mentioned below:
$object->click(‘link=www.somelink.com’);
//This will make the page to wait to load dynamically till HTML Field id=username becomes accessible and will solve our permission denied problem.
$object->wait_for_element(‘id=username’);
$object->type(‘id=username’);
Hope it helps. Let me know if you are unable to fix any Permission denied error with above mentioned solution.
Thanks,
QuickSilver1183
QuickSilver1183@gmail.com
Tagged with: , , ,
Posted in Selenium/Webdriver

Invalid argument error on Selenium RC Server while selecting option from drop down in IE

If you try to select a label in a drop down using type command in selenium, what do you expect it to do??

Throw error??

or work fine??

Well, both the statements are true. The first one holds true for IE, and second one holds true for Firefox.

If you try to use the following command for to select option from the drop down using a type command, Firefox will work cool, but IE will throw an Invalid argument error.

//Here type function sends the type command to selenium incorrectly to select a label from drop down.
$object->type(‘id=id_for_drop_down’, ‘label_in_a_drop_down’);

This will throw the said error in IE and we can not blame it on IE all the times.

So just change it to send the command to select the label and it will work fine for you. So the new code will look like this:

//HERE SELECT OPTION FUNCTION SENDS THE SELECT FROM DROP DOWN COMMAND TO SELENIUM
$object->select_option(‘id=id_for_drop_down’, ‘label_in_a_drop_down’);

Hope it helps.

Thanks,
QuickSilver1183
QuickSilver1183@gmail.com

Posted in Selenium/Webdriver

Configuring ActiveMQ with Perl

By default ActiveMQ is configured to start the browser for C++, Java, C# etc by using Messaging protocol as Openwire. But to make it work with Perl we need couple of things to do as below:

1. First of all we need a client to handle the requests [Both pushing and retrieving]. We can download Net::Stomp library from CPAN. This is a very simple implementation to send and receive messages using STOMP Protocol.

2. We need to configure transport Connectors in file activemq.xml to use STOMP instead of openwire which is used by default as follows:

<transportConnectors>
<transportConnector name=”openwire” uri=”tcp://localhost:61616″/>
</transportConnectors>

to

<transportConnectors>
<transportConnector name=”stomp” uri=”stomp://0.0.0.0:61616″/>
</transportConnectors>

3. If you are using activeMQ version 5.3.1 and above, there is a strong probability that your webconsole for admin [http://localhost:8161/admin] may not work as explained at following link [http://activemq.apache.org/web-console.html], so you need to modify the file [apache-activemq-5.3.1\webapps\camel\WEB-INF\applicationContext.xml] as follows :

<!– configure the camel activemq component to use the current broker –>
<bean id=”activemq” class=”org.apache.activemq.camel.component.ActiveMQComponent” >
<property name=”brokerURL” value=”vm://localhost?create=false&waitForStart=10000″ />
<property name=”userName” value=”${activemq.username}”/>
<property name=”password” value=”${activemq.password}”/>
</bean>

Now you are ready to start the activeMQ, just double click on the activeMQ app file at bin location, and at console you can notice that activeMQ has started.

Lets write a simple Perl script to push messages in the queue:

use Net::Stomp;
my $stomp = Net::Stomp->new( { hostname => ‘QuickSilver-PC’, port => ‘61616’ } );
$stomp->connect( { login => ‘system’, passcode => ‘manager’ } );
$stomp->send(
{ destination => ‘/queue/example.A/’, body => ‘This is my first Stomp Message’ } );
$stomp->disconnect;

# subscribe to messages from the queue ‘example.A’
use Net::Stomp;
my $stomp = Net::Stomp->new( { hostname => ‘localhost’, port => ‘61616’ } );
$stomp->connect( { login => ‘system’, passcode => ‘manager’ } );
$stomp->subscribe(
{ destination => ‘/queue/example.A’,
‘ack’ => ‘client’,
‘activemq.prefetchSize’ => 1
}
);
while (1) {
my $frame = $stomp->receive_frame;
warn $frame->body; # do something here
$stomp->ack( { frame => $frame } );
}
$stomp->disconnect;

# write your own frame
my $frame = Net::Stomp::Frame->new(
{ command => $command, headers => $conf, body => $body } );
$stomp->send_frame($frame);

You can notice in your webconsole that message “This is my first stomp Message” is pushed to the queue example.A

Hope it helps.

Thanks,
QuickSilver1183
QuickSilver1183@gmail.com

Tagged with: , ,
Posted in Tools

How keyPressNative works on Selenium

Lately I had been facing an issue to simulate a functionality with Selenium. The functionality was of Auto Complete feature. [Yes, just like you see when typing something in Google search box.

I tried to use type command, but it just placed the value into the text field and Auto Complete feature was not showing up.

After banging my head with code and different functions I was finally able to find something that I would like to share with all. [Guys, my understanding can be wrong as well, so please correct me If I am wrong, and leave a comment regarding the same].

There are two types of code:

a. char code

b. key code

Consider we have two char ‘a’ and ‘A’. Now, simplest way to differentiate between char code and key code is that, both ‘a’ and ‘A’ have same key code but different char code.

Does it make any sense to you? If not read it again. This time one word at a time, and slowly.

“char code for ‘a’ and ‘A’ are different, but keycode for both of them is same. “

Selenium key_press_native accepts the ascii code for the char that you want to type in. But internally it matches it with the keycode of the char and therefore when you try to type in ‘A’ or ‘a’ [with their ascii values], you will see different results as selenium will not be able to find the key code for ‘a’.

Similar issue is faced while typing in the value for keys that require us to press SHIFT key. e.g. To type in #, we have to do first press ‘SHIFT’ and then press numeric ‘3’. Similarly fr & we need to press first ‘SHIFT’ and then ‘7’.

so the solution that I used finally for this was as follows:

Algo:

1. Prepare a hash map for special symbols, that require us to press SHIFT before using them. The key here would be the symbol and value would be the ascii code for the key that is to be pressed after pressing shift.

2. For UPPERCASE char also, we need to press first of all the ‘SHIFT’ key and then the key that is to be pressed.

so let us write down a small function doing this trick for us.

I am Perl guy, so do not mind me writing the code with Perl reference here.

sub type_some_text {
   my ($class, $locator, $text) = @_;

   $class->focus($locator);

   my $ascii_map = (
 SHIFT => 16,
 ':' => 59,
 '<' => 44,
 '>' => 46,
 '?' => 47,
 '"' => 222,
 '_' => 45,
 '+' => 61,
 '{' => 91,
 '}' => 93,
 '(' => 57,
 ')' => 48,
 '&'=> 55,
 '%'=> 53,
 '*'=> 56,
 '$'=> 52,
 '!' => 49,
 '@' => 50,
);
 
 my @char_arr = split(//, $text);
 foreach my $char (@char_arr) {
my $upper_case = 0;
 my $special_char = 0;
  if($char ne lc($char) {
   $upper_case = 1;
  }
  if($ascii_map{$char}) {
   $special_char = 1;
  }
  if($upper_case || $special_char) {
   $selenium->key_down_native($ascii_map{'SHIFT'});
  }
  if($ascii_map{$char}) {
   $selenium->key_press_native($ascii_map{$char});
  }else {
   $selenium->key_press_native(ord(uc($char)));
  }
  if($upper_case || $special_char) {
   $selenium->key_up_native($ascii_map{'SHIFT'});
  }
 }
}

Now with the above stated function, we can simulate the key strokes,and therefore the Auto Complete feature.

Hope it was of some help.

Edit: Updated the code to re-initialize upper_case & special_char values in for loop per comments from Itamar. Thanks buddy.

Tagged with: , , ,
Posted in Selenium/Webdriver