Fred Garcia Fred Garcia - 2 years ago 112
Python Question

Time format on Python tests

I would like to test if the format is something like '03:10 ~ 04:10' in python tests, however I have a problem getting it correctly.
This is my code

def test_activity_today_time_format(self):
"""Test time format is HH:MM """
driver = self.driver
# Get page by URL
driver.get(self.base_url + "/userk/inbox/")
# Check time format
self.assertEqual("%H:%M ~ %H:%M", driver.find_element_by_xpath("//td[3]").text)

I think i got a problem with this logic

"%H:%M ~ %H:%M"

Answer Source

You are literally comparing the string that is supposed to be time format with the date itself; can't do that. You can use regular expressions. Something like this:

text = driver.find_element_by_xpath("//td[3]").text
pattern = re.compile(r"((1?\d)|2[0-4]):[1-5]?\d ~ ((1?\d)|2[0-4]):[1-5]?\d")
self.assertRegexpMatches(text, pattern, "Date format not correct")
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download