2

Let's say I want to verify that an upload button is not present in a page. I cannot use any locator here since I don't know the object locator value. So how do I assert in my test that it is never present?

  • 1
    Possible duplicate of How to assert that particular element is not displayed? – Moro May 14 at 7:55
  • Saurav, do you mean the button is never present? If not, you can either use the locator is should have if it was there and check for the not displaying. If that's the case, I can make it an answer, placing the code. – João Farias May 14 at 8:36
  • Is there a specific button you want to check the absence of? If so, why don't you know how you'd locate it if it was present? If not, why are you testing that an unknown button isn't there; why might it be? – jonrsharpe May 14 at 9:49
  • I think this question is more general and better left open than the downvoted code and error dump that the linked question basically is. I'm voting to leave this one open and close the other as a duplicate of this one. – c32hedge May 14 at 14:38
2

Normally "upload button" is a HTML input of type file which looks like:

<input type="file" ...... >

So you need to verify whether at least 1 input of type file is present at the page. It can be done using the following XPath selector:

//input[@type='file']

Example code (Python)

file_upload_elements = driver.find_elements_by_xpath("//input[@type='file']")

if (len(file_upload_elements) > 0):
    print('File upload input is present')
else:
    print('File upload input is absent')
1

In my opinion, there is no such thing as I can not use any locator here. An (assumed) upload button is part of the DOM and can be located in some way.

Maybe the button is always in the same place, so you can check if By.css(".container button") is present. Maybe you can ask the devs to provide a better locator. Or you could count the visible buttons on the page, maybe these are pretty constant so a +1 change would mean the button is present.

If maybe the button text is always the same you could use something like driver.findAll(By.css("button")).stream().filter(e -> e.getText().equals("buttontext")).count() == 0

There are definitely more ways, but I hope these ideas give you a direction.

0

Using Java

 boolean textPresent = false;   
 try {
 if(driver.findElement(By.xpath("//*[contains(text(),'Update’)]”))){
   textPresent = true;
 } catch (Exception e) {
   textPresent = false; 
 }

Using Ruby with capybara

expect(Capybara.current_session).to have_content('Update')

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.