How can I handle alert using if else condition in selenium?












4















else is perfectly executed but if block is not executed - can anyone help me?



WebDriverWait wait = new WebDriverWait(Maf_Base.getDriver(), 10 /*timeout in seconds*/);

if(wait.until(ExpectedConditions.alertIsPresent())==null) {
System.out.println("alert was not present");
agobj.logout.click();
} else {
Alert alert = Maf_Base.getDriver().switchTo().alert();
alert.accept();
System.out.println("alert was present and accepted");
}









share|improve this question




















  • 2





    Two thoughts - is an alert actually present? - Is waiting until alertISPresent to be null just timing out after 10 seconds?

    – Michael Durrant
    Mar 20 at 9:39
















4















else is perfectly executed but if block is not executed - can anyone help me?



WebDriverWait wait = new WebDriverWait(Maf_Base.getDriver(), 10 /*timeout in seconds*/);

if(wait.until(ExpectedConditions.alertIsPresent())==null) {
System.out.println("alert was not present");
agobj.logout.click();
} else {
Alert alert = Maf_Base.getDriver().switchTo().alert();
alert.accept();
System.out.println("alert was present and accepted");
}









share|improve this question




















  • 2





    Two thoughts - is an alert actually present? - Is waiting until alertISPresent to be null just timing out after 10 seconds?

    – Michael Durrant
    Mar 20 at 9:39














4












4








4








else is perfectly executed but if block is not executed - can anyone help me?



WebDriverWait wait = new WebDriverWait(Maf_Base.getDriver(), 10 /*timeout in seconds*/);

if(wait.until(ExpectedConditions.alertIsPresent())==null) {
System.out.println("alert was not present");
agobj.logout.click();
} else {
Alert alert = Maf_Base.getDriver().switchTo().alert();
alert.accept();
System.out.println("alert was present and accepted");
}









share|improve this question
















else is perfectly executed but if block is not executed - can anyone help me?



WebDriverWait wait = new WebDriverWait(Maf_Base.getDriver(), 10 /*timeout in seconds*/);

if(wait.until(ExpectedConditions.alertIsPresent())==null) {
System.out.println("alert was not present");
agobj.logout.click();
} else {
Alert alert = Maf_Base.getDriver().switchTo().alert();
alert.accept();
System.out.println("alert was present and accepted");
}






automated-testing selenium-webdriver selenium2






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 20 at 8:31









trashpanda

1,5221929




1,5221929










asked Mar 20 at 6:31









anjith neerukondaanjith neerukonda

202




202








  • 2





    Two thoughts - is an alert actually present? - Is waiting until alertISPresent to be null just timing out after 10 seconds?

    – Michael Durrant
    Mar 20 at 9:39














  • 2





    Two thoughts - is an alert actually present? - Is waiting until alertISPresent to be null just timing out after 10 seconds?

    – Michael Durrant
    Mar 20 at 9:39








2




2





Two thoughts - is an alert actually present? - Is waiting until alertISPresent to be null just timing out after 10 seconds?

– Michael Durrant
Mar 20 at 9:39





Two thoughts - is an alert actually present? - Is waiting until alertISPresent to be null just timing out after 10 seconds?

– Michael Durrant
Mar 20 at 9:39










2 Answers
2






active

oldest

votes


















4














Try a try catch, if something in the try gives a timeout exception (e.g. alert not found) it will execute the code in the catch block.



WebDriverWait wait = new WebDriverWait(Maf_Base.getDriver(), 10);

try {
wait.until(ExpectedConditions.alertIsPresent())
Alert alert = Maf_Base.getDriver().switchTo().alert();
alert.accept();
System.out.println("alert was present and accepted");
catch(Exception e) {
System.out.println("alert was not present");
System.out.print(e);
agobj.logout.click();
}





share|improve this answer


























  • "Thus it reads: if(functionObject == null) {} and a functionObject is not null it is a Object." it is not actually true (if I got you right). ExpectedConditions.alertIsPresent() is "function object" but not wait.until(functionObject). So it does not read if(functionObject == null) {} but rather if(alert==null) where alert is a product of functionObject.apply() that is called inside until method. However alert cannot be null since until just throw an exception if it would be null. However I agree with general idea of looking up the alert.

    – Alexey R.
    Mar 20 at 10:02











  • @AlexeyR. I am not a Java expert, but the wait.until returns an alert, but while it is waiting it is not an alert. An IF comparison in most languages do not wait for the return of a function, it just compares the objects in its current state. Still the try/catch will probably solve the issue.

    – Niels van Reijmersdal
    Mar 20 at 10:45






  • 1





    @AlexeyR. Removed my assumption of how Java works :)

    – Niels van Reijmersdal
    Mar 20 at 10:47











  • @NielsvanReijmersdal thanks, bro!! it's working properly

    – anjith neerukonda
    Mar 20 at 12:50



















3














If you check wait.until() code:



enter image description here



You will know that it never returns null. It either returns found alert (see lines 88-92) or throws an exception. This is why your if does not work.






share|improve this answer
























    Your Answer








    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "244"
    };
    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: false,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: null,
    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%2fsqa.stackexchange.com%2fquestions%2f38327%2fhow-can-i-handle-alert-using-if-else-condition-in-selenium%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    4














    Try a try catch, if something in the try gives a timeout exception (e.g. alert not found) it will execute the code in the catch block.



    WebDriverWait wait = new WebDriverWait(Maf_Base.getDriver(), 10);

    try {
    wait.until(ExpectedConditions.alertIsPresent())
    Alert alert = Maf_Base.getDriver().switchTo().alert();
    alert.accept();
    System.out.println("alert was present and accepted");
    catch(Exception e) {
    System.out.println("alert was not present");
    System.out.print(e);
    agobj.logout.click();
    }





    share|improve this answer


























    • "Thus it reads: if(functionObject == null) {} and a functionObject is not null it is a Object." it is not actually true (if I got you right). ExpectedConditions.alertIsPresent() is "function object" but not wait.until(functionObject). So it does not read if(functionObject == null) {} but rather if(alert==null) where alert is a product of functionObject.apply() that is called inside until method. However alert cannot be null since until just throw an exception if it would be null. However I agree with general idea of looking up the alert.

      – Alexey R.
      Mar 20 at 10:02











    • @AlexeyR. I am not a Java expert, but the wait.until returns an alert, but while it is waiting it is not an alert. An IF comparison in most languages do not wait for the return of a function, it just compares the objects in its current state. Still the try/catch will probably solve the issue.

      – Niels van Reijmersdal
      Mar 20 at 10:45






    • 1





      @AlexeyR. Removed my assumption of how Java works :)

      – Niels van Reijmersdal
      Mar 20 at 10:47











    • @NielsvanReijmersdal thanks, bro!! it's working properly

      – anjith neerukonda
      Mar 20 at 12:50
















    4














    Try a try catch, if something in the try gives a timeout exception (e.g. alert not found) it will execute the code in the catch block.



    WebDriverWait wait = new WebDriverWait(Maf_Base.getDriver(), 10);

    try {
    wait.until(ExpectedConditions.alertIsPresent())
    Alert alert = Maf_Base.getDriver().switchTo().alert();
    alert.accept();
    System.out.println("alert was present and accepted");
    catch(Exception e) {
    System.out.println("alert was not present");
    System.out.print(e);
    agobj.logout.click();
    }





    share|improve this answer


























    • "Thus it reads: if(functionObject == null) {} and a functionObject is not null it is a Object." it is not actually true (if I got you right). ExpectedConditions.alertIsPresent() is "function object" but not wait.until(functionObject). So it does not read if(functionObject == null) {} but rather if(alert==null) where alert is a product of functionObject.apply() that is called inside until method. However alert cannot be null since until just throw an exception if it would be null. However I agree with general idea of looking up the alert.

      – Alexey R.
      Mar 20 at 10:02











    • @AlexeyR. I am not a Java expert, but the wait.until returns an alert, but while it is waiting it is not an alert. An IF comparison in most languages do not wait for the return of a function, it just compares the objects in its current state. Still the try/catch will probably solve the issue.

      – Niels van Reijmersdal
      Mar 20 at 10:45






    • 1





      @AlexeyR. Removed my assumption of how Java works :)

      – Niels van Reijmersdal
      Mar 20 at 10:47











    • @NielsvanReijmersdal thanks, bro!! it's working properly

      – anjith neerukonda
      Mar 20 at 12:50














    4












    4








    4







    Try a try catch, if something in the try gives a timeout exception (e.g. alert not found) it will execute the code in the catch block.



    WebDriverWait wait = new WebDriverWait(Maf_Base.getDriver(), 10);

    try {
    wait.until(ExpectedConditions.alertIsPresent())
    Alert alert = Maf_Base.getDriver().switchTo().alert();
    alert.accept();
    System.out.println("alert was present and accepted");
    catch(Exception e) {
    System.out.println("alert was not present");
    System.out.print(e);
    agobj.logout.click();
    }





    share|improve this answer















    Try a try catch, if something in the try gives a timeout exception (e.g. alert not found) it will execute the code in the catch block.



    WebDriverWait wait = new WebDriverWait(Maf_Base.getDriver(), 10);

    try {
    wait.until(ExpectedConditions.alertIsPresent())
    Alert alert = Maf_Base.getDriver().switchTo().alert();
    alert.accept();
    System.out.println("alert was present and accepted");
    catch(Exception e) {
    System.out.println("alert was not present");
    System.out.print(e);
    agobj.logout.click();
    }






    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Mar 20 at 10:46

























    answered Mar 20 at 9:47









    Niels van ReijmersdalNiels van Reijmersdal

    21.1k23172




    21.1k23172













    • "Thus it reads: if(functionObject == null) {} and a functionObject is not null it is a Object." it is not actually true (if I got you right). ExpectedConditions.alertIsPresent() is "function object" but not wait.until(functionObject). So it does not read if(functionObject == null) {} but rather if(alert==null) where alert is a product of functionObject.apply() that is called inside until method. However alert cannot be null since until just throw an exception if it would be null. However I agree with general idea of looking up the alert.

      – Alexey R.
      Mar 20 at 10:02











    • @AlexeyR. I am not a Java expert, but the wait.until returns an alert, but while it is waiting it is not an alert. An IF comparison in most languages do not wait for the return of a function, it just compares the objects in its current state. Still the try/catch will probably solve the issue.

      – Niels van Reijmersdal
      Mar 20 at 10:45






    • 1





      @AlexeyR. Removed my assumption of how Java works :)

      – Niels van Reijmersdal
      Mar 20 at 10:47











    • @NielsvanReijmersdal thanks, bro!! it's working properly

      – anjith neerukonda
      Mar 20 at 12:50



















    • "Thus it reads: if(functionObject == null) {} and a functionObject is not null it is a Object." it is not actually true (if I got you right). ExpectedConditions.alertIsPresent() is "function object" but not wait.until(functionObject). So it does not read if(functionObject == null) {} but rather if(alert==null) where alert is a product of functionObject.apply() that is called inside until method. However alert cannot be null since until just throw an exception if it would be null. However I agree with general idea of looking up the alert.

      – Alexey R.
      Mar 20 at 10:02











    • @AlexeyR. I am not a Java expert, but the wait.until returns an alert, but while it is waiting it is not an alert. An IF comparison in most languages do not wait for the return of a function, it just compares the objects in its current state. Still the try/catch will probably solve the issue.

      – Niels van Reijmersdal
      Mar 20 at 10:45






    • 1





      @AlexeyR. Removed my assumption of how Java works :)

      – Niels van Reijmersdal
      Mar 20 at 10:47











    • @NielsvanReijmersdal thanks, bro!! it's working properly

      – anjith neerukonda
      Mar 20 at 12:50

















    "Thus it reads: if(functionObject == null) {} and a functionObject is not null it is a Object." it is not actually true (if I got you right). ExpectedConditions.alertIsPresent() is "function object" but not wait.until(functionObject). So it does not read if(functionObject == null) {} but rather if(alert==null) where alert is a product of functionObject.apply() that is called inside until method. However alert cannot be null since until just throw an exception if it would be null. However I agree with general idea of looking up the alert.

    – Alexey R.
    Mar 20 at 10:02





    "Thus it reads: if(functionObject == null) {} and a functionObject is not null it is a Object." it is not actually true (if I got you right). ExpectedConditions.alertIsPresent() is "function object" but not wait.until(functionObject). So it does not read if(functionObject == null) {} but rather if(alert==null) where alert is a product of functionObject.apply() that is called inside until method. However alert cannot be null since until just throw an exception if it would be null. However I agree with general idea of looking up the alert.

    – Alexey R.
    Mar 20 at 10:02













    @AlexeyR. I am not a Java expert, but the wait.until returns an alert, but while it is waiting it is not an alert. An IF comparison in most languages do not wait for the return of a function, it just compares the objects in its current state. Still the try/catch will probably solve the issue.

    – Niels van Reijmersdal
    Mar 20 at 10:45





    @AlexeyR. I am not a Java expert, but the wait.until returns an alert, but while it is waiting it is not an alert. An IF comparison in most languages do not wait for the return of a function, it just compares the objects in its current state. Still the try/catch will probably solve the issue.

    – Niels van Reijmersdal
    Mar 20 at 10:45




    1




    1





    @AlexeyR. Removed my assumption of how Java works :)

    – Niels van Reijmersdal
    Mar 20 at 10:47





    @AlexeyR. Removed my assumption of how Java works :)

    – Niels van Reijmersdal
    Mar 20 at 10:47













    @NielsvanReijmersdal thanks, bro!! it's working properly

    – anjith neerukonda
    Mar 20 at 12:50





    @NielsvanReijmersdal thanks, bro!! it's working properly

    – anjith neerukonda
    Mar 20 at 12:50











    3














    If you check wait.until() code:



    enter image description here



    You will know that it never returns null. It either returns found alert (see lines 88-92) or throws an exception. This is why your if does not work.






    share|improve this answer




























      3














      If you check wait.until() code:



      enter image description here



      You will know that it never returns null. It either returns found alert (see lines 88-92) or throws an exception. This is why your if does not work.






      share|improve this answer


























        3












        3








        3







        If you check wait.until() code:



        enter image description here



        You will know that it never returns null. It either returns found alert (see lines 88-92) or throws an exception. This is why your if does not work.






        share|improve this answer













        If you check wait.until() code:



        enter image description here



        You will know that it never returns null. It either returns found alert (see lines 88-92) or throws an exception. This is why your if does not work.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Mar 20 at 9:53









        Alexey R.Alexey R.

        8,08511033




        8,08511033






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Software Quality Assurance & Testing Stack Exchange!


            • 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%2fsqa.stackexchange.com%2fquestions%2f38327%2fhow-can-i-handle-alert-using-if-else-condition-in-selenium%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 send String Array data to Server using php in android

            Title Spacing in Bjornstrup Chapter, Removing Chapter Number From Contents

            Is anime1.com a legal site for watching anime?