19、Page Object 实例
2018-04-23 16:42 软件测试汪 阅读(195) 评论(0) 编辑 收藏 举报项目目录介绍:
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>