UI automation testing: how to locate the pop-up window (Toast) that will disappear?

foreword

Seeing the title, some friends may be confused. What is Toast? In fact, everyone has seen Toast. It is a bullet box that usually stays on our page for about 2 to 3 seconds and then disappears automatically. Since we want to automate, Maybe Toast also needs everyone to test, so today I will introduce how to locate Toast on the page.

Toast

Toast is a simple lightweight notification method, which usually appears on the screen in a short-lived form and disappears automatically after a period of time. Toasts can be used in any part of the application, but cannot be interacted with by the user.

how to locate

Since it is a fast 2~3 seconds, how do we locate whether it exists? First of all, the editor first found a piece of Toast code on the Internet, and then made a simple modification, and opened it in html format:

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" />
    <title>Toast</title>

</head>

<center> 
<body>
    <button id="anjing" onclick="clickme();">点击有惊喜!</but-ton>  
</body>
</center>
<script>  

function showToast(msg,duration){  
    duration=isNaN(duration)?3000:duration;  
    var m = document.createElement('div');  
    m.innerHTML = msg;  
    m.style.cssText="width:60%; min-width:180px; background:#000; opacity:0.6; height:auto;min-height: 30px; color:#fff; line-height:30px; text-align:center; border-radius:4px; position:fixed; top:60%; left:20%; z-index:999999;";  
    document.body.appendChild(m);  
    setTimeout(function() {  
        var d = 0.5;  
        m.style.webkitTransition = '-webkit-transform ' + d + 's ease-in, opacity ' + d + 's ease-in';  
        m.style.opacity = '0';  
        setTimeout(function() { document.body.removeChild(m) }, d * 1000);  
    }, duration);  
}  

function clickme(){
    showToast("祝您身体健康!",3000);
}
</script>   
</html>

(Swipe left and right to view the complete code)

Open the browser, and the next step is how to get the element value of Toast. We can locate the element value after we find it. Open F12 to enter the source page, click the button first, and then let the Toast pop up, click the pause button immediately at this time, then the page will be still, and the Toast will not disappear, and then through the normal positioning of the Toast The element position will do.

Determine whether the Toast is correct

The editor above has introduced how to locate the Toast, then after learning the positioning method, we can start to operate directly. Usually, this type of automated test case only needs to judge whether there is a Toast and whether the content of the Toast in different situations is consistent. , all editors will only judge whether the text of Toast is correct when writing code today:

# coding:utf-8
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium import webdriver
from selenium.webdriver.common.by import By
def test_01():
    driver = webdriver.Chrome()
    driver.get(r'D:\test_daily\123.html')
    driver.find_element_by_id('anjing').click()
    # 元素位置
    locator = (By.XPATH, '/html/body/div')
    # 显示等待获取元素
    WebDriverWait(driver, 20,0.5).until(EC.presence_of_element_located(locator))
    # 获取toast
    t = driver.find_element_by_xpath('/html/body/div').text
    print(t)
    assert t =='祝您身体健康!'

(Swipe left and right to view the complete code)

After simply writing the code, we run it directly through Pytest. From the results, we can see that our test case has passed.

Summarize

This question mainly introduces what is Toast and how to locate Toast and how to write test cases in automation.

Whether it is Web automation or App automation, the scene of Toast is very likely to exist in the UI scene, especially in App automation. Since there is no App environment installed on the editor's computer, only the Web one is listed. In fact, both the App and the Web positioning methods are the same. In the learning process, the main learning method is learned when the method is learned.

Well, thank you for reading, I hope this article is helpful to you.

The following are supporting learning materials. For friends who do [software testing], it should be the most comprehensive and complete preparation warehouse. This warehouse also accompanied me through the most difficult journey. I hope it can help you too!

Software testing interview applet

The software test question bank maxed out by millions of people! ! ! Who is who knows! ! ! The most comprehensive quiz mini program on the whole network, you can use your mobile phone to do the quizzes, on the subway or on the bus, roll it up!

The following interview question sections are covered:

1. Basic theory of software testing, 2. web, app, interface function testing, 3. network, 4. database, 5. linux

6. web, app, interface automation, 7. performance testing, 8. programming basics, 9. hr interview questions, 10. open test questions, 11. security testing, 12. computer basics

Information acquisition method:

Guess you like

Origin blog.csdn.net/jiangjunsss/article/details/132610609