Software Testing/Test Development丨Automated Testing Positioning Strategy Actual Combat-Tester Forum Search

This article is a sharing of learning notes for students of Hogwarts Test Development Society

Original link: https://ceshiren.com/t/topic/24857

1. Automated testing positioning strategy

  • Not sure which targeting method to use?
  • The element cannot be positioned and cannot be resolved?

Location methods

image.png

General web targeting methods

Positioning Strategy Description
class name Locate elements by class attribute
css selector Locate elements by matching css selector
id Match elements by id attribute
name Locate elements by name attribute
link text Locate the element through the text text in the middle of the text tag
partial link text Locate the element through part of the content of the text text in the middle of the text tag
tag name Locate elements by tag name
xpath Match elements by xpath expression

General principles for selecting a locator

  1. Attributes agreed with R&D take precedence (class attribute: [name='locate'])

  2. Identity attribute id, name (web positioning)

  3. Complex scenes use combined positioning:

  • xpath, css
  • Attributes change dynamically (id, text)
  • Duplicate element attributes (id, text, class)
  • Parent-Child Positioning (Child Positioning Parent)
  1. js positioning

Related sections

  • advanced positioning-xpath
  • advanced positioning-css
  • Execute JavaScript script
  • Interview Question – Unable to locate element

Web bullet box positioning

  • Scenes
    • web page alert box
  • solve:
    • web needs to be handled with driver.switchTo().alert()

Location of drop-down box/date control

  • Scenes:
    • The drop-down box of the tag combination cannot be located
    • The date control of the tag combination cannot be positioned
  • solve:
    • In the face of these elements, we can introduce JS injection technology to solve the problem.

File upload location

  • Scenes:
    • input tag file upload
  • solve:
    • The input tag directly uses the send_keys() method

Please learn the specific chapters step by step.

2. Automated testing of tester forum search function

Table of Contents

  • product analysis
  • Test case analysis
  • write script
  • script optimization

Product Analysis

  • Product: Tester Forum
  • Function: search

https://ceshiren.com

Test case analysis

Writing scripts

"""
__author__ = 'Hogwarts Test Development Society'
__desc__ = 'For more discussion on test development technology, please visit: https://ceshiren.com/t/topic/15860'
"""
# Combined with pytest test framework
# Use case title = file name + class name + method name
import time

from selenium import webdriver
from selenium.webdriver.common.by import By


class TestCeshiren:
    def test_search(self):
        """
        Prerequisites: Enter the search page of the tester forum
        Test steps: 1. Enter search keywords
                  2. Click the Search button
        expected result/actual result
        :return:
        """
        # open browser
        driver = webdriver. Chrome()
        driver. implicitly_wait(3)
        # Open the address under test
        driver.get("https://ceshire.com/search?expanded=true")
        # Locate the search input box and enter the search content
        driver.find_element(By.CSS_SELECTOR, "[placeholder='search']").send_keys("appium")
        # Locate the search button and click it
        driver.find_element(By.CSS_SELECTOR, ".search-cta").click()
        #assertion
        web_element=driver.find_element(By.CSS_SELECTOR, ".topic-title"),
        assert "appium" in web_element.text.lower()

Script optimization – pre and post

  • Prefix: setup
    • Initialize the browser driver
  • Post: teardown
    • close browser
from selenium import webdriver
from selenium.webdriver.common.by import By


class TestCeshireLinear:

    # Pre-processing: initialization
    def setup(self):
        self.browser = webdriver.Chrome()
        self.browser.implicitly_wait(10)
        self. browser. maximize_window()

    # Post-processing: ending
    def teardown(self):
        self. browser. quit()

    # test case
    def test_search(self):

        # Access tester community
        self.browser.get("https://ceshire.com/")

        # Click the search button
        search_button = self. browser. find_element(By. ID, "search-button")
        search_button. click()

        # Enter search keywords
        search_input = self.browser.find_element(By.ID, "search-term")
        search_input. clear()
        search_input. send_keys("hogwarts")

        # Click on the search result item
        result_items = self.browser.find_elements(By.CSS_SELECTOR, "div.user-titles")
        top_item = result_items[0]
        top_item_name = top_item.find_element(By.CSS_SELECTOR, "span.name").text
        top_item. click()

Script optimization – add assertions

  • assert statement
import logging
import time

import allure
from selenium import webdriver
from selenium.webdriver.common.by import By


class TestCeshireLinear:

    # Pre-processing: initialization
    def setup(self):
        self.driver = webdriver.Chrome()
        self.driver.implicitly_wait(10)
        self.driver.maximize_window()

    # Post-processing: ending
    def teardown(self):
        self. driver. quit()

    # test case
    def test_search(self):

        # Access tester community
        self.driver.get("https://ceshire.com/")

        # Click the search button
        logging.info("Click the search button")
        search_button = self. browser. find_element(By. ID, "search-button")
        search_button. click()

        # Enter search keywords
        logging.info("Enter search keywords")
        search_input = self.browser.find_element(By.ID, "search-term")
        search_input. clear()
        search_input.send_keys("Hogwarts Testing Institute official")

        logging.info("Click the search result item")
        xpath_expr = "//div[@class='user-titles']/span[contains(text(), 'Hogwarts Testing Academy Official')]"
        target_item = self.browser.find_element(By.XPATH, xpath_expr)
        target_item. click()

        # test assertion
        logging.info("Test Assertion")
        full_name = self.browser.find_element(By.CSS_SELECTOR, "h2.full-name").text
        assert "Hogwarts Testing Academy Official" == full_name
# combined with pytest test framework
# Use case title = file name + class name + method name
import time

from selenium import webdriver
from selenium.webdriver.common.by import By

#============== Optimization 1
#Problem: 1. There is no pre- and post-processing action 2. The driver does not do quit() after it starts
# If there is no quit() action, it will cause a large number of chromedriver processes to exist all the time. Mac uses ps -ef | grep chromedriver, and window sees the task management period

class TestCeshiren:

    def setup(self):
        """
        Prerequisites: Enter the search page of the tester forum
        :return:
        """
        self.driver = webdriver.Chrome()
        self.driver.implicitly_wait(3)
        # Open the address under test
        self.driver.get("https://ceshire.com/search?expanded=true")

    def teardown(self):
        # Optimization problem: 2. The driver did not do quit() after it started
        # After each use case ends, the process of chromedriver will be closed, and the browser will also be closed
        self. driver. quit()

    # def test_search2(self):
        # self. driver.

    def test_search(self):
        """
        Test steps: 1. Enter search keywords
                  2. Click the Search button
        expected result/actual result
        :return:
        """
        # open browser

        # Locate the search input box and enter the search content
        self.driver.find_element(By.CSS_SELECTOR, "[placeholder='search']").send_keys("appium")
        # Locate the search button and click it
        self.driver.find_element(By.CSS_SELECTOR, ".search-cta").click()
        # Assertion = the result of comparing the expected result with the actual result
        # Get the actual results, that is, get the title content of the search result list
        # The first way, get the first search result,
        # time.sleep(3) # Add a mandatory wait of 3 seconds to wait for the page rendering to complete. If no error is reported, it proves that there is no error in positioning. Otherwise, there may be errors in positioning or other reasons
        web_element=self.driver.find_element(By.CSS_SELECTOR, ".topic-title")
        # Get the actual result of the text class Assert whether the appium keyword is in the actual result text obtained
        # Two solutions: 1. Unification, such as asserting Appium in
        # 2. It is to agree with the obtained content and the expected result. Use .lower to make the uppercase letters lowercase
        assert "appium" in web_element.text.lower()

syntaxbug.com © 2021 All Rights Reserved.
Use Case Heading Preconditions Use Case Steps Expected Result Actual Result
Tester search function Enter the tester forum homepage 1. Click the search button 2 . Enter the search keyword 3. Click the search button 1. The search is successful2. The search result list contains the keyword