启动模拟器
cd ANDROID_HOME/tools/
emulator -list-avds
emuator @Nexus_5X_API_26
演练 app:https://github.com/appium/appium/raw/master/sample-code/apps/ApiDemos-debug.apk
pip install -U pytest
pip install allure-pytest
pytest --collect-only xueqiu_unit.py
pytest --junitxml=junit.xml --alluredir=allure_report xueqiu_unit.py
import unittest
from appium import webdriver
from appium.webdriver.common.mobileby import MobileBy
from time import sleep
import allure
from appium.webdriver.common.touch_action import TouchAction
class TestStringMethods(unittest.TestCase):
@classmethod
def setUpClassDemo(cls):
caps = {}
caps["platformName"] = "android"
caps["deviceName"] = "demo"
caps["appPackage"] = "com.xueqiu.android"
caps["appActivity"] = ".view.WelcomeActivityAlias"
caps["udid"] = "VED7N18403003958"
caps["app"] = "/Users/seveniruby/Downloads/com.xueqiu.android_11.10.2_190.apk"
cls.driver = webdriver.Remote("http://localhost:4723/wd/hub", caps)
cls.driver.implicitly_wait(10)
cls.allow()
@classmethod
def allow(cls):
el1 = cls.driver.find_element_by_id("com.xueqiu.android:id/open")
el1.click()
el2 = cls.driver.find_element_by_id("com.android.packageinstaller:id/permission_allow_button")
el2.click()
el3 = cls.driver.find_element_by_id("com.android.packageinstaller:id/permission_allow_button")
el3.click()
el4 = cls.driver.find_element_by_id("com.xueqiu.android:id/agree")
el4.click()
el5 = cls.driver.find_element_by_id("com.android.packageinstaller:id/permission_allow_button")
el5.click()
def setUpXueqiu(self):
caps = {}
caps["platformName"] = "android"
caps["deviceName"] = "demo"
caps["appPackage"] = "com.xueqiu.android"
caps["appActivity"] = ".view.WelcomeActivityAlias"
#caps["udid"] = "VED7N18403003958"
caps["unicodeKeyboard"] = "true"
caps["autoGrantPermissions"] = "true"
caps["nativeWebScreenshot"] = "true"
caps["chromedriverExecutable"] = '/Users/seveniruby/projects/chromedriver/2.20/chromedriver'
self.driver = webdriver.Remote("http://localhost:4723/wd/hub", caps)
self.driver.implicitly_wait(10)
def setUpBrowser(self):
caps={}
caps["platformName"] = "android"
caps["deviceName"] = "demo"
caps["browserName"]="browser"
caps["chromedriverExecutable"] = '/Users/seveniruby/projects/chromedriver/2.20/chromedriver'
self.driver = webdriver.Remote("http://localhost:4723/wd/hub", caps)
#se# lf.driver.implicitly_wait(10)
def setUp(self):
self.setUpXueqiu()
def test_xueqiu_start_search_alibaba(self):
self.driver.find
el6 = self.driver.find_element_by_id("com.xueqiu.android:id/tv_search")
el6.click()
el7 = self.driver.find_element_by_id("com.xueqiu.android:id/search_input_text")
el7.send_keys("alibaba")
def test_xueqiu_start_search_pdd(self):
self.driver.find_element_by_id("tv_search").click()
self.driver.find_element_by_id("search_input_text").send_keys("PDD")
element=self.driver.find_element_by_id("stockName")
assert 1 == len(self.driver.find_elements_by_xpath(
"//*[@resource-id='com.xueqiu.android:id/ll_stock_item_container']/*[1]//*[@text='拼多多' and contains(@resource-id, 'stockName')]"
))
assert "拼多多" == element.text
def test_swipe(self):
self.driver.find_element_by_xpath("//*[@text='交易']")
#self.driver.swipe(800, 900, 200, 900)
rect=self.driver.get_window_rect()
for i in range(5):
sleep(1)
self.swipe(0.8, 0.5, 0.1, 0.5)
def swipe(self, x1, y1, x2, y2):
rect = self.driver.get_window_rect()
TouchAction(self.driver).long_press(
x=rect['width']*x1,
y=rect['height']*y1)\
.move_to(x=rect['width']*x2, y=rect['height']*y2).release().perform()
def test_page_source(self):
print(self.driver.page_source)
def test_msg(self):
result = self.driver.execute_script('mobile: shell', {
'command': 'ps',
'args': ['-A'],
'includeStderr': True,
'timeout': 5000
})
print(result)
#self.driver.execute_script("mobile: send_sms", { 'phoneNumber':"15600534760", "message": "demo"})
def test_webview_native(self):
xpath="//*[@text='交易' and contains(@resource-id, 'tab_name')]"
self.driver.find_element_by_xpath(xpath)
self.driver.find_element_by_xpath(xpath).click()
self.driver.find_element_by_accessibility_id("A股开户").click()
@allure.step
def step_1(self):
xpath="//*[@text='交易' and contains(@resource-id, 'tab_name')]"
self.driver.find_element_by_xpath(xpath)
print(self.driver.contexts)
print(self.driver.current_context)
self.driver.get_screenshot_as_file("1.png")
allure.attach.file("./1.png", attachment_type=allure.attachment_type.PNG)
self.driver.find_element_by_xpath(xpath).click()
@allure.step
def step_2(self):
for i in range(3):
print(i)
print(self.driver.contexts)
print(self.driver.current_context)
sleep(1)
self.driver.get_screenshot_as_file("2.png")
allure.attach.file("./2.png", attachment_type=allure.attachment_type.PNG)
print(self.driver.page_source)
@allure.description("webview testing demo")
@allure.link("http://testerhome.com/topics/6040", "click me")
def test_webview_web(self):
self.step_1()
self.step_2()
self.driver.switch_to.context(self.driver.contexts[1])
self.driver.get_screenshot_as_file("3.png")
allure.attach.file("./3.png", attachment_type=allure.attachment_type.PNG)
print(self.driver.page_source)
#self.driver.find_element_by_accessibility_id("A股开户").click()
self.driver.find_element_by_css_selector(".trade_home_xueying_SJY").click()
print(self.driver.contexts)
print(self.driver.current_context)
def xtest_chrome(self):
self.driver.get("https://testerhome.com")
self.driver.find_element_by_css_selector(
"#main > div > div.col-md-9.home-main > div:nth-child(1) > div.panel-body.topics.row > div:nth-child(2) > div.topic.media.topic-17012 > div.infos.media-body > div.title.media-heading > a").click()
if __name__ == '__main__':
unittest.main()