0

I was looking at the file uploads section in the Watir wiki Watir File Uploads

The environment which I am using:

  • Ruby: 1.8.7
  • Watir: 1.8.1

my code looks like this:

ie = Watir::Browser.start("tinypic.com") 
ie.file_field(:id, "the_file").set("C:\\image.png")

Now, when the file "image.png" does not exist, it produces a popup as shown below Error Popup

I tried to use the popup-handlers present in the Javacript popup section on the Wiki, but after debugging, I found out that the set method does not return unless the popup is handled.

What would be a good way to handle it? While searching on Google, I found out somewhere that you could launch a different process in Ruby and try to close the popup window down. If that is the case, can I get some sample code or guidelines as to how to proceed doing the above mentioned thing?

Thanks for any help.

chaitanya
  • 1,591
  • 2
  • 24
  • 39
  • Are you using Watir or Watir-Webdriver? you tagged both but did not specify which you are using in your question. This is important as they have different facilities for dealing with popups. – Chuck van der Linden Dec 12 '11 at 21:47
  • I am using watir, just added the information in the question – chaitanya Dec 12 '11 at 21:51
  • Why are you trying to upload the file that does not exist? – Željko Filipin Dec 13 '11 at 10:09
  • well I am wrapping this Watir method in a method of mine and I was running it as a part of a negative unit test and it failed – chaitanya Dec 13 '11 at 17:08
  • Negative tests are not the best test cases to automate in general. In this case, it's a negative test case on what, the file upload capability of the operating system? I have never seen that case mishandled. I would not spend automation time on that type of test. Just my advice. – Dave McNulla Dec 15 '11 at 06:13
  • Dave I completely agree with your point, but some websites like imageshack.us also check whether a file having certain valid extensions is uploaded or not. So, instead of a .gif,.jpeg, if you upload a .zip file, I guess there might be some server side validation involved which determines if the file extension is valid or not and generates a popup. Please correct me if I'm wrong – chaitanya Dec 15 '11 at 17:20
  • That is a good point. The key is knowing the boundary, if you have server side logic that needs to be validated, then 'negative tests' are valid. The same goes for how the app handles OS or Browser generated errors, if there is any special error handling involved. But any test (negative or positive) that extends past testing the code your devs are creating is almost always providing no value to your employer. (exceptions exist, but even then most of them are not worth automating) – Chuck van der Linden Dec 18 '11 at 23:28

2 Answers2

0

I'm pretty interested in this answer as well, and I have some information... I'm using watir-webdriver, but I think that we may be having the same issue here.

In watir-webdriver, I can handle most popups by using the AlertHelper extension, or by manually overwriting the javascript function. These two techniques are described at http://watirwebdriver.com/javascript-dialogs/ ...and the "manual override" (further down on the page) should certainly work for IE Watir as well.

You'd include this line: ie.execute_script("window.alert = function() {}")

immediately before your line: ie.file_field(:id, "the_file").set("C:\image.png")

That might work for you.

However, these techniques don't work if an alert or dialog is generated dynamically by AJAX and PHP. I've been searching for a solution as to how to interact with dynamically-loaded javascript alerts that are called when the PHP sends them to the page. The discussion at https://github.com/jarib/watir-webdriver/issues/103 leads me to believe that selenium-webdriver can do just it by using alert_box.text and alert_box.dismiss methods, but the watir-webdriver API hasn't taken advantage of that yet, as far as I can see.

Thanks to anyone who can present a more thorough solution as to how to handle dynamically generated alerts in watir and watir-webdriver.

Kevin

emery
  • 8,603
  • 10
  • 44
  • 51
  • I think Webdriver does do that, or at least something similar. See this answer (http://stackoverflow.com/a/8172888/409820) which describes the methods watir-webdriver has for dealing with javascript dialogs. – Chuck van der Linden Dec 18 '11 at 23:37
0

Thanks for your input Kevin,

I actually came up with this answer [which I tweaked it from the Watir Wiki method 1] Credits to George for the following post on Watir General. I wrote a popup handler like this:

require 'win32ole' 
title = "Choose File to Upload"
  
      begin
         popupOccurred = false
         autoIt = WIN32OLE.new('AutoItX3.Control')
         timeout = 15
         autoIt.WinWait(title, '', timeout)
         autoIt.ControlClick(title, "", "&Open")
         sleep 1
         autoIt.WinWait(title, '', timeout)
         isClicked = autoIt.ControlClick(title, "", "OK")
         sleep 1
         autoIt.ControlClick(title, "", "Cancel") if (isClicked == 1)
         autoIt.ole_free
      rescue
        puts "Error closing popup, please close Manually"
      end

and in my code snippet, I did the following:

require 'watir'

b = Watir::Browser.start("tinypic.com")
#-- Launches the popup closer in a different process. other methods like
#-- Process.create or system(start rubyw C:\\popup_handler2.rb) or having the
#-- popup handler in a Thread did not work for me.
system("start ruby C:\\popup_handler2.rb") 
b.file_field(:id, "the_file").set("C:\\image.png")

I would definitely appreciate some feedback from the Watir gurus/devs/users out there.

Thanks,

Chaitanya

Community
  • 1
  • 1
chaitanya
  • 1,591
  • 2
  • 24
  • 39