RobotFramework自動化測試架構-移動手機自動化測試Input Text和Click Button關鍵字的使用

來源:互聯網
上載者:User

標籤:elf   des   amp   tomat   indexer   try   ica   ima   樣本   

Input Text和Click Button

Input Text 關鍵字一般用來給輸入框進行輸入操作,該關鍵字接收兩個參數[ locator | text ]。

樣本1:啟動安卓手機上一個APP的MainActivity,在開啟Activity,進入介面後,分別向兩個EditText輸入框中輸入12,並且點擊按鈕“計算”來計算出輸入的這兩個數位乘積。

APP的介面如下,提供了兩個輸入框,還有一個計算的Button按鈕。

在寫這個自動化案例前,我們可以使用安卓SDK提供的Ui Automator Viewer工具來進行這個介面的資源定位。通過定位後,可以看到第一個EditText輸入框的resource-id為com.example.calculator:id/factorone,class為android.widget.EditText,name為請輸入數字,如所示。

第二個EditText輸入框的resource-id為com.example.calculator:id/factortwo,class為android.widget.EditText,name為請輸入數字,如所示。

Button按鈕的resource-id為com.example.calculator:id/commit,class為android.widget.Button,name為計算,如所示。

Click Button關鍵字用來類比點擊APP上的一個button按鈕,該關鍵字接收一個參數[ index_or_name ]。

Open Application    http://localhost:4723/wd/hub  platformName=Android platformVersion=22       deviceName=98YFBP522VSU       app=C:/Users/yongqing/Desktop/app-debug.apk   appPackage=com.example.calculator    appActivity=MainActivity

Input Text      id=com.example.calculator:id/factorone   12                                

Input Text      id=com.example.calculator:id/factortwo   12                                

Click Button     計算      

執行結果:

可以看到已經執行成功,上面是通過resource-id的方式來定位EditText輸入框,並且通過name的方式來定位button按鈕。

下面通過另一種方式,用name的方式來定位EditText輸入框,通過index的方式來點擊button按鈕

Open Application   http://localhost:4723/wd/hub  platformName=Android platformVersion=22       deviceName=98YFBP522VSU       app=C:/Users/yongqing/Desktop/app-debug.apk   appPackage=com.example.calculator

Input Text       name=請輸入數字 12                        

Input Text       name=請輸入數字 14                        

Click Button   index=0                              

執行結果:

在通過index的方式來點擊button按鈕的時候,需要注意index的取值,不要和通過Ui Automator Viewer工具看到的index混淆,先看一段AppiumLibrary庫的源碼,在這裡選取了源碼中的三個函數。從如下三個函數中可以看到click_button關鍵字支援name和index兩種方式來定位一個button,在使用index的時候,是根據class_name,即通過android.widget.Button這個class_name來找出當前介面中有幾個button按鈕(源碼中通過elements = self._find_elements_by_class_name(class_name)來尋找有幾個button按鈕,就會返回幾個element),然後每個 button按鈕按照index的方式來取出(源碼中通過element = elements[index]來得到具體的一個button按鈕)。

AppiumLibrary庫函數1:

    def click_button(self, index_or_name):

        """ Click button """

        _platform_class_dict = {‘ios‘: ‘UIAButton‘,

                                ‘android‘: ‘android.widget.Button‘}

        if self._is_support_platform(_platform_class_dict):

            class_name = self._get_class(_platform_class_dict)

            self._click_element_by_class_name(class_name, index_or_name)

AppiumLibrary庫函數2:

def _click_element_by_class_name(self, class_name, index_or_name):

        element = self._find_element_by_class_name(class_name, index_or_name)

        self._info("Clicking element ‘%s‘." % element.text)

        try:

            element.click()

        except Exception as e:

            raise ‘Cannot click the %s element "%s"‘ % (class_name, index_or_name)

AppiumLibrary庫函數3:

    def _find_element_by_class_name(self, class_name, index_or_name):

        elements = self._find_elements_by_class_name(class_name)

        print ‘elements:"%s"‘ % elements

 

        if self._is_index(index_or_name):

            try:

                index = int(index_or_name.split(‘=‘)[-1])

                print ‘index:"%s"‘ % index

                element = elements[index]

                print ‘element:‘ , element

            except (IndexError, TypeError):

                raise ‘Cannot find the element with index "%s"‘ % index_or_name

        else:

            found = False

            for element in elements:

                self._info("‘%s‘." % element.text)

                if element.text == index_or_name:

                    found = True

                    break

            if not found:

                raise ‘Cannot find the element with name "%s"‘ % index_or_name

下面這個介面中,放入了兩個button按鈕,一個button按鈕是計算按鈕,一個button按鈕是取消按鈕。

在執行時,通過elements = self._find_elements_by_class_name(class_name)得到所有的button按鈕後,再用print ‘elements:"%s"‘ % elements可以列印出擷取到的elements,也就是所有的button,從如下的輸出結果可以看到,elements會存放在一個list列表中,該list 列表中,共有兩個元素,代表取到了兩個button按鈕。

列印輸出結果:

elements:"[<appium.webdriver.webelement.WebElement (session="8e85c12f-2243-4b82-abfc-d091fddbed8b", element="4")>, <appium.webdriver.webelement.WebElement (session="8e85c12f-2243-4b82-abfc-d091fddbed8b", element="5")>]"

當index 為0時,會取到第一個按鈕,也就是“計算”這個button按鈕,當index為1時,會取到第二個按鈕,也就是“取消”這個button按鈕。當index超過1後,那麼就會報錯啦,此時源碼中會通過raise ‘Cannot find the element with index "%s"‘ % index_or_name來拋出一個異常,告訴使用者,不能通過當前的index擷取到element(也就是此時無法擷取到任何button按鈕啦)。

樣本2:通過xpath的方式定位元素,這裡依舊用上面的APP介面為樣本。

用xpath的方式定位第一個EditText輸入框和第二個EditText輸入框,樣本如下:

Open Application    http://localhost:4723/wd/hub    platformName=Android    platformVersion=22    deviceName=98YFBP522VSU app=C:/Users/yongqing/Desktop/app-debug.apk appPackage=com.example.calculator    appActivity=MainActivity

Input Text  xpath=//android.widget.EditText[1]  12                

Input Text  xpath=//android.widget.EditText[2]  14                 

Click Button    計算

執行結果:

Starting test: RobotFrameworkTest1.TestSuite5.TestCase005

20170510 13:45:07.381 :  INFO : Typing text ‘12‘ into text field ‘xpath=//android.widget.EditText[1]‘

20170510 13:45:07.381 :  INFO : msg:find xpath=//android.widget.EditText[1]

20170510 13:45:07.381 :  INFO : prefix: xpath

20170510 13:45:07.397 :  INFO : criteria: //android.widget.EditText[1]

20170510 13:45:10.462 :  INFO : elements: [<appium.webdriver.webelement.WebElement (session="ec48b38a-9cbe-457d-94a0-dec662d3f9cb", element="1")>]

20170510 13:45:15.313 :  INFO : Typing text ‘14‘ into text field ‘xpath=//android.widget.EditText[2]‘

20170510 13:45:15.313 :  INFO : msg:find xpath=//android.widget.EditText[2]

20170510 13:45:15.313 :  INFO : prefix: xpath

20170510 13:45:15.313 :  INFO : criteria: //android.widget.EditText[2]

20170510 13:45:15.906 :  INFO : elements: [<appium.webdriver.webelement.WebElement (session="ec48b38a-9cbe-457d-94a0-dec662d3f9cb", element="2")>]

20170510 13:45:21.307 :  INFO : ‘計算‘.

20170510 13:45:21.385 :  INFO : Clicking element ‘計算‘.

Ending test:   RobotFrameworkTest1.TestSuite5.TestCase005

從上面的執行結果看,通過xpath=//android.widget.EditText[1] 定位到了第一個輸入框,通過xpath=//android.widget.EditText[2] 定位到了第二個輸入框。

樣本3:通過accessibility_id的方式定位元素,accessibility_id對應到安卓APP後,其對應的屬性為content-desc,這裡依舊用上面的APP介面為樣本,但是我們對第一個EditText輸入框加入了content-desc屬性,如所示。

Open Application    http://localhost:4723/wd/hub    platformName=Android    platformVersion=22    deviceName=98YFBP522VSU app=C:/Users/yongqing/Desktop/app-debug.apk appPackage=com.example.calculator    appActivity=MainActivity

Input Text  accessibility_id=輸入框 23                

Input Text  id=com.example.calculator:id/factortwo 12                  

Click Button    計算   

執行結果:

Starting test: RobotFrameworkTest1.TestSuite5.TestCase006

20170510 14:23:09.735 :  INFO : Typing text ‘23‘ into text field ‘accessibility_id=輸入框‘

20170510 14:23:09.735 :  INFO : msg:find accessibility_id=輸入框

20170510 14:23:16.573 :  INFO : Typing text ‘12‘ into text field ‘id=com.example.calculator:id/factortwo‘

20170510 14:23:16.573 :  INFO : msg:find id=com.example.calculator:id/factortwo

20170510 14:23:22.799 :  INFO : ‘計算‘.

20170510 14:23:22.901 :  INFO : Clicking element ‘計算‘.

Ending test:   RobotFrameworkTest1.TestSuite5.TestCase006  

從執行結果看,通過   accessibility_id=輸入框 也可以定位到EditText輸入框。

RobotFramework自動化測試架構-移動手機自動化測試Input Text和Click Button關鍵字的使用

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.