May 31, 2020

Selenium Python Tutorial 4 | selenium python drop down list | Python WebDriver

This is the fourth tutorial in the Selenium Python Tutorials for Beginners. This Selenium Python beginner tutorial explains how to work with a DropDown using Selenium WebDriver. First, view the Selenium Python Drop Down List tutorial. Then read on.
 A dropdown is a web element that you can click to open a number of values. You may select any value from the options. Here is the test case to test the Journey Planner application.

Test stepExpected result
Navigate to the Journey Planner application.
Enter a distance, speed and time to travel.
Click the Calculate Results button.The result in days should appear.
Repeat the previous two test steps for each option in the DropDown box.

Here is my Selenium Python example that tests each option in the Selenium Python drop down list. I have put explanations in the comments within the code (lines or phrases starting with #).

# Selenium WebDriver Python coding
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait as W
from selenium.webdriver.support import expected_conditions as E
# The Select library operates the drop down list.
from selenium.webdriver.support.ui import Select
import time

exec_path = r"E:\Training\SeleniumPython\Downloads\geckodriver-v0.26.0-win32\geckodriver.exe"
URL = "https://inderpsingh.blogspot.com/2014/08/demowebapp_24.html"
distance_id_locator = "distance"
distance = 0 # Assign some initial value to distance.
speed_id_locator = "speed"
speed = 45 # Assign some initial value to speed.
time_id_locator = "hours"
calculate_css_locator = ".post-body > div:nth-child(1) > div:nth-child(1) > form:nth-child(1) > button:nth-child(20)"
result_id_locator = "result"
wait_time_out = 5

# Define the Python WebDriver
driver = webdriver.Firefox(executable_path=exec_path)
driver.get(URL)
wait_variable = W(driver, wait_time_out)
# JavaScript to scroll window down by 240 pixels
driver.execute_script("window.scrollBy(0,240)", "")
# Find web elements using WebDriverWait Python.
distance_element = wait_variable.until(E.presence_of_element_located((By.ID, distance_id_locator)))
speed_element = wait_variable.until(E.presence_of_element_located((By.ID, speed_id_locator)))
# Time_element is a drop down list. Call the Select method on Explicit Wait.
time_element = Select(wait_variable.until(E.presence_of_element_located((By.ID, time_id_locator))))
calculate_element = wait_variable.until(E.presence_of_element_located((By.CSS_SELECTOR, calculate_css_locator)))
result_element = wait_variable.until(E.presence_of_element_located((By.ID, result_id_locator)))
# time_element.options is the list of all the options in the dropdown list.
for option in time_element.options:
    distance_element.clear()
    distance += 100 # Generate distance test data by adding 100 to it's previous value.
    distance_element.send_keys(distance)
    speed_element.clear()
    speed += 1 # Generate speed test data by adding 1 to it's previous value.
    speed_element.send_keys(speed)
    time_element.select_by_visible_text(option.text) # Select the dropdown option.
    calculate_element.click()
    time.sleep(1)
    # Calculate the expected result using my own calculation.
    expected_result = round(float(distance)/float(speed)/float(option.get_attribute("value")),4)
    if expected_result == int(expected_result): expected_result = int(expected_result)
    # Validate by comparing expected result with the actual result from the application.
    if str(expected_result) in result_element.text:
        print ("Passed", str(expected_result), result_element.text)
    else:
        print ("Failed", str(expected_result), result_element.text)

Want to learn more e.g. how to find the web element locators and see the above code working? Please view my Selenium Python Drop Down List tutorial. Thank you.

May 24, 2020

Selenium Python Tutorial 3 | selenium python Checkbox | Example to click Check Boxes Combinations

This is the third tutorial in the Selenium Python Tutorials for Beginners. This Selenium Python beginner tutorial explains how to work with selenium python checkbox. A checkbox is a web element that belongs in a group of checkboxes. You can select none, any or all check boxes in that group.. First, view the Selenium Python Checkbox tutorial. Then read below.
Here is my Selenium Python example to click checkboxes combination until the answer is correct, for each question in my HTML Quiz.

# Selenium WebDriver Python coding
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait as W
from selenium.webdriver.support import expected_conditions as E
# Import the checkbox user defined function file below.
import CheckboxFunctions as C

exec_path = r"E:\Training\SeleniumPython\Downloads\geckodriver-v0.26.0-win32\geckodriver.exe"
URL = "https://inderpsingh.blogspot.com/2013/01/HTMLCSSQuiz1.html"
wait_time_out = 15
check_name_locator = "option"
driver = webdriver.Firefox(executable_path=exec_path)
wait = W(driver, wait_time_out)
# Navigate to the Quiz web page.
driver.get(URL)

i = 0
while i < 10:
    # The counter variable, i, represents the question number in the quiz.
    i += 1
    # Selenium Python scrolling the web page down by 120 pixels
    driver.execute_script("window.scrollBy(0,120)","")
    check_element_1 = wait.until(E.presence_of_element_located((By.NAME, check_name_locator + str(i) + "1")))
    check_element_2 = wait.until(E.presence_of_element_located((By.NAME, check_name_locator + str(i) + "2")))
    check_element_3 = wait.until(E.presence_of_element_located((By.NAME, check_name_locator + str(i) + "3")))
    # Check each checkbox by clicking it. 
    check_element_1.click()
    check_element_2.click()
    check_element_3.click() # Check checkbox 1, 2 and 3.
    # C is the alias for CheckboxFunctions Python script.
    # If answered returns True, skip the remainder of the while loop with continue statement.
    if C.answered(driver, i): continue
    check_element_1.click() # Check checkbox 2 and 3 only.
    if C.answered(driver, i): continue
    check_element_1.click()
    check_element_2.click() # Check checkbox 1 and 3 only.
    if C.answered(driver, i): continue
    check_element_2.click()
    check_element_3.click() # Check checkbox 1 and 2 only.
    if C.answered(driver, i): continue
    check_element_2.click() # Check checkbox 1 only.
    if C.answered(driver, i): continue
    check_element_1.click()
    check_element_2.click() # Check checkbox 2 only.
    if C.answered(driver, i): continue
    check_element_2.click()
    check_element_3.click() # Check checkbox 3 only.
    if C.answered(driver, i): continue
#driver.quit()

The above Test Automation code calls the answered user defined function that I wrote in CheckboxFunctions.py file below.

# Selenium WebDriver Python coding
from selenium.webdriver.common.by import By
# The following two import statements are for webdriverwait Python (explicit wait).
from selenium.webdriver.support.ui import WebDriverWait as W
from selenium.webdriver.support import expected_conditions as E
import time
wait_time_out = 15

def answered(d, question_number):
    # function documentation on the next line
    """Locate the answer element. Return True or False based on the value attribute."""
    wait_variable = W(d, wait_time_out)
    answer_element = wait_variable.until(E.presence_of_element_located((By.NAME, "answer" + str(question_number))))
    time.sleep(0.25)
    if "Correct." in answer_element.get_attribute("value"):
        return True
    else:
        return False

Want to understand the Selenium Python code better and see it working? Please view my Selenium Python Checkbox tutorial. Thank you.

May 14, 2020

Selenium Python Tutorial 2 | Python Automation Testing | Selenium Python Radio Button

This is the second tutorial in the Selenium Python Tutorials for Beginners. This Selenium Python beginner tutorial explains how to work with radiobuttons. First, view the Selenium Python Radio Button tutorial. Then read below.
 

A radiobutton is a web element that belongs in a group of radiobuttons. Exactly one radiobutton can be selected in that group, meaning the radio button selection is mutually exclusive. Here is my first Selenium Python example to answer Question 1 of my Selenium WebDriver Quiz.

# Selenium WebDriver Python coding
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait as W
from selenium.webdriver.support import expected_conditions as E

exec_path = r"E:\Training\SeleniumPython\Downloads\geckodriver-v0.26.0-win32\geckodriver.exe"
URL = "https://inderpsingh.blogspot.com/2013/04/SeleniumWebDriverQuiz4.html"
wait_time_out = 5
# Give the locator for Selenium Python Radio Button
answer1_radio_id_locator = "13"
answer1_name_locator = "answer1"

driver = webdriver.Firefox(executable_path=exec_path)
wait = W(driver, wait_time_out)
driver.get(URL)
# Use explicit wait (webdriverwait python) to find the radiobutton.
radio_element = wait.until(E.presence_of_element_located((By.ID, answer1_radio_id_locator)))
radio_element.click()
answer1_element = wait.until(E.presence_of_element_located((By.NAME, answer1_name_locator)))

# Validate the test result.
if "Correct." in answer1_element.get_attribute("value"):
    print ("Test passed.")
else:
    print ("Test failed.")

Here is my second Selenium Python example, that answers the quiz automatically using Python browser automation.

# Selenium WebDriver Python coding
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait as W
from selenium.webdriver.support import expected_conditions as E
import time

exec_path = r"E:\Training\SeleniumPython\Downloads\geckodriver-v0.26.0-win32\geckodriver.exe"
URL = "https://inderpsingh.blogspot.com/2013/04/SeleniumWebDriverQuiz4.html"
wait_time_out = 5
answer_name_locator = "answer"
score_id_locator = "score"
driver = webdriver.Firefox(executable_path=exec_path)
wait = W(driver, wait_time_out)
driver.get(URL)
# Run a loop for the question number 1 through 6.
for q in range(1, 7):
    # Run a loop for the answer number 1 through 4.
    for a in range(1, 5):
        # Craft the locator for Selenium Python Radio Button.
        radio_element = wait.until(E.presence_of_element_located((By.ID, str(q) + str(a))))
        radio_element.click()
        time.sleep(1)
        answer_element = wait.until(E.visibility_of_element_located((By.NAME, answer_name_locator + str(q))))
        if "Correct." in answer_element.get_attribute("value"): break

#validation
score_element = wait.until(E.visibility_of_element_located((By.ID, score_id_locator)))
if "6/6" in score_element.text:
    print ("Test is passed.")
else:
    print ("Test is failed.")

That is all in this Python Automation Testing tutorial. Want to see the above Selenium Python code working in the PyCharm IDE? Please see my Selenium Python Radio Button tutorial. Thank you.

May 10, 2020

Selenium Python Tutorial 1 | Selenium WebDriver python | Python Automation Testing | Python WebDriver

This is the first tutorial in the Selenium Python Tutorials for Beginners. This Selenium Python beginner tutorial gives an introduction to Python and Selenium WebDriver, pip install Selenium and multiple selenium python example. First, view the Selenium Python tutorial 1. Then read below.
You can do Python browser automation with Selenium WebDriver. Python programming language is also used for web development, game development, machine learning and other applications. The Selenium library Python is open source and free. Selenium tests web applications in multiple browsers.

In order to work with selenium using python, we need to install Python 3, which we can install from the Python official website. Next, how to Python install Selenium? We can use pip, which is the Python package installer. In the command prompt, the pip install Selenium command is below. This command gets the Selenium Python download or the Selenium package from the Selenium official website.

pip install -U selenium

For Python Selenium automation, we can create a new project in the PyCharm IDE. Click File> New Project. Then give the name of the project. In order to configure Selenium WebDriver in this project, select the project. Click File > Settings > Project > Project interpreter. It shows the packages that are available. Click + button, which is the Install button. It shows all the available packages. In the search text box, type selenium. Select selenium and click Install Package button. It gives the message, Package 'selenium' installed successfully. Click close button. Then it shows selenium in the packages list also. Click OK button. Next, right-click the Project > New > Python file. Give a name and click OK button. We need to download the Python WebDriver libraries for the browsers. We can go to the Selenium website  and click on the Downloads tab. In the Selenium Client & WebDriver Language Bindings section, find Python and click the Download link. Here are the drivers for the browsers like Firefox (GeckoDriver), Chrome (ChromeDriver) and Edge (EdgeDriver) which we can download. Now, we are ready for selenium using python. Here is my first Selenium Python example:

# Selenium WebDriver Python coding
# Import WebDriver library from the Selenium package
from selenium import webdriver
# Import By to find elements on the web page
from selenium.webdriver.common.by import By

# Give the location of the browser driver. r means a raw Python string.
exec_path = r"E:\Training\SeleniumPython\Downloads\geckodriver-v0.26.0-win32\geckodriver.exe"
URL = r"https://www.wikipedia.org/"
english_link_locator = "js-link-box-en"
search_locator = "searchInput"
search_text = "Software"

# Define the Selenium WebDriver variable with the executable path.
driver = webdriver.Firefox(executable_path=exec_path)
# Navigate to the URL.
driver.get(URL)
driver.maximize_window()
# Find the English link.
english_link_element = driver.find_element(By.ID, english_link_locator)
english_link_element.click()
# Find the Search text box.
input_box_element = driver.find_element(By.ID, search_locator)
input_box_element.send_keys(search_text)
input_box_element.submit()
# The quit method closes the browser windows.
# driver.quit()

Here is my second Selenium Python example with WebDriverWait Python using multiple language link locators :

# Selenium WebDriver Python coding
from selenium import webdriver
from selenium.webdriver.common.by import By
# Import statements for explicit wait
from selenium.webdriver.support.ui import WebDriverWait as W
from selenium.webdriver.support import expected_conditions as E
import time

exec_path = r"E:\Training\SeleniumPython\Downloads\geckodriver-v0.26.0-win32\geckodriver.exe"
URL = r"https://www.wikipedia.org/"
# Define a list of locators for the language links.
language_locators = ["js-link-box-en", "js-link-box-ru", "js-link-box-de"]
search_locator = "searchInput"
search_text = "Software"
wait_time = 5

driver = webdriver.Firefox(executable_path=exec_path)
driver.get(URL)
# Define the wait variable for explicit wait.
wait = W(driver, wait_time)
driver.maximize_window()
for i in range(len(language_locators)):
    language_link = wait.until(E.presence_of_element_located((By.ID, language_locators[i])))
    language_link.click()
    input_box_element = wait.until(E.presence_of_element_located((By.ID, search_locator)))
    input_box_element.send_keys(search_text)
    input_box_element.submit()
    # Pause the script for a few seconds.
    time.sleep(4)
    driver.back()
    driver.back()
# driver.quit()

That is all in this Python Automation Testing tutorial. Want to learn more details like how to find the locators? Want to know about the synchronization issue in Test Automation and how to resolve it? Or want to see the above Selenium Python code working? Please view my Selenium Python tutorial 1. Thank you.

May 03, 2020

Big Data Tutorial 3 | big data in Healthcare | Big data testing

This is the third tutorial in the Big Data Tutorials for Beginners. This Big Data beginner tutorial explains Big Data in HealthCare, Big Data challenges, Big Data Testing, Big Data Testing challenges and Big Data Testing Tools. Please view the Big Data tutorial 3 or read on... First, let us learn about big data in healthcare. The healthcare industry is highly regulated and uses healthcare big data like patient health records, laboratory test results and test reports, prescriptions, claims and payments.

The main problem has been to analyze the healthcare big data quickly. The Hadoop framework is widely used in the healthcare industry to host the big data for quick processing by Map Reduce jobs. The use cases that I mentioned in my Big Data Tutorial 2 are applicable in the healthcare industry e.g. 360-degree view creation of patients and physicians and patient classification for care personalization and efficiency.  Big data examples in healthcare may enable improved prescription accuracy, reduced treatment cost and epidemic prediction. In the future, big data will be used to provide continuous patient monitoring using wearable sensors and Internet of Things devices.

Big data challenges are to perform Data Capture, Data Storage and Data Transfer actions quickly and cost effectively and to blend the data in multiple formats together in Data Analysis. For example, one of the the challenges in Data Capture is data ingestion in Hadoop. Data ingestion means migrating data from source systems to a Hadoop cluster. Since there can be numerous source systems and different ways to ingest data to Hadoop, it can become very complex. Big Data Search, Data Sharing, Data Visualization and Information Privacy are also challenging.

Big Data Testing: Big data testing deals with testing the data quality. High quality big data allows an organization to take accurate business decisions. Big data testing includes big data applications testing, data testing, functional testing and performance testing. Data testing includes:
  •  Data Staging Validation: It is data ingestion testing. It validates the data being loaded into the Hadoop framework. It compares the source data with the data loaded into Hadoop. It also tests that data has been correctly loaded into the Hadoop framework at the correct location. Data staging validation checks the completeness, accuracy, integrity, consistency, validity, standardization and lack of duplicates in the data. Data staging validation of structured data is simpler than that of semi-structured data and unstructured data.
  • Map Reduce Validation: It is the data processing testing to test the business logic and the outputs of the big data applications working on Hadoop. Map reduce validation checks that the Map Reduce process implements the data segregation and data aggregation rules and generates the key value pairs correctly.
  • Output Validation: This is the output testing to test that the Hadoop data matches with the data moved into target systems like data warehouses. Output validation checks the data quality of output data files generated by Hadoop. Then, it tests the ETL process. Finally, it compares the Hadoop data to check complete and accurate data load in the target system.
Functional testing of the big data applications consists of testing the functionality of the big data applications provided in their user interface. Performance testing of the big data applications consists of measuring the data ingestion speed and data processing speed (of Map Reduce jobs) with metrics like throughput (of data ingestion), core utilization and memory utilization. Performance testing includes failover testing (to find if Big Data processing continues in the presence of failed nodes) and sub-component performance testing (to test each component of the Hadoop framework in isolation).

Big Data Testing challenges include availability of enough source test data, QA environment complexity and needing skills to build it, unstructured data testing complexity and needing multiple tools and test automation of big data testing requiring high skills (because unforeseen issues that may occur in unstructured data).

Big Data Testing tools: the Big Data Tester can use the tools in the Hadoop ecosystem for big data testing. Due to the complexity of the big data QA environment and big data volume, velocity and variety, no single tool can do end to end big data testing currently. Some big data testing tools are
  • Tricentis Tosca BI and Data Warehouse Testing tests data integrity with built-in automated tests like pre-screening tests, ETL tests like completeness, uniqueness and referential integrity tests and other tests. 
  • QuerySurge compares the source and target data systems and highlights data differences automatically. It also has features like test management integration, test monitoring and reporting and it's own API.
  • TestingWhiz works with Hadoop, MongoDB and Teradata. It allows data validations tests and performance tests in big data testing.
Want to learn more including Big Data challenges in Healthcare and Big Data Questions and Answers? Please view my Big Data tutorial 3. Thank you.