I am using a PageFactory in Selenium to get the element on the page but it is always null. What am I missing ? I am getting a null reference exception as txtUser is always null.
I have create a base Page class.
public class Page
{
static IWebDriver driver = new RemoteWebDriver(new Uri("http://localhost:7171/wd/hub"), DesiredCapabilities.Firefox());
public static string CurrentURL { get { return driver.Url; } }
public static ISearchContext Driver
{
get { return driver; }
}
}
I have created a LoginPage class and a Helper class that creates a static instance of LoginPage class.
public class LoginHelper
{
public static LoginPage LoginPage
{
get
{
var loginPage = new LoginPage();
PageFactory.InitElements(loginPage, new RetryingElementLocator(Page.Driver,TimeSpan.FromSeconds(300)));
return loginPage;
}
}
}
public class LoginPage
{
[FindsBy(How = How.Id, Using = "JobLogPageContentPlaceHolder_UserName")]
static IWebElement txtUser {get;set;}
//This Works OK. It opens the browser and takes to login page
public void GoToLoginPage()
{
Page.GoToPage(TestData.BaseURL + "Login.aspx");
}
public void LoginUser(string user, string pass)
{
if (txtUser != null) // This is always null
{
txtUser.SendKeys(user);
}
txtPassword.SendKeys(pass);
loginButton.Click();
}
I have written a test as below.
public void CanOpenApplicationAndLogin()
{
LoginHelper.LoginPage.GoToLoginPage();
LoginHelper.LoginPage.LoginUser(TestData.UserName, TestData.Password); // This fails
Assert.AreEqual(TestData.BaseURL + "UploadPO.aspx", Page.CurrentURL);
}
Aucun commentaire:
Enregistrer un commentaire