简体   繁体   中英

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.

您可以将reverse选项设置为truewaitForExist

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

我能够验证页面上不存在这样的元素:

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

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM