Automation features make very effective and interesting use of pytest and selenium. In this tutorial we will log into Facebook using Pytest, you need to use Pytest and selenium. selenium is used to automate browser and control browser and Pytest framework is used for testing the successful login of Facebook.
Prerequisites
- Python 3.6+ or pypy3
- Install Selenium ChromeDriver on your PC
Automate Registration form in Python using Pytest
In this example, we will automate a Facebook registration form, and below is the Scenario,
Test cases:
- Open Chrome
- Navigate to Facebook URL.
- Search and enter first name, Lastname, email, password, date of birth, month, and year.
- Click on Signup
- Then we will verify the user is registered successfully.
Note: We can configure selenium to use any browser like Firefox, Chrome, Safari, etc.
Automation Script to register in Facebook using Pytest.
Create a project folder with the name “test_Facebook” then create a test file named test_facebook.py.
test_facebook.py
from selenium import webdriver from selenium.webdriver.chrome.service import Service from selenium.webdriver.common.by import By from selenium.webdriver.support.select import Select import pytest @pytest.fixture() def test_set(): s=Service("D:\\python\\DATA SCIENCE\\chromedriver.exe") global driver driver = webdriver.Chrome(executable_path="D:\\python\\DATA SCIENCE\\chromedriver.exe") driver.maximize_window() def test_login(test_set): driver.get("https://www.facebook.com/") #getting to URL driver.find_element(By.XPATH,"//*[text()='Create New Account']").click() # driver.implicitly_wait(10) driver.find_element(By.NAME,"firstname").send_keys("Admin") driver.find_element(By.NAME,"lastname").send_keys("pass") driver.find_element(By.NAME,"reg_email__").send_keys("Admin@gmail.com") driver.find_element(By.NAME,"reg_email_confirmation__").send_keys("Admin@gmail.com") driver.find_element(By.ID,"password_step_input").send_keys("XXXX123") day = Select(driver.find_element(By.XPATH,"//select[@title='Day']")) day.select_by_visible_text("10") month = Select(driver.find_element(By.NAME,"birthday_month")) month.select_by_visible_text("Jul") year = Select(driver.find_element(By.NAME,"birthday_year")) year.select_by_visible_text("2000") driver.find_element(By.XPATH,"//label[text()='Female']").click() driver.find_element(By.XPATH,"//button[text()='Sign Up']").click()
Execute the above code using the command pytest test_.py and Here, is the output of the code test passed successfully and we will be able to login Facebook using pytest.
Code Explanation:
Importing necessary modules
from selenium import webdriver from selenium.webdriver.chrome.service import Service from selenium.webdriver.common.by import By from selenium.webdriver.support.select import Select import pytest
Declaring fixture @pytest.fixture and defining test_set() function to give the path of chromedriver.exe in function webdriver.Chrome() this will open chrome browser and then make the variable driver global so it can be accessed in other test functions also.
@pytest.fixture() def test_set(): s=Service("D:\\python\\DATA SCIENCE\\chromedriver.exe") global driver driver = webdriver.Chrome(executable_path="D:\\python\\DATA SCIENCE\\chromedriver.exe") driver.maximize_window()
Defining function test_login(test_set) taking test_set as input parameter
def test_login(test_set):
Using driver.get() function to get URL.
driver.get("https://www.facebook.com/") #getting to URL
Use inspect element tool on the browser to inspect name, id, XPath. In this case, we will be inspecting the first name box, Lastname box, email box, re-entering the email box to find their names.
Using driver.find_element() function we can find required elements like (Xpath , Name , id) and by send_keys() we can input data into the box. driver.implicitly_wait() function will wait for input seconds to execute the next commands.
driver.get("https://www.facebook.com/") #getting to URL driver.find_element(By.XPATH,"//*[text()='Create New Account']").click() # driver.implicitly_wait(10) driver.find_element(By.NAME,"firstname").send_keys("Admin") driver.find_element(By.NAME,"lastname").send_keys("pass") driver.find_element(By.NAME,"reg_email__").send_keys("Admin@gmail.com") driver.find_element(By.NAME,"reg_email_confirmation__").send_keys("Admin@gmail.com") driver.find_element(By.ID,"password_step_input").send_keys("XXXX123") day = Select(driver.find_element(By.XPATH,"//select[@title='Day']")) day.select_by_visible_text("10") month = Select(driver.find_element(By.NAME,"birthday_month")) month.select_by_visible_text("Jul") year = Select(driver.find_element(By.NAME,"birthday_year")) year.select_by_visible_text("2000") driver.find_element(By.XPATH,"//label[text()='Female']").click() driver.find_element(By.XPATH,"//button[text()='Sign Up']").click()
In this Sign-Up form, all details will get filled automatically as per the user input data and create a Facebook account for the user with username Admin and Lastname pass with an email address Admin@gmail.com.
Similarly, we can automate other websites like Twitter, Instagram, etc. thus in this tutorial we have learned how to automate and register in Facebook using pytest and selenium.