{"id":1170466,"date":"2025-01-15T16:22:53","date_gmt":"2025-01-15T08:22:53","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1170466.html"},"modified":"2025-01-15T16:22:55","modified_gmt":"2025-01-15T08:22:55","slug":"python-%e5%a6%82%e4%bd%95%e5%81%9a%e6%89%8b%e6%9c%ba%e8%84%9a%e6%9c%ac","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1170466.html","title":{"rendered":"python \u5982\u4f55\u505a\u624b\u673a\u811a\u672c"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/26071140\/6a1547ae-0e5f-4ac4-bd91-78e8fb71fc30.webp\" alt=\"python \u5982\u4f55\u505a\u624b\u673a\u811a\u672c\" \/><\/p>\n<p><p> <strong>Python\u505a\u624b\u673a\u811a\u672c\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u6bd4\u5982\u4f7f\u7528Appium\u3001ADB\uff08Android Debug Bridge\uff09\u3001MonkeyRunner\u7b49\u5de5\u5177\u3002<\/strong>\u5176\u4e2d\uff0c<strong>Appium<\/strong> \u662f\u6bd4\u8f83\u5e38\u89c1\u548c\u5e7f\u6cdb\u4f7f\u7528\u7684\u65b9\u6cd5\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528Appium\u8fdb\u884c\u624b\u673a\u811a\u672c\u7684\u7f16\u5199\u548c\u6267\u884c\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u5b89\u88c5\u548c\u914d\u7f6e\u73af\u5883<\/h3>\n<\/p>\n<p><h4>1\u3001\u5b89\u88c5Python<\/h4>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u4f60\u9700\u8981\u5b89\u88c5Python\u3002\u53ef\u4ee5\u4ecePython\u7684\u5b98\u65b9\u7f51\u7ad9\u4e0b\u8f7d\u6700\u65b0\u7248\u672c\u7684Python\uff0c\u5e76\u6309\u7167\u63d0\u793a\u8fdb\u884c\u5b89\u88c5\u3002\u786e\u4fdd\u5728\u5b89\u88c5\u65f6\u9009\u62e9\u6dfb\u52a0Python\u5230\u7cfb\u7edf\u73af\u5883\u53d8\u91cf\u9009\u9879\u3002<\/p>\n<\/p>\n<p><h4>2\u3001\u5b89\u88c5Appium<\/h4>\n<\/p>\n<p><p>\u63a5\u4e0b\u6765\uff0c\u5b89\u88c5Appium\u3002\u4f60\u53ef\u4ee5\u4f7f\u7528npm\u6765\u5b89\u88c5Appium\u3002\u9996\u5148\u786e\u4fdd\u4f60\u5df2\u7ecf\u5b89\u88c5\u4e86Node.js\uff0c\u7136\u540e\u5728\u547d\u4ee4\u884c\u4e2d\u8fd0\u884c\u4ee5\u4e0b\u547d\u4ee4\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">npm install -g appium<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3\u3001\u5b89\u88c5Appium-Python-Client<\/h4>\n<\/p>\n<p><p>\u5728\u5b89\u88c5\u4e86Appium\u4e4b\u540e\uff0c\u4f60\u9700\u8981\u5b89\u88c5Appium-Python-Client\uff0c\u8fd9\u662f\u7528\u4e8e\u7f16\u5199\u548c\u6267\u884cPython\u811a\u672c\u7684\u5e93\u3002\u4f60\u53ef\u4ee5\u4f7f\u7528pip\u6765\u5b89\u88c5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install Appium-Python-Client<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>4\u3001\u5b89\u88c5\u548c\u914d\u7f6eAndroid SDK<\/h4>\n<\/p>\n<p><p>\u4f60\u8fd8\u9700\u8981\u5b89\u88c5Android SDK\uff0c\u4ee5\u4fbf\u4f7f\u7528adb\u547d\u4ee4\u548c\u5176\u4ed6Android\u76f8\u5173\u5de5\u5177\u3002\u4f60\u53ef\u4ee5\u4eceAndroid\u5f00\u53d1\u8005\u7f51\u7ad9\u4e0b\u8f7dAndroid Studio\uff0c\u5b83\u5305\u542b\u4e86SDK\u7ba1\u7406\u5668\u3002\u5b89\u88c5\u5b8c\u6210\u540e\uff0c\u901a\u8fc7SDK\u7ba1\u7406\u5668\u4e0b\u8f7d\u6240\u9700\u7684SDK\u5de5\u5177\u548c\u5e73\u53f0\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u8fde\u63a5\u8bbe\u5907<\/h3>\n<\/p>\n<p><h4>1\u3001\u542f\u7528\u5f00\u53d1\u8005\u9009\u9879\u548cUSB\u8c03\u8bd5<\/h4>\n<\/p>\n<p><p>\u5728\u4f60\u7684Android\u8bbe\u5907\u4e0a\uff0c\u8fdb\u5165\u8bbe\u7f6e-&gt;\u5173\u4e8e\u624b\u673a\uff0c\u8fde\u7eed\u70b9\u51fb\u201c\u7248\u672c\u53f7\u201d\u4e03\u6b21\u4ee5\u542f\u7528\u5f00\u53d1\u8005\u9009\u9879\u3002\u7136\u540e\u8fd4\u56de\u8bbe\u7f6e\uff0c\u8fdb\u5165\u5f00\u53d1\u8005\u9009\u9879\uff0c\u542f\u7528\u201cUSB\u8c03\u8bd5\u201d\u3002<\/p>\n<\/p>\n<p><h4>2\u3001\u8fde\u63a5\u8bbe\u5907\u5230\u7535\u8111<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528USB\u6570\u636e\u7ebf\u5c06\u4f60\u7684Android\u8bbe\u5907\u8fde\u63a5\u5230\u7535\u8111\u3002\u786e\u4fdd\u8bbe\u5907\u9a71\u52a8\u7a0b\u5e8f\u5df2\u6b63\u786e\u5b89\u88c5\u3002\u4f60\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u6765\u786e\u8ba4\u8bbe\u5907\u662f\u5426\u5df2\u8fde\u63a5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">adb devices<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5982\u679c\u8bbe\u5907\u5df2\u8fde\u63a5\uff0c\u4f60\u5c06\u770b\u5230\u8bbe\u5907\u7684\u5e8f\u5217\u53f7\u51fa\u73b0\u5728\u5217\u8868\u4e2d\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u7f16\u5199\u548c\u6267\u884c\u811a\u672c<\/h3>\n<\/p>\n<p><h4>1\u3001\u542f\u52a8Appium\u670d\u52a1\u5668<\/h4>\n<\/p>\n<p><p>\u5728\u547d\u4ee4\u884c\u4e2d\u8fd0\u884c\u4ee5\u4e0b\u547d\u4ee4\u542f\u52a8Appium\u670d\u52a1\u5668\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">appium<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>Appium\u670d\u52a1\u5668\u542f\u52a8\u540e\uff0c\u5c06\u76d1\u542c\u9ed8\u8ba4\u76844723\u7aef\u53e3\u3002<\/p>\n<\/p>\n<p><h4>2\u3001\u7f16\u5199Python\u811a\u672c<\/h4>\n<\/p>\n<p><p>\u521b\u5efa\u4e00\u4e2aPython\u811a\u672c\u6587\u4ef6\uff0c\u4f8b\u5982<code>test_appium.py<\/code>\uff0c\u5e76\u7f16\u5199\u4ee5\u4e0b\u5185\u5bb9\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from appium import webdriver<\/p>\n<p>desired_caps = {<\/p>\n<p>    &quot;platformName&quot;: &quot;Android&quot;,<\/p>\n<p>    &quot;platformVersion&quot;: &quot;9&quot;,  # \u4f60\u7684\u8bbe\u5907Android\u7248\u672c<\/p>\n<p>    &quot;deviceName&quot;: &quot;emulator-5554&quot;,  # \u4f60\u7684\u8bbe\u5907\u540d\u79f0\uff0c\u53ef\u4ee5\u901a\u8fc7adb devices\u547d\u4ee4\u83b7\u53d6<\/p>\n<p>    &quot;appPackage&quot;: &quot;com.example.android&quot;,  # \u4f60\u8981\u6d4b\u8bd5\u7684\u5e94\u7528\u5305\u540d<\/p>\n<p>    &quot;appActivity&quot;: &quot;.M<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>nActivity&quot;  # \u4f60\u8981\u6d4b\u8bd5\u7684\u5e94\u7528\u4e3b\u6d3b\u52a8<\/p>\n<p>}<\/p>\n<p>driver = webdriver.Remote(&quot;http:\/\/localhost:4723\/wd\/hub&quot;, desired_caps)<\/p>\n<h2><strong>\u5728\u8fd9\u91cc\u7f16\u5199\u4f60\u7684\u6d4b\u8bd5\u811a\u672c\uff0c\u4f8b\u5982\u70b9\u51fb\u4e00\u4e2a\u6309\u94ae<\/strong><\/h2>\n<p>element = driver.find_element_by_id(&quot;com.example.android:id\/button&quot;)<\/p>\n<p>element.click()<\/p>\n<h2><strong>\u5173\u95edAppium\u4f1a\u8bdd<\/strong><\/h2>\n<p>driver.quit()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3\u3001\u6267\u884cPython\u811a\u672c<\/h4>\n<\/p>\n<p><p>\u5728\u547d\u4ee4\u884c\u4e2d\u8fd0\u884c\u4ee5\u4e0b\u547d\u4ee4\u6765\u6267\u884c\u4f60\u7684Python\u811a\u672c\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">python test_appium.py<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5982\u679c\u4e00\u5207\u914d\u7f6e\u6b63\u786e\uff0c\u4f60\u7684\u811a\u672c\u5c06\u8fde\u63a5\u5230Appium\u670d\u52a1\u5668\uff0c\u5e76\u5728\u4f60\u7684\u8bbe\u5907\u4e0a\u6267\u884c\u76f8\u5e94\u7684\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u5e38\u7528\u7684Appium\u64cd\u4f5c<\/h3>\n<\/p>\n<p><h4>1\u3001\u67e5\u627e\u5143\u7d20<\/h4>\n<\/p>\n<p><p>\u4f60\u53ef\u4ee5\u4f7f\u7528\u591a\u79cd\u65b9\u6cd5\u6765\u67e5\u627e\u5143\u7d20\uff0c\u4f8b\u5982\u901a\u8fc7ID\u3001XPath\u3001Class Name\u7b49\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">element = driver.find_element_by_id(&quot;com.example.android:id\/button&quot;)<\/p>\n<p>element = driver.find_element_by_xpath(&quot;\/\/android.widget.Button[@text=&#39;Click me&#39;]&quot;)<\/p>\n<p>element = driver.find_element_by_class_name(&quot;android.widget.Button&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u70b9\u51fb\u548c\u8f93\u5165\u6587\u672c<\/h4>\n<\/p>\n<p><p>\u4f60\u53ef\u4ee5\u4f7f\u7528<code>click<\/code>\u65b9\u6cd5\u6765\u70b9\u51fb\u5143\u7d20\uff0c\u4f7f\u7528<code>send_keys<\/code>\u65b9\u6cd5\u6765\u8f93\u5165\u6587\u672c\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">element.click()<\/p>\n<p>element.send_keys(&quot;Hello World&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3\u3001\u6ed1\u52a8\u548c\u6eda\u52a8<\/h4>\n<\/p>\n<p><p>\u4f60\u53ef\u4ee5\u4f7f\u7528<code>swipe<\/code>\u548c<code>scroll<\/code>\u65b9\u6cd5\u6765\u6ed1\u52a8\u548c\u6eda\u52a8\u5c4f\u5e55\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">driver.swipe(start_x=100, start_y=100, end_x=100, end_y=500, duration=800)<\/p>\n<p>driver.scroll(origin_element, destination_element)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>4\u3001\u83b7\u53d6\u5143\u7d20\u5c5e\u6027\u548c\u6587\u672c<\/h4>\n<\/p>\n<p><p>\u4f60\u53ef\u4ee5\u4f7f\u7528<code>get_attribute<\/code>\u65b9\u6cd5\u6765\u83b7\u53d6\u5143\u7d20\u7684\u5c5e\u6027\uff0c\u4f7f\u7528<code>text<\/code>\u5c5e\u6027\u6765\u83b7\u53d6\u5143\u7d20\u7684\u6587\u672c\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">attr = element.get_attribute(&quot;contentDescription&quot;)<\/p>\n<p>text = element.text<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e94\u3001\u6267\u884c\u66f4\u591a\u590d\u6742\u64cd\u4f5c<\/h3>\n<\/p>\n<p><h4>1\u3001\u7b49\u5f85\u5143\u7d20\u51fa\u73b0<\/h4>\n<\/p>\n<p><p>\u6709\u65f6\u4f60\u9700\u8981\u7b49\u5f85\u67d0\u4e2a\u5143\u7d20\u51fa\u73b0\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528WebDriverWait\u548cexpected_conditions\u6765\u5b9e\u73b0\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from selenium.webdriver.common.by import By<\/p>\n<p>from selenium.webdriver.support.ui import WebDriverWait<\/p>\n<p>from selenium.webdriver.support import expected_conditions as EC<\/p>\n<p>element = WebDriverWait(driver, 10).until(<\/p>\n<p>    EC.presence_of_element_located((By.ID, &quot;com.example.android:id\/button&quot;))<\/p>\n<p>)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u5207\u6362\u4e0a\u4e0b\u6587<\/h4>\n<\/p>\n<p><p>\u5982\u679c\u4f60\u7684\u5e94\u7528\u5305\u542bWebView\uff0c\u4f60\u53ef\u4ee5\u5207\u6362\u4e0a\u4e0b\u6587\u6765\u64cd\u4f5cWeb\u5143\u7d20\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u83b7\u53d6\u6240\u6709\u4e0a\u4e0b\u6587<\/p>\n<p>contexts = driver.contexts<\/p>\n<p>print(contexts)<\/p>\n<h2><strong>\u5207\u6362\u5230WebView\u4e0a\u4e0b\u6587<\/strong><\/h2>\n<p>driver.switch_to.context(&quot;WEBVIEW_com.example.android&quot;)<\/p>\n<h2><strong>\u6267\u884cWeb\u64cd\u4f5c<\/strong><\/h2>\n<p>element = driver.find_element_by_css_selector(&quot;button&quot;)<\/p>\n<p>element.click()<\/p>\n<h2><strong>\u5207\u6362\u56de\u539f\u751f\u4e0a\u4e0b\u6587<\/strong><\/h2>\n<p>driver.switch_to.context(&quot;NATIVE_APP&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3\u3001\u5904\u7406\u5f39\u7a97\u548c\u8b66\u544a<\/h4>\n<\/p>\n<p><p>\u5982\u679c\u4f60\u7684\u5e94\u7528\u4f1a\u5f39\u51fa\u5bf9\u8bdd\u6846\u6216\u8005\u8b66\u544a\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528<code>alert<\/code>\u65b9\u6cd5\u6765\u5904\u7406\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">alert = driver.switch_to.alert<\/p>\n<p>alert.accept()  # \u63a5\u53d7\u5f39\u7a97<\/p>\n<p>alert.dismiss()  # \u53d6\u6d88\u5f39\u7a97<\/p>\n<p>alert.send_keys(&quot;input text&quot;)  # \u8f93\u5165\u6587\u672c<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u516d\u3001\u4f7f\u7528Page Object Model<\/h3>\n<\/p>\n<p><p>Page Object Model (POM) \u662f\u4e00\u79cd\u8bbe\u8ba1\u6a21\u5f0f\uff0c\u7528\u4e8e\u63d0\u9ad8\u81ea\u52a8\u5316\u6d4b\u8bd5\u4ee3\u7801\u7684\u53ef\u7ef4\u62a4\u6027\u548c\u53ef\u8bfb\u6027\u3002\u4f60\u53ef\u4ee5\u5c06\u6bcf\u4e2a\u9875\u9762\u7684\u5143\u7d20\u548c\u64cd\u4f5c\u5c01\u88c5\u5230\u4e00\u4e2a\u7c7b\u4e2d\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u521b\u5efa\u9875\u9762\u7c7b<\/h4>\n<\/p>\n<p><p>\u521b\u5efa\u4e00\u4e2a\u9875\u9762\u7c7b\uff0c\u4f8b\u5982<code>MainPage.py<\/code>\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">class MainPage:<\/p>\n<p>    def __init__(self, driver):<\/p>\n<p>        self.driver = driver<\/p>\n<p>        self.button = &quot;com.example.android:id\/button&quot;<\/p>\n<p>    def click_button(self):<\/p>\n<p>        self.driver.find_element_by_id(self.button).click()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u5728\u6d4b\u8bd5\u811a\u672c\u4e2d\u4f7f\u7528\u9875\u9762\u7c7b<\/h4>\n<\/p>\n<p><p>\u5728\u4f60\u7684\u6d4b\u8bd5\u811a\u672c\u4e2d\u4f7f\u7528\u9875\u9762\u7c7b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from appium import webdriver<\/p>\n<p>from MainPage import MainPage<\/p>\n<p>desired_caps = {<\/p>\n<p>    &quot;platformName&quot;: &quot;Android&quot;,<\/p>\n<p>    &quot;platformVersion&quot;: &quot;9&quot;,<\/p>\n<p>    &quot;deviceName&quot;: &quot;emulator-5554&quot;,<\/p>\n<p>    &quot;appPackage&quot;: &quot;com.example.android&quot;,<\/p>\n<p>    &quot;appActivity&quot;: &quot;.MainActivity&quot;<\/p>\n<p>}<\/p>\n<p>driver = webdriver.Remote(&quot;http:\/\/localhost:4723\/wd\/hub&quot;, desired_caps)<\/p>\n<p>main_page = MainPage(driver)<\/p>\n<p>main_page.click_button()<\/p>\n<p>driver.quit()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e03\u3001\u4f7f\u7528Appium Inspector<\/h3>\n<\/p>\n<p><p>Appium Inspector \u662f\u4e00\u4e2aGUI\u5de5\u5177\uff0c\u7528\u4e8e\u68c0\u67e5\u5e94\u7528\u7684\u5143\u7d20\u548c\u751f\u6210\u4ee3\u7801\u3002\u4f60\u53ef\u4ee5\u4eceAppium Desktop\u5ba2\u6237\u7aef\u4e2d\u542f\u52a8Appium Inspector\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u542f\u52a8Appium Desktop<\/h4>\n<\/p>\n<p><p>\u4e0b\u8f7d\u5e76\u5b89\u88c5Appium Desktop\u5ba2\u6237\u7aef\u3002\u542f\u52a8Appium Desktop\uff0c\u5e76\u70b9\u51fb\u201cStart Server\u201d\u542f\u52a8Appium\u670d\u52a1\u5668\u3002<\/p>\n<\/p>\n<p><h4>2\u3001\u542f\u52a8Appium Inspector<\/h4>\n<\/p>\n<p><p>\u5728Appium Desktop\u5ba2\u6237\u7aef\u4e2d\uff0c\u70b9\u51fb\u201cStart Inspector Session\u201d\u3002\u586b\u5199Desired Capabilities\uff0c\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-json\">{<\/p>\n<p>  &quot;platformName&quot;: &quot;Android&quot;,<\/p>\n<p>  &quot;platformVersion&quot;: &quot;9&quot;,<\/p>\n<p>  &quot;deviceName&quot;: &quot;emulator-5554&quot;,<\/p>\n<p>  &quot;appPackage&quot;: &quot;com.example.android&quot;,<\/p>\n<p>  &quot;appActivity&quot;: &quot;.MainActivity&quot;<\/p>\n<p>}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u70b9\u51fb\u201cStart Session\u201d\u5f00\u59cb\u4f1a\u8bdd\u3002<\/p>\n<\/p>\n<p><h4>3\u3001\u68c0\u67e5\u5143\u7d20\u548c\u751f\u6210\u4ee3\u7801<\/h4>\n<\/p>\n<p><p>\u5728Appium Inspector\u4e2d\uff0c\u4f60\u53ef\u4ee5\u67e5\u770b\u5e94\u7528\u7684\u9875\u9762\u7ed3\u6784\u3001\u5143\u7d20\u5c5e\u6027\uff0c\u5e76\u751f\u6210\u76f8\u5e94\u7684\u4ee3\u7801\u7247\u6bb5\u3002\u8fd9\u6709\u52a9\u4e8e\u4f60\u7f16\u5199\u548c\u8c03\u8bd5\u811a\u672c\u3002<\/p>\n<\/p>\n<p><h3>\u516b\u3001\u4f7f\u7528<a href=\"https:\/\/docs.pingcode.com\/blog\/devops\/52691.html\" target=\"_blank\">CI\/CD<\/a>\u96c6\u6210\u81ea\u52a8\u5316\u6d4b\u8bd5<\/h3>\n<\/p>\n<p><p>\u4e3a\u4e86\u5728\u6301\u7eed\u96c6\u6210\/\u6301\u7eed\u4ea4\u4ed8\uff08CI\/CD\uff09\u7ba1\u9053\u4e2d\u6267\u884c\u81ea\u52a8\u5316\u6d4b\u8bd5\uff0c\u4f60\u53ef\u4ee5\u5c06Appium\u6d4b\u8bd5\u96c6\u6210\u5230CI\u5de5\u5177\u4e2d\uff0c\u4f8b\u5982Jenkins\u3001GitLab CI\u7b49\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u914d\u7f6eCI\u73af\u5883<\/h4>\n<\/p>\n<p><p>\u5728\u4f60\u7684CI\u73af\u5883\u4e2d\uff0c\u5b89\u88c5\u6240\u9700\u7684\u4f9d\u8d56\u9879\uff0c\u4f8b\u5982Python\u3001Appium\u3001Android SDK\u7b49\u3002\u786e\u4fdd\u80fd\u591f\u8fde\u63a5\u5230\u6d4b\u8bd5\u8bbe\u5907\uff08\u4f8b\u5982\u901a\u8fc7ADB\uff09\u3002<\/p>\n<\/p>\n<p><h4>2\u3001\u7f16\u5199CI\u811a\u672c<\/h4>\n<\/p>\n<p><p>\u7f16\u5199CI\u811a\u672c\u6765\u6267\u884cAppium\u6d4b\u8bd5\u3002\u4f8b\u5982\uff0c\u5728Jenkins\u4e2d\uff0c\u4f60\u53ef\u4ee5\u7f16\u5199\u4e00\u4e2aPipeline\u811a\u672c\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-groovy\">pipeline {<\/p>\n<p>    agent any<\/p>\n<p>    stages {<\/p>\n<p>        stage(&#39;Setup&#39;) {<\/p>\n<p>            steps {<\/p>\n<p>                sh &#39;pip install Appium-Python-Client&#39;<\/p>\n<p>                sh &#39;sdkmanager &quot;platform-tools&quot;&#39;<\/p>\n<p>            }<\/p>\n<p>        }<\/p>\n<p>        stage(&#39;Test&#39;) {<\/p>\n<p>            steps {<\/p>\n<p>                sh &#39;python test_appium.py&#39;<\/p>\n<p>            }<\/p>\n<p>        }<\/p>\n<p>    }<\/p>\n<p>}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3\u3001\u914d\u7f6e\u548c\u8fd0\u884cCI\u4efb\u52a1<\/h4>\n<\/p>\n<p><p>\u5728CI\u5de5\u5177\u4e2d\uff0c\u914d\u7f6e\u548c\u8fd0\u884c\u4efb\u52a1\u3002CI\u5de5\u5177\u5c06\u81ea\u52a8\u6267\u884c\u4f60\u7684Appium\u6d4b\u8bd5\uff0c\u5e76\u62a5\u544a\u6d4b\u8bd5\u7ed3\u679c\u3002<\/p>\n<\/p>\n<p><h3>\u4e5d\u3001\u8c03\u8bd5\u548c\u65e5\u5fd7\u8bb0\u5f55<\/h3>\n<\/p>\n<p><h4>1\u3001\u542f\u7528Appium\u65e5\u5fd7<\/h4>\n<\/p>\n<p><p>\u4f60\u53ef\u4ee5\u542f\u7528Appium\u65e5\u5fd7\u6765\u8c03\u8bd5\u95ee\u9898\u3002\u5728\u542f\u52a8Appium\u670d\u52a1\u5668\u65f6\uff0c\u6dfb\u52a0<code>--log-level<\/code>\u53c2\u6570\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">appium --log-level debug<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u6355\u83b7\u548c\u5206\u6790\u65e5\u5fd7<\/h4>\n<\/p>\n<p><p>\u5728\u6d4b\u8bd5\u811a\u672c\u4e2d\uff0c\u4f60\u53ef\u4ee5\u6355\u83b7\u5e76\u5206\u6790\u65e5\u5fd7\u4fe1\u606f\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import logging<\/p>\n<p>logging.basicConfig(level=logging.INFO)<\/p>\n<p>logger = logging.getLogger(__name__)<\/p>\n<p>logger.info(&quot;Starting Appium test&quot;)<\/p>\n<p>try:<\/p>\n<p>    driver = webdriver.Remote(&quot;http:\/\/localhost:4723\/wd\/hub&quot;, desired_caps)<\/p>\n<p>    logger.info(&quot;Appium session started&quot;)<\/p>\n<p>    element = driver.find_element_by_id(&quot;com.example.android:id\/button&quot;)<\/p>\n<p>    element.click()<\/p>\n<p>    logger.info(&quot;Button clicked&quot;)<\/p>\n<p>finally:<\/p>\n<p>    driver.quit()<\/p>\n<p>    logger.info(&quot;Appium session ended&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u5341\u3001\u5e38\u89c1\u95ee\u9898\u548c\u89e3\u51b3\u65b9\u6cd5<\/h3>\n<\/p>\n<p><h4>1\u3001\u8bbe\u5907\u672a\u8fde\u63a5<\/h4>\n<\/p>\n<p><p>\u786e\u4fdd\u8bbe\u5907\u5df2\u6b63\u786e\u8fde\u63a5\uff0c\u5e76\u542f\u7528\u4e86USB\u8c03\u8bd5\u3002\u4f7f\u7528<code>adb devices<\/code>\u547d\u4ee4\u68c0\u67e5\u8bbe\u5907\u72b6\u6001\u3002\u5982\u679c\u8bbe\u5907\u672a\u663e\u793a\uff0c\u5c1d\u8bd5\u91cd\u65b0\u8fde\u63a5\u8bbe\u5907\u6216\u91cd\u65b0\u542f\u52a8ADB\u670d\u52a1\u5668\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">adb kill-server<\/p>\n<p>adb start-server<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u65e0\u6cd5\u627e\u5230\u5143\u7d20<\/h4>\n<\/p>\n<p><p>\u68c0\u67e5\u5143\u7d20\u5b9a\u4f4d\u5668\u662f\u5426\u6b63\u786e\u3002\u4f7f\u7528Appium Inspector\u6216UI Automator Viewer\uff08Android SDK\u5de5\u5177\uff09\u6765\u68c0\u67e5\u5143\u7d20\u7684\u5c5e\u6027\u548c\u5c42\u6b21\u7ed3\u6784\u3002<\/p>\n<\/p>\n<p><h4>3\u3001\u4f1a\u8bdd\u8d85\u65f6<\/h4>\n<\/p>\n<p><p>\u786e\u4fddAppium\u670d\u52a1\u5668\u5df2\u542f\u52a8\uff0c\u5e76\u4e14Desired Capabilities\u914d\u7f6e\u6b63\u786e\u3002\u68c0\u67e5\u7f51\u7edc\u8fde\u63a5\uff0c\u786e\u4fdd\u8bbe\u5907\u548cAppium\u670d\u52a1\u5668\u5728\u540c\u4e00\u7f51\u7edc\u4e2d\u3002<\/p>\n<\/p>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u6b65\u9aa4\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528Python\u7f16\u5199\u548c\u6267\u884c\u624b\u673a\u81ea\u52a8\u5316\u6d4b\u8bd5\u811a\u672c\u3002Appium\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u529f\u80fd\u548c\u7075\u6d3b\u6027\uff0c\u4f7f\u4f60\u80fd\u591f\u8f7b\u677e\u5730\u81ea\u52a8\u5316\u6d4b\u8bd5\u79fb\u52a8\u5e94\u7528\u3002\u5e0c\u671b\u8fd9\u7bc7\u6587\u7ae0\u5bf9\u4f60\u6709\u6240\u5e2e\u52a9\uff0c\u795d\u4f60\u5728\u81ea\u52a8\u5316\u6d4b\u8bd5\u7684\u9053\u8def\u4e0a\u53d6\u5f97\u6210\u529f\uff01<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5f00\u59cb\u4f7f\u7528Python\u7f16\u5199\u624b\u673a\u811a\u672c\uff1f<\/strong><br \/>\u8981\u5f00\u59cb\u4f7f\u7528Python\u7f16\u5199\u624b\u673a\u811a\u672c\uff0c\u9996\u5148\u9700\u8981\u9009\u62e9\u5408\u9002\u7684\u5f00\u53d1\u73af\u5883\u3002\u53ef\u4ee5\u4f7f\u7528\u50cfKivy\u6216BeeWare\u8fd9\u6837\u7684\u6846\u67b6\uff0c\u8fd9\u4e9b\u6846\u67b6\u5141\u8bb8\u4f60\u4f7f\u7528Python\u7f16\u5199\u8de8\u5e73\u53f0\u7684\u79fb\u52a8\u5e94\u7528\u3002\u6b64\u5916\uff0c\u5b89\u88c5\u76f8\u5173\u7684\u4f9d\u8d56\u5e93\u548c\u5de5\u5177\u5305\u4e5f\u662f\u5fc5\u4e0d\u53ef\u5c11\u7684\u3002\u8bbe\u7f6e\u597d\u73af\u5883\u540e\uff0c\u719f\u6089Python\u7684\u57fa\u672c\u8bed\u6cd5\u548c\u79fb\u52a8\u8bbe\u5907\u7684\u7279\u6027\u5c06\u6709\u52a9\u4e8e\u66f4\u9ad8\u6548\u5730\u5f00\u53d1\u811a\u672c\u3002<\/p>\n<p><strong>Python\u811a\u672c\u5728\u624b\u673a\u4e0a\u8fd0\u884c\u9700\u8981\u54ea\u4e9b\u5de5\u5177\u6216\u5e93\uff1f<\/strong><br \/>\u5728\u624b\u673a\u4e0a\u8fd0\u884cPython\u811a\u672c\u901a\u5e38\u9700\u8981\u4e00\u4e9b\u7279\u5b9a\u7684\u5de5\u5177\u548c\u5e93\u3002\u6bd4\u5982\uff0cKivy\u662f\u4e00\u4e2a\u6d41\u884c\u7684Python\u6846\u67b6\uff0c\u4e13\u95e8\u7528\u4e8e\u5f00\u53d1\u591a\u70b9\u89e6\u63a7\u5e94\u7528\u3002\u6b64\u5916\uff0cPyqtdeploy\u548cSL4A\uff08Scripting Layer for Android\uff09\u7b49\u5de5\u5177\u4e5f\u53ef\u4ee5\u5e2e\u52a9\u4f60\u5728Android\u8bbe\u5907\u4e0a\u8fd0\u884cPython\u811a\u672c\u3002\u786e\u4fdd\u5b89\u88c5\u4e86\u6240\u9700\u7684\u4f9d\u8d56\u5305\uff0c\u5e76\u4e86\u89e3\u5982\u4f55\u5728\u76ee\u6807\u8bbe\u5907\u4e0a\u914d\u7f6e\u73af\u5883\u3002<\/p>\n<p><strong>\u80fd\u5426\u5c06Python\u811a\u672c\u6253\u5305\u6210\u624b\u673a\u5e94\u7528\uff1f<\/strong><br \/>\u662f\u7684\uff0c\u53ef\u4ee5\u5c06Python\u811a\u672c\u6253\u5305\u6210\u624b\u673a\u5e94\u7528\u3002\u4f7f\u7528Kivy\u6216PyInstaller\u7b49\u5de5\u5177\uff0c\u4f60\u53ef\u4ee5\u5c06Python\u4ee3\u7801\u8f6c\u6362\u4e3a\u53ef\u5728Android\u6216iOS\u4e0a\u8fd0\u884c\u7684\u5e94\u7528\u7a0b\u5e8f\u3002Kivy\u7684Buildozer\u5de5\u5177\u4e13\u95e8\u7528\u4e8eAndroid\u5e94\u7528\u7684\u6253\u5305\uff0c\u63d0\u4f9b\u4e86\u7b80\u5316\u7684\u8fc7\u7a0b\u3002\u5bf9\u4e8eiOS\u5e94\u7528\uff0c\u53ef\u4ee5\u4f7f\u7528Xcode\u7ed3\u5408Kivy\u8fdb\u884c\u6253\u5305\u3002\u8fd9\u6837\uff0c\u4f60\u5c31\u53ef\u4ee5\u5c06\u4f60\u7684Python\u811a\u672c\u4f5c\u4e3a\u72ec\u7acb\u7684\u5e94\u7528\u5206\u53d1\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u505a\u624b\u673a\u811a\u672c\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u6bd4\u5982\u4f7f\u7528Appium\u3001ADB\uff08Android Debug Bridge\uff09\u3001 [&hellip;]","protected":false},"author":3,"featured_media":1170469,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[37],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1170466"}],"collection":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/comments?post=1170466"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1170466\/revisions"}],"predecessor-version":[{"id":1170470,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1170466\/revisions\/1170470"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1170469"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1170466"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1170466"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1170466"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}