Warning: fopen(/tmp/SghXTngBZPli-m3FClf.tmp): failed to open stream: Disk quota exceeded in /home/executea/public_html/blog/wp-admin/includes/class-wp-filesystem-ftpext.php on line 139
Warning: unlink(/tmp/SghXTngBZPli-m3FClf.tmp): No such file or directory in /home/executea/public_html/blog/wp-admin/includes/class-wp-filesystem-ftpext.php on line 142
Page Object Model (POM) is best known for modeling the UI objects of page within test codes was discussed a lot more detail as article as well as in video in our blog as shown below
In this article we will discuss how to work with Page Object Model in Selenium with C# also we will discuss about some of the minor changes in Selenium with C# and Java
Minor Changes in Selenium C#
JAVA
C#
POM of Java works even without specifying the @FindBy attribute, since by default Java POM picks the WebElements by their ID or Name specified as the name of the property in POM class
In C# POM the option is not available and it throws NOSUCHELEMENTEXCEPTION
POM in Java first identifies the element with ID and if it fails, then it try to identify the control using its Name
C# it identifies element only with ID but not with Name
Here is the complete video of the above discussion
Here is the snippet of code from the above video
EAPageObject
class EAPageObject
{
public EAPageObject()
{
PageFactory.InitElements(PropertiesCollection.driver, this);
}
[FindsBy(How = How.Id, Using = "TitleId")]
public IWebElement ddlTitleID { get; set; }
[FindsBy(How = How.Name, Using = "Initial")]
public IWebElement txtInitial { get; set; }
[FindsBy(How = How.Name, Using = "FirstName")]
public IWebElement txtFirstName { get; set; }
[FindsBy(How = How.Name, Using = "Save")]
public IWebElement btnSave { get; set; }
}
Thanks for watching the video and reading the post!!
Please leave your comments and let me know if there is anything need to be improved in the post!!!
Thanks,
Karthik KK
Post Author:
Karthik kk
43 Replies to “POM in Selenium C#”
Hello,
I attempted to follow your teaching in video 8 POM. I’m getting an error when I write the FindsBy attribute on the How key word – the error is “LaunchQuest.FindsByAttribute does not contain a definition for How”
This is the code.
using OpenQA.Selenium;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using OpenQA.Selenium.Support.PageObjects;
namespace LaunchQuest
{
class PersonPageObjects
{
[FindsBy(How = How.Id,using=”a.mainNav-person”)]
public IWebElement ClickPerson { get; set; }
}
}
Hi Karthik,
These all videos are of great use to people who want to learn Selenium.
Thanks for these videos. Can you also suggest some dummy sites, where we can practice selenium with some more web elements available.
How to handle explicit wait in POM (If we use Page factory). In the Expected conditions class there is no method for webelement. All methods support (By by). So if we take webelement then how to handle this.
I am trying to implement POM in my Selenium c# Nunit framework. Just wanted to ask if it is a good idea to save the ‘Using’ values in an excel sheet and read it from there?
Example:
@FindBy(How=How.id, Using=”abc”);
Instead of the above statement, can we store the”abc” in an excel sheet and populate it from there? is it a good practice?
Its not a good practice to bring property name from excel sheet, but the best way is to keep them in Java file, since anyways any change has to be modified either in excel or java file, so its better keep them in java file.
Oops, since you have mentioned @FindBy(How=How.id, Using=”abc”); I thought you are looking for solution in Java, since in C# there is no @FindBy its only [FindsBy(How = How.Id, Using='abc')]
Once again, maintaining the identification property in C# file is best practice and recommended.
i have created POM code and initialized using Pagefactory class. while executing code i’m getting an exception
An exception of type ‘System.ArgumentException’ occurred in WebDriver.Support.dll but was not handled in user code
Additional information: Type of member ‘SelectElement’ is not IWebElement or IList
Code:
public POMTest()
{
PageFactory.InitElements(PropertiesCollection.driver, this);
}
[FindsBy(How=How.XPath, Using = “//*[text()=’REGISTER’]”)]
public IWebElement RegisterLink { get; set; }
[FindsBy(How = How.Name, Using = “firstName”)]
public IWebElement FirstName { get; set; }
[FindsBy(How = How.Name, Using = “country”)]
public SelectElement Country { get; set; }
Hi Karthik,
How to handle explicit wait in POM (If we use Page factory). In the Expected conditions class there is no method for webelement. All methods support (By by). So if we take webelement then how to handle this.
I am not able to find any solution for that.
Still i tried below below two lines of code in the method but i am not able to run testcases.
Here is code which i tried,
public static void waitUntillElementIsVisible(this IWebElement element)
{
driver = ((IWrapsDriver)element).WrappedDriver;
//code 1
1. new WebDriverWait(driver, TimeSpan.FromSeconds(new Constant().normalwait)).Until((driver) => element.Displayed);
//code 2
2. new WebDriverWait(driver, TimeSpan.FromSeconds(10)).Until(ExpectedConditions.ElementExists(((By)element)));
Hi Karthik can u please help me with following error message with pageobject
Message: System.ArgumentException : The SearchContext of the locator object cannot be null
Parameter name: locator
Hi, Thank you for the videos, they are really helpful.
I am trying to implement POM in my project and followed every step till Video 7 . Once I created Page factory and tried to run I am getting the below error, please help.
Result Message: System.TypeLoadException : Method ‘GetProperty’ in type ‘OpenQA.Selenium.Support.PageObjects.WebElementProxy’ from assembly ‘WebDriver.Support, Version=2.35.0.0, Culture=neutral, PublicKeyToken=null’ does not have an implementation.
However, I experience NoSuchElementExceptions whenever an element is not found using an expression body. Normally i used my extension method to check whether an element exists, but the new method throws the error already. Do you have a recommended solution for this?
Yes, this will happen for sure, this issue can be fixed by writing a custom extension method of your own for IWebElement and call the extention method in the lambda expression
Please see kindly below:
(…)
public IWebDriver Driver;
public BoaRegistrationPage(IWebDriver driver)
{
this.Driver = driver;
PageFactory.InitElements(driver, this);
}
[FindsBy(How = How.Id, Using = “ReportingPeriodName”)]
public SeleniumKendoDropDownList ReportingPeriodDropDown { get; set; }
[FindsBy(How=How.Id, Using = “Type”)]
public SeleniumKendoDropDownList BranchType { get; set; }
I got an error when running tests:
Message: System.ArgumentException : Type of member ‘SeleniumKendoDropDownList’ is not IWebElement or IList
How can I solve it? I cannot use IWebElement because I have KendoDropDownLists in the application.
public class SeleniumKendoDropDownList : KendoDropDownList
{
public SeleniumKendoDropDownList(IWebElement webElement) : base(webElement)
{
this.CopyInternalId(webElement);
}
Hi Karthik,
As you know page factory feature in Selenium C# has been deprecated. I followed one your video on this topic and made changes to my Loginpage object code as follows. But this has lead to issue where Loginpage object method in program.cs file is expecting an argument. Please let me know how argument should be provided if my Loginpage object method is as follows;
class OpenCMSloginPageObject
{
private readonly RemoteWebDriver _driver;
public OpenCMSloginPageObject(RemoteWebDriver driver) => _driver = driver;
I saw your videos. it very helpful for me to start creating my first Nunit project in Visual Studio using C#. but I need some direction about how to pass driver instance to page object class using base and hooks class. I am using Selenium 3.11 driver.
Your blogs are v good and easy to understand. I follow you on your UTube channel as well.
Can you please put the points here which you have explained in video .
I
Hello,
I attempted to follow your teaching in video 8 POM. I’m getting an error when I write the FindsBy attribute on the How key word – the error is “LaunchQuest.FindsByAttribute does not contain a definition for How”
This is the code.
using OpenQA.Selenium;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using OpenQA.Selenium.Support.PageObjects;
namespace LaunchQuest
{
class PersonPageObjects
{
[FindsBy(How = How.Id,using=”a.mainNav-person”)]
public IWebElement ClickPerson { get; set; }
}
}
Hi Karthik,
Although the title says POM in C#. The video is giving the tutorial in Java.
Can you please check?
Thanks,
Ankita
Hi Ankita,
You are correct, the videos
Page Object Model in Selenium — Part 1
Page Object Model in Selenium — Part 2
Are in Java, but the core video is still in C#, since the Part 1 and Part 2 gives a detail theoretical explanation as well !!!
Thanks,
Karthik KK
Were do i find the core video ?
What do you mean by core video ?
Hi Karthik,
Seems the Part-1 and Part-2 videos mentioned above are exactly the same. Please let me know if I understood it wrong.
All the videos are of great use though.
Thanks.
Its the Page Navigation model difference !
Hi Karthik,
I am new to selenium your videos are very helpful, Can you please explain about implicit and explicit waits and when they can be used safely?
Thanks,
swat
Checkout video from here https://www.youtube.com/watch?v=0eSORVemv80&index=3&list=PL6tu16kXT9PoIHrwaqkBitAmjlrG1Bgxp
Hi Karthik,
These all videos are of great use to people who want to learn Selenium.
Thanks for these videos. Can you also suggest some dummy sites, where we can practice selenium with some more web elements available.
There is one right here executeautomation.com/demosite/Login.html, not sure if the controls are enough for your practice !!!
How to handle explicit wait in POM (If we use Page factory). In the Expected conditions class there is no method for webelement. All methods support (By by). So if we take webelement then how to handle this.
Hi Yaswanth
I am also in the same condition did you get any answer for your question.
Hi Karthik,
I am trying to implement POM in my Selenium c# Nunit framework. Just wanted to ask if it is a good idea to save the ‘Using’ values in an excel sheet and read it from there?
Example:
@FindBy(How=How.id, Using=”abc”);
Instead of the above statement, can we store the”abc” in an excel sheet and populate it from there? is it a good practice?
HI Jigna,
Its not a good practice to bring property name from excel sheet, but the best way is to keep them in Java file, since anyways any change has to be modified either in excel or java file, so its better keep them in java file.
Thanks,
Karthik KK
I dint quite understand this. did you mean c# files or java doc commented file. My environment is selenium in C#
Oops, since you have mentioned
@FindBy(How=How.id, Using=”abc”);
I thought you are looking for solution in Java, since in C# there is no@FindBy
its only[FindsBy(How = How.Id, Using='abc')]
Once again, maintaining the identification property in C# file is best practice and recommended.
Thanks,
Karthik KK
Thanks a ton.. Got it and helped a lot
Hi Karthik,
i have created POM code and initialized using Pagefactory class. while executing code i’m getting an exception
An exception of type ‘System.ArgumentException’ occurred in WebDriver.Support.dll but was not handled in user code
Additional information: Type of member ‘SelectElement’ is not IWebElement or IList
Code:
public POMTest()
{
PageFactory.InitElements(PropertiesCollection.driver, this);
}
[FindsBy(How=How.XPath, Using = “//*[text()=’REGISTER’]”)]
public IWebElement RegisterLink { get; set; }
[FindsBy(How = How.Name, Using = “firstName”)]
public IWebElement FirstName { get; set; }
[FindsBy(How = How.Name, Using = “country”)]
public SelectElement Country { get; set; }
With page factore:
[FindsBy(How = How.Name, Using = “firstName”)]
public IWebElement FirstName { get; set; }
How to use explicit wait:
wait.Until(ExpectedConditions.ElementIsVisible(??));
?? = expects ‘By’ but we are defining it as ‘IWebelement’
Hi Karthik,
How to handle explicit wait in POM (If we use Page factory). In the Expected conditions class there is no method for webelement. All methods support (By by). So if we take webelement then how to handle this.
I am not able to find any solution for that.
Still i tried below below two lines of code in the method but i am not able to run testcases.
Here is code which i tried,
public static void waitUntillElementIsVisible(this IWebElement element)
{
driver = ((IWrapsDriver)element).WrappedDriver;
//code 1
1. new WebDriverWait(driver, TimeSpan.FromSeconds(new Constant().normalwait)).Until((driver) => element.Displayed);
//code 2
2. new WebDriverWait(driver, TimeSpan.FromSeconds(10)).Until(ExpectedConditions.ElementExists(((By)element)));
}
Hope I have answered your query in email !
Yes Karthik,
Thanks
Hi Karthik,
Even i have the same question on how to handle explicit wait in POM. Please suggest
Hey karthik ,
i am looking to log the selenium testcases in C#. Can you please suggest the best way to do so?
thanks,
pp
Hi Karthik can u please help me with following error message with pageobject
Message: System.ArgumentException : The SearchContext of the locator object cannot be null
Parameter name: locator
Hi, Thank you for the videos, they are really helpful.
I am trying to implement POM in my project and followed every step till Video 7 . Once I created Page factory and tried to run I am getting the below error, please help.
Result Message: System.TypeLoadException : Method ‘GetProperty’ in type ‘OpenQA.Selenium.Support.PageObjects.WebElementProxy’ from assembly ‘WebDriver.Support, Version=2.35.0.0, Culture=neutral, PublicKeyToken=null’ does not have an implementation.
Hi Karthik,
I am reading that page factory is deprecated. Would you still recommend learning and using the syntax? Do most shops use it still? Thanks!
Not atleast now, I have to update the article 🙂
PageFactory.InitElements(PropertiesCollection.driver, this);
pagefactory code is not working kindly give any solution????
Here is the fix https://www.youtube.com/watch?v=jgKgbVcQ-1U&ab_channel=ExecuteAutomation
What is Alternative of POM in C#. How can we define and use page objects because now POM is depreciated.
Please suggest
Its covered here https://www.youtube.com/watch?v=jgKgbVcQ-1U
Hi Karthik,
I wanted to switch to your solution mentioned in this video:
https://www.youtube.com/watch?v=jgKgbVcQ-1U
However, I experience NoSuchElementExceptions whenever an element is not found using an expression body. Normally i used my extension method to check whether an element exists, but the new method throws the error already. Do you have a recommended solution for this?
Kind regards,
Simon
Yes, this will happen for sure, this issue can be fixed by writing a custom extension method of your own for IWebElement and call the extention method in the lambda expression
public static IWebElement FindByCss(this IWebElement driver, string element)
{
try
{
if (driver.FindElementByCssSelector(element).IsElementPresent()) ;
return driver.FindElementByCssSelector(element);
}
catch (Exception e)
{
return null;
}
}
Thanks a lot, stupid that i didn’t think of it. Then for me there is no need to switch back to the old PageFactory model
Correct you dont have to switch to old model !
Please see kindly below:
(…)
public IWebDriver Driver;
public BoaRegistrationPage(IWebDriver driver)
{
this.Driver = driver;
PageFactory.InitElements(driver, this);
}
[FindsBy(How = How.Id, Using = “ReportingPeriodName”)]
public SeleniumKendoDropDownList ReportingPeriodDropDown { get; set; }
[FindsBy(How=How.Id, Using = “Type”)]
public SeleniumKendoDropDownList BranchType { get; set; }
I got an error when running tests:
Message: System.ArgumentException : Type of member ‘SeleniumKendoDropDownList’ is not IWebElement or IList
How can I solve it? I cannot use IWebElement because I have KendoDropDownLists in the application.
public class SeleniumKendoDropDownList : KendoDropDownList
{
public SeleniumKendoDropDownList(IWebElement webElement) : base(webElement)
{
this.CopyInternalId(webElement);
}
public void SelectByDataItemProperty(string propertyName, string text)
{
Driver.JavaScripts()
.ExecuteScript(
string.Format(
CultureInfo.InvariantCulture,
“$(‘{0}’).data(‘{1}’).select(function(dataItem) {{return dataItem.{3} === ‘{2}’;}});”,
ElementCssSelector,
SelectType,
text,
propertyName));
}
Thank you in advance for help 🙁
Hi Karthik,
As you know page factory feature in Selenium C# has been deprecated. I followed one your video on this topic and made changes to my Loginpage object code as follows. But this has lead to issue where Loginpage object method in program.cs file is expecting an argument. Please let me know how argument should be provided if my Loginpage object method is as follows;
class OpenCMSloginPageObject
{
private readonly RemoteWebDriver _driver;
public OpenCMSloginPageObject(RemoteWebDriver driver) => _driver = driver;
IWebElement txtUsername => _driver.FindElementByName(“txtUsername”);
IWebElement txtPassword => _driver.FindElementByName(“txtPassword”);
IWebElement btnLogin => _driver.FindElementById(“btnLogin”);
}
Did you passed the driver object in the parameter of OpenCMSLoginPageObject class?
Hi Karthik,
I saw your videos. it very helpful for me to start creating my first Nunit project in Visual Studio using C#. but I need some direction about how to pass driver instance to page object class using base and hooks class. I am using Selenium 3.11 driver.
Thanks
The most sophesticated way to do is available in my advanced course and explains in much detail https://www.udemy.com/course/framework-development-with-selenium-csharp-advanced/
Please let me know if you are interested, I will share you the coupon code.
Thanks,
Karthik KK
Hi Karthik,
Your blogs are v good and easy to understand. I follow you on your UTube channel as well.
Can you please put the points here which you have explained in video .
I
Thank You….