Selenium message-error and message-success in java












1














I'm facing a problem with finding an element that does not exist. When I try to login into the application, if it failed login it will show the element -



dr.findElement(By.className("message-error ")).getText();


And after a successful login it will show this:



dr.findElement(By.className("message-success")).getText();


When I run the code and it doesn't find the element, then execution stops with the exception: element is not found



String mes=null;        

mes=dr.findElement(By.className("message-success")).getText();
if(mes!=null) {
File out= new File("success.txt");
FileWriter fr =new FileWriter(out,true);
PrintWriter pw=new PrintWriter(fr);
pw.println(mes+"|"+user.get(i)+"|"+pass.get(i));
pw.close();
}




    mes=dr.findElement(By.className("message-error")).getText();
if(mes!=null) {
File out= new File("error.txt");
FileWriter fr =new FileWriter(out,true);
PrintWriter pw=new PrintWriter(fr);
pw.println(mes+"|"+user.get(i)+"|"+pass.get(i));
pw.close();
}


The element does not appear.



For example, the success element will not shown until it is successful and the error element will not appear in the CSS until it gets an error.



So how can I tell it if element should exit or come to live or appear do an action?



What is the right thing to do in an if statement if the login is successful? Do this and login fail do this?










share|improve this question





























    1














    I'm facing a problem with finding an element that does not exist. When I try to login into the application, if it failed login it will show the element -



    dr.findElement(By.className("message-error ")).getText();


    And after a successful login it will show this:



    dr.findElement(By.className("message-success")).getText();


    When I run the code and it doesn't find the element, then execution stops with the exception: element is not found



    String mes=null;        

    mes=dr.findElement(By.className("message-success")).getText();
    if(mes!=null) {
    File out= new File("success.txt");
    FileWriter fr =new FileWriter(out,true);
    PrintWriter pw=new PrintWriter(fr);
    pw.println(mes+"|"+user.get(i)+"|"+pass.get(i));
    pw.close();
    }




        mes=dr.findElement(By.className("message-error")).getText();
    if(mes!=null) {
    File out= new File("error.txt");
    FileWriter fr =new FileWriter(out,true);
    PrintWriter pw=new PrintWriter(fr);
    pw.println(mes+"|"+user.get(i)+"|"+pass.get(i));
    pw.close();
    }


    The element does not appear.



    For example, the success element will not shown until it is successful and the error element will not appear in the CSS until it gets an error.



    So how can I tell it if element should exit or come to live or appear do an action?



    What is the right thing to do in an if statement if the login is successful? Do this and login fail do this?










    share|improve this question



























      1












      1








      1







      I'm facing a problem with finding an element that does not exist. When I try to login into the application, if it failed login it will show the element -



      dr.findElement(By.className("message-error ")).getText();


      And after a successful login it will show this:



      dr.findElement(By.className("message-success")).getText();


      When I run the code and it doesn't find the element, then execution stops with the exception: element is not found



      String mes=null;        

      mes=dr.findElement(By.className("message-success")).getText();
      if(mes!=null) {
      File out= new File("success.txt");
      FileWriter fr =new FileWriter(out,true);
      PrintWriter pw=new PrintWriter(fr);
      pw.println(mes+"|"+user.get(i)+"|"+pass.get(i));
      pw.close();
      }




          mes=dr.findElement(By.className("message-error")).getText();
      if(mes!=null) {
      File out= new File("error.txt");
      FileWriter fr =new FileWriter(out,true);
      PrintWriter pw=new PrintWriter(fr);
      pw.println(mes+"|"+user.get(i)+"|"+pass.get(i));
      pw.close();
      }


      The element does not appear.



      For example, the success element will not shown until it is successful and the error element will not appear in the CSS until it gets an error.



      So how can I tell it if element should exit or come to live or appear do an action?



      What is the right thing to do in an if statement if the login is successful? Do this and login fail do this?










      share|improve this question















      I'm facing a problem with finding an element that does not exist. When I try to login into the application, if it failed login it will show the element -



      dr.findElement(By.className("message-error ")).getText();


      And after a successful login it will show this:



      dr.findElement(By.className("message-success")).getText();


      When I run the code and it doesn't find the element, then execution stops with the exception: element is not found



      String mes=null;        

      mes=dr.findElement(By.className("message-success")).getText();
      if(mes!=null) {
      File out= new File("success.txt");
      FileWriter fr =new FileWriter(out,true);
      PrintWriter pw=new PrintWriter(fr);
      pw.println(mes+"|"+user.get(i)+"|"+pass.get(i));
      pw.close();
      }




          mes=dr.findElement(By.className("message-error")).getText();
      if(mes!=null) {
      File out= new File("error.txt");
      FileWriter fr =new FileWriter(out,true);
      PrintWriter pw=new PrintWriter(fr);
      pw.println(mes+"|"+user.get(i)+"|"+pass.get(i));
      pw.close();
      }


      The element does not appear.



      For example, the success element will not shown until it is successful and the error element will not appear in the CSS until it gets an error.



      So how can I tell it if element should exit or come to live or appear do an action?



      What is the right thing to do in an if statement if the login is successful? Do this and login fail do this?







      java selenium selenium-webdriver automated-tests






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 18 at 0:39









      Mozahler

      2,03641625




      2,03641625










      asked Nov 15 at 22:00









      Alholi Hamed

      65




      65
























          4 Answers
          4






          active

          oldest

          votes


















          0














          Use WebdriverWait to wait for the visibility of success message,



           // After  valid login 
          WebDriverWait wait = new WebDriverWait(driver, 60);
          WebElement successmessage wait.until(ExpectedConditions.visibilityOfElementLocated(By.className("message-success")));
          sucessmessage.getText();


          Similarly for error message,



          // After  invalid login
          WebElement errormessage wait.until(ExpectedConditions.visibilityOfElementLocated(By.className("message-error")));
          errormessage.getText();





          share|improve this answer























          • the things is the program stop when there is an error want to use it in if statement if it right like if the successes paper do this else do the error thing
            – Alholi Hamed
            Nov 15 at 22:48












          • Are u testing or scrapping?
            – Navarasu
            Nov 17 at 7:52



















          0














          What i think is you have same element but the class getting change as per application behavior. suppose if you are able to login in the application then it show the message element with class having attribute message-success and if it won't allow then error message with class having attribute 'message-error' in the same element.



          I've handled the same in below code -



          // first get the message element with some other locator or other attribute (don't use class name)

          WebElement message = driver.findElement(By.locator);

          if(message.getAttribute("class").contains("message-success")){

          System.out.println("Success message is " + message.getText())
          // write the code to perform further action you want on login success

          }else if (message.getAttribute("class").contains("message-error")){

          System.out.println("Error message is " + message.getText())
          // write the code to perform further action you want on login Fail

          }else{

          System.out.println("Message is empty" + message.getText())
          }


          Let me know if you have further queries.






          share|improve this answer





























            0














            My Choice is to use Webdriver wait. it is the perfect way to find an element.



                public WebDriverWait(WebDriver driver,15)
            WebElement successmessage = wait.until(ExpectedConditions.visibilityOfallElementLocatedby(By.className("message-success")));
            sucessmessage.getText();


            visibilityOfallElementLocatedby :



            An expectation for checking that all elements present on the web page that match the locator are visible. Visibility means that the elements are not only displayed but also have a height and width that is greater than 0.



            The above i wrote is for success Message, similar way try for invalid login.



            To use different types of wait, check this doc - https://seleniumhq.github.io/selenium/docs/api/java/allclasses-noframe.html



            search Wait in that document.






            share|improve this answer





























              0














              this is the answer for the question it work with me



                  try {
              WebElement sucmes=dr.findElement(By.xpath("//div[@class='message']"));
              String suclogin="login success:";
              if(sucmes.getText().contains(suclogin)) {
              File suclogin= new File("suclog.txt");
              FileWriter suclogr =new FileWriter(suclogin,true);
              PrintWriter suclogrw=new PrintWriter(suclogr);
              suclogrw.println(sucmes.getText());
              suclogrw.close();
              }else{
              //the other action here
              }
              }





              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',
                autoActivateHeartbeat: false,
                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%2f53328493%2fselenium-message-error-and-message-success-in-java%23new-answer', 'question_page');
                }
                );

                Post as a guest















                Required, but never shown

























                4 Answers
                4






                active

                oldest

                votes








                4 Answers
                4






                active

                oldest

                votes









                active

                oldest

                votes






                active

                oldest

                votes









                0














                Use WebdriverWait to wait for the visibility of success message,



                 // After  valid login 
                WebDriverWait wait = new WebDriverWait(driver, 60);
                WebElement successmessage wait.until(ExpectedConditions.visibilityOfElementLocated(By.className("message-success")));
                sucessmessage.getText();


                Similarly for error message,



                // After  invalid login
                WebElement errormessage wait.until(ExpectedConditions.visibilityOfElementLocated(By.className("message-error")));
                errormessage.getText();





                share|improve this answer























                • the things is the program stop when there is an error want to use it in if statement if it right like if the successes paper do this else do the error thing
                  – Alholi Hamed
                  Nov 15 at 22:48












                • Are u testing or scrapping?
                  – Navarasu
                  Nov 17 at 7:52
















                0














                Use WebdriverWait to wait for the visibility of success message,



                 // After  valid login 
                WebDriverWait wait = new WebDriverWait(driver, 60);
                WebElement successmessage wait.until(ExpectedConditions.visibilityOfElementLocated(By.className("message-success")));
                sucessmessage.getText();


                Similarly for error message,



                // After  invalid login
                WebElement errormessage wait.until(ExpectedConditions.visibilityOfElementLocated(By.className("message-error")));
                errormessage.getText();





                share|improve this answer























                • the things is the program stop when there is an error want to use it in if statement if it right like if the successes paper do this else do the error thing
                  – Alholi Hamed
                  Nov 15 at 22:48












                • Are u testing or scrapping?
                  – Navarasu
                  Nov 17 at 7:52














                0












                0








                0






                Use WebdriverWait to wait for the visibility of success message,



                 // After  valid login 
                WebDriverWait wait = new WebDriverWait(driver, 60);
                WebElement successmessage wait.until(ExpectedConditions.visibilityOfElementLocated(By.className("message-success")));
                sucessmessage.getText();


                Similarly for error message,



                // After  invalid login
                WebElement errormessage wait.until(ExpectedConditions.visibilityOfElementLocated(By.className("message-error")));
                errormessage.getText();





                share|improve this answer














                Use WebdriverWait to wait for the visibility of success message,



                 // After  valid login 
                WebDriverWait wait = new WebDriverWait(driver, 60);
                WebElement successmessage wait.until(ExpectedConditions.visibilityOfElementLocated(By.className("message-success")));
                sucessmessage.getText();


                Similarly for error message,



                // After  invalid login
                WebElement errormessage wait.until(ExpectedConditions.visibilityOfElementLocated(By.className("message-error")));
                errormessage.getText();






                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Nov 15 at 22:48

























                answered Nov 15 at 22:43









                Navarasu

                1,8811721




                1,8811721












                • the things is the program stop when there is an error want to use it in if statement if it right like if the successes paper do this else do the error thing
                  – Alholi Hamed
                  Nov 15 at 22:48












                • Are u testing or scrapping?
                  – Navarasu
                  Nov 17 at 7:52


















                • the things is the program stop when there is an error want to use it in if statement if it right like if the successes paper do this else do the error thing
                  – Alholi Hamed
                  Nov 15 at 22:48












                • Are u testing or scrapping?
                  – Navarasu
                  Nov 17 at 7:52
















                the things is the program stop when there is an error want to use it in if statement if it right like if the successes paper do this else do the error thing
                – Alholi Hamed
                Nov 15 at 22:48






                the things is the program stop when there is an error want to use it in if statement if it right like if the successes paper do this else do the error thing
                – Alholi Hamed
                Nov 15 at 22:48














                Are u testing or scrapping?
                – Navarasu
                Nov 17 at 7:52




                Are u testing or scrapping?
                – Navarasu
                Nov 17 at 7:52













                0














                What i think is you have same element but the class getting change as per application behavior. suppose if you are able to login in the application then it show the message element with class having attribute message-success and if it won't allow then error message with class having attribute 'message-error' in the same element.



                I've handled the same in below code -



                // first get the message element with some other locator or other attribute (don't use class name)

                WebElement message = driver.findElement(By.locator);

                if(message.getAttribute("class").contains("message-success")){

                System.out.println("Success message is " + message.getText())
                // write the code to perform further action you want on login success

                }else if (message.getAttribute("class").contains("message-error")){

                System.out.println("Error message is " + message.getText())
                // write the code to perform further action you want on login Fail

                }else{

                System.out.println("Message is empty" + message.getText())
                }


                Let me know if you have further queries.






                share|improve this answer


























                  0














                  What i think is you have same element but the class getting change as per application behavior. suppose if you are able to login in the application then it show the message element with class having attribute message-success and if it won't allow then error message with class having attribute 'message-error' in the same element.



                  I've handled the same in below code -



                  // first get the message element with some other locator or other attribute (don't use class name)

                  WebElement message = driver.findElement(By.locator);

                  if(message.getAttribute("class").contains("message-success")){

                  System.out.println("Success message is " + message.getText())
                  // write the code to perform further action you want on login success

                  }else if (message.getAttribute("class").contains("message-error")){

                  System.out.println("Error message is " + message.getText())
                  // write the code to perform further action you want on login Fail

                  }else{

                  System.out.println("Message is empty" + message.getText())
                  }


                  Let me know if you have further queries.






                  share|improve this answer
























                    0












                    0








                    0






                    What i think is you have same element but the class getting change as per application behavior. suppose if you are able to login in the application then it show the message element with class having attribute message-success and if it won't allow then error message with class having attribute 'message-error' in the same element.



                    I've handled the same in below code -



                    // first get the message element with some other locator or other attribute (don't use class name)

                    WebElement message = driver.findElement(By.locator);

                    if(message.getAttribute("class").contains("message-success")){

                    System.out.println("Success message is " + message.getText())
                    // write the code to perform further action you want on login success

                    }else if (message.getAttribute("class").contains("message-error")){

                    System.out.println("Error message is " + message.getText())
                    // write the code to perform further action you want on login Fail

                    }else{

                    System.out.println("Message is empty" + message.getText())
                    }


                    Let me know if you have further queries.






                    share|improve this answer












                    What i think is you have same element but the class getting change as per application behavior. suppose if you are able to login in the application then it show the message element with class having attribute message-success and if it won't allow then error message with class having attribute 'message-error' in the same element.



                    I've handled the same in below code -



                    // first get the message element with some other locator or other attribute (don't use class name)

                    WebElement message = driver.findElement(By.locator);

                    if(message.getAttribute("class").contains("message-success")){

                    System.out.println("Success message is " + message.getText())
                    // write the code to perform further action you want on login success

                    }else if (message.getAttribute("class").contains("message-error")){

                    System.out.println("Error message is " + message.getText())
                    // write the code to perform further action you want on login Fail

                    }else{

                    System.out.println("Message is empty" + message.getText())
                    }


                    Let me know if you have further queries.







                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Nov 16 at 6:56









                    NarendraR

                    3,75951743




                    3,75951743























                        0














                        My Choice is to use Webdriver wait. it is the perfect way to find an element.



                            public WebDriverWait(WebDriver driver,15)
                        WebElement successmessage = wait.until(ExpectedConditions.visibilityOfallElementLocatedby(By.className("message-success")));
                        sucessmessage.getText();


                        visibilityOfallElementLocatedby :



                        An expectation for checking that all elements present on the web page that match the locator are visible. Visibility means that the elements are not only displayed but also have a height and width that is greater than 0.



                        The above i wrote is for success Message, similar way try for invalid login.



                        To use different types of wait, check this doc - https://seleniumhq.github.io/selenium/docs/api/java/allclasses-noframe.html



                        search Wait in that document.






                        share|improve this answer


























                          0














                          My Choice is to use Webdriver wait. it is the perfect way to find an element.



                              public WebDriverWait(WebDriver driver,15)
                          WebElement successmessage = wait.until(ExpectedConditions.visibilityOfallElementLocatedby(By.className("message-success")));
                          sucessmessage.getText();


                          visibilityOfallElementLocatedby :



                          An expectation for checking that all elements present on the web page that match the locator are visible. Visibility means that the elements are not only displayed but also have a height and width that is greater than 0.



                          The above i wrote is for success Message, similar way try for invalid login.



                          To use different types of wait, check this doc - https://seleniumhq.github.io/selenium/docs/api/java/allclasses-noframe.html



                          search Wait in that document.






                          share|improve this answer
























                            0












                            0








                            0






                            My Choice is to use Webdriver wait. it is the perfect way to find an element.



                                public WebDriverWait(WebDriver driver,15)
                            WebElement successmessage = wait.until(ExpectedConditions.visibilityOfallElementLocatedby(By.className("message-success")));
                            sucessmessage.getText();


                            visibilityOfallElementLocatedby :



                            An expectation for checking that all elements present on the web page that match the locator are visible. Visibility means that the elements are not only displayed but also have a height and width that is greater than 0.



                            The above i wrote is for success Message, similar way try for invalid login.



                            To use different types of wait, check this doc - https://seleniumhq.github.io/selenium/docs/api/java/allclasses-noframe.html



                            search Wait in that document.






                            share|improve this answer












                            My Choice is to use Webdriver wait. it is the perfect way to find an element.



                                public WebDriverWait(WebDriver driver,15)
                            WebElement successmessage = wait.until(ExpectedConditions.visibilityOfallElementLocatedby(By.className("message-success")));
                            sucessmessage.getText();


                            visibilityOfallElementLocatedby :



                            An expectation for checking that all elements present on the web page that match the locator are visible. Visibility means that the elements are not only displayed but also have a height and width that is greater than 0.



                            The above i wrote is for success Message, similar way try for invalid login.



                            To use different types of wait, check this doc - https://seleniumhq.github.io/selenium/docs/api/java/allclasses-noframe.html



                            search Wait in that document.







                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered Nov 16 at 7:05









                            koushick

                            16313




                            16313























                                0














                                this is the answer for the question it work with me



                                    try {
                                WebElement sucmes=dr.findElement(By.xpath("//div[@class='message']"));
                                String suclogin="login success:";
                                if(sucmes.getText().contains(suclogin)) {
                                File suclogin= new File("suclog.txt");
                                FileWriter suclogr =new FileWriter(suclogin,true);
                                PrintWriter suclogrw=new PrintWriter(suclogr);
                                suclogrw.println(sucmes.getText());
                                suclogrw.close();
                                }else{
                                //the other action here
                                }
                                }





                                share|improve this answer


























                                  0














                                  this is the answer for the question it work with me



                                      try {
                                  WebElement sucmes=dr.findElement(By.xpath("//div[@class='message']"));
                                  String suclogin="login success:";
                                  if(sucmes.getText().contains(suclogin)) {
                                  File suclogin= new File("suclog.txt");
                                  FileWriter suclogr =new FileWriter(suclogin,true);
                                  PrintWriter suclogrw=new PrintWriter(suclogr);
                                  suclogrw.println(sucmes.getText());
                                  suclogrw.close();
                                  }else{
                                  //the other action here
                                  }
                                  }





                                  share|improve this answer
























                                    0












                                    0








                                    0






                                    this is the answer for the question it work with me



                                        try {
                                    WebElement sucmes=dr.findElement(By.xpath("//div[@class='message']"));
                                    String suclogin="login success:";
                                    if(sucmes.getText().contains(suclogin)) {
                                    File suclogin= new File("suclog.txt");
                                    FileWriter suclogr =new FileWriter(suclogin,true);
                                    PrintWriter suclogrw=new PrintWriter(suclogr);
                                    suclogrw.println(sucmes.getText());
                                    suclogrw.close();
                                    }else{
                                    //the other action here
                                    }
                                    }





                                    share|improve this answer












                                    this is the answer for the question it work with me



                                        try {
                                    WebElement sucmes=dr.findElement(By.xpath("//div[@class='message']"));
                                    String suclogin="login success:";
                                    if(sucmes.getText().contains(suclogin)) {
                                    File suclogin= new File("suclog.txt");
                                    FileWriter suclogr =new FileWriter(suclogin,true);
                                    PrintWriter suclogrw=new PrintWriter(suclogr);
                                    suclogrw.println(sucmes.getText());
                                    suclogrw.close();
                                    }else{
                                    //the other action here
                                    }
                                    }






                                    share|improve this answer












                                    share|improve this answer



                                    share|improve this answer










                                    answered Nov 17 at 17:13









                                    Alholi Hamed

                                    65




                                    65






























                                        draft saved

                                        draft discarded




















































                                        Thanks for contributing an answer to Stack Overflow!


                                        • Please be sure to answer the question. Provide details and share your research!

                                        But avoid



                                        • Asking for help, clarification, or responding to other answers.

                                        • Making statements based on opinion; back them up with references or personal experience.


                                        To learn more, see our tips on writing great answers.





                                        Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                                        Please pay close attention to the following guidance:


                                        • Please be sure to answer the question. Provide details and share your research!

                                        But avoid



                                        • Asking for help, clarification, or responding to other answers.

                                        • Making statements based on opinion; back them up with references or personal experience.


                                        To learn more, see our tips on writing great answers.




                                        draft saved


                                        draft discarded














                                        StackExchange.ready(
                                        function () {
                                        StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53328493%2fselenium-message-error-and-message-success-in-java%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 change which sound is reproduced for terminal bell?

                                        Can I use Tabulator js library in my java Spring + Thymeleaf project?

                                        Title Spacing in Bjornstrup Chapter, Removing Chapter Number From Contents