Test Android WAP pages with selenium automation

Source: Internet
Author: User
Tags testng

http://blogs.360.cn/360qtest/2014/04/01/%E5%88%A9%E7%94%A8selenium%E8%87%AA%E5%8A%A8%E5%8C%96%E6%B5%8B%E8%AF% 95android-wap%e9%a1%b5/

There are two main types of automated testing for smartphone-side applications: One is app-based APK automation test and the other is the WAP page test of the browser. At present, more is the first case, the application of automated testing framework is also more, such as Nativedriver, Robotium, Calabash, etc., and the second case of the automated testing framework is less, now more commonly used for the selenium.

and the test framework using Selenium has Selenium+junit, selenium+testng as the most commonly used. Here I would like to introduce the use of SELENIUM+TESTNG test mobile phone-side WAP page display.

The obvious drawback of this framework test is that it uses the Androiddriver driver to provide webview to display the content returned by the HTTP request and not to run on another browser, meaning that the browser's compatibility cannot be tested. But this can test different models of mobile phones, do adaptation testing. Because WebView uses the Android built-in Webkitt kernel, different models of the kernel are dissimilar.

The first step is to use the framework to test your mobile WAP pages, and you must import Selenium-server-standalone-2.25.0.jar and Testng.jar in your test project (Java project). At the same time, you need to install (Webdriver) android-server-xxx.apk as the server to distribute the HTTP request command and display the return content.

The second step, in the project, in the preparation of the above conditions, you can use the command to open the phone side of the Webdriver and connection server (start the mobile phone side of the Webdriver)-put in the test case before the execution, of course, you can also manually start Webdriver and run commands under DOS:

Such as

@BeforeTestpublic void SetUp () {//Start the webdriver--on the phone as the server, no other foreground application try {//Open webdriverruntime.getruntime (). EXEC ("adb Shell am start-a Android.intent.action.MAIN "+"-n org.openqa.selenium.android.app/. Mainactivity ");/* Connect the server side Webdriver, connect with a port of 8080, if the Local has to open or use the port, need to shut down (Http://localhost:8080/exit), Otherwise the connection is not on the server webdriver   */Runtime.getruntime (). EXEC ("adb forward tcp:8080 tcp:8080");} catch (IOException e) { E.printstacktrace ();} Driver = new Androiddriver ();}

The third step is to write test cases such as

When foreign data is used in a use case, it is passed as a parameter queryString1, and the source is Dataprovider = data in "TestData"

@Test (Dataprovider = "testdata") public    void TestQuery1 (String queryString1) {//Specific use case execution (including call slip and screenshot to get desired page)}

Fourth, after the use case is finished, the termination driver needs to be closed

@AfterTestpublic void TearDown () {driver.quit ();}

Second, about the testng use case with parameters in the data source acquisition, there are about three ways in testng: One is configured in the Testng.xml, with the @parameters reference, the second is the use of Dataprovider, the third is to inherit the Feedtest class to obtain data.

Here we mainly talk about the second and third kinds of use:

1. When using Dataprovider, there will be @test (Dataprovider = "TestData") before the use case method, such as

@Test (Dataprovider = "testdata") public void TestQuery1 (String queryString1) {try {//Unified encoding Format query = Urlencoder.encode ( Querystring1.trim (), "Utf-8");} catch (Unsupportedencodingexception E1) {//TODO auto-generated catch Blocke1.printstacktrace ();} Specific use-Case execution}

At the same time, there is a way to get the data source, such as the following example, read the data from the TXT file, and put in filelist, in order to dataprovider convenient execution, return the list of iterators. When reading the data, there is an encoding encoding processing, this is to ensure that the TXT file storage format (UTF-8) is consistent, the use of the data used in the execution of the case does not appear inconsistent data representation. Of course, in order to be anywhere consistent, the use case for data is also best handled, as above.

@DataProvider (name = "TestData") public iterator<string[]> getData () {arraylist<string[]> filelist = new Arraylist<string[]> (); string filepath = data.getfilestring (); try {String encoding = "UTF-8"; File File = new file (filepath), if (File.isfile () && file.exists ()) {//To determine if files exist inputstreamreader read = new InputS Treamreader (new FileInputStream (file), encoding);//considering the encoding format BufferedReader BufferedReader = new BufferedReader (read); String linetxt = ""; while ((Linetxt = Bufferedreader.readline ()) = null) {string[] str = new String[1];str[0] = linetxt.t Rim (); Filelist.add (str);} Read.close ();} else {System.out.println ("The specified file cannot be found");}} catch (Exception e) {System.out.println ("Error reading file contents"); E.printstacktrace ();} return Filelist.iterator ();}

2, when using Feedtest to obtain data, you must import the jar package in the feed4testng-1.0-dist to use, and then inherit the Feedtest class in the test class, this can automatically read the data, now supports automatically read the. xls and. cvs files

Such as

public class Onetest extends Feedtest {     @Test (Dataprovider = "feeder")     //Data source Excel file xls (version 97-2003)    @ The Source ("D:/case/testcase2.xls") public void TestQuery1 (String queryString1) {/* parameter is the first column reading the XLS file 、、、, the first row of data cannot be read, That's because the default data field name is  */     }}

Using this method to read the data, we can no longer write our own method to read the data. The disadvantage is that the path to the data source is fixed, and it is not possible to change the source of the data at run time.

In addition to supporting the two files can be @source automatically read, you can read other types of files and databases, in particular, see:

Http://databene.org/feed4testng.html

Third, for the Automated Test WAP page display problem, when the display page to be tested by entering a search word in the page query return results, and query more. In this case we manually input and then look at the page display, the workload is larger. So we need automated testing (using selenium) so that we don't have to manually enter query and then swipe the page to see the page, just look at the cut-off page to show the picture. The automation that we want to implement has the search word to display, slide the page to see in the viewport, the screenshot.

    1. For the search term query, there is a note: The search term can not have Chinese, such as Element.sendkeys ("BYD"); just can't, webdriver when the input method, only English input that interface. The way to solve this problem is to put the search word query in the URL, such as Driver.get ("http://m.so.com/s?q=" + "BYD");
    2. For the results page of the search term, the current screen is not necessarily the required display, and we want to intercept the screen, but also only to intercept the current display, then you can only slide the page to the current display of the interface.

There are two ways to slide: both methods depend on the elements element of the positioning

For example, the following code indicates that I want to find some kind of display of the search term for "car" in the test category, and the result shows the number of bars >=1.

The method can be changed according to the test requirements, (what elements to navigate to) public webelement scrollcondition (webdriver driver, String queryclass) {//Find out whether the current display to reach the required display, If not, swipe if (!queryclass.isempty ()) {if (Queryclass.contains ("Car")) {//Auto class elements = Driver.findelements (By.xpath ("//h1 [@class = ' car-title icon-mobile ']);}} if ((Elements! = null) && (elements.size () > 0)) {element = Elements.get (0);}}

Method One:

This method does not stop after sliding, is not conducive to screen capture, do not recommend the use of      actions action=new actions (driver);    Action.movetoelement (Element). Perform ();

Method Two:

((Javascriptexecutor) driver). Executescript ("Arguments[0].scrollintoview ();", Element);

Screenshots can be intercepted using Ddmlib (Ddmlib.jar required)

http://blogs.360.cn/360qtest/2014/02/28/through Ddmlib and access to mobile phone-related information/

Test Android WAP pages with selenium automation

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.