Heim  >  Artikel  >  Backend-Entwicklung  >  Python脚本在Appium库上对移动应用实现自动化测试

Python脚本在Appium库上对移动应用实现自动化测试

WBOY
WBOYOriginal
2016-06-10 15:14:562369Durchsuche

 采用Appium进行自动化的功能性测试最酷的一点是,你可以使用具有最适合你的测试工具的任何一门语言来写你的测试代码。大家选择最多的一个测试编程语言就是Python。 使用Appium和Python为iOS和Android应用编写测试代码非常容易。

在这篇博文中我们将详细讲解使用Appium下的Python编写的测试的例子代码对一个iOS的样例应用进行测试所涉及的各个步骤,而对Android应用进行测试所需的步骤与此非常类似。

开始,先自https://github.com/appium/appiumfork并clone Appium,然后按照安装指南,在你的机器上安装好Appium。


我还需要安装Appium的所有依赖并对样例apps进行编译。在Appium的工作目录下运行下列命令即可完成此任务:
 

$ ./reset.sh --ios

编译完成后,就可以运行下面的命令启动Appium了:
 

$ grunt appium

现在,Appium已经运行起来了,然后就切换当前目录到sample-code/examples/python。接着使用pip命令安装所有依赖库(如果不是在虚拟环境virtualenv之下,你就需要使用sudo命令):
 

$ pip install -r requirements.txt

接下来运行样例测试:
 

$ nosetests simple.py

既然安装完所需软件并运行了测试代码,大致了解了Appium的工作过程,现在让我们进一步详细看看刚才运行的样例测试代码。该测试先是启动了样例应用,然后在几个输入框中填写了一些内容,最后对运行结果和所期望的结果进行了比对。首先,我们创建了测试类及其setUp方法:
 

classTestSequenceFunctions(unittest.TestCase):
 
  defsetUp(self):
    app=os.path.join(os.path.dirname(__file__),
              '../../apps/TestApp/build/Release-iphonesimulator',
              'TestApp.app')
    app=os.path.abspath(app)
    self.driver=webdriver.Remote(
      command_executor='http://127.0.0.1:4723/wd/hub',
      desired_capabilities={
        'browserName':'iOS',
        'platform':'Mac',
        'version':'6.0',
        'app': app
      })
    self._values=[]

“desired_capabilities”参数用来指定运行平台(iOS 6.0)以及我们想测试的应用。接下来我们还添加了一个tearDown方法,在每个测试完成后发送了退出命令:
 

deftearDown(self):
  self.driver.quit()

最后,我们定义了用于填写form的辅助方法和主测试方法:

 

def_populate(self):
  # populate text fields with two random number
  elems=self.driver.find_elements_by_tag_name('textField')
  foreleminelems:
    rndNum=randint(0,10)
    elem.send_keys(rndNum)
    self._values.append(rndNum)
 
deftest_ui_computation(self):
  # populate text fields with values
  self._populate()
  # trigger computation by using the button
  buttons=self.driver.find_elements_by_tag_name("button")
  buttons[0].click()
  # is sum equal ?
  texts=self.driver.find_elements_by_tag_name("staticText")
  self.assertEqual(int(texts[0].text),self._values[0]+self._values[1])

就是这样啦!Appium的样例测试代码中还有许多Python的例子。如果你对使用Nose和Python来运行Appium测试有任何问题或看法,烦请告知。

Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn