4

I have to use Selenium to test an application with a live time stamp.

My code is:

String validationmessage=driver.findElement(By.xpath("//div[@id='part_log_details']/p[1]")).getText();

System.out.println(validationmessage);

        Assert.assertEquals("12-27-2017 - 23:00:47 :  updated inventory from 20 to 21. Reason : Quantity received from PO #SCT1075", validationmessage);

But if I run it again, the time stamp and inventory will change to the following:

12-27-2017 - 23:45:47 : updated inventory from 21 to 22. Reason : Quantity received from PO "#SCT1076"

Any one give me the solution

The HTML is:

<div id="part_log_details" style="">
  <p>12-28-2017 - 02:21:21 :  updated inventory from 23 to 25. Reason : Quantity received from PO #SCT1077</p>
  <p>12-28-2017 - 02:16:05 :updated inventory from 21 to 23. Reason : Quantity received from PO #SCT1076
  <p>12-27-2017 - 23:00:47 : updated inventory from 20 to 21. Reason : Quantity received from PO #SCT1075</p>

How do I handle this situation?

3

What you need to do is some string parsing.

I'm going to assume that you know what quantity you've added to the inventory as part of your code, but you don't know what the PO was.

So, as part of your test setup, you save current system datetime. You know the update timestamp is going to be close to your saved datetime, so you use date & time libraries to create a time range.

If your system timestamp is 12-28-2017 - 05:00:15, and you're working with a 1 minute time range, you'd use your language's date/time library to add 1 minute and give you a maximum timestamp of 12-28-2017 - 05:01:15.

Now comes the parsing fun:

  • First, you get the substring from before the first :.
  • From that you can use a string to date conversion to get the actual timestamp logged.
  • Then you assert that the actual timestamp is between your minimum and maximum timestamps. You may need a few attempts to get the time range to where you're not getting overlap and accurately catching the true timestamp.
  • To get the quantity change, you take the substring between the words from and to, trim leading and trailing spaces, then convert to an integer. That's your old quantity.
  • Then take the substring between the word to and the first period (.). Again, trim leading and trailing spaces, then convert to an integer to get your new quantity.
  • Your final assert is that the difference between new quantity and old quantity is the quantity you added to the inventory.

It's fiddly and messy, but when you're dealing with something like this, unless you have access to either a database where you can directly check timestamps or some other kind of information you're maintaining independently, there aren't many options.

Honestly, in this situation, I'd be looking for something more easily tested than trying to parse dates and quantity changes from one long string.

  • 1
    What OP needs is some common programming skills: decompose problem into smaller, design data structures to help. – Peter M. - stands for Monica Dec 28 '17 at 15:11
  • 1
    The final sentence in this answer is, in my opinion, the most-important consideration. Unless this feature is high risk/high impact, and unit tests don't adequately cover it, and it is difficult or time-intensive to test manually, it may be best to focus on creating Selenium tests for other areas of the product. This test is likely to be fragile no matter how the problem is solved. – VanderLinden Dec 28 '17 at 15:37
  • 1
    @VanderLinden - well, yes. This is one of the reasons I took the time to answer. We were all that... new to testing once. – Kate Paulk Dec 28 '17 at 15:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.