19、Page Object 实例
2024-09-06 06:19:35
项目目录介绍:
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>
最新文章
- 浅谈大数据神器Spark中的RDD
- commonJS — 全局操作(for Window)
- DropMaster
- Android笔记——Handler更新UI示例
- HDU 3790 最短路径问题【Dijkstra】
- c++实现输入法窗口自定义的代码
- keepalived深度结合lvs_dr模式
- 指路Reactive Programming
- 一个不错的Html5 DatePicker控件,主要支持手机端。
- Fiddler修改图片显示
- JAVA面对对象(四)——抽象类
- 构建eureka-server异常ClassNotFoundException: org.springframework.boot.context.embedded.FilterRegistrationBean
- Windows下SVN服务器搭建方法整理(apache)
- Swift - UITableView展开缩放动画
- GPS精度因子(GDOP,PDOP,HDOP,VDOP,TDOP)
- HIbernate编程模型
- Vscode 修改为中文语言
- CodeForces - 55D Beautiful numbers(数位DP+Hash)题解
- C#机器学习插件 ---- AForge.NET
- Mike