There is a delay in updating articles on this site, if you want to see articles about Python + Appium, please follow me over to Testhome. Testerhome.com/topics/2780…

This article mainly describes how to automatically obtain the SMS verification code and how to automatically obtain the picture verification code, and write it into the corresponding input box (the following are using the weibo password as an example)

You can obtain the SMS verification code in the following three ways:

  • Obtain the contents of SMS messages from the notification bar of the mobile phone
  • Obtain SMS verification codes by monitoring mobile phone logs
  • Obtain the SMS verification code through Redis

The first method of retrieving the message content from the notification bar is listed below, and the following two methods will be shared later.

Method of obtaining image verification code:

Through Baidu’S OCR character recognition, identify and obtain the letter, text or digital verification code in the picture.

1. Obtain the SMS verification code

Method: After sending the VERIFICATION code, open the notification bar of the mobile phone, locate the content of the SMS message, and fill the content into the verification code input box. The core code is as follows:

# Open the notification bar
self.driver.open_notifications()
# Get location message content (encapsulated element location, detailed operation can be seen in the previous post encapsulated blog)
message = self.find_element(self._message_content)
Convert SMS content to text
message_content = message.text
# Match captcha in SMS content by re (use r prefix to escape automatically, no need to manually convert string, 6 means 6 digit captcha)
ver_code = re.findall(r'[\d]{6}', message_content)
# Close notifications (click the phone back button to close notifications)
self.driver.press_keycode(4)
# Auto-fill the verification code (automatically fill the verification obtained into the verification code input box)
self.input_verification_code(ver_code)
Copy the code

Note: Since I inherit the BasePage class from my class, and the BasePage class declares that the driver belongs to the WebDriver library (which belongs to the Selenium framework), The open_notifications() and press_keycode() methods in the above code belong to the WebDriver library (which belongs to the Appium framework), so you need to import the WebDriver class into that class. Declare driver as WebDriver in the class, otherwise the following error will occur:

Import and declare as follows:

# import webdriver
from appium import webdriver
from page.base_page import BasePage

class PhoneLoginPage(BasePage) :
	# the statement
	driver: webdriver = None
Copy the code

2. Apply for Baidu OCR recognition interface and download the SDK file of the corresponding language

Before invoking Baidu OCR image recognition, it is necessary to apply for Baidu General character recognition interface. The application method is as follows:

1. Log in to Baidu AI platform, apply for Baidu General Character recognition interface, and activate the permission to use the AI platform for free

Baidu AI platform website: ai.baidu.com

Enter open capability → Text recognition → General text recognition, as shown below:

Click Use Now:

Creating an application:

After the application is successfully created, you can enter the management application and view the AppID, API Key and Secret Key of the created application, as shown in the following figure:

2. View the SDKS for different platforms, languages, and functions

Text recognition link: ai.baidu.com/sdk#ocr

Download the SDK that requires the language:

I use Python, so I downloaded the corresponding Python SDK (Python version: 2.7.+, 3.+). After downloading, there are two ways to install it:

① If PIP is installed, run the following command to open the command prompt

pip install baidu-aip
Copy the code

2 SetupTools has been installed. Open the command prompt and run the following command: sh setupTools

python setup.py install
Copy the code

The following figure indicates that the installation is successful:

3. Text recognition interface Description (Reference document)

Text recognition interface Description Link: ai.baidu.com/ai-doc/OCR/…

Or you can go to the above picture (download the SDK for the required language) → Instructions → Interface instructions (this document will be used later, it is recommended to read it before writing the script)

3. Image verification code recognition

Methods: Automatically create a store images in the project folder, relocation of image authentication code controls, intercepting captcha images, intercepts the image file name, use the automatically generated rules stored in creating pictures folder, after stored on baidu’s OCR character recognition, access to the save the screenshot, character recognition, Then the result of identification is output to the verification code input box

Note:

Q: Why do images have regular names?

Answer: Facilitate follow-up search and verification

Q: Why can’t screenshots be saved on the local disk instead of under the local project directory?

A: If you save images on a local fixed disk, someone else will need to change the path to save your code. If you save images under the local project directory, someone else will not need to manually create folders

1. Screenshot. py – Screenshots of verification codes and stored in a specific folder according to specific rules

import time
from common.image_recognition import ImageRecognition
from page.base_page import BasePage

class Screen(BasePage) :
    # Picture verification code input box
    _img_check_code = "/hierarchy/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.RelativeLay out/android.widget.FrameLayout/android.webkit.WebView/android.webkit.WebView/android.widget.Image"

    # Capture an image of a specific area of the page
    def get_part_screen(self) :
        Save the image to a fixed location
        img_folder = 'E:\\study\\Fork\\Weibo_Demo\\Weibo\\image\\'
        The file name composition of the screenshot
        times = time.strftime('%Y%m%d%H%M', time.localtime(time.time()))
        # Image storage location + file name
        screen_save_path = img_folder + times + '.png'
        # Capture the image of a specific location and save it in the defined storage location and specified file name
        self.find_xpath(self._img_check_code).screenshot(screen_save_path)
        # instantiate ImageRecognition, passing in the image position to be recognized
        ir = ImageRecognition(screen_save_path)
        a = ir.ocr()
				return a
Copy the code

2. Image_recognition. Py — Call Baidu OCR text recognition

from aip import AipOcr

class ImageRecognition:
    Initialize path as a character type
    def __init__(self, path: ' ') :
        self.path = path

    # Your APPID AK SK
    APP_ID = '123456'
    API_KEY = 'fydhafbuebfuebiufwbiufe'
    SECRET_KEY = 'difwebfubweufbweffefefefefefef'
    client = AipOcr(APP_ID, API_KEY, SECRET_KEY)

    # configure parameters to identify the image content, need to output the content
    def get_garameter(self) :
        options = {
            Recognize the language type. Default is CHN_ENG
            "language_type": "ENG".# whether to detect image orientation. Default: false
            "detect_language": "true"
            Whether to return the confidence of each line in the recognition result
            # "probability": "true"
        }
        return options

    # Open file
    def get_file_content(self, file_path) :
        with open(file_path, 'rb') as fp:
            return fp.read()

    # The path of the image to be identified
    def get_image(self) :
        return self.get_file_content(self.path)

    Call Baidu OCR interface for accurate identification
    def ocr(self) :
        image_content = self.client.basicAccurate(self.get_image()), self.get_garameter()
        return image_content
Copy the code

Note: To invoke Baidu OCR identification, baidu-AIP needs to be installed under the operating environment (project). The installation steps are as follows:

Open Setting→Python Interpreter and click the + sign

Search: Bidu-aip, click the first one in the search results list, click Install Package

The following figure is displayed on the Setting→Python Interpreter page, indicating that the installation is successful

3. Retrieve_password_page. py – The obtained image verification code is parsed

from time import sleep
from selenium.webdriver.common.by import By
from common.screenshot import Screen
from page.base_page import BasePage

class RetrievePasswordPage(BasePage) :
    Initialization, used to instantiate class Screen
    def __init__(self, driver) :
        super().__init__(driver)
        self.screen = Screen(driver)

    # Automatically obtain the content of the image verification code
    def auto_get_check_code(self, telephone) :
        self.input_telephone(telephone)
        # obtain image verification code, obtain a tuple
        check_code_tuple = self.screen.get_part_screen()
        print(check_code_tuple)
        # retrieve the first data in a tuple (the subscript position of the tuple starts at 0), and the retrieved data is a dictionary (the dictionary is a set of key-value pairs).
        check_code_list = check_code_tuple[0]
        print(check_code_list)
        Select 'words_result' as the third data from the dictionary
        word_result = check_code_list['words_result']
        # check whether the value of the obtained list is empty, if not, define a dictionary "word_result" value in the list
        if word_result:
            print(word_result)
        else:
            word_result = [{'words': 'F3HA'}]
        print(word_result)
        Extract a dictionary from the list
        dic_words = word_result[0]
        print(dic_words)
        # print "words" in the dictionary
        word = dic_words['words']
        print(word)
        self.input_image_check_code(word)
        self.find_xpath(self._btn_confirm).click()
        sleep(1)
Copy the code

Note: The content identified through Baidu OCR is a dictionary, as follows:

({'log_id': 6483429546783692489.'words_result_num': 1.'words_result': [{'words': ' F3H'}]}, {'language_type': 'ENG'.'detect_language': 'true'})
Copy the code

After processing, the above content is converted into JSON format, which is shown in the following figure:

We need to get the words, so we need a layer to obtain element, specific access is see a python dictionary, tuples, lists, may refer to novice tutorial: www.runoob.com/python3/pyt…

Note: through experiments, this method has a low recognition probability for the more bizarre picture captcha, and a high success rate for the recognition of the normal picture captcha

4. Testcase.py — Test cases

Retrieve password test case script
import pytest
from common.init import AppStart

class TestRetrievePassword:
    def setup(self) :
        self.retrievepassword = AppStart.start().enter_retrieve_password()

    # Automatically obtain image verification code
    def test_auto_get_check_code(self) :
        telephone = "18056120351"
        self.retrievepassword.auto_get_check_code(telephone)
        assert self.retrievepassword.get_phone_and_check_code_tips() == "Please enter the correct verification code."

    def teardown(self) :
        AppStart.quit()
Copy the code

If the above content is incorrect, welcome to mention it, thank you!