5

I am using Marathon (Java desktop application testing tool) to automate regression testing. Marathon uses Jython so I can use Java libraries and Python libraries. As my script goes through filling out certain fields, various fields appear (or don't appear) based on values that I entered in previous fields. I need to skip over the fields that aren't there for the obvious reason. When fields are disabled, but still there, this is fine because I can use

    if Component.isEnabled():
        #do something
    else:
        #do something

The problem is when the component doesn't exist. Is there any way, in Java, to test for the existence of a component? For example, Component.exists() would suit my needs, but there isn't such a method in the component class.

I would prefer to solve my problem by using an if Component.exists(): statement, but I am able to get around it using a try, except block. However, this leads to major execution time issues for the script. It tries to find the component for ~2 or 3 minutes before it throws an exception. The only way I could see around this issue is if there was some sort of statement like try for x seconds and move on if component is not found. Is there any way to limit the amount of time you try any given statement?

Ross Allen
  • 43,772
  • 14
  • 97
  • 95
Nick L
  • 281
  • 1
  • 6
  • 18
  • 1
    On the Java side you can use a `FutureTask` and an `Executor`. `FutureTask` implements `Future` which has a `.get()` with timeout (throwing `TimeoutException` if timeout) – fge Mar 20 '14 at 15:20

2 Answers2

2

I found some code to throw a time-out exception in your code as an answer to another Stackoverflow-question: What should I do if socket.setdefaulttimeout() is not working?, it however only works on linux-machines as stated in the link.

Concretely:

import signal, time

class Timeout():
  """Timeout class using ALARM signal"""
  class Timeout(Exception): pass

  def __init__(self, sec):
    self.sec = sec

  def __enter__(self):
    signal.signal(signal.SIGALRM, self.raise_timeout)
    signal.alarm(self.sec)

  def __exit__(self, *args):
    signal.alarm(0) # disable alarm

  def raise_timeout(self, *args):
    raise Timeout.Timeout()

# Run block of code with timeouts
try:
  with Timeout(60):
    #do something

except Timeout.Timeout:
  #do something else

This will try "do something" for 60 seconds and moves on if the execution exceeds 60 seconds...

Community
  • 1
  • 1
Fematich
  • 1,588
  • 14
  • 26
  • I appreciate the response, but unfortunately I don't have a Linux machine. – Nick L Mar 21 '14 at 18:56
  • This example worked for what I was looking for. However, it seems I can not catch the error name. For instance, if you catch the exception like this `except Exception as e:` and do a `print(e)`, nothing is printed. – muammar Jul 16 '20 at 18:31
1

this is the solution to your problem:

import signal
def time_out(self):
    def signal_handler(signum, frame):
        raise Exception("Timed out!")

    signal.signal(signal.SIGALRM, signal_handler)
    signal.alarm(3)  
    all_games = []
    try:
        while True:
            #do what you want to do

    except Exception as msg:
        print msg

3 is the time for the function time out

best regards, Shlomy

Shalom Balulu
  • 379
  • 1
  • 9
  • 20