Detailed explanation of Webdriver API (3)

 

①Manipulate the selection list of multiple selections

HTML code under test

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>操作多选列表</title>
</head>
<body>
    <select name="fruit" size="6" multiple=true>
        <option id="peach" value="taozi">桃子</option>
        <option id="watermelon" value="xigua">西瓜</option>
        <option id="orange" value="juzi">橘子</option>
        <option id="kiwifruit" value="nihoutao">猕猴桃</option>
        <option id="maybush" value="shanzha">山楂</option>
        <option id="litchi" value="lizhi">荔枝</option>
    </select>
</body>
</html>

Call API instance code

 Example code

 output

illustrate

The effect you see when running this code is to first select 3 options and print the text value of the selected option, then select 3 options again and cancel the 3 previously selected options. For a multi-select operation list, the above Several methods are very practical. Of course, you may encounter various situations in practice, and you need to accumulate more experience and use different methods for different problems.

② Operate the drop-down list that can be input (simulate keystrokes while inputting)

HTML code under test

<!DOCTYPE html> 
<html lang="en"> 
<head> 
    <meta charset="UTF-8"> 
    <title>The operation can be input into the drop-down list, and the keys are simulated while inputting</title> 
</head> 
<body > 
    <div style="position:relative;">
        <input list="pasta" id="select">
        <datalist id="pasta">
            <option>Bavette</option>
            <option>Rigatoni</option>
            <option>Fiorentine</option>
            <option>Gnocchi</option>
            <option>Tagliatelle</option>
            <option>Penne lisce</option>
            <option>Pici</option>
            <option>Pappardelle</option>
            <option>Spaghetti</option><option>Spaghetti</option><option>Spaghetti</option>
            <option>Cannelloni</option><option>Cannelloni</option><option>Cannelloni</option>
            <option>Cancl</option>
        </datalist>
    </div>
</body>
</html>

Call API instance code

 Operation input drop-down list

illustrate

When you run this code, you can see that when you enter c in the input box, the drop-down option will filter out the data, and the first filtered item will be selected, but the effect will not be seen in some browsers (I saw it when I finished writing and running There is no effect). The keys module provides many other simulated keys. You can view the functions of Keys through dir()

③Operation radio button

HTML code under test

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>操作单选框</title>
</head>
<body>
    <form>
        <input type="radio" name="fruit" value="berry" />草莓</input>
        <br />
        <input type="radio" name="fruit" value="watermelon" />西瓜</input>
         <br />
        <input type="radio" name="fruit" value="orange" />橙子</input>
    </form>
</body>
</html>

Call API instance code

 Example code

④Operation check box

HTML code under test

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>操作复选框</title>
</head>
<body>
    <form name="form1">
        <input type="checkbox" name="fruit" value="berry" />草莓</input>
        <input type="checkbox" name="fruit" value="watermelon" />西瓜</input>
        <input type="checkbox" name="fruit" value="orange" />橙子</input>
    </form>
</body>
</html>

Call API instance code

 Example code

⑤Assert keywords in the page source code

Tested address

http://www.baidu.com

 Example code

illustrate

Sometimes there are keywords to be asserted on the page, but the assertion still fails. This may be because the assertion statement is started before the page is fully loaded, causing the content to be asserted not to be found in the page source code.

⑥ Take a screenshot of the current browser window

Tested address

http://www.baidu.com

 Example code

screenshot

illustrate

Calling the screenshot function get_screenshot_as_file() will return True after a successful screenshot. If an IOError exception occurs, False will be returned. The parameters passed in the function can be absolute paths or relative paths; when the expected results are not achieved during the automated testing process, you can save a screenshot of the page to locate the problem more quickly.

⑦Drag and drop page elements

Tested address

http://jqueryui.com/resources/demos/draggable/scroll.html

Call API instance code

 Example code

Note: The ActionChains module has been mentioned before. All actions related to mouse operations need to be simulated using this module.

⑧Simulate single key operation on keyboard

Tested address

http://www.sogou.com

Call API instance code

 Example code

illustrate

Some computers may not see the effect when running this code, because the F12 key of some computers needs to be combined with Fn to take effect.

Summarize

This concludes today’s collection. To be honest, I don’t know how helpful it will be to the people who read my article, but for me personally, it is another experience of sorting out knowledge. I slowly remembered everything I had forgotten before. Although each example looked simple, it actually cost me a lot of energy and time, because I wanted people who read my blog to only read the code once and run it once. You can know what function is implemented by using the example, and you can apply this function to complex test scenarios. In fact, this is also my own self-summary!


If you want to learn automated testing, then the following set of videos should help you a lot 

How to force yourself to finish learning automated testing in one month, and get a job immediately after learning. Even a novice can get it at his fingertips. You can take it away without any thanks, and you are allowed to have sex for free...

Finally, I will share with you some documents and learning materials that I have accumulated and organized. If you need them, you can just get them directly. The above content should be the

most comprehensive and complete preparation warehouse for software testing friends. For the better To organize each module carefully, I also referred to many high-quality blog posts and projects on the Internet, trying not to miss any knowledge point. Many friends relied on these contents to review and got offers from major manufacturers such as BATJ. This warehouse has also helped I have learned a lot about software testing, and I hope it can help you too.

Guess you like

Origin blog.csdn.net/weixin_56331124/article/details/133279973