Blog on selenium

 

Test automation with Selenium WebDriver and pytest

 
 

A brief about Selenium WebDriver

Selenium is an open-source tool that automates web application testing across numerous platforms such as Windows, Mac, and Linux. With Selenium, you can perform web testing against some of the popular browsers like Chrome, Firefox, Opera, Microsoft Edge, etc. It also supports various programming languages like Python, C#, Ruby, PERL, Java, etc.

Selenium WebDriver is one of the core components of the Selenium framework. It is a collection of open-source APIs that are used to automate the testing of a web application. It allows test automation to open a browser, then sends clicks, type keys, scrape text, and finally exit the browser cleanly. There are particular drivers for the browsers that include Chrome, Firefox, Opera, Microsoft Edge.

 

  • driver = Chrome() - Chrome() initializes the ChromeDriver instance using the default options on your local machine.
  • driver.implicitly_wait(10) - The challenging part of web UI test automation is to wait for the page to load/change just after firing an interaction as the page takes time to render new elements. If this automation tries to access the new elements even before they occur, WebDriver will raise a NoSuchElementException in this scenario. It takes up to 10 seconds for elements to exist as the implicity_wait method as mentioned above tells you to wait while trying to find those elements. It is just once that these implicit waits are declared before being used automatically for all elements.
  • yield driver - A value should be returned by the pytest fixture that represents the setup. To the initialized WebDriver the fixture returns a reference. The WebDriver does not use a return statement but uses yield which means the fixture is a generator. In this case the first iteration of the fixture, the WebDriver initialization is the “setup” phase that can be defined before a test begins. The second iteration is to quit calling that denotes a “cleanup” phase and can be used after a test finishes. When you write the fixtures as generators it helps you to keep together the related setup and cleanup operations as a single concern.
  • driver.close() -Always quit your WebDriver instance at the end of a test. When the test automation ends it is not necessary the driver processes on the test machine would always die. If you happen to fail in quitting a driver instance it may keep on running as a zombie process that could consume as well as lock the system resources.

 Example code :


import time
from selenium import webdriver
from selenium.webdriver import ActionChains
from webdriver_manager.chrome import ChromeDriverManager


@pytest.fixture()
def setUp():
global driver, name
name = input("enter product : ")
driver = webdriver.Chrome(ChromeDriverManager().install())
driver.maximize_window()
yield
time.sleep(5)
driver.close()


def test_flip1(setUp):

driver.get("https://www.flipkart.com/")
time.sleep(2)
driver.find_element_by_xpath("/html/body/div[2]/div/div/button").click()
time.sleep(2)
driver.find_element_by_name("q").send_keys(name)
time.sleep(2)
driver.find_element_by_class_name("L0Z3Pu").click()
time.sleep(2)
ac = ActionChains(driver)
ele = driver.find_element_by_xpath("/html/body/div/div/div[3]/div[1]/div[2]/div[14]/div/div/div/a/div[2]/div[1]/div[1]")
time.sleep(2)
ac.move_to_element(ele)
ac.click(ele)
time.sleep(2)
ac.perform()
time.sleep(4)
import pytest

 

Comments

Popular posts from this blog

Queue in Java

Using lists , tuples, sets, dictionaries

Blog on Pytest