Selenium - test doesn't click on menu item but if I click it, the test completes
up vote
0
down vote
favorite
I started using Selenium WebDriver today and I'm writing a login/logout test.
[Test]
public void TestLoginLogout()
{
//Define browser and target URL
IWebDriver browser = new ChromeDriver();
browser.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(10);
browser.Navigate().GoToUrl("http://test-test.net");
browser.Manage().Window.Maximize();
//Define login credentials and webpage elements
IWebElement fieldUsername = browser.FindElement(By.Id("mat-input-0"));
IWebElement fieldPassword = browser.FindElement(By.Id("mat-input-1"));
//Perform login using login credentials
fieldUsername.SendKeys("tester");
fieldPassword.SendKeys("tester123");
IWebElement buttonLogin = browser.FindElement(By.Id("LoginButton"));
buttonLogin.Click();
//Perform logout action
Actions action = new Actions(browser);
IWebElement buttonLogout = browser.FindElement(By.XPath(".//div[@class='menu']/div/ul[@class='list']/li[last()]/a"));
action.MoveToElement(buttonLogout).Click();
//Locate confirm button in modal window
browser.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(5);
IWebElement buttonConfirm = browser.FindElement(By.XPath(".//button[@class='swal-button swal-button--confirm']"));
buttonConfirm.Click();
}
When the test logs in, it should click on Logout button in the menu and then click on confirm button when the modal dialog pops up. The problem is,the test doesn't click on the Logout button, but if I manually click on the Logout button while the test halts, then the test completes.
Any suggestions?
selenium selenium-webdriver automated-tests
add a comment |
up vote
0
down vote
favorite
I started using Selenium WebDriver today and I'm writing a login/logout test.
[Test]
public void TestLoginLogout()
{
//Define browser and target URL
IWebDriver browser = new ChromeDriver();
browser.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(10);
browser.Navigate().GoToUrl("http://test-test.net");
browser.Manage().Window.Maximize();
//Define login credentials and webpage elements
IWebElement fieldUsername = browser.FindElement(By.Id("mat-input-0"));
IWebElement fieldPassword = browser.FindElement(By.Id("mat-input-1"));
//Perform login using login credentials
fieldUsername.SendKeys("tester");
fieldPassword.SendKeys("tester123");
IWebElement buttonLogin = browser.FindElement(By.Id("LoginButton"));
buttonLogin.Click();
//Perform logout action
Actions action = new Actions(browser);
IWebElement buttonLogout = browser.FindElement(By.XPath(".//div[@class='menu']/div/ul[@class='list']/li[last()]/a"));
action.MoveToElement(buttonLogout).Click();
//Locate confirm button in modal window
browser.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(5);
IWebElement buttonConfirm = browser.FindElement(By.XPath(".//button[@class='swal-button swal-button--confirm']"));
buttonConfirm.Click();
}
When the test logs in, it should click on Logout button in the menu and then click on confirm button when the modal dialog pops up. The problem is,the test doesn't click on the Logout button, but if I manually click on the Logout button while the test halts, then the test completes.
Any suggestions?
selenium selenium-webdriver automated-tests
What's the value of buttonLogout when you debug this code ?
– auburg
Nov 8 at 12:10
@auburg hello, I've posted a new reply - found a solution!
– Bernard Polman
Nov 8 at 12:37
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I started using Selenium WebDriver today and I'm writing a login/logout test.
[Test]
public void TestLoginLogout()
{
//Define browser and target URL
IWebDriver browser = new ChromeDriver();
browser.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(10);
browser.Navigate().GoToUrl("http://test-test.net");
browser.Manage().Window.Maximize();
//Define login credentials and webpage elements
IWebElement fieldUsername = browser.FindElement(By.Id("mat-input-0"));
IWebElement fieldPassword = browser.FindElement(By.Id("mat-input-1"));
//Perform login using login credentials
fieldUsername.SendKeys("tester");
fieldPassword.SendKeys("tester123");
IWebElement buttonLogin = browser.FindElement(By.Id("LoginButton"));
buttonLogin.Click();
//Perform logout action
Actions action = new Actions(browser);
IWebElement buttonLogout = browser.FindElement(By.XPath(".//div[@class='menu']/div/ul[@class='list']/li[last()]/a"));
action.MoveToElement(buttonLogout).Click();
//Locate confirm button in modal window
browser.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(5);
IWebElement buttonConfirm = browser.FindElement(By.XPath(".//button[@class='swal-button swal-button--confirm']"));
buttonConfirm.Click();
}
When the test logs in, it should click on Logout button in the menu and then click on confirm button when the modal dialog pops up. The problem is,the test doesn't click on the Logout button, but if I manually click on the Logout button while the test halts, then the test completes.
Any suggestions?
selenium selenium-webdriver automated-tests
I started using Selenium WebDriver today and I'm writing a login/logout test.
[Test]
public void TestLoginLogout()
{
//Define browser and target URL
IWebDriver browser = new ChromeDriver();
browser.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(10);
browser.Navigate().GoToUrl("http://test-test.net");
browser.Manage().Window.Maximize();
//Define login credentials and webpage elements
IWebElement fieldUsername = browser.FindElement(By.Id("mat-input-0"));
IWebElement fieldPassword = browser.FindElement(By.Id("mat-input-1"));
//Perform login using login credentials
fieldUsername.SendKeys("tester");
fieldPassword.SendKeys("tester123");
IWebElement buttonLogin = browser.FindElement(By.Id("LoginButton"));
buttonLogin.Click();
//Perform logout action
Actions action = new Actions(browser);
IWebElement buttonLogout = browser.FindElement(By.XPath(".//div[@class='menu']/div/ul[@class='list']/li[last()]/a"));
action.MoveToElement(buttonLogout).Click();
//Locate confirm button in modal window
browser.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(5);
IWebElement buttonConfirm = browser.FindElement(By.XPath(".//button[@class='swal-button swal-button--confirm']"));
buttonConfirm.Click();
}
When the test logs in, it should click on Logout button in the menu and then click on confirm button when the modal dialog pops up. The problem is,the test doesn't click on the Logout button, but if I manually click on the Logout button while the test halts, then the test completes.
Any suggestions?
selenium selenium-webdriver automated-tests
selenium selenium-webdriver automated-tests
asked Nov 8 at 12:05
Bernard Polman
10919
10919
What's the value of buttonLogout when you debug this code ?
– auburg
Nov 8 at 12:10
@auburg hello, I've posted a new reply - found a solution!
– Bernard Polman
Nov 8 at 12:37
add a comment |
What's the value of buttonLogout when you debug this code ?
– auburg
Nov 8 at 12:10
@auburg hello, I've posted a new reply - found a solution!
– Bernard Polman
Nov 8 at 12:37
What's the value of buttonLogout when you debug this code ?
– auburg
Nov 8 at 12:10
What's the value of buttonLogout when you debug this code ?
– auburg
Nov 8 at 12:10
@auburg hello, I've posted a new reply - found a solution!
– Bernard Polman
Nov 8 at 12:37
@auburg hello, I've posted a new reply - found a solution!
– Bernard Polman
Nov 8 at 12:37
add a comment |
1 Answer
1
active
oldest
votes
up vote
1
down vote
UPDATE: The problem was with the clicking of Logout button in the menu. Seems like the test runs too fast and since our application has a loader element between the login page and the homepage, the test tried to click on the logout button while the loader element was still present. This is what I did:
Since ExpectedConditions is deprecated in current WebDrivers version, I downloaded the following NuGet package: DotNetSeleniumExtras.WaitHelpers
I declared a variable that defined how long the browser will have to wait for element to be present/clickable before throwing a time-out and a IJavaScript executor that handles the clicking:
IJavaScriptExecutor executor = (IJavaScriptExecutor)browser;
var wait = new WebDriverWait(browser, new TimeSpan(0, 0, 10));
And finally, I changed the way that logout button and confirm button (inside modal) are clicked:
//Perform logout action
var buttonLogout = wait.Until(SeleniumExtras.WaitHelpers.ExpectedConditions.ElementIsVisible(By.XPath(".//div[@class='menu']/div/ul[@class='list']/li[last()]/a")));
executor.ExecuteScript("arguments[0].click();", buttonLogout);
//Locate confirm button in modal window
var buttonConfirm = wait.Until(SeleniumExtras.WaitHelpers.ExpectedConditions.ElementIsVisible(By.XPath(".//button[@class='swal-button swal-button--confirm']")));
executor.ExecuteScript("arguments[0].click();", buttonConfirm);
And now the test executes successfully!
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
UPDATE: The problem was with the clicking of Logout button in the menu. Seems like the test runs too fast and since our application has a loader element between the login page and the homepage, the test tried to click on the logout button while the loader element was still present. This is what I did:
Since ExpectedConditions is deprecated in current WebDrivers version, I downloaded the following NuGet package: DotNetSeleniumExtras.WaitHelpers
I declared a variable that defined how long the browser will have to wait for element to be present/clickable before throwing a time-out and a IJavaScript executor that handles the clicking:
IJavaScriptExecutor executor = (IJavaScriptExecutor)browser;
var wait = new WebDriverWait(browser, new TimeSpan(0, 0, 10));
And finally, I changed the way that logout button and confirm button (inside modal) are clicked:
//Perform logout action
var buttonLogout = wait.Until(SeleniumExtras.WaitHelpers.ExpectedConditions.ElementIsVisible(By.XPath(".//div[@class='menu']/div/ul[@class='list']/li[last()]/a")));
executor.ExecuteScript("arguments[0].click();", buttonLogout);
//Locate confirm button in modal window
var buttonConfirm = wait.Until(SeleniumExtras.WaitHelpers.ExpectedConditions.ElementIsVisible(By.XPath(".//button[@class='swal-button swal-button--confirm']")));
executor.ExecuteScript("arguments[0].click();", buttonConfirm);
And now the test executes successfully!
add a comment |
up vote
1
down vote
UPDATE: The problem was with the clicking of Logout button in the menu. Seems like the test runs too fast and since our application has a loader element between the login page and the homepage, the test tried to click on the logout button while the loader element was still present. This is what I did:
Since ExpectedConditions is deprecated in current WebDrivers version, I downloaded the following NuGet package: DotNetSeleniumExtras.WaitHelpers
I declared a variable that defined how long the browser will have to wait for element to be present/clickable before throwing a time-out and a IJavaScript executor that handles the clicking:
IJavaScriptExecutor executor = (IJavaScriptExecutor)browser;
var wait = new WebDriverWait(browser, new TimeSpan(0, 0, 10));
And finally, I changed the way that logout button and confirm button (inside modal) are clicked:
//Perform logout action
var buttonLogout = wait.Until(SeleniumExtras.WaitHelpers.ExpectedConditions.ElementIsVisible(By.XPath(".//div[@class='menu']/div/ul[@class='list']/li[last()]/a")));
executor.ExecuteScript("arguments[0].click();", buttonLogout);
//Locate confirm button in modal window
var buttonConfirm = wait.Until(SeleniumExtras.WaitHelpers.ExpectedConditions.ElementIsVisible(By.XPath(".//button[@class='swal-button swal-button--confirm']")));
executor.ExecuteScript("arguments[0].click();", buttonConfirm);
And now the test executes successfully!
add a comment |
up vote
1
down vote
up vote
1
down vote
UPDATE: The problem was with the clicking of Logout button in the menu. Seems like the test runs too fast and since our application has a loader element between the login page and the homepage, the test tried to click on the logout button while the loader element was still present. This is what I did:
Since ExpectedConditions is deprecated in current WebDrivers version, I downloaded the following NuGet package: DotNetSeleniumExtras.WaitHelpers
I declared a variable that defined how long the browser will have to wait for element to be present/clickable before throwing a time-out and a IJavaScript executor that handles the clicking:
IJavaScriptExecutor executor = (IJavaScriptExecutor)browser;
var wait = new WebDriverWait(browser, new TimeSpan(0, 0, 10));
And finally, I changed the way that logout button and confirm button (inside modal) are clicked:
//Perform logout action
var buttonLogout = wait.Until(SeleniumExtras.WaitHelpers.ExpectedConditions.ElementIsVisible(By.XPath(".//div[@class='menu']/div/ul[@class='list']/li[last()]/a")));
executor.ExecuteScript("arguments[0].click();", buttonLogout);
//Locate confirm button in modal window
var buttonConfirm = wait.Until(SeleniumExtras.WaitHelpers.ExpectedConditions.ElementIsVisible(By.XPath(".//button[@class='swal-button swal-button--confirm']")));
executor.ExecuteScript("arguments[0].click();", buttonConfirm);
And now the test executes successfully!
UPDATE: The problem was with the clicking of Logout button in the menu. Seems like the test runs too fast and since our application has a loader element between the login page and the homepage, the test tried to click on the logout button while the loader element was still present. This is what I did:
Since ExpectedConditions is deprecated in current WebDrivers version, I downloaded the following NuGet package: DotNetSeleniumExtras.WaitHelpers
I declared a variable that defined how long the browser will have to wait for element to be present/clickable before throwing a time-out and a IJavaScript executor that handles the clicking:
IJavaScriptExecutor executor = (IJavaScriptExecutor)browser;
var wait = new WebDriverWait(browser, new TimeSpan(0, 0, 10));
And finally, I changed the way that logout button and confirm button (inside modal) are clicked:
//Perform logout action
var buttonLogout = wait.Until(SeleniumExtras.WaitHelpers.ExpectedConditions.ElementIsVisible(By.XPath(".//div[@class='menu']/div/ul[@class='list']/li[last()]/a")));
executor.ExecuteScript("arguments[0].click();", buttonLogout);
//Locate confirm button in modal window
var buttonConfirm = wait.Until(SeleniumExtras.WaitHelpers.ExpectedConditions.ElementIsVisible(By.XPath(".//button[@class='swal-button swal-button--confirm']")));
executor.ExecuteScript("arguments[0].click();", buttonConfirm);
And now the test executes successfully!
answered Nov 8 at 12:33
Bernard Polman
10919
10919
add a comment |
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53207398%2fselenium-test-doesnt-click-on-menu-item-but-if-i-click-it-the-test-completes%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
What's the value of buttonLogout when you debug this code ?
– auburg
Nov 8 at 12:10
@auburg hello, I've posted a new reply - found a solution!
– Bernard Polman
Nov 8 at 12:37