How can I handle alert using if else condition in selenium?
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
add a comment |
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
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
add a comment |
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
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
automated-testing selenium-webdriver selenium2
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
add a comment |
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
add a comment |
2 Answers
2
active
oldest
votes
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();
}
"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 notwait.until(functionObject). So it does not readif(functionObject == null) {}but ratherif(alert==null)wherealertis a product offunctionObject.apply()that is called insideuntilmethod. Howeveralertcannot benullsinceuntiljust 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
add a comment |
If you check wait.until() code:

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.
add a comment |
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
});
}
});
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%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
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();
}
"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 notwait.until(functionObject). So it does not readif(functionObject == null) {}but ratherif(alert==null)wherealertis a product offunctionObject.apply()that is called insideuntilmethod. Howeveralertcannot benullsinceuntiljust 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
add a comment |
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();
}
"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 notwait.until(functionObject). So it does not readif(functionObject == null) {}but ratherif(alert==null)wherealertis a product offunctionObject.apply()that is called insideuntilmethod. Howeveralertcannot benullsinceuntiljust 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
add a comment |
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();
}
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();
}
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 notwait.until(functionObject). So it does not readif(functionObject == null) {}but ratherif(alert==null)wherealertis a product offunctionObject.apply()that is called insideuntilmethod. Howeveralertcannot benullsinceuntiljust 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
add a comment |
"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 notwait.until(functionObject). So it does not readif(functionObject == null) {}but ratherif(alert==null)wherealertis a product offunctionObject.apply()that is called insideuntilmethod. Howeveralertcannot benullsinceuntiljust 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
add a comment |
If you check wait.until() code:

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.
add a comment |
If you check wait.until() code:

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.
add a comment |
If you check wait.until() code:

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.
If you check wait.until() code:

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.
answered Mar 20 at 9:53
Alexey R.Alexey R.
8,08511033
8,08511033
add a comment |
add a comment |
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.
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%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
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
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