在Appium-java中寻找良好的编码格式以进行完整的应用测试 [英] Looking for a good Coding format in Appium-java for Full app testing

查看:79
本文介绍了在Appium-java中寻找良好的编码格式以进行完整的应用测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

关于Appium脚本的一些疑问 1.我上面尝试过的脚本编写是否正确? 2.完成quickpay之后,我要执行下一个方案,那么如何重定向到下一个班来进行下一个方案? 我尝试的代码如下 基类

some doubts regarding Appium Scripting 1.Scripting i tried above is in a proper way? 2.After completing the quickpay, i want to do the next scenario so how to redirect to next class for do the next scenario? Code i tried is given below Base class

   package ********;

import io.appium.java_client.android.AndroidDriver;    
import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;


import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.remote.CapabilityType;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;

public class ************* {
     protected AndroidDriver driver;
        protected WebDriverWait wait;

        //before Test Annotation makes a java function to run every time before a TestNG test case
        @BeforeTest
        protected void createAppiumDriver() throws MalformedURLException, InterruptedException {

              DesiredCapabilities capabilities = new DesiredCapabilities();
              capabilities.setCapability("deviceName", "0123456789ABCDEF");
              capabilities.setCapability(CapabilityType.BROWSER_NAME, "Android");
              capabilities.setCapability(CapabilityType.VERSION, "5.0");
              capabilities.setCapability("platformName", "Android");
              capabilities.setCapability("appPackage", "**********);
              capabilities.setCapability("appActivity", "********");
              capabilities.setCapability("unicodekeyboard", true);
              capabilities.setCapability("resetkeyboard", true);
              driver = new AndroidDriver(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);          
              driver.manage().timeouts().implicitlyWait(50, TimeUnit.SECONDS);
              WebDriverWait wait = new WebDriverWait(driver, 300);
              wait.until(ExpectedConditions.elementToBeClickable(By.className("android.widget.FrameLayout")));
        }

        //After Test Annotation makes a java function to run every time after a TestNG test case
        @AfterTest
        public void afterTest(){

        //quit the driver
        driver.quit();
        }

    }

屏幕课程

package **********;

        import io.appium.java_client.MobileBy;

        import org.openqa.selenium.By;
        import org.openqa.selenium.support.ui.ExpectedConditions;
        import org.testng.annotations.Test;


        public class QuickPay extends ************ {
            /*--------------------------------Enter pin Starts---------------------------------*/

             //Enter 4 digit pin to login
             @Test
             public void T4a_Login() {      
                driver.findElement(By.xpath("//android.view.View[contains(@resource-id,'ext-button-1')]")).sendKeys("1234");
             } 


        /*--------------------------------Enter pin Ends---------------------------------*/


        /*QuickPay Starts*/

        /*--------------------------------Quick pay to Federal Bank Account starts---------------------------------*/

             //Click on quick pay
             @Test
             public void T5a_QuickpayF() {      
                driver.findElement(By.xpath("//android.view.View[contains(@resource-id,'ext-button-30')]")).click();    
             } 

             //Enter account number , Amount and click quick pay
             @Test
             public void T5b_QuickpayF() {      
                driver.findElement(By.xpath("//android.widget.EditText[contains(@resource-id,'ext-element-229')]")).sendKeys("10015000301404"); 
                driver.findElement(By.xpath("//android.widget.EditText[contains(@resource-id,'ext-element-236')]")).sendKeys("50"); 
                driver.hideKeyboard();  
                driver.findElement(By.xpath("//android.view.View[contains(@resource-id,'ext-button-34')]")).click();
             }

             //Click on confirm button
             @Test
             public void T5c_QuickpayF() {      
                 driver.findElement(By.xpath("//android.view.View[contains(@resource-id,'ext-button-38') and @index='0']")).click();
             }

             //Enter pin for Quick paya-Federal bank
             @Test
             public void T5d_QuickpayF() {      
                 driver.findElement(By.xpath("//android.view.View[contains(@resource-id,'ext-button-45') and @index='1']")).click();    
                 driver.findElement(By.xpath("//android.view.View[contains(@resource-id,'ext-button-46') and @index='2']")).click();
                 driver.findElement(By.xpath("//android.view.View[contains(@resource-id,'ext-button-47') and @index='3']")).click();
                 driver.findElement(By.xpath("//android.view.View[contains(@resource-id,'ext-button-48') and @index='4']")).click();
             }

             //Click on home for redirect to Home page
             @Test
             public void T5e_QuickpayF() {  
                 System.out.println("testtt");  
                 driver.findElement(By.xpath("//android.view.View[contains(@resource-id,'ext-button-60')and @index='0']")).click();
                 System.out.println("rt");
             }



        /*--------------------------------Quick pay to Federal Bank Account Ends---------------------------------*/

        }

PoM.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.aws.appium</groupId>
  <artifactId>appium-android-test</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>jar</packaging>

 <build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
             <artifactId>maven-jar-plugin</artifactId>
             <version>2.6</version>
             <executions>
                <execution>
                     <goals>
                        <goal>test-jar</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
             <artifactId>maven-jar-plugin</artifactId>
             <version>2.6</version>
            <executions>
                 <execution>
                     <goals>
                         <goal>test-jar</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
 </build>
   <properties>
        <appium.version>3.3.0</appium.version>
        <testng.version>6.9.10</testng.version>
    </properties>

    <dependencies>

        <!-- Appium -->
        <dependency>
            <groupId>io.appium</groupId>
            <artifactId>java-client</artifactId>
            <version>${appium.version}</version>
        </dependency>

        <!-- testng -->
        <dependency>
            <groupId>org.testng</groupId>
            <artifactId>testng</artifactId>
            <version>${testng.version}</version>
            <scope>test</scope>
        </dependency>

    </dependencies>

</project>

推荐答案

您的测试难以维护和阅读.您应该阅读有关页面对象模式的信息,简而言之,它通过行为将定义已测试应用程序屏幕上的元素的部分分开,并检查您要对此元素执行的操作.

Your test is hard to maintain and to read. You should read about the Page Object Pattern, in short it splits the part of defining the elements that are on the screen of the tested app with the behaviours and checks that you want to perform with this elements.

基本上,而不是每次都调用.findByXPath()(这将使您需要维护数十个或几百个步骤),而是要调用一些描述要实现的操作的方法. loginPage.setUsername()其中loginPage是您在其中定义所有元素的类. @FindBy(xpath = "//android.some.xpath") MobileElement loginField和方法public void setUsername()这样,当应用程序中发生某些更改时,您只更改类中的一个方法,而不更改所有测试.

Basically instead of calling .findByXPath() every time (which at point when you will have dozens or hundreds of steps will get hell to maintain), you want to call some methods which describe action you want to achieve f.e. loginPage.setUsername() where loginPage is the class where you defined all the elements f.e. @FindBy(xpath = "//android.some.xpath") MobileElement loginField and methods public void setUsername() This way, when something changes in the app you only change one method in class and not all your tests.

页面对象的应用示例

硒最佳实践指南

这篇关于在Appium-java中寻找良好的编码格式以进行完整的应用测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆