Skip to content

Nischalkhadka/selenium-starter-kit

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

25 Commits
 
 
 
 
 
 
 
 

Repository files navigation

Installation

Install Selenium library

  1. Method I: Open Terminal/cmd and enter command as:

python -m pip install selenium

  1. Method II: You can download the source distribution here, unarchive it, and run the command below:

python setup.py install

Install Webdrivers

Download Chomedriver to drive Chrome or Chromium

Go to ChromeDriver Downloads. Find the latest version of the driver for your platform and download it.

In Linux:

  • Extract the file
  • Make it executable chmod +x chromedriver_linux64.zip
  • Move file to usr/local/bin sudo mv chromedriver_linux64.zip /usr/local/bin

Download geckodriver to drive Firefox

It works with Firefox 48 and newer.

In Linux:

  • Go to Geckodriver Downloads. Find the latest version of the driver for your platform and download it.
  • Extract the file with: tar -xvzf geckodriver*
  • Make it executable: chmod +x geckodriver
  • Move file to usr/local/bin sudo mv geckodriver /usr/local/bin/

In Windows:

  • Download the GeckoDriver
  • Extract it using WinRar or any application you may have.
  • Add it to Path using Command Prompt setx path "%path%;GeckoDriver Path"

For Example:-

setx path "%path%;c:/user/Nischal/Desktop/geckodriver-v0.26.0-win64/geckodriver.exe"

Once, the webdrivers are installed, you can run the tests in browsers (Chrome, Chromium and Firefox).

Getting Started

Using Selenium to write tests

  1. Create a test file "firstTest.py" inside 'tests' folder.
  2. Add the following code in the file
from selenium import webdriver 

#driver = webdriver.Firefox()
driver = webdriver.Chrome() 
driver.get("https://www.lftechnology.com/") 
  1. Run the test file python firstTest.py

Leapfrog homepage should open in Chrome browser.

Updating the test file 'firstTest.py'

Example: Login in phpTravels demo app

from selenium import webdriver
from selenium.webdriver.common.keys import Keys 

# Select the browser to run the tests
#driver = webdriver.Firefox()
driver = webdriver.Chrome()

# Go to phptrabel login page
driver.get('https://www.phptravels.net/login')

#Enter Email
email = driver.find_element_by_name('username')
email.send_keys("user@phptravels.com")

#Enter Password
password = driver.find_element_by_name('password')
password.send_keys("demouser")

#Hit enter 
password.send_keys(Keys.RETURN)

driver.close()

Organizing the test

Lets add another test file 'validLogin.py' and see how the test cases can be organized.

  1. Importing basic modules
import unittest
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
  • 'unittest' module is a built-in Python based on Java's JUnit. It provides the frameworkfor organizing the test cases.
  • The 'selenium.webdriver' module provides all the Webdriver implementation.
  • The 'Keys' class provide keys in the keyboard like RETURN, F1, ALT etc.
  1. class PythonOrgSearch(unittest.TestCase):
  • The test case class is inherited from 'unittest.TestCase'. It is the way to tell 'unittest' module that this is a test case.
  1. Initialization
    def setUp(self):
        self.driver = webdriver.Firefox()
  • The setUp is the part of initialization. It will be called before every test function which we are going write in the test case class. Here we are creating the instance of Forefox Webdriver.
  1. Test Case
def test_demo_user_login(self):
driver = self.driver    
  • This is the test case method. The first line inside this method createa a local reference to the driver object created in 'setUp' method. Note: It should always start with characters 'test'.
  1. Test steps:
  • Go to login form
  • Enter username
  • Enter password ahd hit enter
  • Close the browser after successful login
  1. Teardown method
def tearDown(self):
self.driver.close()
  • The tearDown method will get called after every test method. In the current method, the browser window is closed.
  • The quit will exit the entire browser, whereas close will close a tab, but if it is the only tab opened, by default most browser will exit entirely.:
if __name__ == "__main__":
    unittest.main()

These are some boiler plate code to run the test suite.

References

About

Getting started with Selenium using python

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages