App Appium& Browser SeleniumTesting

1、Appium 是什么?

Appium是一个开源的,适用于原生或混合移动应用( hybrid mobile apps )的自动化测试工具,Appium应用WebDriver: JSON wire protocol驱动安卓和iOS移动应用。

2、Appium的特点:

支持多平台(Android、iOS等)

支持多语言(python、java、ruby、js、c#等)

Appium是跨平台的,可以用在OSX,Windows以及Linux桌面系统上运行。

Appium选择了Client/Server的设计模式。只要client能够发送http请求给server,那么的话client用什么语言来实现都是可以的,这就是如何做到支持多语言的原因;

Appium扩展了WebDriver的协议,没有自己重新去实现一套。这样的好处是以前的WebDriver API能够直接被继承过来,以前的Selenium(WebDriver)各种语言的binding都可以拿来就用,省去了为每种语言开发一个client的工作量。

3、Appium的哲学:

不需要重新编译或者修改应用

不被一种语言或者框架约束

不重复造轮子

开源免费

4、Appium原理:

原理图:

image

Appium是c/s架构的 ;

Appium是基于webdriver协议添加了对移动设备自动化api扩展而成的, webdriver是基于http协议的,第一连接会建立一个session会话,并通过post发送一个json告知服务端相关测试信息 。

Appium会首先开启一个监听4723端口的server,接收测试脚本发送过来的对应请求,再将对应的请求发送给中间件Bootstrap.jar(比如点击一个APP中的一个Button就是一条请求)。

Bootstrap.jar:

监听4724端口由Appium发送过来的相关请求,并且将请求转换成UiAutomator可以识别的命令发给UiAutomator进行处理。

那么Bootstrap作用是什么?

Bootstrap是Appium在初始化的时推送到Android手机上的一个UiAutomator测试脚本,该脚本的唯一一个测试方法所做的事情是在手机端开启一个SocketServer(通信模块),用来监听Appium从PC端过来的命令发送给UiAutomator来执行处理。它会监听4724端口获得命令然后传递给UiAutomator来做处理。

Bootstrap是uiautomator的测试脚本,它的入口类bootstrap继承于UiautomatorTestCase, 所以Uiautomator可以正常运行它 ,它也可以正常使用uiautomator的方法,这个就是appium的命令可以转换成uiautomator命令的关键;

其次,bootstrap是一个socket服务器,专门监听4724端口过来的appium的连接和命令数据,并把appium的命令转换成uiautomator的命令来让uiautomator进行处理;

总体来说:

AppiumBootstrap是一个jar包; Appium会将这个jar包打到device中; 然后使用adb shell uiautomator runtest AppiumBootstrap.jar -c io.appium.android.bootstrap.Bootstrap会使用一个SocketServer用于监听发送过来的命令; 得到命令之后,扔给Uiautomator执行;

Selenium是一个用于Web应用程序自动化测试工具。Selenium测试直接运行在浏览器中,就像真正的用户在操作一样。支持的浏览器包括IE(7, 8, 9, 10, 11),Mozilla Firefox,Safari,Google Chrome,Opera等。

主要功能包括:测试与浏览器的兼容性——测试你的应用程序看是否能够很好得工作在不同浏览器和操作系统之上。

测试系统功能——创建回归测试检验软件功能和用户需求。支持自动录制动作和自动生成 .Net、Java、Perl等不同语言的测试脚本。

Selenium也是一款同样使用Apache License 2.0协议发布的开源框架。

1、****支持平台

WebDriver支持Android和BlackBerry两个移动平台的浏览器测试。Android目前为市场占有率第一的移动平台,对于在其上面进行自动化测试,推荐Appium,Appium扩展了WebDriver的协议,支持ios平台和Android平台上的原生应用、Web应用和混合应用等。

2、支持浏览器

WebDriver 目前所支持的浏览器包括:Firefox、Chrome、IE、Edge、Opera、Safari. 为什么会选择上面几款浏览器进行支持呢?主要与浏览器的内核有关。

3、支持模式

HtmlUnit和PhantomJS是两个比较特殊的模式,我们可以把它们看作是伪浏览器,在这种模式下支持htmlJava Saript等的解析,但不会真正地渲染出页面。由于不进行CSS及GUI渲染,所以运行效率上要比真实的浏览器快很多,主要用在功能性测试上面。

在开始appium元素定位的话题之前,首先让我们了解下appium的一些基础知识:

一、测试对象

appium 是一个自动化测试开源工具,支持iOS和 Android平台上的原生应用,web应用和混合应用。

移动原生应用:单纯用ios或者android开发语言编写的、针对具体某类移动设备、可直接被安装到设备里的应用,一般可通过应用商店获取;

移动web应用:使用移动浏览器访问的应用(appium支持iOS上的Safari和Android上的 Chrome),不需要下载到设备上,而是通过浏览器直接访问;

混合应用:同时使用网页语言与程序语言开发,通过应用商店区分移动操作系统分发,用户需要安装使用的移动应用。

二、支持平台及语言

appium是跨平台的,支持OSX,Windows以及Linux系统。它允许测试人员在不同的平台(iOS,Android)使用同一套API来写自动化测试脚本,这样大大增加了iOS和Android测试套件间代码的复用性 appium支持多语言,采用C/S设计模式,只要满足client能够发送http请求给server即可

三、元素定位

1.格式:find_element_by_定位方式(value)

我根据手机自带的计算器来解释

image

通过id定位

通过工具获取到按钮的 resource-id

driver.find_element_by_id('resource-id')

实例: driver.find_element_by_id(' com.android.bbkcalculator:id/clear')

通过class定位

通过工具获取到按钮的class

driver.find_element_by_class_name('class')

实例: driver.find_element_by_class_name(' android.widget.ImageButton ')

通过text定位

appium1.5及之后的版本废弃了name属性(如name=百度一下,需要使用uiautomator的定位方式,使用text的内容)

driver.find_element_by_android_uiautomator("text(\中文\)")

实例:driver.find_element_by_android_uiautomator ("text("百度一下")")

备注:需要确认这个中文是当前页面唯一值,否则还是定位不到,注意写法,不是直接把中文复制进去

通过xpath定位

driver.find_element_by_xpath("xpath内容 ")

实例:driver.find_element_by_xpath("//android.widget.FrameLayout/android.widget.ImageButton[4]")

以下方式暂时没有用到,如果有问题以后会更改

通过accessibility_id

在UI Automator Viewer界面上并没有找到这个字段,这个对应的字段是content-desc

driver.find_element_by_accessibility_id("accessibility_id")

没有找到实例,要是自己写自动化的APP碰到了,可以用用

通过css_selector定位(webview)

只适用于webview的html页面,继承自webdriver,与pc版本的UI测试一致

driver.find_element_by_css_selector()

通过link_text定位(webview)

只适用于webview容器中的html页面,继承自webdriver,与pc版本的UI测试一致

driver.find_element_by_link_text()

通过name定位

只适用于webview容器中的html页面,继承自webdriver,与pc版本的UI测试一致

driver.find_element_by_name()

2.定位元素的另一种写法:find_element(by,value)

find_element_by_方式(value)实际调用的都是find_element(by,value)

需要导入这个包:from selenium.webdriver.common.by import By

例如:定位id为ag2的元素

方式一:driver.find_element_by_id("ag2”)

方式二:driver.find_element(By.ID,"ag2")

这个操作的好处是可以直接把操作的by和value放到一个元组里,然后调用通用方法来传参获得元素结果。

cateid=(By.ID,"ag2")

driver.find_element(*cateid).click()

by的操作可以是:

By.ID 相当于by_id

By.CLASS_NAME 相当于by_class_name

By.XPATH 相当于by_xpath

By.NAME 相当于by_name

By.TAG_NAME 相当于by_tag_name

By.CSS_SELECTOR 相当于by_css_selector

By.LINK_TEXT 相当于by_link_text

3.find_elements_by_定位方式(value)返回元素数组

用法与find_element_by_方式(value)一致,但是返回一个数组。可以通过数组的索引来访问具体的某个结果。

例如:通过class_name定位到多个元素,我想点击第一个元素

driver.find_elements_by_class_name("android.widget.RelativeLayout”)[0].click()

4.返回元素数组的另一种写法:find_elements(by,value)

用法与find_element(by,value)一致,但是返回一个数组。可以通过数组的索引来访问具体的某个结果。

例如:通过class_name定位到多个元素,我想点击第一个元素

driver.find_elements(By.CLASS_NAME,"android.widget.RelativeLayout”)[0].click()

5.通过元素定位元素

可以先找到某个元素,然后再进一步定位元素

find_element_by_class_xpath(“xxx”).find_element_by_name(“yyy")

四、元素操作

找到元素后可以对元素进行的操作,例如上面讲的进一步定位元素

1.click()

//点击操作

也可以用tab实现点击操作

driver.find_element_by_id("com.wuba.zhuanzhuan:id/ae8").click()

2.clear()

//清空输入框内容

driver.find_element_by_id("com.wuba.zhuanzhuan:id/ij").clear()

3.send(xx)

//输入框内输入内容

driver.find_element_by_id("com.wuba.zhuanzhuan:id/ij").send_keys("test content")

4.text

//获得元素的text内容

print(driver.find_element_by_xpath(" //android.widget.LinearLayout[1]//xxx").text)

四、总结

1、Appium跨平台,支持Android和IOS自动化测试。

2、Appium支持多语言,如java、Object-C、JavaScript、Php、Python、Ruby、C#、Clojure等等

3、支持原生应用,web应用和混合应用

4、仅仅支持UI测试

一、Selenium的前世今生:

04年,Selenium诞生,并正式命名为Selenium Core。

Selenium Core是基于浏览器并且采用JavaScript编程语言的测试工具,运行在浏览器的安全沙箱中,设计理念是将待测试产品、Selenium Core和测试脚本均部署到同一台服务器上

来完成自动化测试的工作。

05年,Selenium RC诞生,即Selenium 1。这个时候,Selenium Core其实是Selenium RC的核心。

Selenium RC让待测试产品、Selenium Core和测试脚本三者分散在不同的服务器上。(测试脚本只关心将HTTP请求发送到指定的URL上,selenium本身不需要关心HTTP请求由于什么程序编程语言编写而成)

Selenium RC包括两部分:一个是Selenium RC Server,一个是提供各种编程语言的客户端驱动来编写测试脚本。

Selenium RC的API组织采用字典形式。

image

07年,WebDriver诞生。WebDriver的设计理念是将端到端测试与底层具体的测试工具分隔离,并采用设计模式Adapter适配器来达到目标。

WebDriver的API组织更多的是面向对象。

image

09年,Selenium RC和WebDriver合并,即Selenium 2,简称Selenium WebDriver。主要特性是将WebDriver API集成进Selenium RC。

合并后,支持多语言绑定; 支持谷歌、火狐、IE,还支持嵌入式设备上基于webKit内核的浏览器,如android、iOS的浏览器。

image

09年后,Selenium 3诞生。

Selenium 3会移除原有的Selenium Core的实现部分,并且Selenium RC的API也将被去掉。其它一些变化包括但不限于以下内容:

image

二、Selenium目前主要包括以下几个套件:

工具 描述
Selenium IDE IDE是一个Firefox插件,可以录制用户的基本操作,生成测试用例。随后可以运行这些测试用例在浏览器里回放,可将测试用例转换为其他语言的自动化脚本。
Selenium RC RC为核心部分。它使用的编程语言,如Java,C#,PHP,Python、Ruby和Perl强大功能来创建更复杂的测试。Selenium RC 分 为 C l i e n t Libraries(编写测试脚本) 和 Selenium Server(控制浏览器行为)。
Selenium WebDriver WebDriver前身是Selenium RC,可以看作是Selenium RC的替代品,直接发送命令给浏览器,并检索结果。2
Selenium Grid 网格用于运行在不同的机器,不同的浏览器并行测试的工具,目的在于加快测试用例运行的速度,从而减少测试运行的总时间。利用Grid可以很方便地实现在多台机器上和异构环境中运行测试用例。

三、Selenium IDE的优点如下:

image

四、selenium与嵌入式:

image

五、WebDriver与Selenium RC的区别:

image

六、使用:

能把Selenium RC脚本和JUnit单元测试结合起来,既能涵盖功能测试,又能涵盖数据或后台Java类测试,从而构成一个完整的Web应用测试解决方案。

首先要安装JRE,从官网(文后参考资料)下载Selenium RC的程序Jar包,存放在硬盘的某个目录中,如E:/Selenium/ 。

启动Selenium RC服务,启动cmd模式,进入到selenium RC所在的目录后,输入: java -jar selenium-server.jar 。启动完成后,可以看到以下信息,即表示selenium server启动成功。

七、SeIenium的特点如下:

  • 开源,免费;
  • 多浏览器支持:Firefox、Chrome、IE、Opera、Edge;
  • 多平台支持:Linux、Windows、MAC;
  • 多语言支持:Java、Python、Ruby、C#、JavaScript、C++;
  • 对…出页面有良好的支持;
  • 简单(API简单)、灵活(用开发语言驱动> ;
  • 支持分布式测试用例执行*

Appium is the most popular open-source framework for mobile app automation testing. It allows QAs to automate tests for popular mobile platforms like Android, iOS, and Windows. Appium uses the mobile JSON wire protocol (an extension of Selenium JSON wire protocol) to drive native, mobile web and hybrid applications.

technologies continue to evolve rapidly, organizations must leverage all the possible ways to increase efficiency, enhance productivity, and minimize costs. More organizations have started adopting an agile approach to software development for this purpose. As a result, automating test cycles has become mandatory for testing teams.

Automation testing helps organizations release new features for apps and web apps faster. Test automation frameworks like Selenium and Appium have become buzzwords in the test community for this very reason.

This article aims to highlight the key differences between Selenium vs Appium. So let’s begin.

Monolithic vs. Microservices Architecture

Microservices are an architectural and organizational approach to software development where software is composed of small independent services that communicate over well-defined APIs. These services are owned by small, self-contained teams.Microservices architectures are an important software trend and one that can have profound implications on not only enterprise IT, but the digital transformation of entire businesses. Microservices architectures make applications easier to scale and faster to develop, enabling innovation and accelerating time-to-market for new features.

With monolithic architectures, all processes are tightly coupled and run as a single service. This means that if one process of the application experiences a spike in demand, the entire architecture must be scaled. Adding or improving a monolithic application’s features becomes more complex as the code base grows. This complexity limits experimentation and makes it difficult to implement new ideas. Monolithic architectures add risk for application availability because many dependent and tightly coupled processes increase the impact of a single process failure. With a microservices architecture, an application is built as independent components that run each application process as a service. These services communicate via a well-defined interface using lightweight APIs.

First, let’s compare microservices vs monolithic architecture. A monolithic application is built as a single unit. Enterprise applications are built in three parts:

  • A database — consisting of many tables usually in a relational database management system
  • A client-side user interface — consisting of HTML pages and/or JavaScript running in a browser)
  • A server-side application — which will handle HTTP requests, execute domain-specific logic, retrieve and update data from the database, and populate the HTML views to be sent to the browser.

This is what makes a monolith architecture monolith — it is a single logical executable. To make any changes to the system, a developer must build and deploy an updated version of the server-side application.

In contrast to a monolithic architecture, microservices’ capabilities are expressed formally with business-oriented APIs. They encapsulate a core business capability and the implementation of the service — which may involve integrations with systems of record — is completely hidden as the interface is defined purely in business terms. The positioning of services as valuable assets to the business implicitly promotes them as adaptable for use in multiple contexts. The same service can be reused in more than one business process or over different business channels or digital touchpoints.

Finally, As a service architecture, the microservices architecture has become increasingly popular. But your own business context, evaluated against the above pros and cons, is essential to deciding if you should start with monolith or microservices. For a lightweight application, a monolithic system often suits better. For a complex, evolving application with clear domains, the microservices architecture will be the better choice. A microservices approach encapsulates each business capability into individual services. Every application process functions as a separate, loosely coupled service with its own logic and database. Updating, deployment, testing and scaling happens within the scope of each service.
Appium is an open-source framework that allows QAs to conduct automated app testing on different platforms like Android, iOS, and Windows.

It automates testing for:

  • Native Mobile Applications that are written using iOS, Android, or Windows SDKs
  • Mobile Web Applications that can be accessed using mobile browsers such as Safari, Chrome, or in-built native browser applications for android devices
  • Hybrid Mobile Applications that have a native wrapper around the web view

Appium is a cross-platform testing framework that is flexible, enabling testers to write test scripts against multiple platforms such as iOS, Windows, and Android using the same API. That means QAs can use the same code for iOS as for Android, time and effort.

Similar to Selenium, Appium allows QAs to write test scripts in different programming languages which include Java, JavaScript, PHP, Ruby, Python, and C#.

brew install node # get node.js
npm install -g appium # get appium
npm install wd # get appium client
appium & # start appium
node your-appium-test.js

Architecture of the Appium Framework

Appium is an HTTP server that is written on the Node.js platform whose set up comprises a REST (Representational State Transfer) API and implements Selenium WebDriver. It works on a client/server architecture. Appium allows the uses of the available WebDriver client for firing the tests.

The REST API performs the following actions:

  • Receives the connection from the client-side
  • Listens to the command
  • Executes the command on a mobile device
  • Returns the command execution status as an HTTP response to the client
Appium Testing Framework

Source: Gitbooks

Appium allows automation in the context of a session. The client initiates this session as per the client library (Java, JavaScript, PHP, Ruby, Python, and C#), which ends up sending a POST /session request to the server with the help of a JSON object that is termed as “desired capabilities” object.

It is then that the server starts the automation session and responds with a session ID that is further used for sending other commands relevant to the given session.
When it comes to mobile app testing, regardless of whether the application is native, hybrid, or mobile web, running on Android, iOS, or Windows, Appium remains the first choice for app automation testing. Its popularity is due to a number of features:

  • It is a server and runs in the background
  • It can be synchronized with the TestNG testing framework offering a wider range of features
  • With an in-built UIAutomator, Appium is capable of producing detailed information logs and has a detailed reporting structure for better analysis of test results better and improved debugging
  • It offers the flexibility to write test code in different languages (Java, JavaScript, PHP, Ruby, Python, and C#)
  • It offers the reusability of the same code for different device platforms such as iOS, Android, and Windows. This saves a lot of time and effort
  • It offers QAs full control of the back-end APIs and databases using the test code. This helps evaluate defects that comes from the back-end APIs or databases
  • Testers do not have to modify or recompile the app under test in any way, as Appium uses the standard automation APIs on all platforms. They don’t need to access the source code of the application under test
  • It allows testing of mobile applications on emulators or simulators or real devices, offering flexibility to choose their testing device and environment. However, bear in mind that emulators and simulators are highly inadequate for final-stage testing. They cannot replicate many devices features such as low battery or unstable network – which is necessary to optimize the app for unsuitable situations. Only real device testing can offer conclusive, accurate results and make an app suitable for working in real user conditions.
  • It offers cross-platform compatibility, enabling the same tests to run on multiple platforms widening the coverage
  • It offers real-life monitoring of tests making their execution more robust
  • Appium allows parallel execution of test automation scripts on different Android or iOS sessions using UIAutomator, UIAutomation, or Xcode9. This speeds up the testing process and ensures scalability

With so many amazing features, Appium is an automation testing framework that stands out as the best choice for mobile application testing.

你可能感兴趣的:(App Appium& Browser SeleniumTesting)