Appium how the wait function of the Python Automation Test series is performed (ix)

Source: Internet
Author: User

? 9.1 Waiting for the use of functions 9.1.1 Why use the wait function

We do the automation time is not very smooth, not because of the app's problem, our script is no problem, but many times will be error, such as a page has an ID of 1 this element, but I no matter how to locate him can not operate, and then error, this is how a situation? Because when our app opens a page, our appium is running too fast, so it's possible that the page's resources will not be parsed and then you can do it. Certainly not, so no error, who is wrong? So in a lot of times we all need to load the wait time. Are we blindly going to load the wait time on each page?

9.1.2 When to use the wait function

The answer is definitely no, the purpose of automation is to be efficient, but do you have to add wait time for each page so that the efficiency of execution is greatly reduced? It is estimated that your leader should speak to you. When loading wait time we need to increase according to our own judgment, for example, some of the page resources are more slow, then you definitely need to add. Is that all? Actually not, so this has the following we need to talk about the knowledge points, several different types of waiting.

9.2 Forcing wait 9.2.1 what is forced wait

So the name of meaning is that you must give me wait, a little bullying meaning. For example: I go to the landing page, just have a force wait function, then the result is no matter the page resources loaded without you have to wait for me. Do you understand me? As long as the time is not up to you wait for me! Haha, just like that kind of an unreasonable ah.

9.2.2 force wait for use

In Python this is better, he calls the time package under the Wait function, the code is as follows:

#coding = "Utf-8" Import timetime.sleep (10)

The first is the need to import the package of time, the following sentence is done, is not convenient, practical? When you debug a program, write it like this, and never use it in a real project. Because this time wait is the thread's death, it will execute this statement anyway, if you run in the actual project then you will find the efficiency is very slow. So the actual project is not recommended.

Note: The time is calculated by the second

9.2.3 forced to wait for encapsulation combat

Before we learned the encapsulation of the function, if we need to use this waiting function in many places, does it have to be in every place? In fact, the most important purpose of the program is that we can write less even a word we have to encapsulate, which is my understanding. Why not pick a simple way to do the same function? Right. Look at the encapsulation code:

 #conding = "Utf-8" Import timefrom appium import webdriverimport os def case ( platformname,platformversion,devicename,app,apppackage,appactivity): PATH = Lambda P:os.path.abspath (Os.path.join ( Os.path.dirname (__file__), p)) #print getconfig ("baseconf", "platformname") Desired_caps = {} desired_caps[' Platformna Me '] = platformname #设置平台 desired_caps[' platformversion ') = platformversion #系统版本 desired_caps[' devicename '] = deviceNam E #设备id desired_caps[' autoLaunch ') = ' true ' #是否自动启动 desired_caps[' noreset '] = ' true ' desired_caps[' newcommandtimeout '] = desired_caps[' app ' = PATH (APP) #安装包路径, placed in the directory of the py file) desired_caps[' apppackage '] = apppackage #包名 desired_caps[' Appac Tivity '] = appactivity #启动的activity Driver = webdriver. Remote (' Http://localhost:4723/wd/hub ', Desired_caps) WaitFor (5) #等待函数def WaitFor (t): Time.sleep (t) 

See this code people are not a kind of hazy feeling? In fact, this is the last chapter for everyone to think about, I will start the parameters of a simple package, and then the wait function is also encapsulated, and then they are the combination of the present look. Isn't it simple? In fact, I want to tell you that this is not automated in the true automation, but we need to slowly develop this thinking, more practice. In the code above we will launch the app's code for a simple refactoring package, this time for beginners are highly recommended to do, or you do not know whether you can start, and the above lines of code what the meaning must be clear.

9.3 Implicit wait

Implicit waiting may be more obscure to the people just learned, do not know what the meaning, we can understand, intelligent waiting. Why do you say that, we need to understand his usefulness first then why do you call it very clear, first we look at the following code:

Driver.implicitly_wait (10)

The code above is an implicit wait, and his statement is simple. But have you ever thought about a question, why is this waiting to be used driver? What needs to be said here is that the wait function is a wait function provided by Webdriver. So the question comes again, since it is the wait function provided by Webdriver. Why didn't you see the waiting object he specified? Have you ever thought about this question? Because the wait function here is for our entire driver. That is to say you use driver to manipulate an object, or an element, when you cannot find this element or object, he will automatically wait for you to set the time-out, if not found in the timeout period, the program will be error. Does it feel like the wait is too big? Not to practice.

Maybe someone will ask, "Why are you this driver, I did not ah, or I follow you forced to wait that will start the encapsulation, and then implicitly wait for me to encapsulate it, and then I this error, why?" First think about it, you go back to the place where we generated the driver, and then run a sentence like this is not the error, try it. But the package is an error, why? Because we do not return driver here, that is, we need to use the driver is not defined now, then will be an error, then how can I and the previous code as encapsulated? Look at the following:

#conding = "Utf-8" Import timefrom appium import webdriverimport os def case (Platformname,platformversion,devicename,app , apppackage,appactivity): PATH = Lambda P:os.path.abspath (os.path.join (Os.path.dirname (__file__), p)) #print GetConfig ("baseconf", "platformname") Desired_caps = {} desired_caps[' platformname '] = platformname #设置平台 desired_caps[ ' platformversion '] = platformversion #系统版本 desired_caps[' devicename ') = devicename #设备id desired_caps[' autoLaunch '] = ' t Rue ' #是否自动启动 desired_caps[' noreset ') = ' true ' desired_caps[' newcommandtimeout '] = desired_caps[' app ' = PATH (APP) #安装 Package path, placed in the directory of the py file) desired_caps[' apppackage ' = apppackage #包名 desired_caps[' appactivity '] = appactivity #启动的activity dri ver = webdriver. Remote (' Http://localhost:4723/wd/hub ', Desired_caps) WaitFor (5) return driver #等待函数def waitFor (t): Time.sleep (t) #隐式等待def implicit_for_wait (t): Driver = case (platformname,platformversion,devicename,app,apppackage,appactivity) Driver.implicitly_wait (t)

Go back and see if the code finds a different place? Here we will initialize the driver to a closure, that is to give a return value, and then we in the implicit wait to call our driver, then he has the driver, so this time can be like the above code to invoke the Wait method.

9.4 Explicit wait

Before we talk about implicit wait and force wait, let's look at the display wait, the same first look at the code:

Webdriverwait (Driver, timeout, poll_frequency=0.5, Ignored_exceptions=none)

Let's start by figuring out the meaning of several parameters in this method:

1, driver: Is our operation of the driver.

2, timeout: Time-out, that is, we find this element to find how long

3, Poll_frequency: Interval time, how to understand? That is, in the timeout period every number of seconds to query, the default is 0.5 seconds once

4, Ignored_exceptions: Exception, is not found the program throws what exception. In the default case is run out: nosuchelementexception

In general, we only need to fill in the first two on the line. See if there is a problem here, this also has no positioning elements, just driver and time. Is it a global one? The answer must be no. In general, the explicit wait is required to be combined with other methods, see the following complete code:

Driver = self.driverwebdriverwait (driver, 10,5). Until (Lambda driver:driver.find_element_by_id ("ssss"))

  This code is not understood again? Let's then refactor the code in the way we just did:

#conding = "Utf-8" Import timefrom appium import webdriverimport os def case (Platformname,platformversion,devicename,app , apppackage,appactivity): PATH = Lambda P:os.path.abspath (os.path.join (Os.path.dirname (__file__), p)) #print GetConfig ("baseconf", "platformname") Desired_caps = {} desired_caps[' platformname '] = platformname #设置平台 desired_caps[ ' platformversion '] = platformversion #系统版本 desired_caps[' devicename ') = devicename #设备id desired_caps[' autoLaunch '] = ' t Rue ' #是否自动启动 desired_caps[' noreset ') = ' true ' desired_caps[' newcommandtimeout '] = desired_caps[' app ' = PATH (APP) #安装 Package path, placed in the directory of the py file) desired_caps[' apppackage ' = apppackage #包名 desired_caps[' appactivity '] = appactivity #启动的activity dri ver = webdriver. Remote (' Http://localhost:4723/wd/hub ', Desired_caps) WaitFor (5) return driver #等待函数def waitFor (t): Time.sleep (t ) #隐式等待def implicit_for_wait (t): Driver = case (platformname,platformversion,devicename,app,apppackage,appactivity) Driver.implicitly_wait (t) #显示等待def wait (t): Driver = case (platformname,platformversion,devicename,app,apppackage,appactivity) webdriverwait (Driver   , 10,5). Until (Lambda driver:driver.find_element_by_id ("ssss"))

Here the Python foundation of the poor reader will have some difficulty, the front does not explain. First look at the code behind the lambda, he is an anonymous function, before the colon is the argument, the colon is followed by the return value, driver is we need to pass in a parameter, similar to the following code:

def appiumdriver (driver):  return driver.find_element_by_id ("xxxxx");

The two of them mean the same thing. Then look at the. Until method, he gives the explanation: Call the method provided by the driver as a parameter until the return value is not False. Then the whole script of the sense of translation is not in 10 seconds every 5 seconds to find an ID for the SSS element, if not found then the error.

In automation we need to constantly learn new ideas, the program is always followed by the idea of walking, if you think very well, then the script how to achieve is relatively simple.

Appium how the wait function of the Python Automation Test series is performed (ix)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.