Young test yo, I heard you want to write automation?
I heard that don’t want to do automated tests, not good development. As a noble Virgoan software test engineer, district automation comes in handy. (I won’t tell you, my boss asked me to write it.)
Here is the background: a project was migrating the technology stack from Ruby to PHP, but some functions were unavailable after the launch due to the absence of testing. So the boss took over the testing and asked to try to write some automation where it could be written.
As the only test of our department, it was incumbent on us to pull up another test and sort out the project from scratch.
One, two, three, start doing it
This time the thing to do is: check the interface display article display is normal.
I have already gone through it manually, but as a young man with ideals, pursuits and ideas, how could HE be satisfied with such mechanical repetition? Therefore, in order to (lazy) conduct a more efficient and high-quality agile test, I turned on PyCharm and created a new Project, starting this road of no return.
Q: How many steps are there to automate the interface?
Step 1 Analyze the interface
Our web page is pure HTML project, there is no framework like Vue, or relatively friendly for testing, many interface elements can be directly located operation.
Our goal is to check if there is a problem with the content displayed on the interface, and reduce complexity to simplicity, that is, the text displayed on the interface does not contain
'<div>','</div>','<a>','</a>','< ','> '
Copy the code
And so on basically no problem.
Step 2: Happily type the script
So with a wave of his hand, he wrote a function:
def check_text(text):
return('<'in text)and('< ' in text)and('> ' in text)
Copy the code
All that’s left is to get the page elements.
We need to check two interfaces this time:
Url = [' link A ', 'link B']Copy the code
Try to get the parts that need to be verified, and then be lazy to simplify the scope. As long as the list of articles, titles and abstracts on the first page of the interface are displayed, it is basically OK.
Get the title and summary of all articles in the current interface:
Item_title = driver. Find_elements_by_class_name item_content_box = (' item_title ') Item_content_box driver. Find_elements_by_class_name (' ')Copy the code
All that’s left is to check everything out:
def check_articles(num, titles, boxes):
for i inrange(0, len(titles)):
title = titles[i].text
box = boxes[i].text
if(check_text(title) or check_text(box)):
print('Link:'+ url[num])
print(title + box+ 'Display error')
driver.quit()
elif i < (len(titles)-1):
continue
else:
print('Link:'+ url[num])
print('All article titles and abstracts display normally')
driver.quit()
Copy the code
Finally, it’s time to start our great WebDriver, but it’s slow. Thankfully, Chrome has added a new headless mode, which eliminates the need for a startup screen and is much faster. Since using headless, my waist is not sore, my legs are not painful, and I can walk vigorously. I can write five test scripts in one breath without any effort.
chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument('--headless')
driver = webdriver.Chrome(chrome_options=chrome_options)
Copy the code
Step 3: Combination
I was going to use uniitest to combine them, but then I thought, just two interfaces.
The last
This time, I only checked the basic display of the interface, and did not check other items, nor did I test the interface and business logic, which will be expanded in detail later.
I am who I am. Different test.
The original address