I am trying to create a simple Selenium script to login to a page directly proceeded by logging out from the page. I am using Selenium with C# and PageFactory to reach the elements on the webpage.
I am able to login to the page, but unable to logout. The login and logout button themselves are made visible by clicking the loginLogoutElementMakeVisible element seen below. This element changes after logging in.
My current solution is to have one reference to the element in my code as seen below:
[Findsby(How = How.CssSelector, Using = "a[href='javascript:void(0)']")]
public IWebElement loginLogoutElementMakeVisible
Html for the button to make login button visible:
<li class="nav-link sm-menu-item more">
<a data-bind="click: function(){ loginClicked(true); }, text: translate('menu_login')" href="javascript:void(0)">Login</a>
</li>
Html for the button to make logout button visible:
<li class="nav-link sm-menu-item more nav-profile">
<a href="javascript:void(0)">
<img data-bind="attr: { 'src': avatarUrl }" src="https://media.licdn.com/mpr/mpr/shrink_200_200/p/1/000/1c2/3a6/04b3e5c.jpg">
</a>
<ul class="submenu sm-submenu">
</li>
The general concept I have used is to have these two action methods in the page object to login and logout.
public void Login()
{
loginLogoutElementMakeVisible.Click();
loginUsernameEntry.Clear();
loginUsernameEntry.SendKeys("Username");
loginPasswordEntry.Clear();
loginPasswordEntry.SendKeys("Password");
loginExecuteButton.Click(); //Now logging in
Browser.WaitForPageReload() //Own code in Browser class see below (1)
}
public void Logout()
{
loginLogoutElementMakeVisible.Click(); //Never clicked in test
logoutExecuteButton.Click() //Now logging out
}
In Browser class (1) ->
public void WaitForPageReload()
{
IWait<IWebDriver> wait = new OpenQA.Selenium.Support.UI.WebDriverWait(driver, TimeSpan.FromSeconds(30.00));
wait.Until(driver1 => ((IJavaScriptExecutor)driver).ExecuteScript("return document.readyState").Equals("complete"));
}
I have also tried creating two separate fieldmember WebElements (e.g. one loginElementMakeVisible and one logoutElementMakeVisible) for the button to display the login and the logout button, but still having them in the same page object and thus initializing them at the same time (Thought this could work since pagefactory uses lazy loading)
I have also tried to have the logout button in another pageobject which is initialized after login in the test.
Is there a way to make the pagefactory update its proxy to the loginLogoutElementMakeVisible so I can logout? Or am I still trying to logout before the page after logging has had a chance to load? Are there other problems which could explain why I cannot click the MakeVisible element?