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?










share|improve this question






















  • 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















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?










share|improve this question






















  • 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













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?










share|improve this question













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






share|improve this question













share|improve this question











share|improve this question




share|improve this question










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


















  • 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












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!






share|improve this answer





















    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














     

    draft saved


    draft discarded


















    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

























    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!






    share|improve this answer

























      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!






      share|improve this answer























        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!






        share|improve this answer












        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!







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 8 at 12:33









        Bernard Polman

        10919




        10919






























             

            draft saved


            draft discarded



















































             


            draft saved


            draft discarded














            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





















































            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







            Popular posts from this blog

            How to pass form data using jquery Ajax to insert data in database?

            National Museum of Racing and Hall of Fame

            Guess what letter conforming each word