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?
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:
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')
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.