0

I'm doing an E2E test on an Angular/C# application with Selenium.

I want to fill in a angular material form with Selenium.

There are 2 form-fields: date1 and date2

Date1 works fine: I clear the field and fill in the new date:

public void Wait(IWebDriver _webDriver, string elementString)
{
   WebDriverWait wait = new WebDriverWait(_webDriver, TimeSpan.FromSeconds(5));
   IWebElement element = wait.Until(ExpectedConditions.ElementIsVisible(By.Id(elementString) ));
} 

public void Test()
{
   Wait(_webDriver, "date1");
   for (int i = 0; i < 15; i++) {
   webDriver.FindElement(By.Id("date1")).SendKeys(Keys.Backspace);
   }
   webDriver.FindElement(By.Id("date1")).SendKeys(DateTime.Now.AddDays(1).ToString("d"));


}

HTML code:

<mat-form-field class="half-width-field">          
   <mat-label> date1</mat-label>
   <input matInput class="form-control"
      formControlName="date1"
      id=date1
      matInput [matDatepicker]="pickerEnd" 
      placeholder="MM/DD/YYYY">
    <mat-datepicker-toggle matSuffix [for]="pickerEnd"></mat-datepicker-toggle>
          <mat-datepicker #pickerEnd></mat-datepicker>
    <mat-error *ngIf="updateProductForm.controls.date1.hasError('required')">
                      date1 is required
    </mat-error>   
    <mat-error *ngIf="updateProductForm.controls.date1.hasError('mismatch')">
              date1 must before due
    </mat-error>       
    </mat-form-field>

<mat-form-field class="half-width-field">          
   <mat-label> date2 </mat-label>
   <input matInput class="form-control"
      formControlName="date2"
      id=date2
      matInput [matDatepicker]="pickerStart" 
      placeholder="MM/DD/YYYY">
    <mat-datepicker-toggle matSuffix [for]="pickerStart"></mat-datepicker-toggle>
          <mat-datepicker #pickerEnd></mat-datepicker>
    <mat-error *ngIf="updateProductForm.controls.date2.hasError('required')">
                       date2 is required
    </mat-error>   
    <mat-error *ngIf="updateProductForm.controls.date2.hasError('mismatch')">
              date2 must before due
    </mat-error>       
</mat-form-field>
  

When I try to do this with date2, I get a NotInteractable exception.

I use wait until visible (and tried also clickable and also a delay of 10 seconds.

webDriver.FindElement(By.Id("date2")).Displayed gives as result true?

What is going wrong?

cruisepandey
  • 28,520
  • 6
  • 20
  • 38

1 Answers1

0

There is a Clear() method available for WebElement in Selenium - C# bindings.

So Instead of this :

for (int i = 0; i < 15; i++) {
   webDriver.FindElement(By.Id("date1")).SendKeys(Keys.Backspace);
   }

Simply remove the loop and use Clear() method

webDriver.FindElement(By.Id("date1")).Clear()

and since the second date input field is displayed. You can try to click first then clear it and then send the keys.

webDriver.FindElement(By.Id("date2")).Click(); 
webDriver.FindElement(By.Id("date2")).Clear(); 
webDriver.FindElement(By.Id("date2")).SendKeys("New date here"); 
cruisepandey
  • 28,520
  • 6
  • 20
  • 38