Selenium — “Po + keyword” teaches you to quickly build a simple automated testing framework

Time:2022-1-7

This is Qing’an, V: Qing_ an_ An, this chapter directly teaches you to write a simple and practical automated testing framework. There is not much content. You can see it at a glance.

You can improve it on this basis and make it a real framework. Used in the project.

No more nonsense. We want to create a new project file. My name is frame_ web_ Interface, why? There will be an interface later, but it is not on this channel, but on the channel of the interface.

I created a web under the project file_ Frame file, used to store the driver and some click, input and other methods. Look at the code:

#-- > > > Qing'an<<<---
from time import sleep
from selenium import webdriver
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.wait import WebDriverWait

"""
    Pass in a driver parameter to open the browser
    driver = getattr(webdriver,option_) () equals the following expression:
    driver = webdriver.Firefox()
"""
def webbrowser(option_):
    try:
        driver = getattr(webdriver,option_)()
    except Exception as e:
        Print (E, 'input browser format error, use default browser')
        driver = webdriver.Firefox()
    return driver


class Webbrowser:

    """
    Use the driver driver and pass in the browser parameters required for opening
    Firefox,Chrome。。。
    """
    def __init__(self, option_):
        self.driver = webbrowser(option_)

    """
    open
    driver. Get ('url ')
    """
    def open(self, url):
        self.driver.get(url)

    """
    close
    driver.quit()
    """
    def quit(self):
        self.driver.quit()

    """
    Element positioning
    driver.find_element(By.ID,'su')
    """
    def loc_ele(self, ele, value):
        return self.driver.find_element(ele, value)

    """
    input
    driver.find_element(By.ID,'su').send_keys(text)
    """
    def input(self, ele, value, text):
        self.loc_ele(ele, value).send_keys(text)

    """
    click
    driver.find_element(By.ID,'su').click()
    """
    def click(self, ele, value):
        self.loc_ele(ele, value).click()

    """
    Forced waiting, depending on the guide package
    time.sleep(num) or sleep(num)
    """
    def wait(self, num):
        sleep(num)

    """
    Display wait
    Determine whether an element exists
        presence_of_element_located(find_element_by_id)
    """
    def wait_presence(self, num, ele, value):
        WebDriverWait(self.driver, num, 0.5).until(EC.presence_of_element_located((ele, value)))

I have written very clear notes here, so I won’t explain much. There are some things stored here. If necessary, you can add the required methods by yourself.

If you have any questions, please leave a comment below, or go directly to the blogger.

Next, let’s look at the use case part. How to write it?

I created a casetest file and threw the driver in it. If you don’t throw it in, just define the driver path yourself

#-- > > > Qing'an<<<---
from web_frame.browser import Webbrowser

#Instantiate the class. Pass in the name of the driver to be used. Pay attention to case
wb = Webbrowser('Chrome')
#Open address
wb.open('https://baidu.com')
#Enter the display waiting for judgment element
wb.wait_presence(3, 'id', 'kw')
#Forced wait for 1 second
wb.wait(1)
#Locate the element and enter the content
wb. Input ('id ',' kW ',' nothing else in peace ')
#Locate the element and click the Baidu search button
wb.click('id', 'su')
#Shut down
wb.quit()

Here I take Baidu as the benchmark. You can directly use it for project practice. Combined with data-driven DDT, you can start automation.

Idea:

Here you can write your own framework and output the report. Specifically, I have seen unittest before. You can directly introduce it here. You can also use pytest

You can add the log module yourself, and the log module will be published in the python module later.

Conclusion of this chapter. Look forward to the subsequent excel keyword drive, which can greatly reduce the workload.

Recommended Today

Proper memory alignment in go language

problem type Part1 struct { a bool b int32 c int8 d int64 e byte } Before we start, I want you to calculatePart1What is the total occupancy size? func main() { fmt.Printf(“bool size: %d\n”, unsafe.Sizeof(bool(true))) fmt.Printf(“int32 size: %d\n”, unsafe.Sizeof(int32(0))) fmt.Printf(“int8 size: %d\n”, unsafe.Sizeof(int8(0))) fmt.Printf(“int64 size: %d\n”, unsafe.Sizeof(int64(0))) fmt.Printf(“byte size: %d\n”, unsafe.Sizeof(byte(0))) fmt.Printf(“string size: %d\n”, […]