Automation Testing Case Study Solution
Automation Testing Case Study Solution
By Id and Name
driver.find_element_by_id()
driver.find_element_by_name()
driver.find_element_by_xpath()
<html>
<body>
<form id="signUpForm">
form_element=driver.find_element_by_xpath("//
form[@id='signUpForm']")
email_input=driver.find_element_by_xpath("//form[input/
@name='emailId/mobileNo']")
<input name="emailId/mobileNo" type="text" />
<input name="password" type="password" />
<input name="continue" type="submit" value="SignUp" />
<input name="continue" type="button" value="Clear" />
</form>
</body>
<html>
driver.find_element_by_css_selector()
<html>
<body>
get_div = driver.find_element_by_css_selector('div.round
button')
<div class="round-button">Click Here</p>
</body>
<html>
By Link Text and Partial Link Text
driver.find_element_by_link_text()
driver.find_element_by_partial_link_text()
driver.find_element_by_class_name()
<html>
<body>
get_div = driver.find_element_by_class_name('round-
button')
<div class="round-button">Click Here</div>
</body>
<html>
driver.find_element_by_tag_name()
<html>
<body>
get_div = driver.find_element_by_tag_name('title')
<title>Hello Python</title>
<p>Learn test automation using Python</p>
</body>
<html>
Find List of Elements
- ID
- Name
- Class Name
- Tag Name
- Link Text
- XPath
- CSS Selectors
With ID
With Class
With Attribute
With Sub-string
- css=<HTML tag><:><contains><(text)>
- : – The dot sign is used to symbolize contains method
- Contains – It is the value of a Class attribute which is being accessed.
- Text – The text that is displayed anywhere on the web page irrespective of its location.
Using Wildcards with CSS Selectors
- [attribute*="value"]
- * wildcard also known as containing wildcard.
- [attribute^=”value”]
- [attribute$=”value”]
Example:
<!DOCTYPE html>
<html>
<head>
<style>
-
- Example:
- from selenium import webdriver
- driver = webdriver.Chrome(executable_path="C:\\
chromedriver.exe")
- driver.implicitly_wait(0.5)
- driver.get("https://www.google.com/")
- #identify element
- l= driver.find_element_by_name("q")
- l.send_keys("q")
- #get_attribute() to get value of input box
- print("Value of input box: " + l.get_attribute('value'))
- driver.close()
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
/**
* @param args
*/
We can check a checkbox in a page in Selenium with the help of click() method.
# identifying the checkbox with xpath, then click
driver.find_element_by_xpath("//input[@value='Automation
Tester']").click()
Implicit Wait
- An implicit wait is a global wait applied to all the elements on the page. The wait time is
provided as arguments to the method. For example, if the wait time is 5 seconds, it shall
wait this period of time before throwing a timeout exception.
- An implicit wait is a dynamic wait which means if the element is available at the third
second, then we shall move to the next step of the test case instead of waiting for the
entire five seconds.
- driver.implicitly_wait(2)
- #setting implicit wait 5 seconds
- driver.implicitly_wait(5)
Explicit Wait
- The explicit wait is also a dynamic in nature which means the wait will be there as long
as necessary. Thus, if the wait time is five seconds and our given condition is satisfied at
the third second, we need not halt the execution for the next two seconds.
- w = WebDriverWait(driver, 7)
- w.until(expected_conditions.presence_of_element_located((
By.ID, "Tutorialspoint")))
- The expected conditions commonly used in explicit wait are listed below:
- title_contains
- visibility_of_element_located
- presence_of_element_located
- title_is
- visibility_of
- element_selection_state_to_be
- presence_of_all_elements_located
- element_located_to_be_selected
- alert_is_present
- element_located_selection_state_to_be
- staleness_of
- element_to_be_clickable
- invisibility_of_element_located
- frame_to_be_available_and_switch_to_it
- text_to_be_present_in_element_value
- text_to_be_present_in_element
- element_to_be_selected
Example for both the waits
Logging to console
- We can get console.log output from Chrome with Selenium Python API bindings. We will
perform this with the DesiredCapabilities class. We shall enable the logging from the
browser with DesiredCapabilities.Chrome setting.
- We have to pass this browser capability to the driver object by passing it as a parameter to
the Chrome class. To enable logging we shall set the property goog:loggingPrefs of the
browser to 'browser':'ALL'.
- Syntax
Syntax:dc = DesiredCapabilities.CHROME
dc['goog:loggingPrefs'] = { 'browser':'ALL' }
driver = webdriver.Chrome(desired_capabilities=dc)
Example:
Logging to File
- A log configuration consists of formatter and FileHandler methods. We need to import a
logging package and then create an object which will be responsible for entire logging.
- Syntax
logger = logging.getLogger(_name_)
- The different types of logger level are listed below. We can add all, some or at least one
logger in our test case.
- logger.debug("Debug log")
- logger.info("Information log")
- logger.warning("Warning log")
- logger.error("Error log")
- logger.critical("Critical log")
Example:
import logging
def loggingDmo():
# getLogger() method takes the test case name as input
logger = logging.getLogger(__name__)
# FileHandler() method takes location and path of log file
fileHandler = logging.FileHandler('logfile.log')
# Formatter() method takes care of the log file formatting
formatter = logging.Formatter("%(asctime)s :%(levelname)s :
%(name)s :%(message)s")
fileHandler.setFormatter(formatter)
# addHandler() method takes fileHandler object as parameter
logger.addHandler(fileHandler)
# setting the logger level
logger.setLevel(logging.DEBUG)
logger.debug("Debug log")
logger.info("Information log ")
logger.warning("Warning log")
logger.error("Error log")
logger.critical("Critical log")
- @pytest.fixture
- A test function can use a fixture by mentioning the fixture name as an input parameter.
- Create a file test_div_by_3_6.py and add the below code to it
- import pytest
-
- @pytest.fixture
- def input_value():
- input = 39
- return input
-
- def test_divisible_by_3(input_value):
- assert input_value % 3 == 0
-
- def test_divisible_by_6(input_value):
- assert input_value % 6 == 0