Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8276289
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T08:19:56+00:00 2026-06-08T08:19:56+00:00

I am back with more silly questions 1) How to fail a test if

  • 0

I am back with more silly questions

1) How to fail a test if AssertEquals is false?

I have this code –

public boolean compareWidthPixels(By by, String expected) {
    System.out.println(driver.findElement(by).getCssValue("width"));
    try {
        assertEquals(expected, driver.findElement(by).getCssValue("width"));
        System.out.println("Width as expected");
        return true;

    } catch (Error e) {
        verificationErrors.append(e.toString());
        System.out.println("Width incorrect");
        return false;

    }

This code displays “Width incorrect” when the width does not match the expected value but the test case passes. I want the test to fail if the width are unequal.

2) How to assert/verify an Element is NOT present?

As a novice I tried many things I found in Google and here in Stack Overflow –
Assert that a WebElement is not present using Selenium WebDriver with java, Selenium WebDriver – Test if element is present, etc. But none of them worked. I am working with JUnit4 and need a function that should pass if the element is not present.

Thanks

Maitreya

P.S: Please feel free edit this question if it looks confusing or disoriented.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T08:19:58+00:00Added an answer on June 8, 2026 at 8:19 am

    Answer 1: To use assert true or false you should use if else instead and then call the function by assert e.g.

    public boolean compareWidthPixels(By by, String expected) {
        System.out.println(driver.findElement(by).getCssValue("width"));
        if (driver.findElement(by).getCssValue("width").equals(expected)){
            System.out.println("Width as expected");
            return true;
        } 
        System.out.println("Width incorrect");
        return false;
    }
    

    Then use compareWidthPixels as ‘AssertTrue(compareWidthPixels(by, expected))‘ in your test

    Similarly for 2nd question you can use following

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

    using is element in assertions.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i have the following code: <p class=more><a href=/stories class=more>Read more</a></p> and CSS: a.more, a.back
back again with some more SQLAlchemy shenanigans. Let me step through this. My table
Hey everyone, I'm back and looking forward to more of your brilliance. I have
I have a string like this that I need to parse into a 2D
So, I'm back with more problems with my Flash App. I have a list
Back in my C/C++ days, coding an infinite loop as while (true) felt more
Back in my days as a BeOS programmer, I read this article by Benoit
I have a large C# code base. It seems quite buggy at times, and
I have run across this where I need to know whether my current viewcontroller
i have this situation foreach($front->getRequest()->getParams() as $key => $value){ if ($value == '1'){ $$key

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.