Selenium FindsByAll, FindsBySequence and FindsBy difference in C#

This article is about difference between FindsByAll, FindsBySequence and FindsBy attributes in Selenium.

They are found in the PageObject namespace – OpenQA.Selenium.Support.PageObjects . A piece of important news about this namespace is that it is obsolete. More information on it – link. An alternative is to use the SeleniumExtras package.

SETUP

To explain, I will be using SauceLabs’s demo site – Swag Labs. On this page, I will focus on finding WebElement for username, password, and login button using the above attributes.

1. FindsBy

We all know the normal way of clicking a WebElement by locator in C#, as below.

driver.FindElement(By.XPath("//button[@id='login-button'']")).Click();

Using PageFactory, we need to use the Page Object Pattern. We generally initialize all the elements using PageFactory.InitElements(driver, this); within the constructor. We can use FindsBy Attribute on the variable declaration within the class.

[FindsBy(How = How.XPath, Using = "//input[@id='login-button']")]  
private IWebElement loginButton;
loginButton.Click(); //execute

Possible enums for How are : XPath, TagName, PartialLinkText, Name, LinkText, Id, CssSelector, ClassName.

Some of the readers would be like – “SOOOO…..WHAT’S SO GREAT ABOUT IT ???? !!?!”. What makes it great is – It allows us to assign multiple locators to a single WebElement. Consider this common scenario in A/B testing – the product manager needs to redesign the login page. This page is live to 25% user. In this scenario, the developer changed the id attribute of User name textbox from username to user-name. In this scenario, QA has two choices of using the newer id or asking the developer to use older id. FindsBy allows using both 🙂

[FindsBy(How = How.Id, Using = "username")]
[FindsBy(How = How.Id, Using = "user-name")]
private IWebElement username;

Above is just a small example. In a real situation, a new developer might be working for 20 to 30 elements and renamed them. Time is less to maintain the locators. The product team can introduce A/B/C variants in testing for multiple elements. In such a situation, adding more FindsBy makes it easy to maintain the scenario code. One can view them as a logical OR of the various FindsBy Attributes declared.

2. FindsByAll

FindsByAll is similar to above but instead of logical OR it is a logical AND. It finds by combining all the break up pieces of a certain web element. For example is password text area.

[FindsByAll]
[FindsBy(How = How.TagName, Using = "input")]
[FindsBy(How = How.Id, Using = "password")]
private IWebElement password;

Personally, this makes it lengthier and more verbose. The above can also be done using [FindsBy(How = How.CssSelector, Using = "Input#password")]

3. FindsBySequence

FindsBySequence is similar to FindsByAll but it will try to find relationship between a parent and child node. For example is Login Button

[FindsBySequence]
[FindsBy(How = How.TagName, Using = "form")]
[FindsBy(How = How.Id, Using = "login-button")]

Personally, this makes it lengthier and more verbose. The above can also be done using [FindsBy(How = How.XPath, Using = "//form//input[@id='login-button']")]

Leave a Reply

Your email address will not be published.