Selenium WebDriver (Java): How can I nest these NoSuchElement Exception tests? - java

OS: Windows 7 32bit
ChromeDriver version: 2.30
Selenium Webdriver version: 3.4.0
Java 8
I've tried a few different ways to clean this code up and not have to repeat the same try/catch blocks. I'm trying to check that various elements are present on a page I'm testing. I can gracefully report to the console and this code does work with no problems.
The issue I'm having is with the ungraceful code. Is there a way to nest these try/catch blocks, or put them inside of an if/else loop?
try {
driver.findElement(By.xpath("/html/head/title"));
System.out.println("Title found...");
Thread.sleep(1000);
} catch (NoSuchElementException ex) {
System.out.println("Title NOT FOUND...");
// ex.printStackTrace();
}
try {
driver.findElement(By.xpath("//*[#id='logindiv']"));
System.out.println("Login form found...");
Thread.sleep(1000);
} catch (NoSuchElementException ex) {
System.out.println("Login form NOT FOUND...");
// ex.printStackTrace();
}
try {
driver.findElement(By.id("username"));
System.out.println("'Username' field found...");
Thread.sleep(1000);
} catch (NoSuchElementException ex) {
System.out.println("'Username' form NOT FOUND...");
// ex.printStackTrace();
}
try {
driver.findElement(By.id("password"));
System.out.println("'Password' field found...");
Thread.sleep(1000);
} catch (NoSuchElementException ex) {
System.out.println("'Password' form NOT FOUND...");
// ex.printStackTrace();
}
try {
driver.findElement(By.id("loginSubmit")).getText();
System.out.println("Login button found...");
Thread.sleep(1000);
} catch (NoSuchElementException ex) {
System.out.println("Login button NOT FOUND...");
// ex.printStackTrace();
}

A few things...
I'm a firm believer that exceptions should be exceptional, meaning exceptions shouldn't be used as flow control. This is further supported by the docs,
findElement should not be used to look for non-present elements, use findElements(By) and assert zero length response instead.
so you should replace .findElement() and try-catch with .findElements() and test for empty. See example in #2.
You really should use some Assert library like JUnit, etc. It makes these validations so much easier/cleaner.
This whole thing
try
{
driver.findElement(By.id("username"));
System.out.println("'Username' field found...");
Thread.sleep(1000);
}
catch (NoSuchElementException ex)
{
System.out.println("'Username' form NOT FOUND...");
// ex.printStackTrace();
}
should/could look like
Assert.assertFalse(driver.findElements(By.id("username")).isEmpty(), "Validate Username field exists");
Arguments could be made that it's faster, so on and so forth but... it hurts me to see people use something complicated like an XPath to do no more than locate an element by ID, e.g. By.xpath("//*[#id='logindiv']"). This is so much simpler and easier to read as By.id("logindiv").
You can do some googling to see all the details but Thread.Sleep() is a bad practice and should be avoided. Instead use WebDriverWait. Explore ExpectedConditions to see all what can be waited for and use it liberally. In the cases you posted in your code, I don't see any reason to wait for any of these so that's several seconds of unnecessary wasted time.
Your last example is pulling .getText() but not using it. Since you are just checking that the button exists, you can safely remove the .getText() from the end.

You can try this:
public void checkElementVisibile()throws InterruptedException {
element = driver.findElement(By.xpath("/html/head/title"));
Thread.sleep(1000);
if(isElementVisibile(element))
System.out.println("Title found...");
else
System.out.println("Title not found...");
element = driver.findElement(By.xpath("//*[#id='logindiv']"));
Thread.sleep(1000);
if(isElementVisibile(element))
System.out.println("Login form found...");
else
System.out.println("Login form not found...");
element = driver.findElement(By.id("username"));
Thread.sleep(1000);
if(isElementVisibile(element))
System.out.println("'Username' field found...");
else
System.out.println("'Username' field not found...");
element = driver.findElement(By.id("password"));
Thread.sleep(1000);
if(isElementVisibile(element))
System.out.println("'Password' field found...");
else
System.out.println("'Password' field not found...");
}
public static boolean isElementVisibile(WebElement element){
try {
return element.isDisplayed();
}catch (NoSuchElementException ex)
{
return false;
}
}

Related

NoSuchElementException can't be thrown

I'm trying to use an element for testing and I want to continue the test if the element even couldn't be found.
I used NoSuchElementException for this part of my codes.
Here is what I've tried before:
try {
WebElement site_width_full_width = wait.until(
ExpectedConditions.visibilityOfElementLocated(
By.cssSelector("label[for=site_width-full_width]")));
site_width_full_width.click();
Thread.sleep(1000);
System.out.println("FullWidth Label Found!");
} catch (NoSuchElementException e) {
System.out.println("FullWidth Label not found!");
System.out.println(e);
}
But when the element isn't available it can't be thrown into the NoSuchElementException and all test breaks and fails.
What's the solution and how could I continue the test when the element isn't available.
Thanks in advance.
You might be getting the exception of a different derived class type. You can catch it using the parent class 'Exception' and then further drill down the exact exception type.
try using;
try
{
WebElement site_width_full_width = wait.until(
ExpectedConditions.visibilityOfElementLocated(
By.cssSelector("label[for=site_width-full_width]")));
site_width_full_width.click();
Thread.sleep(1000);
System.out.println("FullWidth Label Found!");
}
catch (Exception e)
{
if (e instanceof NoSuchElementException)
{
System.out.println("FullWidth Label not found!");
System.out.println(e);
}
else
{
System.out.println("Unexpected exception!");
System.out.println(e);
}
}
Hope this helps.
You can try with its parent classes like Throwable or Exception in catch block. In my case, I am Throwable in catch block which works as expected
You are catching a NoSuchElementException, but an explicit wait throws a TimeoutException if nothing is found. To get what you have working you should modify your code to do the following:
try {
WebElement site_width_full_width = wait.until(
ExpectedConditions.visibilityOfElementLocated(
By.cssSelector("label[for=site_width-full_width]")
));
site_width_full_width.click();
System.out.println("FullWidth Label Found!");
} catch (NoSuchElementException | TimeoutException e) {
System.out.println("FullWidth Label not found!");
System.out.println(e);
}
However using Try/Catch for execution flow is generally a code anti-pattern. You would be much better off doing something like this:
List<WebElement> site_width_full_width =
driver.findElements(By.cssSelector("label[for=site_width-full_width]"));
if (site_width_full_width.size() > 0) {
System.out.println("FullWidth Label Found!");
site_width_full_width.get(0).click();
} else {
System.out.println("FullWidth Label not found!");
}
Webdriver already provide the way to deal this problem in much simpler way. you can use following way
WebDriverWait wait= new WebDriverWait(driver, TimeSpan.FromSeconds(120));
wait.IgnoreExceptionTypes(typeof(NoSuchElementException), typeof(ElementNotVisibleException));
WebElement site_width_full_width = wait.until(
ExpectedConditions.visibilityOfElementLocated(
By.cssSelector("label[for=site_width-full_width]")));
site_width_full_width.click();
Thread.sleep(1000);
System.out.println("FullWidth Label Found!");
Note : You can add all types of exception need to be ignored.

Selenium: Open jstree Nodes using Node name as String

I'm trying to automatize some test in order to open Jstree Nodes, using the Node name
I would like to make it clicking the arrow at the left of the node, instead of double-clicking the node itself.
Here is the code:
public void abrirSistema(String node) {
Boolean isPresent = driver.findElements(By.xpath("//*[contains(text(),'" + node + "')]")).size() > 0;
if (isPresent) {
System.out.println("System está abierto");
} else {
System.out.println("Hay que abrir el sistema");
WebElement system = driver.findElement(By.xpath("//*[contains(text(),'" + node + "')]"));
WebElement parent = system.findElement(By.xpath(".."));
String parentId = parent.getAttribute("id");
WebElement flecha = driver.findElement(By.xpath("//*[#id=\"" + parentId + "\"]/i"));
// WebElement arrow = parent.findElement(By.className("jstree-icon"));
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
js.executeScript("arguments[0].scrollIntoView(true);", arrow);
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
wait.until(ExpectedConditions.visibilityOf(flecha));
wait.until(ExpectedConditions.elementToBeClickable(flecha)).click();
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
So basically I locate the Node by the name, cause elements on the tree changes dynamically, then pick the arrow, and click it.
The thing is that sometimes the code works, sometimes it doesn't, and I cannot figure it out why.
I make sure that page is full loaded before trying to run this, and when I run the Scenario, the step is always green although the Node is not opened
Also I would like to let you know that this code runs always after opening the root node, which is working properly. Just in case
Hope someone could help me.
Thanks in advance

Click webelement until hidden

I have a web application where I press submit button until data available on the table.When in my no data are available then submit button hidden.So we can get logic until submitting button hides we will click.And when button, not available we show on success message and load next browser Url.
for (k=0;k>30;k++) {
try {
driver.findElement(By.xpath(".//*[#id='content']/input")).click();
driver.switchTo().alert();
driver.switchTo().alert().accept();
Thread.sleep(20000);
} catch (org.openqa.selenium.NoSuchElementException e){
System.out.println(""+location+" Done");
}
}
Here driver.findElement(By.xpath(".//*[#id='content']/input")).click(); this line click my submit button.And after submit one browser alert shows thats why i accept this. In this loop for (k=0;k>30;k++) Blindly i take 30..Is there any logic or any sugggestion how can i manage this...
You can use existence of the element in a way something like this:-
By by = By.xpath(".//*[#id='content']/input");
List<WebElement> buttons = driver.findElement(by);
while(buttons !=null && !buttons.isEmpty()) {
WebElement yourButton = buttons.get(0); // assuming only and the first element in the list
yourButton.click();
driver.switchTo().alert();
driver.switchTo().alert().accept(); // ideally this should be sufficient
Thread.sleep(20000);
buttons = driver.findElement(by);
}
Below code may help you.
try {
while(driver.findElement(By.xpath(".//*[#id='content']/input")).isDisplayed()){
driver.findElement(By.xpath(".//*[#id='content']/input")).click();
driver.switchTo().alert();
driver.switchTo().alert().accept();
Thread.sleep(20000);
}
}
catch (org.openqa.selenium.NoSuchElementException e){
System.out.println(""+location+" Done");
}
another solution with findElements,
while(driver.findElements(By.xpath(".//*[#id='content']/input")).size()>0)
{
try {
driver.findElement(By.xpath(".//*[#id='content']/input")).click();
driver.switchTo().alert();
driver.switchTo().alert().accept();
Thread.sleep(20000);
}
catch (org.openqa.selenium.NoSuchElementException e){
System.out.println(""+location+" Done");
}
}
Implement below Logic :
public void test() throws InterruptedException
{
WebElement button = driver.findElement(By.xpath(".//*[#id='content']/input"));
while(checkButton(button))
{
button.click();
driver.switchTo().alert().accept();
Thread.sleep(1000);
}
}
public static boolean checkButton(WebElement element)
{
if(element.isDisplayed())
return true;
else
return false;
}
It will click until your element get invisible once it your can perform your action further. Hope this will help.
Simply checked with isDisplayed() to check whether element is displayed or not
WebElement ele=driver.findElement(By.xpath(".//*[#id='content']/input"));
//check element is present or not
try {
if(ele.size()>0){
if(ele.isDisplayed()){
ele.click();
}
}
//switch to alert and perform operation
driver.switchTo().alert();
driver.switchTo().alert().accept();
Thread.sleep(20000);
}
catch (Exception e){
System.out.println(""+location+" Done");
}
Following way I solve my Problem..Hope Its will help to next visitor
do
{
try {
driver.findElement(By.xpath(".//*[#name='yt1']")).click();
driver.switchTo().alert();
driver.switchTo().alert().accept();
Thread.sleep(20000);
driver.switchTo().alert();
driver.switchTo().alert().accept();
Thread.sleep(2000);
driver.navigate().refresh();
}
catch (org.openqa.selenium.NoSuchElementException e){
System.out.println(""+location+" Done");
}
}
while ((driver.findElements(By.xpath(".//*[#class='google_data_save']")).size()>0));

Best practice - Generic webdriverWait till loading popup exist, if error appears fail the test

My problem is I have customWaitMethods such as:
public void waitForLoading(WebElement loadingElement, WebElement errorElement) {
long timeOut = Long.parseLong(PropertyReader.getInstance().getProperty("DEFAULT_TIME_OUT"));
try {
WebDriverWait wait = new WebDriverWait(DriverFactory.getInstance().getDriver(), timeOut);
wait.until(ExpectedConditions.invisibilityOfElementLocated(By.id(loadingElement.toString())));
if (errorElement.isDisplayed()) {
throw new TestException();
}
} catch (TimeoutException e) {
System.out.println("Timed out after default time out");
} catch (TestException e) {
System.out.println("Unexpected error occurred, environment error");
e.printStackTrace();
}
}
I need some generic customWait methods. I do a search, but several cases need to be handled. Error msg appear -> fail the test. wait for the loading content, and it disappeared, -> check the search result.
How can I extend this code if I would like to check continuously some error_message element appears as well and in this case I would throw an exception? So independently I can handle the timeout exception and the other, error msg?
This sript is failing because of the IF. ErrorElement does not appear on the page, ---> nosuchelementException
You can catch different Exceptions as you see fit. In your case, you want to catch the TimeoutException to handle time outs. Then catch a different type of exception to handle the error message:
public void waitForLoading() {
long timeOut = Long.parseLong(...);
try {
WebDriverWait wait = new WebDriverWait(...);
wait.until(ExpectedConditions.invisibilityOfElementLocated(...));
if (<error-message-appears>) {
throw new CustomErrorMessageAppearedException();
}
} catch (TimeoutException e) {
System.out.println("Timed out after...");
} catch (CustomErrorMessageAppearedException e) {
// handle error message
}
}
The easiest approach I see is:
public void waitForLoading() {
long timeOut = Long.parseLong(PropertyReader.getInstance().getProperty("DEFAULT_TIME_OUT"));
try {
WebDriverWait wait = new WebDriverWait(DriverFactory.getInstance().getDriver(), timeOut);
if (!wait.until(ExpectedConditions.invisibilityOfElementLocated(By.id("wait_element")));)
{
throw new NoSuchElementException();
}
} catch (TimeOutException e) {
System.out.println("Timed out after " + timeOut + "seconds waiting for loading the results.");
}
}

A Robust Try Catch Method to use in WebDriver?

A Robust Try Catch Method to use in WebDriver?
Can someone advice from there experiece whether the following method looks correct in the likely scenario where searching for an element gets timed out or the incorrect locator has been used?
The timeout Exception dosnt seem to be printing my System.out.println after i set the wait to 2seconds and change the locator with the wrong xpath
My Code:
public void clickSupercarsLink() throws Exception {
try {
this.wait.until(ExpectedConditions.elementToBeClickable(link_Supercars)).click();
} catch (TimeoutException e) {
System.out.println("UNABLE TO FIND ELEMENT : Timeout");
} catch (Exception e) {
System.out.println("UNABLE TO FIND ELEMENT : Exception");
throw (e);
}
}
New Code:
public void clickSupercarsLink() throws Exception {
try {
this.wait.until(ExpectedConditions.elementToBeClickable(link_Supercars)).click();
} catch (TimeoutException e) {
System.out.println("Timed out attempting to click on element: <" + link_Supercars.toString() + ">");
} catch (Exception e) {
System.out.println("Unable to click on element: " + "<" + link_Supercars.toString() + ">");
}
}
#Phil I would want you to throw that exception and handle it at high level. In current scenario, if there is a critical exception, your test will method calling your method clickSupercarsLink will not know that there was an exception.
Any way you are throwing exception, why do you have to catch it and do nothing with it then just printing!! This is not why you throw exception right?
public void clickSupercarsLink() throws Exception {
this.wait.until(ExpectedConditions.elementToBeClickable(link_Supercars)).click();
}

Categories