PageRenderTime 70ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 1ms

/lib/Win32/IEAutomation.pm

https://github.com/gitpan/Win32-IEAutomation
Perl | 1393 lines | 1224 code | 136 blank | 33 comment | 120 complexity | 557c65f2463de512f6c569d974bdb4a5 MD5 | raw file
  1. package Win32::IEAutomation;
  2. use strict;
  3. use Win32::OLE qw(EVENTS);
  4. use Win32::IEAutomation::Element;
  5. use Win32::IEAutomation::Table;
  6. use Win32::IEAutomation::WinClicker;
  7. use vars qw($VERSION $warn);
  8. $VERSION = '0.5';
  9. sub new {
  10. my $class = shift;
  11. my %options = @_;
  12. my $self = bless ({ }, $class);
  13. my ($visible, $maximize);
  14. if (exists $options{visible}){
  15. $visible = $options{visible};
  16. }else{
  17. $visible = 1;
  18. }
  19. if (exists $options{maximize}){
  20. $maximize = $options{maximize};
  21. }
  22. if (exists $options{warnings}){
  23. $warn = $options{warnings};
  24. }
  25. $self->_startIE($visible, $maximize);
  26. }
  27. sub _startIE{
  28. my ($self, $visible, $maximize) = @_;
  29. defined $self->{agent} and return;
  30. $self->{agent} = Win32::OLE->new("InternetExplorer.Application") || die "Could not start Internet Explorer Application through OLE\n";
  31. Win32::OLE->Option(Warn => 0);
  32. Win32::OLE->WithEvents($self->{agent});
  33. $self->{agent}->{Visible} = $visible;
  34. if ($maximize){
  35. my $clicker = Win32::IEAutomation::WinClicker->new();
  36. $clicker->maximize_ie();
  37. undef $clicker;
  38. }
  39. return $self;
  40. }
  41. sub getAgent {
  42. my $self = shift;
  43. $self->{agent};
  44. }
  45. sub getElement {
  46. my $self = shift;
  47. $self->{element};
  48. }
  49. sub closeIE{
  50. my $self = shift;
  51. my $agent = $self->{agent};
  52. $agent->Quit;
  53. }
  54. sub gotoURL{
  55. my ($self, $url, $nowait) = @_;
  56. my $agent = $self->{agent};
  57. $agent->Navigate($url);
  58. $self->WaitforDone unless $nowait;
  59. }
  60. sub Back{
  61. my $self = shift;
  62. my $agent = $self->{agent};
  63. $agent->GoBack;
  64. $self->WaitforDone;
  65. }
  66. sub Reload{
  67. my $self = shift;
  68. my $agent = $self->{agent};
  69. $agent->Refresh2;
  70. $self->WaitforDone;
  71. }
  72. sub URL{
  73. my $self = shift;
  74. my $agent = $self->{agent};
  75. $agent->LocationURL;
  76. }
  77. sub Title{
  78. my $self = shift;
  79. my $agent = $self->{agent};
  80. $agent->document->title;
  81. }
  82. sub Content{
  83. my $self = shift;
  84. my $agent = $self->{agent};
  85. my $html = $agent->document->documentElement->{outerHTML};
  86. $html =~ s/\r//g;
  87. my @file = split (/\n/, $html);
  88. if (wantarray){
  89. return @file;
  90. }else{
  91. return $html;
  92. }
  93. }
  94. # sub VerifyText{
  95. # my ($self, $string) = @_;
  96. # my @text = $self->PageText;
  97. # foreach my $line (@text){
  98. # $line =~ s/^\s+//;
  99. # $line =~ s/\s+$//;
  100. # if ($line eq $string || $line =~ m/$string/){
  101. # return 1;
  102. # }
  103. # }
  104. # }
  105. sub VerifyText{
  106. my ($self, $string, $flag) = @_;
  107. $flag = 0 unless $flag;
  108. my $textrange = $self->{agent}->document->body->createTextRange;
  109. return $textrange->findText($string, 0 , $flag);
  110. }
  111. sub PageText{
  112. my $self = shift;
  113. my $text = $self->getAgent->document->documentElement->outerText;
  114. $text =~ s/\r//g;
  115. my @file = split (/\n/, $text);
  116. if (wantarray){
  117. return @file;
  118. }else{
  119. return $text;
  120. }
  121. }
  122. sub getLink{
  123. my ($self, $how, $what) = @_;
  124. my $agent = $self->{agent};
  125. my $links = $agent->Document->links;
  126. my $target_link = __getObject($links, $how, $what) if ($links);
  127. my $link_object;
  128. if ($target_link){
  129. $link_object = Win32::IEAutomation::Element->new();
  130. $link_object->{element} = $target_link;
  131. $link_object->{parent} = $self;
  132. }else{
  133. $link_object = undef;
  134. print "WARNING: No link is present in the document with your specified option $how $what\n" if $warn;
  135. }
  136. return $link_object;
  137. }
  138. sub getAllLinks{
  139. my $self = shift;
  140. my $agent = $self->{agent};
  141. my @links_array;
  142. my $links = $agent->Document->links;
  143. for (my $n = 0; $n <= $links->length - 1; $n++){
  144. my $link_object = Win32::IEAutomation::Element->new();
  145. $link_object->{element} = $links->item($n);
  146. $link_object->{parent} = $self;
  147. push (@links_array, $link_object);
  148. }
  149. return @links_array;
  150. }
  151. sub getButton{
  152. my ($self, $how, $what) = @_;
  153. my $agent = $self->{agent};
  154. my $buttons = $agent->Document->all->tags("input");
  155. my $target_button = __getObject($buttons, $how, $what) if ($buttons);
  156. my $button_object;
  157. if ($target_button){
  158. $button_object = Win32::IEAutomation::Element->new();
  159. $button_object->{element} = $target_button;
  160. $button_object->{parent} = $self;
  161. }else{
  162. $button_object = undef;
  163. print "WARNING: No button is present in the document with your specified option $how $what\n" if $warn;
  164. }
  165. return $button_object;
  166. }
  167. sub getImage{
  168. my ($self, $how, $what) = @_;
  169. my $agent = $self->{agent};
  170. my $images = $agent->Document->images;
  171. my $target_image = __getObject($images, $how, $what) if ($images);
  172. my $image_object;
  173. if ($target_image){
  174. $image_object = Win32::IEAutomation::Element->new();
  175. $image_object->{element} = $target_image;
  176. $image_object->{parent} = $self;
  177. }else{
  178. $image_object = undef;
  179. print "WARNING: No image is present in the document with your specified option $how $what\n" if $warn;
  180. }
  181. return $image_object;
  182. }
  183. sub getAllImages{
  184. my $self = shift;
  185. my $agent = $self->{agent};
  186. my @image_array;
  187. my $images = $agent->Document->images;
  188. for (my $n = 0; $n <= $images->length - 1; $n++){
  189. my $image_object = Win32::IEAutomation::Element->new();
  190. $image_object->{element} = $images->item($n);
  191. $image_object->{parent} = $self;
  192. push (@image_array, $image_object);
  193. }
  194. return @image_array;
  195. }
  196. sub getRadio{
  197. my ($self, $how, $what) = @_;
  198. my $agent = $self->{agent};
  199. my $inputs;
  200. if ($how eq "beforetext:" || $how eq "aftertext:"){
  201. $inputs = $agent->Document->all;
  202. }else{
  203. $inputs = $agent->Document->all->tags("input");
  204. }
  205. my $target_radio = __getObject($inputs, $how, $what, "radio") if ($inputs);
  206. my $radio_object;
  207. if ($target_radio){
  208. $radio_object = Win32::IEAutomation::Element->new();
  209. $radio_object->{element} = $target_radio;
  210. $radio_object->{parent} = $self;
  211. }else{
  212. $radio_object = undef;
  213. print "WARNING: No radio button is present in the document with your specified option $how $what\n" if $warn;
  214. }
  215. return $radio_object;
  216. }
  217. sub getCheckbox{
  218. my ($self, $how, $what) = @_;
  219. my $agent = $self->{agent};
  220. my $inputs;
  221. if ($how eq "beforetext:" || $how eq "aftertext:"){
  222. $inputs = $agent->Document->all;
  223. }else{
  224. $inputs = $agent->Document->all->tags("input");
  225. }
  226. my $target_checkbox = __getObject($inputs, $how, $what, "checkbox") if ($inputs);
  227. my $checkbox_object;
  228. if ($target_checkbox){
  229. $checkbox_object = Win32::IEAutomation::Element->new();
  230. $checkbox_object->{element} = $target_checkbox;
  231. $checkbox_object->{parent} = $self;
  232. }else{
  233. $checkbox_object = undef;
  234. print "WARNING: No checkbox is present in the document with your specified option $how $what\n" if $warn;
  235. }
  236. return $checkbox_object;
  237. }
  238. sub getSelectList{
  239. my ($self, $how, $what) = @_;
  240. my $agent = $self->{agent};
  241. my $select_lists = $agent->Document->all->tags("select");
  242. my $target_list = __getObject($select_lists, $how, $what, "select-one|select-multiple") if ($select_lists);
  243. my $list_object;
  244. if ($target_list){
  245. $list_object = Win32::IEAutomation::Element->new();
  246. $list_object->{element} = $target_list;
  247. $list_object->{parent} = $self;
  248. }else{
  249. $list_object = undef;
  250. print "WARNING: No select list is present in the document with your specified option $how $what\n" if $warn;
  251. }
  252. return $list_object;
  253. }
  254. sub getTextBox{
  255. my ($self, $how, $what) = @_;
  256. my $agent = $self->{agent};
  257. my ($inputs, $target_field);
  258. if ($how eq "beforetext:" || $how eq "aftertext:"){
  259. $inputs = $agent->Document->all;
  260. }else{
  261. $inputs = $agent->Document->all->tags("input");
  262. }
  263. if ($inputs){
  264. $target_field = __getObject($inputs, $how, $what, "text|password|file");
  265. }
  266. my $text_object;
  267. if ($target_field){
  268. $text_object = Win32::IEAutomation::Element->new();
  269. $text_object->{element} = $target_field;
  270. $text_object->{parent} = $self;
  271. }else{
  272. $text_object = undef;
  273. print "WARNING: No text box is present in the document with your specified option $how $what\n" if $warn;
  274. }
  275. return $text_object;
  276. }
  277. sub getTextArea{
  278. my ($self, $how, $what) = @_;
  279. my $agent = $self->{agent};
  280. my ($inputs, $target_field);
  281. if ($how eq "beforetext:" || $how eq "aftertext:"){
  282. $inputs = $agent->Document->all;
  283. }else{
  284. $inputs = $agent->Document->all->tags("textarea");
  285. }
  286. if ($inputs){
  287. $target_field = __getObject($inputs, $how, $what, "textarea");
  288. }
  289. my $text_object;
  290. if ($target_field){
  291. $text_object = Win32::IEAutomation::Element->new();
  292. $text_object->{element} = $target_field;
  293. $text_object->{parent} = $self;
  294. }else{
  295. $text_object = undef;
  296. print "WARNING: No text area is present in the document with your specified option $how $what\n" if $warn;
  297. }
  298. return $text_object;
  299. }
  300. sub getTable{
  301. my ($self, $how, $what) = @_;
  302. my $agent = $self->{agent};
  303. my ($inputs, $target_table);
  304. if ($how eq "beforetext:" || $how eq "aftertext:"){
  305. $inputs = $agent->Document->all;
  306. }else{
  307. $inputs = $agent->Document->all->tags("table");
  308. }
  309. if ($inputs){
  310. $target_table = __getObject($inputs, $how, $what);
  311. }
  312. my $table_object;
  313. if ($target_table){
  314. $table_object = Win32::IEAutomation::Table->new();
  315. $table_object->{table} = $target_table;
  316. $table_object->{parent} = $self;
  317. }else{
  318. $table_object = undef;
  319. print "WARNING: No table is present in the document with your specified option $how $what\n" if $warn;
  320. }
  321. return $table_object;
  322. }
  323. sub getAllTables{
  324. my $self = shift;
  325. my $agent = $self->{agent};
  326. my @links_array;
  327. my $links = $agent->Document->all->tags("table");
  328. for (my $n = 0; $n < $links->length; $n++){
  329. my $link_object = Win32::IEAutomation::Element->new();
  330. $link_object->{element} = $links->item($n);
  331. $link_object->{parent} = $self;
  332. push (@links_array, $link_object);
  333. }
  334. return @links_array;
  335. }
  336. sub __getObject{
  337. my ($coll, $how, $what, $type) = @_;
  338. my ($aftertext_flag, $input, $index_counter, $regex_flag);
  339. $regex_flag = 1 if ($what =~ /^?-xism:/);
  340. for (my $n = 0; $n <= $coll->length - 1; $n++){
  341. if ($how eq "linktext:") {
  342. my $text = $coll->item($n)->outerText;
  343. $text = trim_white_spaces($text);
  344. if ($regex_flag){
  345. return $coll->item($n) if ($text =~ $what);
  346. }else{
  347. return $coll->item($n) if ($text eq $what);
  348. }
  349. }
  350. elsif ($how eq "tabtext:") {
  351. my $text = $coll->item($n)->outerText;
  352. $text = trim_white_spaces($text);
  353. if ($regex_flag){
  354. return $coll->item($n) if ($text =~ $what);
  355. }else{
  356. return $coll->item($n) if ($text eq $what);
  357. }
  358. }
  359. elsif ($how eq "id:") {
  360. my $id = $coll->item($n)->id;
  361. return $coll->item($n) if ($id eq $what);
  362. }
  363. elsif ($how eq "name:") {
  364. my $name = $coll->item($n)->name;
  365. if ($regex_flag){
  366. return $coll->item($n) if ($name =~ $what);
  367. }else{
  368. return $coll->item($n) if ($name eq $what);
  369. }
  370. }
  371. elsif ($how eq "value:") {
  372. my $value = $coll->item($n)->value;
  373. if ($regex_flag){
  374. return $coll->item($n) if ($value =~ $what);
  375. }else{
  376. return $coll->item($n) if ($value eq $what);
  377. }
  378. }
  379. elsif ($how eq "class:") {
  380. my $class = $coll->item($n)->{className};
  381. if ($regex_flag){
  382. return $coll->item($n) if ($class =~ $what);
  383. }else{
  384. return $coll->item($n) if ($class eq $what);
  385. }
  386. }
  387. elsif ($how eq "index:") {
  388. $index_counter++ if ($coll->item($n)->type =~ m/^($type)$/);
  389. return $coll->item($n) if ($index_counter == $what);
  390. }
  391. elsif ($how eq "caption:") {
  392. my $value = $coll->item($n)->value;
  393. if ($regex_flag){
  394. return $coll->item($n) if ($value =~ $what);
  395. }else{
  396. return $coll->item($n) if ($value eq $what);
  397. }
  398. }
  399. elsif ($how eq "linkurl:") {
  400. my $url = $coll->item($n)->href;
  401. if ($regex_flag){
  402. return $coll->item($n) if ($url =~ $what);
  403. }else{
  404. return $coll->item($n) if ($url eq $what);
  405. }
  406. }
  407. elsif ($how eq "imgurl:") {
  408. my $imgurl = $coll->item($n)->src;
  409. if ($regex_flag){
  410. return $coll->item($n) if ($imgurl =~ $what);
  411. }else{
  412. return $coll->item($n) if ($imgurl eq $what);
  413. }
  414. }
  415. elsif ($how eq "alt:") {
  416. my $imgurl = $coll->item($n)->alt;
  417. if ($regex_flag){
  418. return $coll->item($n) if ($imgurl =~ $what);
  419. }else{
  420. return $coll->item($n) if ($imgurl eq $what);
  421. }
  422. }
  423. elsif ($how eq "beforetext:") {
  424. $input = $coll->item($n) if ($coll->item($n)->tagname eq "INPUT");
  425. my $text = $coll->item($n)->getAdjacentText("beforeEnd");
  426. $text = trim_white_spaces($text);
  427. if ($regex_flag){
  428. return $input if ($text =~ $what);
  429. }else{
  430. return $input if ($text eq $what);
  431. }
  432. $text = $coll->item($n)->getAdjacentText("afterEnd");
  433. $text = trim_white_spaces($text);
  434. if ($regex_flag){
  435. return $input if ($text =~ $what);
  436. }else{
  437. return $input if ($text eq $what);
  438. }
  439. }
  440. elsif ($how eq "aftertext:") {
  441. undef $input;
  442. $input = $coll->item($n) if (($coll->item($n)->tagName =~ m/^(INPUT|TEXTAREA)$/) && $coll->item($n)->type =~ m/^($type)$/);
  443. #print $coll->item($n)->{type}."\n" if ($aftertext_flag == 1 && $input);
  444. return $input if ($aftertext_flag == 1 && $input);
  445. unless ($aftertext_flag){
  446. my $text = $coll->item($n)->getAdjacentText("beforeEnd");
  447. $text = trim_white_spaces($text);
  448. if ($regex_flag){
  449. $aftertext_flag = 1 if ($text =~ $what);
  450. }else{
  451. $aftertext_flag = 1 if ($text eq $what);
  452. }
  453. $text = $coll->item($n)->getAdjacentText("afterEnd");
  454. $text = trim_white_spaces($text);
  455. if ($regex_flag){
  456. $aftertext_flag = 1 if ($text =~ $what);
  457. }else{
  458. $aftertext_flag = 1 if ($text eq $what);
  459. }
  460. }
  461. }
  462. else{
  463. print "WARNING: \'$how\' is not supported to get the object\n";
  464. }
  465. }
  466. }
  467. sub getFrame{
  468. my ($self, $how, $what) = @_;
  469. my $target_frame;
  470. my $agent = $self->{agent};
  471. my $frames = $agent->Document->frames;
  472. $target_frame = __getObject($frames, $how, $what) if ($frames);
  473. if ($target_frame){
  474. my %frame = %{$self};
  475. my $frameref = \%frame;
  476. $frameref->{agent} = $target_frame;
  477. bless $frameref;
  478. return $frameref;
  479. }else{
  480. print "WARNING: No frame is present in the document with your specified option $how $what\n" if $warn;
  481. }
  482. }
  483. sub getPopupWindow{
  484. my ($self, $what, $wait) = @_;
  485. my $counter;
  486. $wait = 2 unless $wait;
  487. while($counter <= $wait ){
  488. my $shApp = Win32::OLE->new("Shell.Application") || die "Could not start Shell.Application\n";
  489. my $windows = $shApp->Windows;
  490. for (my $n = 0; $n <= $windows->count - 1; $n++){
  491. my $window = $windows->Item($n);
  492. my $title = $window->document->title if $window;
  493. if ($title eq $what){
  494. my %popup = %{$self};
  495. my $popupref = \%popup;
  496. $popupref->{agent} = $window;
  497. bless $popupref;
  498. $popupref->WaitforDone;
  499. return $popupref;
  500. }
  501. }
  502. sleep 1;
  503. $counter++
  504. }
  505. print "WARNING: No popup window is present with your specified title: $what\n" if $warn;
  506. }
  507. sub WaitforDone{
  508. my $self = shift;
  509. my $agent = $self->{agent};
  510. while ($agent->Busy || $agent->document->readystate ne "complete"){
  511. sleep 1;
  512. }
  513. }
  514. sub WaitforDocumentComplete{
  515. my $self = shift;
  516. my $agent = $self->{agent};
  517. while ($agent->document->readystate ne "complete"){
  518. sleep 1;
  519. }
  520. }
  521. sub trim_white_spaces{
  522. my $string = shift;
  523. $string =~ s/^\s+//;
  524. $string =~ s/\s+$//;
  525. return $string;
  526. }
  527. 1;
  528. __END__
  529. #######################################################################
  530. # DOCUMENTATION
  531. #
  532. =head1 NAME
  533. Win32::IEAutomation - Web application automation using Internet Explorer
  534. =head1 SYNOPSIS
  535. use Win32::IEAutomation;
  536. # Creating new instance of Internet Explorer
  537. my $ie = Win32::IEAutomation->new( visible => 1, maximize => 1);
  538. # Site navigation
  539. $ie->gotoURL('http://www.google.com');
  540. # Finding hyperlinks and clicking them
  541. # Using 'linktext:' option (text of the link shown on web page)
  542. $ie->getLink('linktext:', "About Google")->Click;
  543. # Or using 'linktext:' option with pattern matching
  544. $ie->getLink('linktext:', qr/About Google/)->Click;
  545. # Or using 'id:' option ( <a id=1a class=q href=......>)
  546. $ie->getLink('id:', "1a")->Click;
  547. # Finding checkbox and selecting it
  548. # Using 'name:' option ( <input type = "checkbox" name = "checkme" value = "1"> )
  549. $ie->getCheckbox('name:', "checkme")->Select;
  550. # Or using 'aftertext:' option (for checkbox after some text on the web page)
  551. $ie->getCheckbox('aftertext:', "some text here")->Select;
  552. # Finding text field and entering data into it
  553. # Using 'name:' option ( <input type="text" name="username" .......> )
  554. $ie->getTextBox('name:', "username")->SetValue($user);
  555. # Finding button and clicking it
  556. # using 'caption:' option
  557. $ie->getButton('caption:', "Google Search")->Click;
  558. # Accessing controls under frame
  559. $ie->getFrame("name:", "content")->getLink("linktext:", "All Documents")->Click;
  560. # Nested frames
  561. $ie->getFrame("name:", "first_frame")->getFrame("name:", "nested_frame");
  562. # Catching the popup as new window and accessing controls in it
  563. my $popup = $ie->getPopupWindow("title of popup window");
  564. $popup->getButton('value:', "button_value")->Click;
  565. Additionally it provides methods to interact with security alert dialog, conformation dialog, logon dialog etc.
  566. # To navigate to some secure site and pushing 'Yes' button of security alert dialog box
  567. use Win32::IEAutomation;
  568. use Win32::IEAutomation::WinClicker; # this will provide methods to interact with dialog box
  569. my $ie = Win32::IEAutomation->new();
  570. $ie->gotoURL("https://some_secure_site.com", 1); # second argument says that don't wait for complete document loading and allow code to go to next line.
  571. my $clicker = Win32::IEAutomation::WinClicker->new();
  572. $clicker->push_security_alert_yes();
  573. $ie->WaitforDone; # we will wait here for complete loading of navigated site
  574. =head1 DESCRIPTION
  575. This module tries to give web application automation using internet explorer on windows platform. It internally uses Win32::OLE to create automation object for IE.
  576. It drives internet explorer using its DOM properties and methods. The module allows user to interact with web page controls like links, buttons, radios, checkbox, text fields etc.
  577. It also supports frames, popup window and interaction with javascript dialogs like security alert, confirmation, warning dialog etc.
  578. =head1 METHODS
  579. =head2 CONSTRUCTION AND GENERIC METHODS
  580. =over 4
  581. =item * Win32::IEAutomation->new( visible => 1, maximize => 1, warnings => 1 )
  582. This is the constructor for new Internet Explorer instance through Win32::OLE. Calling this function will create
  583. a perl object which internally contains a automation object for internet explorer. Three options are supported to this method in hash format.
  584. All of these are optional.
  585. visible
  586. It sets the visibility of Internet Explorer window. The default value is 1, means by default it will be visible if you don't provide this option.
  587. You can set it to 0 to run it in invisible mode.
  588. maximize
  589. Default value of this is 0, means it runs IE in the size of last run. Setting this to 1 will maximize the window.
  590. warnings
  591. Default value of this is 0. If you want to print warning messages for any object not found, then set it to 1. This is optional.
  592. =item * gotoURL($url, [$nowait])
  593. This method navigates to the given url and waits for it to be loaded completely if second argument is not provided
  594. Second argument is optional and it represents the boolean value for not waiting till page gets loaded.
  595. Giving second argument as 1 (true boolean value) makes your code not to wait for page loading in this 'gotoURL' method.
  596. This is useful if you need to interact with any dialog like security alert. In that case use this method with second argument, then
  597. interact with dialog (methods for interacting with dialog are described below) and then using method 'WaitforDone' (described below)
  598. wait for IE to load page completely.
  599. =item * Back()
  600. Works as IE's Back button and waits for it to be loaded completely
  601. =item * Reload()
  602. Works as IE's Refresh button and waits for it to be loaded completely
  603. =item * URL()
  604. This will return the URL of the current document
  605. =item * Title()
  606. This will return title of the current document
  607. =item * Content()
  608. This will return the HTML of the current document. In scalar context return a single string (with \n characters) and in list context returns array.
  609. Please note that all the tags are UPPER CASED.
  610. =item * VerifyText($string, [iflags])
  611. Verifies that given string is present in the current document text. It returns 1 on success and undefined on failure.
  612. Second parameter iflags is optional. It is integer value that specifies one or more of the following flags to indicate the type of search.
  613. 0 Default. Match partial words.
  614. 1 Match backwards.
  615. 2 Match whole words only.
  616. 4 Match case.
  617. =item * PageText()
  618. It returns the text in the current page. (no html tags). In scalar context return a single sting (with \n characters) and in list context returns array.
  619. It will assist for using VerifyText method. User can print the returned array to some file and see what string he/she can pass as an argument to the VerifyText method.
  620. =item * WaitforDone()
  621. Waits till IE had came out of busy state and document is loaded completly.
  622. This will poll IE for every one second and check its busy state and document complete state, before we move on.
  623. =item * closeIE()
  624. It will close the instance of internet explorer.
  625. =item * getAgent()
  626. It will return a reference to the Internet Explorer automation object, created using Win32::OLE.
  627. =back
  628. =head2 LINK METHODS
  629. =over 4
  630. =item * getLink($how, $what)
  631. This is the method to access link on web page. It returns Win32::IEAutomation::Element object containing html link element.
  632. $how : This is the option how you want to access the link
  633. $what : This is string or integer, what you are looking for. For this, it also supports pattern matching using 'qr' operator of perl (see example below)
  634. Valid options to use for $how:
  635. 'linktext:' - Find the link by matching the link text i.e. the text that is displayed to the user
  636. 'id:' - Find the link by matching id attribute
  637. 'name:' - Find the link by matching name attribute
  638. 'linkurl:' - Find the link by matching url attribute of the link
  639. 'class:' - Find the link by matching class attribute
  640. Typical Usage:
  641. $ie->getLink('linktext:', "Sign in"); # access the link that has Sign in as its text
  642. $ie->getLink('linktext:', qr/About Google/); # access the link whose text matches with 'About Google'
  643. $ie->getLink('id:', 5); # access the link whose id attribute is having value 5
  644. =item * getAllLinks()
  645. This will return a list of all links present in the current document, in form of Win32::IEAutomation::Element objects.
  646. For return value, in scalar context, gives number of links present and in list context gives array of all link objects.
  647. Each object in the array is similar to one we get using getLink method.
  648. =item B<Methods supported for link object>
  649. =item Click($nowait);
  650. Clicks the link and waits till document is completely loaded.
  651. As it uses click method of DOM, it supports clicking link with javascript in it.
  652. $nowait: This is optional. Giving this argument as 1 (true boolean value) makes your code not to wait for complete page loading after clicking link.
  653. This is useful if you need to interact with any dialog (like logon dialog) after clicking the link. (please see logon() method example for details)
  654. =item linkText();
  655. Returns text of the link
  656. =item linkUrl();
  657. Returns url of the link
  658. =item getProperty($property)
  659. Retrieves the value of the given property for link object. This makes easy to get value of any property that is supported by html link.
  660. =back
  661. =head2 IMAGE METHODS
  662. =over 4
  663. =item * getImage($how, $what)
  664. This is the method to access image on web page. It returns Win32::IEAutomation::Element object containing html image element.
  665. $how : This is the option how you want to access the image
  666. $what : This is string or integer, what you are looking for. For this, it also supports pattern matching using 'qr' operator of perl (see example below)
  667. Valid options to use for $how:
  668. 'id:' - Find the image by matching id attribute
  669. 'name:' - Find the image by matching name attribute
  670. 'imgurl:' - Find the image by matching src attribute of the image
  671. 'alt:' - Find the image by matching alt attribute of the image
  672. 'class:' - Find the image by matching class attribute
  673. Typical Usage:
  674. $ie->getImage('imgurl:', qr/logo.gif$/); # access the image that matches logo.gif at last of string of its url (src)
  675. $ie->getImage('alt:', "Google"); # access the image whose alt attribute is 'Google'
  676. $ie->getImage('class:', $some_class); # access the image whose class attribute is having value $some_class
  677. =item * getAllImages()
  678. This will return a list of all images present in the current document, in form of Win32::IEAutomation::Element objects.
  679. For return value, in scalar context, gives number of images present and in list context gives array of all image objects.
  680. Each object in the array is similar to one we get using getImage method.
  681. =item B<Methods supported for image object>
  682. =item Click($nowait)
  683. Clicks the image and waits till document is completely loaded.
  684. As it uses click method of DOM, it supports clicking link with javascript in it.
  685. $nowait: This is optional. Giving this argument as 1 (true boolean value) makes your code not to wait for complete page loading after clicking image.
  686. This is useful if you need to interact with any dialog (like logon dialog) after clicking the image. (please see logon() method example for details)
  687. =item imgUrl()
  688. Returns url of the image
  689. =item getProperty($property)
  690. Retrieves the value of the given property for image object. This makes easy to get value of any property that is supported by html image.
  691. =back
  692. =head2 BUTTON METHODS
  693. =over 4
  694. =item * getButton($how, $what)
  695. This is the method to access button on web page. It returns Win32::IEAutomation::Element object containing html input type=button or submit element.
  696. $how : This is the option how you want to access the button
  697. $what : This is string or integer, what you are looking for. For this, it also supports pattern matching using 'qr' operator of perl (see example below)
  698. Valid options to use for $how:
  699. 'id:' - Find the button by matching id attribute
  700. 'name:' - Find the button by matching name attribute
  701. 'value:' - Find the button by matching value attribute
  702. 'caption:' - Find the button by matching text shown on button
  703. 'class:' - Find the button by matching class attribute
  704. If there are more than one button having same value for the option you are quering, then it returns first button of the collection.
  705. Typical Usage:
  706. $ie->getButton('caption:', "Google Search"); # access the button with 'Google Search' as its caption
  707. $ie->getButton('name:', "btnG"); # access the button whose name attribute is 'btnG'
  708. =item B<Methods supported for button object>
  709. =item Click($nowait)
  710. Clicks the button and waits till document is completely loaded.
  711. $nowait: This is optional. Giving this argument as 1 (true boolean value) makes your code not to wait for complete page loading after clicking button.
  712. This is useful if you need to interact with any dialog (like logon dialog) after clicking the button. (please see logon() method example for details)
  713. =item getProperty($property)
  714. Retrieves the value of the given property for button object. This makes easy to get value of any property that is supported by html button.
  715. =back
  716. =head2 RADIO and CHECKBOX METHODS
  717. =over 4
  718. =item * getRadio($how, $what)
  719. This is the method to access radio button on web page. It returns Win32::IEAutomation::Element object containing html input type=radio element.
  720. =item * getCheckbox($how, $what)
  721. This is the method to access checkbox on web page. It returns Win32::IEAutomation::Element object containing html input type=checkbox element.
  722. $how : This is the option how you want to access the radio or checkbox
  723. $what : This is string or integer, what you are looking for. For this, it also supports pattern matching using 'qr' operator of perl (see example below)
  724. Valid options to use for $how:
  725. 'id:' - Find the radio or checkbox by matching id attribute
  726. 'name:' - Find the radio or checkbox by matching name attribute
  727. 'value:' - Find the radio or checkbox by matching value attribute
  728. 'class:' - Find the radio or checkbox by matching class attribute
  729. 'index:' - Find the radio or checkbox using the index in the total collection of the radio or checkbox. (see example below)
  730. 'beforetext:' - Find the radio or checkbox before the specified text.
  731. 'aftertext:' - Find the radio or checkbox after the specified text.
  732. If there are more than one object having same value for the option you are quering, then it returns first object of the collection.
  733. Typical Usage:
  734. $ie->getRadio('beforetext:', "Option One"); # access the radio which appears before text 'Option One'
  735. $ie->getRadio('value:', $radio_value); # access the radio whose value attribute is $radio_value
  736. $ie->getCheckbox('aftertext:', "Remember Password"); # access the checkbox which appears after text 'Remember Password'
  737. $ie->getCheckbox('index:', 3); # access third checkbox from the collection of checkbox on the current web page
  738. =item B<Methods supported for radio or checkbox object>
  739. =item Select()
  740. It selects the specified radio or checkbox, provided it is not already selected.
  741. $ie->getRadio('beforetext:', "Option One")->Select;
  742. =item deSelect()
  743. It deselects the specified radio or checkbox, provided it is not already deselected.
  744. $ie->getCheckbox('aftertext:', "Remember Password")->deSelect;
  745. =item getProperty($property)
  746. Retrieves the value of the given property for radio or checkbox object. This makes easy to get value of any property that is supported by html radio or checkbox.
  747. =back
  748. =head2 SELECT LIST METHODS
  749. =over 4
  750. =item * getSelectList($how, $what)
  751. This is the method to access select list on web page. It returns Win32::IEAutomation::Element object containing html select element.
  752. $how : This is the option how you want to access the select list
  753. $what : This is string or integer, what you are looking for. For this, it also supports pattern matching using 'qr' operator of perl (see example below)
  754. Valid options to use for $how:
  755. 'id:' - Find the select list by matching id attribute
  756. 'name:' - Find the select list by matching name attribute
  757. 'class:' - Find the select list by matching class attribute
  758. 'index:' - Find the select list using the index in the total collection of the select lists. (see example below)
  759. 'beforetext:' - Find the select list before the specified text.
  760. 'aftertext:' - Find the select list after the specified text.
  761. If there are more than one object having same value for the option you are quering, then it returns first object of the collection.
  762. Typical Usage:
  763. $ie->getSelectList('aftertext:', "some text"); # access the select list which appears after text 'some text'
  764. $ie->getSelectList('name:', 'time_zone'); # access the select list whose name attribute is 'time_zone'
  765. $ie->getSelectList('index:', 3); # access third select list from the collection of select lists on the current web page
  766. =item B<Methods supported for select list object>
  767. =item SelectItem()
  768. It selects one or more items from the select list. You can pass a single or multiple item to this method.
  769. Provide the name of item which is visible to user on web page ( and not that is in html code).
  770. Use this method for selection of multiple items only if select list is supporting multiple selection.
  771. $ie->getSelectList('name:', 'time_zone')->SelectItem("India"); # selects one item "India" in the select list
  772. $ie->getSelectList('name:', 'time_zone')->SelectItem("India", "U. S. A.", "Australia"); # selects three items "India", "U. S. A." and "Australia" in the select list
  773. =item deSelectItem()
  774. It deselects one or more items from the select list. You can pass a single or multiple item to this method.
  775. Provide the name of item which is visible to user on web page ( and not that is in html code)
  776. $ie->getSelectList('name:', 'time_zone')->deSelectItem("India"); # deselect one item "India" in the select list
  777. $ie->getSelectList('name:', 'time_zone')->deSelectItem("India", "U. S. A.", "Australia"); # deselect three items "India", "U. S. A." and "Australia" in the select list
  778. =item deSelectAll()
  779. It deselects all items from the select list. Call this method without any argument.
  780. This method is useful, when a select list appears with a random selected item and you need to deselect that.
  781. $ie->getSelectList('name:', 'time_zone')->deSelectAll(); # deselect all items in the select list
  782. =item getProperty($property)
  783. Retrieves the value of the given property for select list object. This makes easy to get value of any property that is supported by html select list.
  784. =back
  785. =head2 TEXTBOX and TEXTAREA METHODS
  786. =over 4
  787. =item * getTextBox($how, $what)
  788. This is the method to access input text field on web page. It returns Win32::IEAutomation::Element object containing html input type=text or password element.
  789. Additionaly this method works for file upload text field also. (i.e. input type=file).
  790. =item * getTextArea($how, $what)
  791. This is the method to access input text area on web page. It returns Win32::IEAutomation::Element object containing html textarea element.
  792. $how : This is the option how you want to access the select list
  793. $what : This is string or integer, what you are looking for. For this, it also supports pattern matching using 'qr' operator of perl (see example below)
  794. Valid options to use for $how:
  795. 'id:' - Find the text field by matching id attribute
  796. 'name:' - Find the text field by matching name attribute
  797. 'value:' - Find the text field by matching value attribute
  798. 'class:' - Find the text field by matching class attribute
  799. 'index:' - Find the text field using the index in the total collection of the text fields. (see example below)
  800. 'beforetext:' - Find the text field before the specified text.
  801. 'aftertext:' - Find the text field after the specified text.
  802. If there are more than one object having same value for the option you are quering, then it returns first object of the collection.
  803. Typical Usage:
  804. $ie->getSelectList('aftertext:', "User Name:"); # access the text fields which appears after text 'User Name:'
  805. $ie->getSelectList('name:', 'password'); # access the text field whose name attribute is 'password'
  806. $ie->getSelectList('index:', 3); # access third text field from the collection of text fields on the current web page
  807. =item B<Methods supported for text field object>
  808. =item SetValue($string)
  809. It will set the value of text field or text area to the string provided.
  810. $ie->getTextBox('name:', "q")->SetValue("web automation"); # it will enter text 'web automation' in the input text filed.
  811. $ie->getTextBox("name:", "importFile")->SetValue("C:\\temp\\somefile"); # to set somefile in file upload text field
  812. =item GetValue()
  813. It will return the default text present in the textbox or text area.
  814. $ie->getTextBox('name:', "q")->GetValue() # return the default text present in the textbox
  815. =item ClearValue()
  816. It will clear the text field.
  817. $ie->getTextBox('name:', "q")->ClearValue() # it will clear the input text field
  818. =item getProperty($property)
  819. Retrieves the value of the given property for text field object. This makes easy to get value of any property that is supported by html input text field.
  820. =back
  821. =head2 TABLE METHOD
  822. =over4
  823. =item * getTable($how, $what)
  824. This is the method to access table on web page. It returns Win32::IEAutomation::Table object containing html table.
  825. $how : This is the option how you want to access the link
  826. $what : This is string or integer, what you are looking for. For this, it also supports pattern matching using 'qr' operator of perl (see example below)
  827. Valid options to use for $how:
  828. 'id:' - Find the table by matching id attribute
  829. 'name:' - Find the table by matching name attribute
  830. 'value:' - Find the table by matching value attribute
  831. 'class:' - Find the table by matching class attribute
  832. If there are more than one object having same value for the option you are quering, then it returns first object of the collection.
  833. Typical Usage:
  834. $ie->getTable("id:", "1a"); # access the table whose id attribute is '1a'
  835. $ie->getTable("class:", "data") # access the table whose class attribute is 'data'
  836. =item * getAllTables()
  837. This will return array containing all table on that page. Each element of an array will be a Win32::IEAutomation::Table object.
  838. Typical Usage:
  839. my @alltables = $ie->getAllTables;
  840. =item B<Methods supported for table object>
  841. =item rows([index of row])
  842. This method returns the row object/objects from the table. It takes an optional argument, the index number of the row you wish to take.
  843. If this method is used without any argument then it returns array of row objects in the table. And if you provide an argument,
  844. then it returns a scalar row object present at specified index. The rows are counted inclusing header row.
  845. Typical Usage:
  846. my @all_rows = $table_object->rows; # get collection of all row objects in the table
  847. my $third_row = $table_objects->rows(3) # get third row of the table.
  848. =item getRowHavingText($string);
  849. This method returns a single row object that contains specifed text. If the text string is present in any of the row of the table, then that row
  850. objetc is returned. This method supports perl pattern matching using 'qr' operator.
  851. Typical Usage:
  852. my $target_row = $table_object->getRowHavingText("Last Name:"); # access the row which contains text "Last Name:". It will try to match eaxact string.
  853. my $target_row = $table_object->getRowHavingText(qr/First Name:/); # access the row by pattern matching text "First Name:"
  854. =item tableCells([$row, $column])
  855. This method returns an array of all cell objects present in the table. Each element of the array will be a cell object. Please see below for methods on cell object.
  856. Additionaly it also supports two optional parameters, first parameter is index of the row and second is index of column. When these two parameters are used,
  857. it returns a scalar cell object using combination of row and column index.
  858. Typical Usage:
  859. my @allcells = $table_object->tableCells; # get all cell objects in an array
  860. my $target_cell = $table_object->tableCells(2, 5); # get cell at second row and fifth column
  861. =item B<Methods supported for row object>
  862. =item cells([index of cell])
  863. This methos returns an array of cell objects present in that cell. Each elemnt of an array will be a cell object.
  864. Additionaly it supports one optional parameter, cell index.
  865. If this method is used without any argument then it returns array of cell objects in that row. And if you provide an argument,
  866. then it returns a scalar cell object present at specified index.
  867. Typical Usage:
  868. my @all_cells = $row_object->cells; # get collection of all cell objects in that row
  869. my $third_cell = $table_objects->cells(3) # get third cell of that row.
  870. =item B<Methods supported for cell object>
  871. =item cellText
  872. It returns a text present in that cell.
  873. Typical Usage:
  874. $text = $cell_object->cellText; # access the text present in that cell.
  875. In addition to this, all methods listed under LINK METHODS, IMAGE METHODS, BUTTON METHODS, RADIO and CHECKBOX METHODS, SELECT LIST METHODS,
  876. TEXTBOX and TEXTAREA METHODS are supported on cell object.
  877. =back
  878. =head2 FRAME METHOD
  879. =over4
  880. =item * getFrame($how, $what)
  881. It will return the Win32::IEAutomation object for frame document. Frame document is having same structure as that of parent IE instance,
  882. so all methods for link, button, image, text field etc. are supported on frame object.
  883. $how : This is the option how you want to access the select list
  884. $what : This is string or integer, what you are looking for. For this, it also supports pattern matching using 'qr' operator of perl (see example below)
  885. Valid options to use for $how:
  886. 'id:' - Find the frame by matching id attribute
  887. 'name:' - Find the frame by matching name attribute
  888. 'value:' - Find the frame by matching value attribute
  889. 'class:' - Find the frame by matching class attribute
  890. Typical Usage:
  891. $ie->getFrame('id:', "f1"); # access the frame whose id attribute is 'f1'
  892. $ie->getFrame('name:', 'groups'); # access the frame whose name attribute is 'groups'
  893. To control any objects under frame, first get the frame object using 'getFrame' method and then use other methods (getLink, getRadio etc.) to access objects.
  894. $ie->getFrame('name:', 'groups')->getRadio('beforetext:', "Option One")->Select; # select the radio which is under frame
  895. There might be case of frame inside frame (nested farmes), so use getFrame method uptp target frame
  896. $ie->getFrame("name:", "first_frame")->getFrame("name:", "nested_frame");
  897. =item B<Methods supported for frame object>
  898. All methods listed under GENERIC METHODS, LINK METHODS, IMAGE METHODS, BUTTON METHODS, RADIO and CHECKBOX METHODS, SELECT LIST METHODS,
  899. TEXTBOX and TEXTAREA METHODS, FRAME METHODare supported.
  900. =back
  901. =head2 POPUP METHOD
  902. =over4
  903. =item * getPopupWindow($title)
  904. It will return the Win32::IEAutomation object for popup window. Popup window is having same structure as that of parent IE instance,
  905. so all methods for link, button, image, text field etc. are supported on popup object.
  906. Provide the exact title of the popup window as an argument to this method.
  907. Typical Usage:
  908. $ie->getPopupWindow("Popup One"); # access the popup window whose title is "Popup One"
  909. To control any objects under popup, first get the popup object using 'getPopupWindow' method and then use other methods (getLink, getRadio etc.) to access objects.
  910. my $popup = $ie->getPopupWindow("Popup One") # access the popup window
  911. $popup->getRadio('beforetext:', "Option One")->Select; # select the radio
  912. There might be case where popup takes time to load its document, so you can use 'WaitforDone' method on popup
  913. my $popup = $ie->getPopupWindow("Popup One") # access the popup window
  914. $popup->WaitforDone; # wait so that popup will load its document completly
  915. =item B<Methods supported for popup object>
  916. All methods listed under GENERIC METHODS, LINK METHODS, IMAGE METHODS, BUTTON METHODS, RADIO and CHECKBOX METHODS, SELECT LIST METHODS,
  917. TEXTBOX and TEXTAREA METHODS, FRAME METHOD are supported.
  918. =back
  919. =head2 DIALOG HANDLING METHODS
  920. Win32::IEAutomation::WinClicker class provides some methods to ineract with dialogs like security alert, confirmation dialog, logon dialog etc.
  921. COM interface to AutoIt (http://www.autoitscript.com/autoit3/) is used to implement these.
  922. You need to create a new instance of Win32::IEAutomation::WinClicker and then use these methods.
  923. =over 4
  924. =item * Win32::IEAutomation::WinClicker->new( warnings => 1)
  925. warnings
  926. Default value of this is 0. If you want to print warning messages for any object not found, then set it to 1. This is optional.
  927. =item * push_security_alert_yes($wait_time)
  928. It will push the 'Yes' button of Security Alert dialog box. Provide wait time so that it will wait for Security Alert dialog box to appear.
  929. Default value of wait time is 5 seconds (if wait time is not provided). It will timeout after wait time, and execute next line of code.
  930. Typical Usage:
  931. # To navigate to some secure site and pushing 'Yes' button of security alert dialog box
  932. use Win32::IEAutomation;
  933. use Win32::IEAutomation::WinClicker; # this will provide methods to interact with dialog box
  934. my $ie = Win32::IEAutomation->new();
  935. $ie->gotoURL("https://some_secure_site.com", 1); # second argument says that don't wait for complete document loading and allow code to go to next line.
  936. my $clicker = Win32::IEAutomation::WinClicker->new();
  937. $clicker->push_security_alert_yes();
  938. $ie->WaitforDone; # we will wait here for complete loading of navigated site
  939. =item * push_button_yes($title, $wait_time)
  940. It will push 'Yes' button of window which is having provided title.
  941. $title: Provide exact title of dialog box
  942. $wait_time: Provide wait time so that it will wait for confirmation dialog box to appear.
  943. Default value of wait time is 5 seconds (if wait time is not provided). It will timeout after wait time, and execute next line of code.
  944. =item * push_confirm_button_ok($title, $wait_time)
  945. It will push the 'OK' button of any confirmation dialog box.
  946. $title: Provide exact title of dialog box
  947. $wait_time: Provide wait time so that it will wait for confirmation dialog box to appear.
  948. Default value of wait time is 5 seconds (if wait time is not provided). It will timeout after wait time, and execute next line of code.
  949. =item * push_confirm_button_cancle($wait_time)
  950. It will push the 'Cancle' button of any confirmation dialog box.
  951. $title: Provide exact title of dialog box
  952. $wait_time: Provide wait time so that it will wait for confirmation dialog box to appear.
  953. Default value of wait time is 5 seconds (if wait time is not provided). It will timeout after wait time, and execute next line of code.
  954. =item * logon($title, $user, $password, $wait_time)
  955. It will fill the logon dialog box with user name and password, and then press enter.
  956. $title: Provide exact title of logon doalog box
  957. $user: Provide user name to enter
  958. $password: Provide password to enter
  959. $wait_time: Provide wait time so that it will wait for logon dialog box to appear.
  960. Default value of wait time is 5 seconds (if wait time is not provided). It will timeout after wait time, and execute next line of code.
  961. Typical Usage:
  962. # To navigate to some authenticated site and pushing 'Yes' button of security alert dialog box
  963. use Win32::IEAutomation;
  964. use Win32::IEAutomation::WinClicker; # this will provide methods to interact with dialog box
  965. my $ie = Win32::IEAutomation->new();
  966. $ie->gotoURL('http://pause.perl.org');
  967. $ie->getLink('linktext:', "Login")->Click(1); # providing no wait argument says that don't wait for complete document loading and allow code to go to next line.
  968. my $clicker = Win32::IEAutomation::WinClicker->new();
  969. $clicker->logon("Enter Network Password", $user, $password, $wait_time); # here 'Enter Network Password' is the title of log on window
  970. $ie->WaitforDone; # we will wait here for complete loading document
  971. =back
  972. =head1 AUTHOR
  973. Prashant Shewale <pvshewale@gmail.com>
  974. =head1 COPYRIGHT AND LICENSE
  975. This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
  976. =cut