Rochelle
Rochelle

Reputation: 161

Verify an element is not existing using webdriver and selenium

I'm using WebdriverIO and selenium-standalone to write automated tests that will verify that various parts of our user interface are working.

I need to verify that an element is not present on the page. For example, our system allows staff to track various types of resources that we are referring clients to. If a staff member accidentally adds the wrong resource, they can delete it, and I want to verify that the resource was actually deleted and is not present on the page.

WebdriverIO has an .isExisting() property, but no way to check if something is not existing (or not visible/present). I could also use Chai assertions to figure this out, but haven't delved into that world yet.

Here's a snippet of my code:

it('I can delete a resource from a need', function() {
    return driver
    .moveToObject('span.ccx-tasklist-task') // Hover mouse over resource
    .click('div.referral-controls a.btn.dropdown-standalone') // Click Resource drop-down
    .click('div.referral-controls.ccx-dropdown-menu-selected li > a') // Delete Resource
    .pause(2000);
    // Need to Verify that resource was deleted here

Any advice? Let me know if you need more information.

Upvotes: 4

Views: 10455

Answers (4)

Ojasvi Monga
Ojasvi Monga

Reputation: 5139

You can refer https://webdriver.io/docs/api/element/waitForExist.html

Wait for an element for the provided amount of milliseconds to be present within the DOM. Returns true if the selector matches at least one element that exists in the DOM, otherwise throws an error. If the reverse flag is true, the command will instead return true if the selector does not match any elements.

resource.waitForExist(1000, true);

where 1000 is the timeout in ms.

Upvotes: 0

Matt
Matt

Reputation: 74879

You can waitForExist with the reverse option set to true.

.waitForExist( '[id$=OpenNeedsPanel] div.commodities', 500, true )

Upvotes: 6

Rochelle
Rochelle

Reputation: 161

I was able to verify that an element didn't exist on the page like this:

.isExisting('[id$=OpenNeedsPanel] div.commodities').should.eventually.equal(false);

Upvotes: 1

Anil Chandna
Anil Chandna

Reputation: 180

you can simply use the below mentioned line of code

int temp=driver.findElements(By.xpath("your x-path expression")).size();

you can even replace your xpath locator with your other locators as well like id,class,link etc.

Now, if the value of temp>0, it means , your element exists

Upvotes: 0

Related Questions