Testing a website by writing a code requires a lot of effort. You have to write the code to test multiple parts of the website. e.g. Website title, elements, content, keywords etc. In this tutorial we are going to test our website title using splinter pythong framework. Let’s get started.
Installation
In order to work with splinter, you need to have python 2.7 onwards installed. If you’ve older version of python then please upgrade it before you work with splinter. Follow the instructions below to install splinter from command line. Make sure you have pip library installed as it helps installing libraries quickly.
# for linux
sudo pip install splinter
#for windows
pip install splinter
If you’ve splinter successfully installed, follow the instructions and let’s start testing splinter if it finds our website title keyword in google search.
Here’s our test process.
- Create browser instance.
- Go to Google search engine webpage.
- Search for website using keywords.
- If the keyword is typed inside the search box, click the button.
- If the website title keyword is found in search result, print success message.
- If the website title keyword is not found then, print failure message.
Step 1 : Create a browser Instance.
Code below imports browser class from splinter library.
from splinter import Browser
browser = Browser()
Step 2 : Go to Google Website
Browser class invokes visit method which takes URL as parameter.
browser.visit('http://google.com')
Step 3: Search for our website title keyword in google search.
Fill the following search keyword in the search box.
browser.fill('q', 'Onecore - Software Development and Testing')
Step 4: Click the search button.
Find the button and then invoke click function to process the click.
browser.find_by_name('btnG').click()
Step 5: Check if the URL matches with the site title in search.
if browser.is_text_present('onecore.net'):
print "Yes, the official website was found!"
else:
print "Not found."
Step 6: Quit Browser
browser.quit()
See the complete code.
from splinter import Browser
browser = Browser()
browser.visit(‘http://google.com’)
browser.fill(‘q’, ‘Onecore – Software Development and Testing’)
browser.find_by_name(‘btnG’).click()
if browser.is_text_present(‘onecore.net’):
print “Yes, the official website was found!”
else:
print “Not found.”
browser.quit()
Execute the code and see the test message in the terminal.
This was simple test to check for the website title and to check website is found in google search for the respective keyword. You can use this snippet for SEO purpose.
Check out the video for the visual instruction of this tutorial.