I want to disable all alerts using webdriver while disabling alerts should affect as true.
Thanks
Akmal Rasool
According to me, you want to check for alerts and if any alert is present you want to accept those alerts.
See the below code for the same.
public boolean alertPresence()
{
boolean presence = false;
try
{
driver.switchTo().alert();
presence = true;
alert.accept();
}
catch (NoAlertPresentException Ex)
{
Ex.printStackTrace;
presence = false;
}
}
Related
I have used appium 1.6.4 and android version is on 5.1 ,6.1 and 7.0
Below is my code
public boolean navigateToLoginPage(){
String strng = null;
newObj = new LogInPage();
wait = new WebDriverWait(driver,20);
wait.until(ExpectedConditions.visibilityOf(GetStarted));
GetStarted.click();
driver.manage().timeouts().implicitlyWait(5000, TimeUnit.MILLISECONDS);
if(Country.isDisplayed()){
Country.click();
wait.until(ExpectedConditions.visibilityOf(Cancel));
Cancel.click();
}
driver.manage().timeouts().implicitlyWait(4000, TimeUnit.MILLISECONDS);
MobileNumber.sendKeys("91**********");
Next.click();
driver.manage().timeouts().implicitlyWait(3000, TimeUnit.MILLISECONDS);
OK.click();// this is the problem , this sometimes works sometimes not
//i have used the alert interface but no use
//if(OK.isEnabled()){OK.click();} it returns true and I have also used .isDisplayed but no use
//When Ok button is not clicked but the code in the try block is executed
//I have also used the tap functionality but no effect
try {
strng =newObj.try1();
} catch (SQLException e) {
e.printStackTrace();
}
wait.until(ExpectedConditions.visibilityOf(EnterOTP));
EnterOTP.sendKeys(strng);
Next.click();
return true;
}
I have automation scenario that sometimes the system return javascript alert and sometimes not at all. I don't know what the cause of this, probably the network issue. I already create the alert handler for this:
public boolean isAlertPresent() {
WebDriverWait wait = new WebDriverWait(driver, 5);
wait.until(ExpectedConditions.alertIsPresent());
return true;
}
I call this in one of my step that sometimes appear alert:
public WSSPage enterAndSearchContent(String title) throws InterruptedException {
waitForElementTextWithEnter(searchTextField, title);
while (isAlertPresent()){
Alert alert = driver.switchTo().alert();
alert.dismiss();
break;
}
return PageFactory.initElements(driver, WSSPage.class);
}
The problem is when the alert doesn't show up, it will give me NoAlertPresentException, and the automation result will be failed. I want the code to move on if the alert doesn't happen by moving to the next line, in this case it will just return PageFactory.initElements(driver, WSSPage.class);
Can you help me provide a better code from this?
Thanks a lot.
JavascriptExecutor worked for you. Just take care that you should execute it before clicking the event which invoke alert.
((JavascriptExecutor) driver).executeScript("window.confirm = function(msg) { return true; }");
Note :- do not use it after clicking on event which invoke alert confirmation box. Above code by default set the confirmation box as true means you are accepting/click on ok on all confirmation box on that page if invoked
Hope it will help you :)
You can modify the method isAlertPresent as given below and try it. It may help you.
public boolean isAlertPresent() {
try{
WebDriverWait wait = new WebDriverWait(driver, 5);
wait.until(ExpectedConditions.alertIsPresent());
return true;
}
catch (NoAlertPresentException noAlert) {
return false;
}
catch (TimeoutException timeOutEx){
return false;
}
}
You can include that particular exception in try catch. Then the exception will be catched and will not through any error and your execution will continue.
Also create a implicit wait to handle this with less timestamp.
I am testing a native iOS mobile app using selenium and appium with Java code. As part of my teardown, I have to keep on clicking "back" button until "setting" button appears from where I can logout of the application.
I tried few things using do while but not working. Can anyone help please ?
Try this code may be help you
try {
boolean flag = true;
while(flag) {
WebElement backBtn = driver.findElementByName("back");
backBtn.click();
Thread.sleep(1000);
boolean isFindSettingBtn = driver.findElementsByName("setting").size() !=0;
if(isFindSettingBtn) {
break;
}
}
}catch(Exception e) {
e.printStackTrace();
}
I am using this code to check for invisibility:
WebDriverWait wait = new WebDriverWait(driver,40);
wait.until(ExpectedConditions.invisibilityOfElementLocated(By.xpath(<some xpath>)));
This works perfectly if there is only one element corresponding to the xpath in the webpage.
I have three in the webpage which I am trying to write a script for, and I need selenium to wait for all three.
Note: I am not using absolute xpath.
ExpectedConditions.invisibilityOfElementLocated check for the first element. In your case you could write your own implementation of ExpectedCondition where you have to check if the object is displayed for each of the element which is found.
For Example (not tested) :
private static void waitTillAllVisible(WebDriverWait wait, By locator) {
wait.until(new ExpectedCondition<Boolean>() {
#Override
public Boolean apply(WebDriver driver) {
Iterator<WebElement> eleIterator = driver.findElements(locator).iterator();
while (eleIterator.hasNext()) {
boolean displayed = false;
try {
displayed = eleIterator.next().isDisplayed();
}
catch (NoSuchElementException | StaleElementReferenceException e) {
// 'No such element' or 'Stale' means element is not available on the page
displayed = false;
}
if (displayed) {
// return false even if one of them is displayed.
return false;
}
}
// this means all are not displayed/invisible
return true;
}
});
}
I have a situation where i want to check if there exists a popup, if yes then accept it otherwise move forward. Kindly help as I am new to selenium.I am using java. Thanks.
It will be something like this.
WebDriverWait wait = new WebDriverWait(driver, 10 /*timeout in seconds*/);
if(wait.until(ExpectedConditions.alertIsPresent())==null){
System.out.println("alert was not present");
}
else
{
Alert alert = driver.switchTo().alert();
alert.accept();
System.out.println("alert was present and accepted");
}
I think this may you:
#Test
public void testAlertOk()
{
//Now we would click on AlertButton
WebElement button = driver.findElement(By.id("AlerButton"));
button.click();
try {
//Now once we hit AlertButton we get the alert
Alert alert = driver.switchTo().alert();
//Text displayed on Alert using getText() method of Alert class
String AlertText = alert.getText();
//accept() method of Alert Class is used for ok button
alert.accept();
//Verify Alert displayed correct message to user
assertEquals("this is alert box",AlertText);
} catch (Exception e) {
e.printStackTrace();
}
}
Source: Click here for more detail understanding