Welcome to the first of maybe more notes on IT ¯\(°_o)/¯. The focus of this article will be on using Selenium to log in to a web app as a registered user. It's pretty simple and shouldn't take much to start testing as long as you have python and pip installed
First though, there are a few items to download to get the environment set up for testing. Here's what to install and download.
pip3 install selenium
if pip is already installed After installing the above, unzip the Chrome driver folder and move the chromedriver.exe
to a location that is stable and easy to remember. I chose an extra hdd partition. We're almost ready to start writing our first Selenium program in Python. So get a text editor ready. ( I prefer Sublime Text Editor )
Now lets look at a simple login use case with Selenium:
The first thing we should notice, besides all the import statements, is that we have a variable for the PATH. Mine is, PATH = 'D:\chromedriver.exe'
. We then use this path to set our driver
variable on line 13 driver = webdriver.Chrome(PATH)
Next you set a variable for the URL of our resource with get_page = 'your page'
. Your page will be the one you are testing.
The next thing we see in the code is the ability to find the login form elements with EC.presence_of_element_located((By.NAME, 'username')
.
Getting the Login functionality to work was a little tricky at first, but by inspecting the webpage code I was able to see that the Login button on my webpage was an individual class, so I selected it with login_button = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.CLASS_NAME, 'btn')))
which eventually successfully logged me in as the test user.