Code Error in clickAndWaitTillAlert utility function | Selenium Forum
M
Posted on 26/10/2015
Hi

In framework, you created "clickAndWaitTillAlert" utility function as follows


public Alert clickAndWaitTillAlert(String elementtobeClicked)
{
Alert al=null;

for(int i=0;i<5;i++)
{
driver.findElement(By.xpath(elementtobeClicked)).click();

try
{
al=driver.switchTo().alert();
return al;
}catch(Exception e)
{
try {
Thread.sleep(3000L);
} catch (InterruptedException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
return al;
}
I feel, there is a flaw in above code i.e I believe if button is not there for clicking as page refresh then it will stop the code since we have not put it in try catch .
Am I right ?


Is my below modified code is perfect ?




public Alert clickAndWaitTillAlert(String elementtobeClicked)
{

Alert al=null;

for(int i=0;i<5;i++)
{
try{
driver.findElement(By.xpath(elementtobeClicked)).click();
}catch(Exception e)
{

}
try
{
al=driver.switchTo().alert();
return al;
}catch(Exception e)
{
try {
Thread.sleep(3000L);
} catch (InterruptedException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}

}
return al;
}

M
Replied on 26/10/2015

try this

[quote:wazpqetn]

public Alert clickAndWaitTillAlert(String elementtobeClicked)
{

Alert al=null;
try{
for(int i=0;i<5;i++)
{

driver.findElement(By.xpath(elementtobeClicked)).click();
}catch(Exception e)
{

}
try
{
al=driver.switchTo().alert();
return al;
}catch(Exception e)
{
try {
Thread.sleep(3000L);
} catch (InterruptedException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}

}
return al;
}[/quote:wazpqetn]