Reputation: 1483
I'm currently testing the behavior of a text input which apply filtering on a list on keyup event while typing the search term.
I have the following piece of code:
public IWebElement txtSearchInput => _chromeDriver.FindElement(By.XPath("//input[contains(@class, 'search-input')]"));
public void TypeSearchInput(string searchTerm)
{
txtSearchInput.Click();
txtSearchInput.SendKeys(searchTerm);
}
which successfully adds the search term in the input but does not fire up the filtering because the keyup event is not simulated (so the list is not filtered).
What I tried so far was to append the above method with txtSearchInput.SendKeys(Keys.Up);
but did not work and I cannot see any workaround on this.
This is a reference for the front-end implementation written on Angular:
<input type="text"
class="search-input"
placeholder="Type to filter.."
[(ngModel)]="searchTerm"
(keyup)="filterUsers()"/>
Any ideas/help would be much appreciated. Just to highlight the setup is SpecFlow, Selenium, ChromeDriver, VS 2022. Thanks
Upvotes: 0
Views: 340
Reputation: 1659
You can use Keys class of Selenium framework and send the Enter key, so I can assume that filtering will work after Enter key send.
txtSearchInput.SendKeys(searchTerm + Keys.Enter);
UPDATE:
Because this is an angular site, Selenium should wait until angular initialization.
So here is the function for waiting until page loads fully:
private void WaitForAngular(IWebDriver driver)
{
WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(20));
wait.Until(webDriver =>
{
return ((IJavaScriptExecutor)webDriver)
.ExecuteScript(
"return window.getAllAngularTestabilities().findIndex(x=>!x.isStable()) === -1")
.Equals(true);
});
}
Thanks this article for right direction.
And the calling code:
driver.Navigate().GoToUrl("http://angularsamplegithubproject.s3-website-us-east-1.amazonaws.com/");
WaitForAngular(driver);
string xpath = "//input[contains(@class, 'search-input')]";
IWebElement txtSearchInput = driver.FindElement(By.XPath(xpath));
txtSearchInput.Click();
txtSearchInput.SendKeys("moj");
Upvotes: 2
Reputation: 1483
After applying several techniques, it seems that the issue was the ChromeDriver speed and not that the keyup is not fired.
As a workaround I ended up with the following amend:
public void TypeSearchInput(string searchTerm)
{
txtSearchInput.Click();
foreach (var character in searchTerm)
{
Thread.Sleep(200);
txtSearchInput.SendKeys(character.ToString());
}
}
I hope this will be helpful for anyone struggling with the same issue.
Upvotes: 1