Automate Twitter login Using Python Pytest

As we have automated facebook in our previous tutorial now we will start automation and login on Twitter using pytest and selenium. using the pytest framework we can test the automation was done successfully or not and by using selenium we can control browser activities.

Prerequisites

  • Python 3.6+ version or pypy3
  • Install Pytest

Automate Twitter Login in Python Using Pytest

In this example, we will automate the Twitter login form, and below is the test scenario.

Test case to login into Twitter

  • Open Chrome
  • Navigate the Twitter URL
  • Search and Enter email address and password
  • Click on Log in
  • Then we will verify that the user logged in successfully

Create a project folder with the name test_twitterlogin then create a test file named twitter_test.py.

twitter_test.py

from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager
import bs4
import time
from selenium.webdriver.chrome.options import Options
import pytest
@pytest.fixture()
def test_login():
    global driver
    driver = webdriver.Chrome(executable_path=ChromeDriverManager().install(), options=Options())
    driver.implicitly_wait(10)
    driver.maximize_window()
def test_setup(test_login):
    driver.get('https://twitter.com/i/flow/login')
    time.sleep(8)
    your_email = "xxxx@gmail.com"
    driver.find_element_by_name('text').send_keys(your_email)
    time.sleep(10)
    div = driver.find_elements_by_class_name('css-18t94o4')
    div[2].click()
    time.sleep(10)
    driver.find_element_by_name('password').send_keys('xxxxabcd')
    login = driver.find_elements_by_class_name('css-18t94o4')
    login[3].click()
    assert True

Execute the above code using the command pytest twitter_test.py and here is the output of the code test passed successfully and we will be able to log in to Twitter successfully.

Code Explanation

Import necessary modules

from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager
import bs4
import time
from selenium.webdriver.chrome.options import Options
import pytest

Define pytest fixture and test_setup() function now make driver variable global. by using driver=webdriver.Chrome(executable_path=ChromeDriverManager().install(),options=Options()) we can directly use selenium web driver every time when code will execute it will install webdriver automatically. use driver.implicitly_wait() function to wait for input seconds to execute another task.

@pytest.fixture()
def test_login():
    global driver
    driver = webdriver.Chrome(executable_path=ChromeDriverManager().install(), options=Options())
    driver.implicitly_wait(10)
    driver.maximize_window()

Now define test_login() function and take test_setup() function as input parameter.

def test_setup(test_login):

Driver. get() will get you to input the URL.

driver.get('https://twitter.com/i/flow/login')

Using driver.find_element_by_name.() we can find elements on the browser by their name to inspect the relevant data.

Using driver.find_element_by_class_name() we can find elements on the browser according to their class name.

Using the send_keys() function we can send input data into the input fields.

def test_setup(test_login):
    driver.get('https://twitter.com/i/flow/login')
    time.sleep(8)
    your_email = "xxxx@gmail.com"
    driver.find_element_by_name('text').send_keys(your_email)
    time.sleep(10)
    div = driver.find_elements_by_class_name('css-18t94o4')
    div[2].click()
    time.sleep(10)
    driver.find_element_by_name('password').send_keys('xxxxabcd')
    login = driver.find_elements_by_class_name('css-18t94o4')
    login[3].click()
    assert True
In this twitter login form, details will get filled automatically according to user input Email and password when you will execute the code and logged into your Twitter account directly.
Thus, in this tutorial, we have learned how to log in and automate Twitter using pytest and selenium.