Implementation of secondary packaging based on Python + selenium

Time:2021-8-5

This is the secondary encapsulation of some common operations written by selenium.webdriver, which is equivalent to rewriting. Instead of using the built-in framework, it is completed with the framework written by myself. In this way, make the code more concise and write the code with your own ideas.

在这里插入图片描述

First, create a subdirectory named common in the root directory as the public data. Take the name as you like. Create basepage.py and dir in the common directory_ Config file( What are the two py files used for

First we open dir_ Config file

在这里插入图片描述

Look at the name, you can clearly see what this is about.
logs_dir = os.path.join(base_dir,“OutPuts/logs”)
(as for why I want to write this, I won’t introduce it. This article is aimed at having a foundation.)
In this line, the directory where the log is stored = the directory pointed by my arrow. Each is the same.
First, configure the directory path to be stored and configure it according to your needs.

Next, we come to the BasePage file

在这里插入图片描述

First import the required modules and packages. As for why we use these modules, we will know later. Hee hee

在这里插入图片描述

Create a class named BasePage
Write an initialization function plus the driver parameter. self.driver = driver

在这里插入图片描述

在这里插入图片描述

This is a rewrite of the wait element operation, which is webdriverwait(). Until (expected)_ conditions.visibility_ of_ element_ Rewriting of located())
I also added a doc parameter later. I wrote a comment on it, which is the module name_ Page name_ Operation name. In this way, if an error is reported, I can clearly see which module, page and operation is wrong in the log, screenshot and report. There are other parameters with comments, and each line of code with comments. I won’t talk about them one by one.

在这里插入图片描述

This is an override of the lookup element
Why write a lookup element? See below

在这里插入图片描述

This is the rewriting of the click operation. I directly called the find function, which is to find the element and click. Over and over.

Like send_ Keys () and so on

在这里插入图片描述

And a log is added before each operation
For example, logging.info (F “{doc}: element: {locator} input content: {text}”)
If it is obtained correctly, the log will show what content is entered in which module and element, and it is written clearly. If it is wrong, on the contrary, I also typed the wrong log( Doc is which module, which page, which operation)
There are many other operations that follow the same routine. If you think my rewriting is good, you can write other operations according to my routine

在这里插入图片描述

Get element content operations and so on, I won’t take screenshots one by one
For example, where there is an error, there will be an error log and screenshot under the specified path

在这里插入图片描述

Moreover, each error will bring a doc to facilitate their own troubleshooting.

OK, let’s practice and use this framework
First, create a py file loginpage for the login page

在这里插入图片描述

Import BasePage and create a class to inherit BasePage
from PageLocators.LoginLocators.login_locators import LoginLocators as loc
This module is the location element of my login page

在这里插入图片描述

在这里插入图片描述

Since we wrote the initialization function in BasePage, we don’t need to write it here
Then we define a login function with username and password parameters, which are formal parameters representing the input username and password
Define another doc
Directly use the secondary encapsulation we just rewritten
First, we need to find the element of user name input box and enter our user name in this element
You can use input directly_ text()
self.input_text(loc.name_text,username,doc)
loc.name_ Text is the positioning expression and positioning element of the user name input box defined by us. I enclose it in the form of Yuanzu. Look at the picture above
Later, username is the formal parameter of the user name. It’s good to pass the user name data when your test case calls the login page operation. I have brought doc for each, so doc must be passed.
All operations are the same. Understanding one can understand all.
Considering why the form of tuples can also be passed in, I’ll give you a close-up here.

在这里插入图片描述

Look, I added this symbol in front of locator. This symbol means unpacking
self.driver.find_element(By.XPATH, ‘//*[contains(@name,“username”)]’)
Actually, that’s what I mean.
Then I pass on a Yuanzu and directly quote it

OK, then we use pytest unit test framework to write test cases, and then reference loginpage

在这里插入图片描述

In the red box are the arguments of username and password. This is the test data for calling login. That’s a good understanding.
Well, this article is over. Is it very concise, practical and convenient for subsequent modification? In fact, it is optimized with its own ideas for the operation page.
If you think it’s good, you can also modify it according to what I wrote. Of course, it’s better to have your own ideas.
Then I also put some contents of pytest here. Don’t worry about what some little partners don’t understand. Later, I will write a separate article on the use of pytest unit testing framework.
Of course, if you don’t understand, you can leave a message. If you want to join the group and study together, you can chat with me privately
Come on together – hoohoohoohoohoohoohoohoohoohoohoohoohoohoohoohoohoohoohoohoohoohoohoohoohoohoohoohoohoohoohoohoohoohoohoohoohoohoohoohoohoohoohoohoohoohoohoohoohoohoohoohoohoo!!!

The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support developpaer.