Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Case sharing, Appium+Python implements the APP startup page to jump to the home page

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Network Security >

Share

Shulou(Shulou.com)06/01 Report--

Take MSN news as an example to realize the function of jumping to the home page after starting APP, including using list to locate elements, try except else to determine whether to start APP for the first time, logging for logging and other functions.

1. Scene:

1. After starting APP, continuously skip welcom, interest and what's new pages to the home page

two。 Determine whether it is started for the first time. If the welcom page appears for the first time, if it is not the first time, go directly to the interest page.

3. Logging using the logging module

Gif picture file:

II. Practice

2.1.After starting APP, skip welcom, interest and what's new pages to the home page

Welcom page

1. First start Appium to answer the session call

2.APP launches to the welcom page, click the not now button, and jump to the next page

Analysis: here I use the list element for positioning. Through observation, we can see that the names of classname and sign in name are the same. If you use classname directly, it will definitely go to the first classname element of the current page, resulting in positioning failure.

List locates to get a set of class names, which are distinguished by array subscripts

A=driver.find_element_by_id ()

A [1] .click ()

3. Code:

Skipwel=driver.find_elements_by_class_name ('android.widget.Button')

Skipwel [1] .click ()

2.2. determine whether it is started for the first time. If the welcom page is displayed for the first time, if it is not the first time, go directly to the interest page.

When you start app for the first time, the welcome page pops up, but the second time it disappears, and the interest page appears. So how do we deal with this judgment?

Here we use the try except statement to judge

Try except syntax:

How it works:

If an exception occurs in the try statement, execute the statement after except matches the name

If there is no exception in the try statement, execute the following statement of else

If an exception occurs in the try statement and the content after the except statement does not match successfully, the exception will be submitted to the upper try.

How to deal with it:

Locate the interest page, if the location fails, prove that the welcome appears and execute the welcome statement; otherwise, execute the interest page statement

Code:

Def welcome ():

Logging.info ('skip welcome')

Skipwel=driver.find_elements_by_class_name ('android.widget.Button')

Skipwel [1] .click ()

Try:

Driver.find_elements_by_class_name ('android.widget.Button')

Except NoSuchElementException:

Welcome ()

Else:

Skipinterest=driver.find_elements_by_class_name ('android.widget.Button')

Skipinterest [0] .click ()

Interets page

The principle is the same as the welcom page

Skipinterest=driver.find_elements_by_class_name ('android.widget.Button')

Skipinterest [0] .click ()

What's new page

The principle is the same as the welcom page

Skipwhatnew=driver.find_elements_by_class_name ('android.widget.Button')

Skipwhatnew [1] .click ()

2.3.Use the logging module to log

The log is used to help us locate the problem, through the log can more accurately determine the problem, we can set the log level, log output format and so on.

Scene:

When starting APP, output the start app log

The navigation page jumps to the home page process, and outputs whether the page executed is an interest page or a welcome page

2.3.1, single py file log

The log output in a single py file is as follows:

1. Log flow

First, import the log module

The basicconfig method then creates a logger and makes a basic configuration for the record.

Log level: level

Log output path: Filename

Log output format: Format

Finally, output the log where you need to add the log, logging.info ('start app')

Code implementation

Import logging

Logging.basicConfig (level=logging.INFO,filename='456.log', format='% (asctime) s% (filename) s [line:% (lineno) d]% (levelname) s% (message) s')

# determine whether to start App for the first time, skip the navigation page and go to the home page

Def welcome ():

Logging.info ('skip welcome')

Skipwel=driver.find_elements_by_class_name ('android.widget.Button')

Skipwel [1] .click ()

Try:

Driver.find_elements_by_class_name ('android.widget.Button')

Except NoSuchElementException:

Logging.info ('run welcome')

Welcome ()

Else:

Logging.info ('run interest')

Skipinterest=driver.find_elements_by_class_name ('android.widget.Button')

Skipinterest [0] .click ()

3. Running

View log output information in 456.log in code

2.3.2 Log module

What is described above is the process of a single function log. Many functions need to be used in the whole APP test. If each function is written repeatedly, it will result in code redundancy.

So, let's extract the configuration parameters of the log and call it directly when needed.

1. First, the log.conf file is established to store configuration parameters and other information.

Log.conf file code:

[loggers]

Keys=root,infoLogger

[logger_root]

Level=DEBUG

Handlers=consoleHander,fileHandler

[logger_infoLogger]

Handlers=consoleHander,fileHandler

Qualname=infoLogger

Propagat=0

[handlers]

Keys=consoleHander,fileHandler

[handler_consoleHander]

Class=StreamHandler

Level=INFO

Formatter=form02

Args= (sys.stdout,)

[handler_fileHandler]

Class=FileHandler

Level=INFO

Formatter=form01

Args= ('456. Logbook and no.')

[formatters]

Keys=form01,form02

two。 Reference the log.conf file in the py file that needs to reference log

Remove the following code from the above

Logging.basicConfig (level=logging.INFO,filename='456.log'

Format='% (asctime) s% (filename) s [line:% (lineno) d]% (levelname) s% (message) s')

Replace it with:

CON_LOG='log.conf'

Logging.config.fileConfig (CON_LOG)

Logging=logging.getLogger ()

Third, error warning

Summary of error reporting tips and solutions in the process of operation

1.Eclipse runs the startup app code, prompting launching new configuration

Eclipse

Solution:

Project-> Properties-> Run/Debug Settings:

Select "Launching New_configuration5"

2. Delete

2.ImportError: cannot import name 'webdriver'

Solution:

From selenium import webdriver

3.TypeError: 'WebElement' object does not support indexing

Solution: change find_element to find_elements?

4.IndexError: list index out of range

Resolve:

It is possible that the array is out of bounds, where the value is marked from 0.

It is also possible that list is empty without an element.

This error occurs when you list [0]

5.module 'logging' has no attribute' basicConfig'

Resolve:

I named a logging.py file, in the code import is the new this, can not find the basicConfig, the file name is changed to logging_test.py, normal operation.

6.int 'object is not callable

Solution: found that the code is written as logging.DEBUG, should be logging.debug ()

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Network Security

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report