Ask a Question

Wildcard Not Recognized in URL within an Email

SOLVED
vondie
Contributor

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.")
9 REPLIES 9
Colin_McCrae
Community Hero

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
Community Hero

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).

Regards,
  /Alex [Community Hero]
____
[Community Heroes] are not employed by SmartBear Software but
are just volunteers who have some experience with the tools by SmartBear Software
and a desire to help others. Posts made by [Community Heroes]
may differ from the official policies of SmartBear Software and should be treated
as the own private opinion of their authors and under no circumstances as an
official answer from SmartBear Software.
The [Community Hero] signature is used with permission by SmartBear Software.
https://community.smartbear.com/t5/custom/page/page-id/hall-of-fame
================================

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.")

 

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 .... Smiley Happy

Appreciate the feedback. Yes, I am aware it could fail if the email changes, but that's exactly what we want to happen in regression testing. Thank you!

And one more note 🙂 :

 

> Browsers.Item[btFirefox].Run(url)
> Log.Message("Test passed!")

The first line of code tries to navigate to the provided url with no verification of success been performed. But instead the code just reports about success without any evidence of it. This might appear to be misleading and incorrect (e.g. url is provided but pointing to the wrong location).

Regards,
  /Alex [Community Hero]
____
[Community Heroes] are not employed by SmartBear Software but
are just volunteers who have some experience with the tools by SmartBear Software
and a desire to help others. Posts made by [Community Heroes]
may differ from the official policies of SmartBear Software and should be treated
as the own private opinion of their authors and under no circumstances as an
official answer from SmartBear Software.
The [Community Hero] signature is used with permission by SmartBear Software.
https://community.smartbear.com/t5/custom/page/page-id/hall-of-fame
================================

I'll echo Alex here.

 

You won't know it's changed or failed with the current test. It captures a pre-defined string (based on hard coded numbers) and tries to navigate to it but doesn't check it works. As long as it finds the first part of the URL in the e-mail, it will pass in it's current state regardless.

 

But if all you want to verify is that you got an e-mail with a password reset URL in it, then take out the navigation part and it's fine.

 

If you want to verify that the link is actually valid and works, you need to do a little more work.

I appreciate the feedback. I did not include the full script in this thread because I didn't think it was relevant to the solution (or for anyone in the future that has the same issue).  Rest assured, the full test contains the actual process for changing the password, logging in again with the new password, and verifying that the dashboard page loads. 

> Rest assured, the full test contains the actual process for changing the password, logging in again with the new password, and verifying that the dashboard page loads.

 

Sounds great! Nothing to add. Test has been approved. :)))))

Regards,
  /Alex [Community Hero]
____
[Community Heroes] are not employed by SmartBear Software but
are just volunteers who have some experience with the tools by SmartBear Software
and a desire to help others. Posts made by [Community Heroes]
may differ from the official policies of SmartBear Software and should be treated
as the own private opinion of their authors and under no circumstances as an
official answer from SmartBear Software.
The [Community Hero] signature is used with permission by SmartBear Software.
https://community.smartbear.com/t5/custom/page/page-id/hall-of-fame
================================
cancel
Showing results for 
Search instead for 
Did you mean: