Experience Recall (Pure Manual) => Python Version - Docker Uses Appium Simple Experiments

Keywords: Python Docker Android simulator

Docker-Appium Installation

Github docker-appium address: https://github.com/appium/app...

Create temporary containers (for simple command testing)

docker run --privileged -d -p 4723:4723 -v ~/.android:/root/.android -v /dev/bus/usb:/dev/bus/usb --name container-appium appium/appium

If using simulator, etc. (non-USB connection)

The adb of the simulator converts USB mode to TCP connection mode:

adb -s 127.0.0.1:62001 tcpip 1119

Remote Docker connects to this simulator:

docker exec -it container-appium adb connect 192.168.0.103:1119

Check if the virtual device was successfully connected

docker exec -it container-appium adb devices

If you use a real machine (USB connection), you can use the following configuration directly

Dockerfile is as follows

FROM python
RUN pip install -i http://pypi.douban.com/simple \
    requests  retrying  appium-python-client --trusted-host pypi.douban.com

docker-compose.yaml reads as follows

version: "3.7"
services:
  myspider:
    build: .
    volumes:
      - /root/mycode:/root/mycode
    command: python /root/mycode/1.py
    depends_on:
      - appium
  appium:
    image: appium/appium # Draw mirror to complete automatic configuration
    ports:
      - "4723:4723"
    privileged: true
    hostname: appium
#    command: adb connect 192.168.0.103:1119
#    command:
#      - /bin/sh
#      - -c
#      - |
#        adb connect 192.168.0.103:1119
#        adb devices
#    entrypoint: adb connect 192.168.0.103:1119
    volumes:
      - ~/.android:/root/.android
      - /dev/bus/usb:/dev/bus/usb

The crawler script code 1.py is as follows

from appium import webdriver
from retrying import retry
import requests
import time

config = {}
config['platformName'] ='Android'
config['platformVersion'] = '7.1.1'
config['deviceName'] = 'Nut Pro 2'
config['noReset'] = True
config['appPackage'] = 'org.mozilla.firefox'
config['appActivity'] = 'org.mozilla.gecko.BrowserApp'
################### See appPackage and appActivity #################
## Be careful:
##     These two values are for the configuration of a software. You need to open your software on the phone and execute the command again.
##     In this case, I use the Firefox browser in my cell phone.
##     So I need to open Firefox first.
##     Then execute the following command to find the configuration you want.
##     Otherwise, check the configuration information of the main interface application of your phone running state.
## The order is as follows:
##     docker exec -it container-appium adb shell    # Enter the adb shell
##     dumpsys activity | grep mFocusedActivity       
##         Returns the result / preceded by the value of appPackage
##         Returns the result / followed by the value of appActivity

@retry(
    stop_max_attempt_number = 1000000,
    stop_max_delay = 10*1000,
)
def verify_request():
    response = requests.get("http://appium:4723/wd/hub",timeout=0.5)
    print(response)
verify_request()

with webdriver.Remote(
    command_executor='http://appium:4723/wd/hub',
    desired_capabilities=config
) as driver:
    driver.get('https://tieba.baidu.com/index.html')
    time.sleep(5)

    with open('/root/mycode/test.html', 'w') as f:
        f.write(driver.page_source)
        print('Write successfully')
    time.sleep(3)

Antecedents link

Pycharm and Docker related operations: https://segmentfault.com/a/11...

The use of Selenium in Docker: https://segmentfault.com/a/11...

Posted by joejoejoe on Tue, 01 Oct 2019 03:38:40 -0700