19、Page Object 实例

项目目录介绍:

 

CalcuatorPage.java文件代码:

package example;

import io.appium.java_client.android.AndroidDriver;
import org.openqa.selenium.By;

public class CalcuatorPage {

    public AndroidDriver driver;

    // 构造方法
    public CalcuatorPage(AndroidDriver driver) {
        this.driver = driver;
    }

    // 加号
    public void add(){
        this.driver.findElement(By.id("com.android.calculator2:id/op_add")).click();
    }

    // 减号
    public void sub(){
        this.driver.findElement(By.id("com.android.calculator2:id/op_sub")).click();
    }

    // 乘号
    public void mul(){
        this.driver.findElement(By.id("com.android.calculator2:id/op_mul")).click();
    }

    // 除号
    public void div(){
        this.driver.findElement(By.id("com.android.calculator2:id/op_div")).click();
    }

    // 删除
    public void del(){
        this.driver.findElement(By.id("com.android.calculator2:id/del")).click();
    }

    // 清除结果
    public void clr(){
        this.driver.findElement(By.id("com.android.calculator2:id/clr")).click();
    }

    // 等号
    public void eq(){
        this.driver.findElement(By.id("com.android.calculator2:id/eq")).click();
    }

    // 结果
    public String result(){
        String result = this.driver.findElement(By.id("com.android.calculator2:id/result")).getText();
        return result;
    }
    public void number_1(){
        this.driver.findElement(By.id("com.android.calculator2:id/digit_1")).click();
    }

    public void number_2(){
        this.driver.findElement(By.id("com.android.calculator2:id/digit_2")).click();
    }
    public void number_3(){
        this.driver.findElement(By.id("com.android.calculator2:id/digit_3")).click();
    }
    public void number_4(){
        this.driver.findElement(By.id("com.android.calculator2:id/digit_4")).click();
    }
    public void number_5(){
        this.driver.findElement(By.id("com.android.calculator2:id/digit_5")).click();
    }
    public void number_6(){
        this.driver.findElement(By.id("com.android.calculator2:id/digit_6")).click();
    }
    public void number_7(){
        this.driver.findElement(By.id("com.android.calculator2:id/digit_7")).click();
    }
    public void number_8(){
        this.driver.findElement(By.id("com.android.calculator2:id/digit_8")).click();
    }
    public void number_9(){
        this.driver.findElement(By.id("com.android.calculator2:id/digit_9")).click();
    }
    public void number_0(){
        this.driver.findElement(By.id("com.android.calculator2:id/digit_0")).click();
    }


}


CalculatorTest.java
文件代码
package example;
import io.appium.java_client.android.AndroidDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.testng.annotations.AfterClass;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

import static org.testng.Assert.assertEquals;
import java.net.MalformedURLException;
import java.net.URL;

public class CalculatorTest {

    private AndroidDriver driver;
    private CalcuatorPage calPage;

    @BeforeClass
    public void startApp() throws MalformedURLException {
        DesiredCapabilities capabilities = new DesiredCapabilities();
        capabilities.setCapability("deviceName", "J1");
        capabilities.setCapability("automationName", "Appium");
        capabilities.setCapability("platformName", "Android");
        capabilities.setCapability("platformVersion", "6.0");
        capabilities.setCapability("appPackage", "com.android.calculator2");
        capabilities.setCapability("appActivity", ".Calculator");

        this.driver = new AndroidDriver(new URL("http://127.0.0.1:4723/wd/hub"),
                capabilities);
        this.calPage = new CalcuatorPage(this.driver);
    }

    // 关闭APP
    @AfterClass
    public void closeApp(){
        driver.quit();
    }

    // 清空结果
    @AfterMethod
    public void clearResult(){
        this.calPage.clr();
    }

    @Test
    public void testAdd(){
        this.calPage.number_1();
        this.calPage.add();
        this.calPage.number_1();
        this.calPage.eq();
        String result = this.calPage.result();
        assertEquals(result, "2");
    }

    @Test
    public void testSub(){
        this.calPage.number_5();
        this.calPage.sub();
        this.calPage.number_3();
        this.calPage.eq();
        String result = this.calPage.result();
        assertEquals(result, "2");
    }

}

 pom.xml 文件

<?xml version="1.0" encoding="UTF-8"?>
<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.test1.cn</groupId>
    <artifactId>testC</artifactId>
    <version>1.0-SNAPSHOT</version>


    <dependencies>
        <!-- https://mvnrepository.com/artifact/org.testng/testng -->
        <dependency>
            <groupId>org.testng</groupId>
            <artifactId>testng</artifactId>
            <version>6.14.3</version>
            <scope>test</scope>
        </dependency>


        <dependency>
            <groupId>io.appium</groupId>
            <artifactId>java-client</artifactId>
            <version>3.2.0</version>
            <scope>test</scope>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <encoding>UTF-8</encoding>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.21.0</version>
                <configuration>
                    <suiteXmlFiles>
                        <suiteXmlFile>testng.xml</suiteXmlFile>
                    </suiteXmlFiles>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

 

转载于:https://www.cnblogs.com/suim1218/p/8918564.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值