Looking to confirm the contents of a text box?

In need of assistance to assert a value that appears in the text field labeled blNo. Struggling with using a proper assert method, any suggestions would be greatly appreciated.

Answer №1

If you need to verify a condition in your test, you can utilize the assertTrue method.

String text = driver.findElement(By.id("blNo")).getText();
assertTrue("Ensure that the text contains the specified value", text.contains("value"));

A custom error message will be displayed if the assertion fails.

Answer №2

Utilizing Testng in conjunction with Selenium has proven to be effective for me. I encountered a challenge when implementing Selenium's assert feature, so I devised my own solution. I am open to constructive feedback on this approach.

To retrieve the text using getText(), simply apply it to your WebElement object as shown below:

WebElement object = driver.findElement(By.id("blNo"));
String text = object.getText();

Next, pass the WebElement object to a method like the AreEqual method detailed here:

public class Assert 
{ 

    public static void AreEqual(String comparethis, String tothis, String falseMsg)
    {

        try
        {
            Log.Info("Compare this object: " + comparethis);
            Log.Info("     to this object: " + tothis);
            if(comparethis.equals(tothis))
            {
                Log.Info(comparethis + " is equal to " + tothis);
            }
            else
            {
                Log.Info(comparethis + " is not equal to " + tothis);
                Fail();
            }
        }

        catch(Exception e)
        {
            Log.Error("[EXCEPTION CAUGHT] : Assert.AreEqual() | " + falseMsg + "  | Exception: " + e);
            throw(e);
        }

    }// AreEqual method

}// Assert Class

Fail method

public static void Fail()
{
    org.testng.Assert.fail("[TEST FAILED]");
    Log.Error("[TEST FAILED] : Assert.Fail()");
}

...

Answer №3

Here is the code snippet you can utilize:

Verify.assertEquals("Desired Content",driver.findElement(By.id("blNo")).getText());

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

Navigating Through Nested Elements with Selenium Using CSS Selectors for BootStrap Styling

Hey everyone, I'm working on a web page that's structured in bootstrap and I need to access the elements of my dropdown menu. I've been using the Firefox Selenium IDE to obtain the XPath, which validates successfully showing that the path ex ...

PhpStorm IDE linking to the default conf.js file for Protractor

I encountered an issue while attempting to execute a basic Protractor script in PhpStorm IDE on Ubuntu. Upon running the conf file, I received the following error message: Usage: protractor [configFile] [options] configFile defaults to protractor.conf.js ...

Protractor unexpectedly giving back a promise instead of the expected attribute value

I'm facing a challenge where I am attempting to extract the value of an HTML attribute and store it in a variable named url_extension. However, instead of getting the desired value, I keep receiving a Promise object. Below is my code snippet: (Please ...

What sets Robot Resource Files apart from Robot Test Suites in Robot Framework?

Currently, I am delving into the world of Robot framework using Python. To streamline my development process, I have seamlessly integrated it with my Eclipse IDE through the RED plugin available in the marketplace. However, upon creating my robot framework ...

Finding a nested div within another div using text that is not tagged with XPath

I need help creating an XPath to select a div with the class "membername" using the parameter "Laura". <div class="label"> <div class="membername"></div> David </div> <div class="label"> < ...

Using Retrofit2 to display JSON data in a RecyclerView

I've been trying to use Retrofit2 to fetch JSON data and then parse it into my RecyclerView adapter without much success. Currently, I can populate one RecyclerView with local DBFlow data, but I'm struggling to do the same for the JSON data retri ...

Having trouble locating or interacting with the xlink:href element using Selenium

Hey there, I've encountered a problem with Selenium where it's unable to locate a button I need to click. Here is how the element appears: <a data-ember-action="792" href="#"></a> <svg xmlns:xlink="http://www.w3.org/1999/xli ...

Timeouts in Selenium C#, specifically with WebDriverWait

Utilizing Visual Studio 2013 and C#, I developed a console application to carry out testing on my website with the help of Selenium. This is the code snippet: public static void Main(string[] args) { IWebDriver driver = new FirefoxDriver( ...

Can Selenium be considered a viable automation tool, similar to how one would utilize cURL?

Due to certain limitations, we are unable to utilize cURL for automating tasks on a third-party website. Instead, we have turned to using Selenium through PHP integration which allows us to successfully perform the necessary tasks. While Selenium is prima ...

Reusing a selenium webdriver object in the event of a test suite being terminated

Is it possible to resume using the same webdriver if a test execution suddenly ends? Currently, I am utilizing a webdriver reference in all of my test cases but I need to reutilize the same driver object when restarting a test suite. After doing some res ...

Tips on how to automatically extract information (KMZ? JSON?) from My Google Maps

I am looking for a solution to automate the extraction of data from My Google Map without having to manually export it each time. Ideally, I want to streamline the process by integrating it into a larger script that runs smoothly without requiring me to lo ...

Retrieving HTML content of a scrolled page using Selenium

I am a beginner with Selenium and currently attempting to scroll to the bottom of Twitter profiles in order to access all tweets for web scraping. My goal is to retrieve the HTML content of the fully scrolled page, but I encountered an issue while trying t ...

I'm facing issues with XPath, even if I directly copied it from Chrome's inspect tool

I've been attempting to automate my tasks and we rely on service-now for our requests. Despite my efforts, I can't seem to get Selenium to function correctly on the service-now website. It works on the login page without any issues, but when it c ...

Encountered a java.io.IOException while trying to run react-native on Android, resulting in failure

ERROR: The build process was unsuccessful due to an exception. What caused the issue: The task ':app:transformClassesAndResourcesWithProguardForDebug' execution failed. java.io.IOException: Unable to write [/home/user/Desktop/pratap/react-pr ...

I keep encountering an exception when I use POJO

Greetings everyone, could you please review my code snippet below? public class Sample1 extends Sample { public static void main(String[] args) { System.setProperty("webdriver.chrome.driver", "C:\\Users\&bsol ...

Navigating through an ajax-based webpage entirely with selenium webdriver

I have attempted to scroll a page entirely using the following code: var scrollToBottom = function() { window.scrollTo(0, Math.max(document.documentElement.scrollHeight, document.body.scrollHeight, document.documentElement.clientHeight)); }; window.on ...

Having trouble selecting the input text box element in Selenium using Java

My goal is to extract data from an input text box and update it without using id or name. Each input tag will have different values for id, name and value. Below is the HTML code snippet for one of the input tags: <input id="rgrdSearchResult_ctl00_ctl0 ...

What are the best practices for managing certificates with Selenium?

I am currently utilizing Selenium to initiate a browser. What is the best approach for handling webpages (URLs) that prompt the browser to accept or deny a certificate? In the case of Firefox, I sometimes encounter websites that require me to accept their ...

Retrieve a nested JSON item using Java code

I'm working with a JSON object that has the following structure: { "accessToken" : "<dont need this>", "clientToken" : "<nor this>", "selectedProfile" : { "id" : "<nope>", "name" : "<I need this>", ...

Adding CSS files to a JSP page in a Struts2 application

Hello, I am currently learning Java and working on a Struts2 project. I have added a stylesheet in the following path: /WEB-INF/common/css/style.css and also included Bootstrap in this directory: /WEB-INF/common/css/bootstrap/bootstrap.min.css I ...