Forum Discussion

vondie's avatar
vondie
Contributor
9 years ago
Solved

Wildcard Not Recognized in URL within an Email

Currently using TC 11.20 and writing scripts using Python. I am working on a script that checks the body content of my last email in my inbox (Outlook).  My end goal is to click a URL within an email to reset a password. My code can identify the URL when I do not put a wildcard at the end, but then the URL is incorrect (should have 'password-reset/b632ffa303463b9445a5685697b0e352' on the end for this example). The URL will change each time the "Forgot Password" link is clicked within the application. Any ideas how I can get this to work? Thanks in advance for your help!

 

def check_email():
  msOutlook = Sys.OleObject["Outlook.Application"]
  
  mapi = msOutlook.GetNamespace("MAPI")
  
  inbox = mapi.Folders("test@test.com").Folders("Inbox")
  
  messages = inbox.Items
  message = messages.getLast()
  body_content = message.body
  url = "http://qaregapps.testurl.com:123456/test/admin/password-reset/*" 
  
  Log.Message(body_content)

  if url in str(body_content): 
    Browsers.Item(btFirefox).Navigate(url)
    Log.Message(url) 
      
  else:
      Log.Message("Email not received.")
  • vondie's avatar
    vondie
    9 years ago

    Thank you both for your input! I decided to follow a little of both pieces of advice and developed the solution below. 

     

    def check_email():
      msOutlook = Sys.OleObject["Outlook.Application"]
      
      mapi = msOutlook.GetNamespace("MAPI")
      
      inbox = mapi.Folders("test@test.com").Folders("Inbox")
      
      messages = inbox.Items
      message = messages.getLast()
      body_content = str(message.body)
    
      s = 'http://qaapps.test.com:12345/test/admin/password-reset/'
      
      time.sleep(10)
    
      if s in body_content:
       url = body_content[237:347] 
     
       Browsers.Item[btFirefox].Run(url)
       
       Log.Message("Test passed!") 
          
      else:
         Log.Message("Email not received. Please open bug in JIRA.")

     

9 Replies

  • You are trying to navigate to a URL with "*" at the end. You're using it to try and find the URL, but you never actually capture the full URL.

     

    Thats never going to work.

     

    You need to use "http://qaregapps.testurl.com:123456/test/admin/pa​ssword-reset" to identify the start point of the URL.

     

    You then need to figure out something that marks the end of the URL string. You need to find something in the body text that denotes the end of the URL and then cut that full section out (you will have start and end points within the body text by this point) and use that for your navigation.

     

    Or if there is any way of identifying where the URL is in the body of the e-mail (does it have anchors or something attached if the link is clickable that you can identify and get co-ordinates of?) then you could click it directly from within the e-mail.

  • AlexKaras's avatar
    AlexKaras
    Champion Level 3

    Hi,

     

    To add to Colin's reply: if the varying part of the url is a set of hexadecimal figures, I would recommend to use regular expressions to extract the url from the message body and then just navigate the browser to the extracted url (even without performing the actual click on the url).

    • vondie's avatar
      vondie
      Contributor

      Thank you both for your input! I decided to follow a little of both pieces of advice and developed the solution below. 

       

      def check_email():
        msOutlook = Sys.OleObject["Outlook.Application"]
        
        mapi = msOutlook.GetNamespace("MAPI")
        
        inbox = mapi.Folders("test@test.com").Folders("Inbox")
        
        messages = inbox.Items
        message = messages.getLast()
        body_content = str(message.body)
      
        s = 'http://qaapps.test.com:12345/test/admin/password-reset/'
        
        time.sleep(10)
      
        if s in body_content:
         url = body_content[237:347] 
       
         Browsers.Item[btFirefox].Run(url)
         
         Log.Message("Test passed!") 
            
        else:
           Log.Message("Email not received. Please open bug in JIRA.")

       

      • Colin_McCrae's avatar
        Colin_McCrae
        Community Hero

        I see how you've done it.

         

        And as long as the URL part always remains in EXACTLY the same position, and stays EXACTLY the same length, it will work. (aka - you've used magic numbers .... which is not recommended)

         

        Personally, I would recommend calculating the start and end points of the capture area at runtime, so if it moves, it won't break.

         

        But you're moving in the right direction .... :smileyhappy: