Automation Testing & Log in script using Selenium & Python

Automation Testing & Log in script using Selenium & Python

Automation Testing

  • We all know, testing applications manually and filling forms while testing consume so much time & energy. To avoid this Automation Testing is used.
  • Automation Testing is used to test and perform tedious tasks with the help of machines
  • Automation Testing is performed by using special tools like Selenium Web Driver & executing scripts in programming languages like Python,Java etc.
  • It also helps in saving the manual time & energy required while testing an application/website.
  • In this article, we are going to see what is selenium & how we can perform automation testing with selenium.

Selenium

se.png

  • Selenium automates browsers to perform test scenarios/ cases that you want to execute by writing scripts for it.
  • The main purpose of selenium is to automate tasks for automation testing but you can use selenium for different automation purposes as well.

Installation of Selenium & Python

  • Let's quickly look at the installation process we need for writing our first automation script using selenium & python.
  • We will need to install

    1. Python
    2. Selenium
    3. Driver (ChromeDriver,FireFox Driver etc.) for running browsers.

      • We are going to use Chrome Browser so we need ChromeDriver
    4. Code Editor or IDE (VSCode, PyCharm etc.) for writing scripts
      • We are going to use VSCode

Writing our first Selenium Script

  • Imagine that you have to log into an application 20-30 times in a day and everytime you have to enter the website url & then enter username & password and then log in to an application.
  • We are going to automate this process by writing a script once for log in and then executing it as many times as we want.
  • I am taking website for this atricle is Github, you can take any website url of your choice & perform the same tasks.

    image.png

    1. First you will need to create a python file in your code editor.

      image.png

    2. Import selenium webdriver

      from selenium import webdriver
      
    3. Initialize the webdriver
      driver = webdriver.Chrome("chromedriver.exe")
      
    4. Navigate to Github's log in page
      driver.get("https://github.com/login")
      
    5. To log in we are interested in 3 elements on the page i.e username, password and log in button. So we are going to inspect the webpage by right clicking on the fields and pressing 'Inspect Element Option' or 'Ctrl + Shift + I' and fetch the elements.

      image.png

    6. As we can see that in the HTML Code, 'username' field has id attribute to it named 'login_field'. So we are going to identify fields on basis of the id attribute.

      driver.find_element_by_id("login_field").send_keys("prasannasomoshi@gmail.com")
      
      • 'driver.find_element_by_id' finds the element on web page on the basis of id and 'send_keys()' method acts /simulates your key pressing on keyboard. So whatever you are going to type on keyboard you can pass that value inside send_keys() method.
    7. Similarly we are going to enter password like above

      driver.find_element_by_id("password").send_keys("yourpassword")
      
    8. Next is to click on the sign in button, if you would have noticed that on inspecting the 'Sign In' button, it does not have id attribute. So what do we do next ? In such cases you can use other attributes like we are going to use 'name' attribute over here.

      driver.find_element_by_name("commit").click()
      
    9. Now it's time to run our first script, open the terminal & run the following command.

      python first_script.py
      
      • Remember 'first_script' is your filename
    10. After running our script you shall be able to see your dashboard page.

      image.png

      • Congratulations! You have executed your first automation script using Selenium & Python.

Conclusion

  • In this article, we saw how we can automate a log in process using automation tools like selenium. This is just the beginning there is way more to go. See you in the next article :)