Home > database >  Time where minutes are not counted in a C# assertion snippet for an appium test
Time where minutes are not counted in a C# assertion snippet for an appium test

Time:10-06

I am attempting to ensure that hours are not off minutes I am not too concerned with but can't seem to break them off the code. Is there a way to allow the assertion to be 10 minutes off?

    public void DeviceTimeAssertion()
    {
        IWebElement TimeTitle = _driver.FindElementByXPath(" 
        (//android.widget.TextView[@content-desc='xpathvalue'])[1]");
        string Time = DateTime.Now.ToString("MM/dd/yy h:mm tt");
        //string Time = DateTime.Now.ToString("MM/dd/yy h:mm"); 
        string ActText = TimeTitle.Text;
        string ExpectedTime = "Check-in: "   Time;
        //if (ExpText.Equals(ActText))
        Assert.AreEqual(ExpectedTime, ActText);

CodePudding user response:

I think you need to do that with actual DateTime objects. You can try to do that:

var time = DateTime.Now;
var actText = TimeTitle.Text;
var isTime = DateTime.TryParse(actText.Split(' ')[1], out var actTime)
if(isTime) 
{
  var diff = TimeSpan.FromMinutes(10);
  Assert.IsTrue(time   diff > actTime && time - diff < actTime);
}
else
{
  throw new ArgumentException("There was no Time in the text :(");
}

Hope that helps

  • Related