1

I have a web table to verify Values:

ID   Date           %   Month
1   30-10-2017   75   JAN
2   18-10-2017   80   FEB
3   01-11-2017   60   MAR
4   22-10-2017   67   APR

I want to verify
1. Date values are within range '15-10-2107' to '15-11-2017'
2. % Range between 50-90
3. Month Range between Jan - May
I am using Selenium C# for verification. What will be the best approach?

2 Answers 2

1
  1. use xpath to narrow the dataset firstly (so that you no need to loop all rows of table)

    string xpath = '//table//td[3][number(text())>=50 and number(text())<=90]';
    first_filter_rows = driver.FindElements(By.xpath(xpath));
    
  2. use month range do the 2rd filter on the first_filter_rows

    Regex reg = new Regex("Jan|Feb|Mar|Apr|May");
    ArrayList second_filter_rows = new ArrayLis();
    foreach(row in first_filter_rows) {
    string month = row.FindElement(By.xpaht('./td[4]')).text;
    if(reg.IsMatch(month)) {
     second_filter_rows.add(row);
    }
    }
    
  3. use date range do the 3rd filter on the second_filter_rows

    var start = DateTime.ParseExact("15-10-2017", "dd-mm-yyyy",
           System.Globalization.CultureInfo.InvariantCulture);`
    
        var end = DateTime.ParseExact("15-11-2017", "dd-mm-yyyy", 
           System.Globalization.CultureInfo.InvariantCulture);
    
        ArrayList third_filter_rows = new ArrayLis();
    
        foreach(row in second_filter_rows) {
          string date = row.FindElement(By.xpaht('./td[2]')).text;
          var dateFromtable = DateTime.ParseExact(date, "dd-mm-yyyy", 
        System.Globalization.CultureInfo.InvariantCulture);
        if(dateFromtable >= start && dateFromtable < end) {
            third_filter_rows.add(row);
            // output to your report
        }
        }
    
        Assert(third_filter_rows.length > 0, IsTrue);
    
Sign up to request clarification or add additional context in comments.

2 Comments

why do you use additional citation around your code blocks? Do you refer to code elsewhere?
the code block toolbar can't work as expected some times.
1

To test a date falls within a specified range, you can construct a DateTime object and assert that its within a 2 set DateTime ranges:

var start = DateTime.ParseExact("15-10-2017", "dd-mm-yyyy", System.Globalization.CultureInfo.InvariantCulture);
var end = DateTime.ParseExact("15-11-2017", "dd-mm-yyyy", System.Globalization.CultureInfo.InvariantCulture);
var dateFromtable = DateTime.ParseExact([DATE_FROM_TABLE], "dd-mm-yyyy", System.Globalization.CultureInfo.InvariantCulture);

Assert.That(dateFromtable  >= start && dateFromtable < end, Is.True);

To test your % a simple int cast and check similar to the date assert above.

Your Month check can be done in many different ways, the simplest most straight forward i believe would be to construct a list that contains acceptable months

var acceptableMonths = new[] {"Jan", "Feb", "Mar", "Apr", "May"}; //Assuming may is included in the list
Assert.That(acceptableMonths.Contains([MONTH_READ_FROM_TABLE]), Is.True)

2 Comments

Is there any other way than using Assert.That, e.g. using if else, as I need to add the results to my reports too...
The first argument in all the 'Assert.That' actually returns a bool obj. So you can do if(dateFromtable >= start && dateFromtable < end) of if(acceptableMonths.Contains([MONTH_READ_FROM_TABLE]) and it should still work

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.