Sometimes our automated tests need to dig a bit deeper. Sometimes, we need to validate more than just the happy path. But sometimes we need to do negative testing. This is an easy task for a human mind, but how do we tell a script to find out how something reacts when the unexpected happens? That is what we’ll be covering today in this article.
First of all, we need to know what negative testing is. According to SmartBear, a negative test ensures that the application under test can handle invalid actions gracefully. In other words, negative tests check what happens when things go wrong.
Some negative tests are easier to implement than others. Most of the time a negative test will need to validate that an error pops up if the user does something that is considered invalid. That’s easy to find with Selenium and shouldn’t be a headache. But what happens when you want to make sure something doesn’t show up? Let’s say that there’s a visual component that’s currently not in the DOM and you want to make sure it doesn’t show up. That’s where it gets tricky.
Today we’ll see a few ways to do negative testing with Selenium, including that tricky scenario. For this article, we will be using Instagram’s website. We’ll be creating some negative tests for it.
Another thing worth pointing out is that we will use the Page Object Model as shown in this tutorial. If anything about the samples below seems confusing, please check this tutorial out.
There a 2 ways a character limit can be implemented on a website. They can either show an error when the user exceeds the character limit, or they can prevent the user from writing after they reach the limit. Since the “showing an error” approach is verified in the same way regardless of the error, we’ll focus on how to test if the site is actually preventing the user from writing after they reach the limit.
If we take a closer look at Instagram’s Username field, it does just this. The user is only able to input 30 characters.
The idea for this is that we will locate the UserName field, send more than 50 characters to it, and then we’ll capture its data to check if the field contains more than 30 characters or not. So let’s do this step by step.
Steps
import pytest as pytest
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.wait import WebDriverWait
class SignUpPage:
def __init__(self, driver):
self.driver = driver
.
.
.
self.username_textbox = WebDriverWait(self.driver.instance, 5).until(
EC.visibility_of_element_located((
By.NAME, "username")))
.
.
.
.python3 -m pytest testcases/test_instagram.py -k test_verify_username_character_limit
Most forms deal with required fields in one of 2 ways. They either show the user in real-time that a field is missing once they go past it, or they show the errors once the user submits the form. Either way, is validated in the same way from Selenium’s point of view. In this case, we’ll see a case of a form that shows the error after the user submits the form.
We will run this negative scenario by writing some invalid information in the “Mobile Number or Email” field, leaving the rest of the fields alone and then click the “Next” button.
Steps
python3 -mpytest testcases/test_instagram.py -k test_verify_required_fields
A good example of this case is that when we open this Instagram site from a browser that has never opened it before (like the clean session Selenium starts for every test), we would not want to see the login screen. But how can we confirm this via the Automation?
The thing is, Selenium cannot look for something that’s not present in the DOM. So how do you check if something doesn’t exist? Well, looking for it and letting the search fail. The tricky thing about this is that when the search fails, we will get an exception and our test will fail. The fix for this is handling that exception and if we get the exception that we’re expecting, then continuing with our test.
Steps
python3 -m pytest testcases/test_instagram.py -k test_login_button_is_not_present
And with this, we are done! If you want to check out the source code, you can find it here.
This article was written by Mike Arias
Senior Software Testing Engineer of TechAID.
Twitter: @theqaboy
Blog: qaboy.com/
SHARE THIS POST
Email: howdy@techaid.co
Phone: +1 (888) 280-0575
Copyright © 2020 TechAID Solutions, All rights reserved.
We won't bombard you with so many emails!
We won't bombard you with so many emails!