The Selenium test is not accurately indicating failures - Passing when configuration should be failing instead of detecting a legitimate failure

Hey there, I'm new to Selenium, scripting in Java and still learning the ropes by piecing together code from various tutorials and recorders. Currently, I'm working on a script to verify the presence of a specific 'element' (I would also like to do the reverse). While the script passes when it successfully locates the 'element', if I intentionally change the element details to ensure failure (since it doesn't actually exist), TestNG still marks the test as passing but flags it as a configuration failure?

I assume that I may be overlooking something to address the failed aspect of the test, but I'm not quite sure how to approach it. Every time I try to run into this issue.

package Links;

import org.testng.annotations.*;
import static org.testng.Assert.*;
import org.openqa.selenium.*;
import org.openqa.selenium.firefox.FirefoxDriver;

public class TestArea {
    private WebDriver driver;
    private StringBuffer verificationErrors = new StringBuffer();

@BeforeClass(alwaysRun = true)
public void setUp() throws Exception {
    System.setProperty("webdriver.gecko.driver", "C:\\Automation\\SeleniumFiles\\Browser Drivers\\geckodriver.exe");
    driver = new FirefoxDriver();

}

@Test
public void Example() throws Exception {
    driver.get(
            "http://MyWebsite");
    try {
        assertTrue(isElementPresent(
                By.xpath("The Element I want look for ")));

    } catch (Error e) {
        verificationErrors.append(e.toString());
    }
}
// -------------------------------------------------------------------------------------

@AfterClass(alwaysRun = true)
public void tearDown() throws Exception {
    driver.quit();
    String verificationErrorString = verificationErrors.toString();
    if (!"".equals(verificationErrorString)) {
        fail(verificationErrorString);
    }
}

private boolean isElementPresent(By by) {
    try {
        driver.findElement(by);
        return true;
    } catch (NoSuchElementException e) {
        return false;
    }
}
}

An example of a "passed" test with a failed configuration.

FAILED CONFIGURATION: @AfterClass tearDown java.lang.AssertionError: java.lang.AssertionError: expected [true] but found [false] at org.testng.Assert.fail(Assert.java:96) at Links.TestArea.tearDown(TestArea.java:39) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) at java.lang.reflect.Method.invoke(Unknown Source) at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:124) at org.testng.internal.MethodInvocationHelper.invokeMethodConsideringTimeout(MethodInvocationHelper.java:59) at org.testng.internal.Invoker.invokeConfigurationMethod(Invoker.java:455) at org.testng.internal.Invoker.invokeConfigurations(Invoker.java:222) at org.testng.internal.Invoker.invokeConfigurations(Invoker.java:142) at org.testng.internal.TestMethodWorker.invokeAfterClassMethods(TestMethodWorker.java:214) at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:111) at org.testng.TestRunner.privateRun(TestRunner.java:648) at org.testng.TestRunner.run(TestRunner.java:505) at org.testng.SuiteRunner.runTest(SuiteRunner.java:455) at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:450) at org.testng.SuiteRunner.privateRun(SuiteRunner.java:415) at org.testng.SuiteRunner.run(SuiteRunner.java:364) at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52) at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:84) at org.testng.TestNG.runSuitesSequentially(TestNG.java:1208) at org.testng.TestNG.runSuitesLocally(TestNG.java:1137) at org.testng.TestNG.runSuites(TestNG.java:1049) at org.testng.TestNG.run(TestNG.java:1017) at org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:114) at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:251) at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:77)

PASSED: Example

=============================================== Default test Tests run: 1, Failures: 0, Skips: 0

Configuration Failures: 1, Skips: 0

=============================================== Default suite Total tests run: 1, Failures: 0, Skips: 0 Configuration Failures: 1, Skips:

0

The configuration issue doesn't arise when the test successfully locates the element.

Thank you very much in advance

Answer №1

It seems there are a few issues in the test code provided.

When using TestNG, a @Test method will fail by default if:

  • An assertion fails
  • The test method raises an Exception

Therefore, it is not necessary to enclose the assertTrue() call within a try..catch block. If you want all assertions to run and have the test method fail at the end, consider using something known as Soft Assertion in TestNG.

Here is a cleaned-up version of the test code for reference:

import org.testng.annotations.*; 
import static org.testng.Assert.*; 
import org.openqa.selenium.*; 
import org.openqa.selenium.firefox.FirefoxDriver;

public class TestArea { 
    private WebDriver driver; 

    @BeforeClass(alwaysRun = true) 
    public void setUp() throws Exception { 
        System.setProperty("webdriver.gecko.driver", "C:\\Automation\\SeleniumFiles\\Browser Drivers\\geckodriver.exe"); 
        driver = new FirefoxDriver();
    }

    @Test 
    public void Example() throws Exception { 
        driver.get( "http://MyWebsite"); 
        assertTrue(isElementPresent( By.xpath("The Element I want look for ")));
    } 

    @AfterClass(alwaysRun = true) 
    public void tearDown() throws Exception { 
        driver.quit(); 
    }

    private boolean isElementPresent(By by) { 
        try { 
            driver.findElement(by); 
            return true; 
        } catch (NoSuchElementException e) { 
            return false; 
        } 
    } 
}

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Error: Invalid JSON format - Unable to convert the value "flush" to type HandType

Having a bit of trouble with the deserialization of a Json to a Java object. The Json structure is as follows: "players": [ { "id": "12345678", "handtype": "flush" }, ...

Exploring the process of setting up Jasmine tests for both services and controllers in Angular.js

I have been facing challenges in creating accurate tests for my Angular services and controllers, even though they are functioning correctly. I have searched extensively for a solution to this problem. Below is an example of my controller: angular.module ...

Challenges with importing Selenium WebDriver

It seems that my confusion regarding Python's Import behavior has led to some issues with Selenium. I would appreciate it if someone could clarify why the following occurs in the context of Selenium. When attempting to use from selenium import *, I e ...

There is a single occurrence that happens when you click on a label alongside an

How can I change the number of events triggered when clicking a label without directly selecting the input element? Is there a way to achieve this using CSS only, or is it necessary to use JavaScript like document.querySelector('label input')? ...

Retrieve information from an SQL database using user input in a Classic ASP form without the need to refresh the page

Currently, I am in the process of incorporating a new function into an existing Classic ASP system. This feature involves allowing users to scan a barcode that will automatically populate a text field within a form inside a bootstrap modal. The scanner has ...

Using Java code in Alfresco, replace data with a word document, while excluding any unnecessary characters

Currently, I am working on a Bulk Upload Task in Alfresco. As part of this task, I have created a custom action to call Java code, successfully read data from an Excel sheet, and located the node references of both the target document and the source docume ...

Splitting array elements into arguments separated by commas on Node.js

If I have a function that can take a varying number of arguments, with the last one being a callback: xyz.add('item1', 'item2', ..., 'item10', callback) { ... } This code looks messy. I would like to be able to separate ...

The Jenkins build on a specific branch is encountering issues with path exporting, causing it to fail

I have a set of files related to a new feature that I am trying to deploy through Jenkins. I have successfully deployed other branches in the past, but I am encountering difficulties with a specific branch. https://i.sstatic.net/BKVbH.png I believe the pro ...

Can anyone provide guidance on retrieving all events from an SQLite database using the Alamkanak Android-Week-View library? I am currently only able to retrieve a single event

I am struggling to dynamically add events from my SQLite database to the Week-view. However, I am only seeing the last event that was added to the database. I am using Alamkanak Week-View and have tried multiple solutions without success. Can someone pleas ...

What steps should I take to enable code-coverage in phpunit for a separate repository?

Query I'm interested in utilizing PhpUnit tests in one repository and separating the code being tested into another. Q: Can I still achieve code coverage? At first glance, this may seem unconventional. To steer clear of responses like "this is not r ...

Choosing the Angular5 model

I'm currently working with an Angular5 project that has a <select> element bound to an array of customers. Here's the code snippet: <select class="form-control" [ngModel]="record.customer_id" (ngModelChange)="setCustomer($event)" name=" ...

Eliminate any view in the RecyclerView that is not linked to the List

I am working on a RecyclerViewAdapter that consists of two item types: ItemViewHolder for all items in the ArrayList, and HeaderViewHolder for a single header view. I know how to remove an item from the ArrayList using notifyItemRemoved(position) to update ...

Updating URL on tab selection in Angular Bootstrap Tabs

I am incorporating Bootstrap's Tabs into my project and I want the URL to change when a tab is clicked. For example: /home/first /home/second Unfortunately, I am struggling to make this work properly. Here is the code snippet from my $routeProvider: ...

the conditional operator used without assigning the result to a variable

Exploring conditional operators on html canvas, I am seeking a streamlined approach to dynamically change stroke color based on conditions. Online examples have not provided a satisfactory solution in a single line of code. Currently, without using a cond ...

Troubleshooting "jest + enzyme + react16: The src attribute of <img> tag is not sending

Currently, I am utilizing jest along with enzyme for testing my react component titled "AnimateImage". This component includes an image element within it: import * as React from 'react'; import { PureComponent } from 'react'; interfac ...

How to open a PDF file in a new tab using Angular

Within my Angular 12 application, I am seeking to enable users to "view" a PDF file stored locally in the application within a new tab on Google Chrome. Currently, when implementing the code below in HTML, the file downloads rather than opening in a new ta ...

Cloning jQuery with varied PHP array value

So, I have the given PHP values: PHP: <?php $names = array("Mike","Sean","Steve"); ?> <script type="text/javascript"> var name_data = <?php echo json_encode($names); ?>; </script> <div class="container"> <div cl ...

Utilizing a Java API for dongle connectivity?

Looking to develop a desktop application that will store all SMS messages and senders' numbers in a MySQL database via a dongle. What methods can be utilized to access the dongle and achieve this specific functionality? ...

Calculating the frequency of specific substrings present in the complete object

I am seeking a method to tally substrings of object values. In other words, instead of the entire object containing a string, I want it where one key equals a string. An effective Xpath in XSLT is: count(//v[contains(.,current-grouping-key())]) However, ...

Pressing on an <a> element with Selenium and Python by specifying -- data-slide="next"

I'm a beginner using Selenium with Python for web scraping and encountering difficulties navigating a webpage. My current code navigates by clicking buttons like this: web.find_element("xpath", '').click() with the correct xpath i ...