1

Sorry for the "I found this code, (how) does it work?" question but I promise it has content.

I came across the following:

button = driver.find_by_css_selector("some selector") 
assert button is not None

First of all, I would state that the assert is not even necessary. if the button is not found, it will throw an uncaught exception, and the rest of the code doesn't matter.

for assert button is not None, how could is not None be helpful? Obviously if the button is none it will fail the assertion anyways... I would say we are better off with assert button so we at least check for a truthy value here. That is provided I am wrong that the whole assert is even necessary.

Am I going wrong here? Please let me know if I am misunderstanding anything from Python/Selenium.

C. Peck
  • 3,641
  • 3
  • 19
  • 36

2 Answers2

0

I would say we are better off with assert button so we at least check for a truthy value here.

When it comes to checking if a value if None, using is not None is actually more efficient than the other.

From if A vs if A is not None:, the statement if button: will call button.__nonzero__(), whereas, if button is not None: compares only the reference button with None to see whether it is the same or not.

Red
  • 26,798
  • 7
  • 36
  • 58
  • This is an interesting answer, but performance of the system is a negligible concern for us as the scripts are taking 20+ seconds (too long for my taste). – C. Peck Aug 07 '21 at 12:47
  • @C.Peck It's still a better practice to use `is not None` if you're only testing for `None` values. – Red Aug 07 '21 at 12:51
0

Your question heading :

If we set a variable to “find an element”, what are all the possible return values?

think about this as a positive and negative returns, that is :-

if found, A web element will be returned.

if Not found, there can be multiple exception raised such as NoSuchElement, etc.

Now coming to assert part. First question that comes in my mind is, What is assert and Why do we need assert in automation, I believe you've the same thinking here.

An assert is there to help you, by alerting you to errors that must never occur in the first place, that must be fixed before the product can be shipped. Errors that do not depend on user input, but on your code doing what it is supposed to do.

reference of this can be found here

additionally they can be good

  • Checks if a particular block of code is reachable or not.
  • Checks if the assumptions made by the developer are correct.
  • They can also check the state of an object.

and may be more..

Now, coming to actual question :-

assert button is not None, how could is not None be helpful?

May be it does not look helpful in this context.

But think, about a situation where a customer expect a table visibility, when they click on this button. and in production environment, due to some glitch it's not loading or showing any content, with the help of assertion you can print or show a message to the customer, or can handle an uncaught exception. I know they are other ways as well, but for me personally in automation if I have assertion at place, and if the condition is met or does not meet, I could see the exact execution status in my test report. So it's beneficial for debugging as well.

cruisepandey
  • 28,520
  • 6
  • 20
  • 38