JavaApp自动化测试系列[v1.0.0][常见APP操作处理]
2023-12-20 17:55:18
[截屏操作]
Java结合TestNG单元测试框架,实现移动端截屏操作【源码】
/*
* @FileName Test_CapturingScreenshots:
* @author davieyang
* @create 2018-11-22 13:48
*/
package testscript;
import io.appium.java_client.android.AndroidDriver;
import io.appium.java_client.remote.AndroidMobileCapabilityType;
import io.appium.java_client.remote.MobileCapabilityType;
import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.concurrent.TimeUnit;
import org.apache.commons.io.FileUtils;
import org.openqa.selenium.By;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
public class Test_CapturingScreenshots {
AndroidDriver driver;
@BeforeClass
public void setUp() throws MalformedURLException{
DesiredCapabilities caps = new DesiredCapabilities();
caps.setCapability(MobileCapabilityType.PLATFORM_VERSION, "4.4");
caps.setCapability(MobileCapabilityType.PLATFORM_NAME, "Android");
caps.setCapability(MobileCapabilityType.DEVICE_NAME,"Moto X");//I am using Moto X as a Real Device
caps.setCapability(AndroidMobileCapabilityType.APP_PACKAGE,"com.android.calculator2");
caps.setCapability(AndroidMobileCapabilityType.APP_ACTIVITY,"com.android.calculator2.Calculator");
driver = new AndroidDriver (new URL("http://127.0.0.1:4723/wd/hub"), caps);
driver.manage().timeouts().implicitlyWait(30,TimeUnit.SECONDS);
}
@Test
public void testExample() throws IOException{
WebElement five=driver.findElement(By.name("5"));
File screenShot=driver.getScreenshotAs(OutputType.FILE);
File location=new File("screenshots");
String screenShotName=location.getAbsolutePath()+File.separator+"testCalculator.png";
FileUtils.copyFile(screenShot,new File(screenShotName));
}
@AfterClass
public void tearDown(){
driver.closeApp();
}
}
[滑动操作]
Java结合TestNG单元测试框架,实现移动端滚动操作【源码】
/*
* @FileName Test_Scroll: the script of testing Scroll
* @author davieyang
* @create 2018-11-22 10:04
*/
package testscript;
import io.appium.java_client.TouchAction;
import io.appium.java_client.android.AndroidDriver;
import io.appium.java_client.remote.AndroidMobileCapabilityType;
import io.appium.java_client.remote.MobileCapabilityType;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
public class Test_Scroll_Swipe {
AndroidDriver driver;
@BeforeClass
public void setUp() throws MalformedURLException{
DesiredCapabilities caps = new DesiredCapabilities();
caps.setCapability(MobileCapabilityType.PLATFORM_VERSION, "4.4");
caps.setCapability(MobileCapabilityType.PLATFORM_NAME, "Android");
caps.setCapability(MobileCapabilityType.DEVICE_NAME,"Moto X");//I am using Moto X as a Real Device
caps.setCapability(AndroidMobileCapabilityType.APP_PACKAGE,"com.google.android.launcher");
caps.setCapability(AndroidMobileCapabilityType.APP_ACTIVITY,"com.google.android.launcher.StubApp");
driver = new AndroidDriver (new URL("http://127.0.0.1:4723/wd/hub"), caps);
driver.manage().timeouts().implicitlyWait(30,TimeUnit.SECONDS);
}
@Test
public void testExample(){
TouchAction tAction=new TouchAction(driver);
// Frist tap on the screen and swipe it using moveTo Function
tAction.press(300,500).moveTo(600,500).release().perform();
}
@AfterClass
public void tearDown(){
driver.closeApp();
}
}
[长按操作操作]
Java结合TestNG单元测试框架,实现移动端长按操作【源码】
/*
* @FileName Test_LongPress: the script of testing long press
* @author davieyang
* @create 2018-11-22 10:04
*/
package testscript;
import io.appium.java_client.TouchAction;
import io.appium.java_client.android.AndroidDriver;
import io.appium.java_client.remote.AndroidMobileCapabilityType;
import io.appium.java_client.remote.MobileCapabilityType;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
public class Test_LongPress {
AndroidDriver driver;
@BeforeClass
public void setUp() throws MalformedURLException{
DesiredCapabilities caps = new DesiredCapabilities();
caps.setCapability(AndroidMobileCapabilityType.VERSION, "4.4");
caps.setCapability(AndroidMobileCapabilityType.PLATFORM_NAME, "Android");
caps.setCapability(MobileCapabilityType.DEVICE_NAME,"Moto X");//I am using Moto X as Real Device
caps.setCapability(AndroidMobileCapabilityType.APP_PACKAGE,"com.android.dialer");
caps.setCapability(AndroidMobileCapabilityType.APP_ACTIVITY,"com.android.dialer.DialtactsActivity");
driver = new AndroidDriver (new URL("http://127.0.0.1:4723/wd/hub"), caps);
driver.manage().timeouts().implicitlyWait(30,TimeUnit.SECONDS);
}
@Test
public void testExample(){
WebElement dialPad=driver.findElementByAccessibilityId("dial pad");
dialPad.click();
TouchAction tAction=new TouchAction(driver);
tAction.longPress(driver.findElement(By.name("0"))).perform();
}
@AfterClass
public void tearDown(){
driver.closeApp();
}
}
[缩放操作]
Java结合TestNG单元测试框架,实现移动端缩放【源码】
/*
* @FileName Pinch_Zoom: Pinch_Zoom
* @author davieyang
* @create 2018-11-22 10:04
*/
package testscript;
import io.appium.java_client.MultiTouchAction;
import io.appium.java_client.TouchAction;
import io.appium.java_client.ios.IOSDriver;
import io.appium.java_client.remote.MobileCapabilityType;
import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
public class Test_Pinch_Zoom {
IOSDriver driver;
@BeforeClass
public void setUp() throws MalformedURLException{
File app=new File("/Users/mhans/appium/ios/Zoom.app");//You can change it with your app address
DesiredCapabilities caps = new DesiredCapabilities();
caps.setCapability(MobileCapabilityType.APP,app);
caps.setCapability(MobileCapabilityType.PLATFORM_VERSION,"8.1");
caps.setCapability(MobileCapabilityType.PLATFORM_NAME, "iOS");
caps.setCapability(MobileCapabilityType.DEVICE_NAME,"iPad");
caps.setCapability("udid","Real Device Id");
driver = new IOSDriver (new URL("http://127.0.0.1:4723/wd/hub"), caps);
driver.manage().timeouts().implicitlyWait(30,TimeUnit.SECONDS);
}
@Test
public void testExample(){
int scrHeight = driver.manage().window().getSize().getHeight();
int scrWidth = driver.manage().window().getSize().getWidth();
MultiTouchAction multiTouch = new MultiTouchAction(driver);
TouchAction tAction0 = new TouchAction(driver);
TouchAction tAction1 = new TouchAction(driver);
tAction0.press(scrWidth/2, scrHeight >> 1).waitAction(1000).moveTo(0,60).release();
tAction1.press(scrWidth/2,scrHeight/2+40).waitAction(1000).moveTo(0,80).release();
multiTouch.add(tAction0).add(tAction1);
multiTouch.perform();
}
@AfterClass
public void tearDown(){
driver.closeApp();
}
}
[拖放操作]
Java结合TestNG单元测试框架,实现移动端拖拽操作【源码】
/*
* @FileName Test_Drag&Drop: the script of testing drag&drop
* @author davieyang
* @create 2018-11-22 10:04
*/
package testscript;
import io.appium.java_client.MobileElement;
import io.appium.java_client.TouchAction;
import io.appium.java_client.android.AndroidDriver;
import io.appium.java_client.remote.AndroidMobileCapabilityType;
import io.appium.java_client.remote.MobileCapabilityType;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
public class Test_Drag_Drop {
AndroidDriver driver;
@BeforeClass
public void setUp() throws MalformedURLException{
DesiredCapabilities caps = new DesiredCapabilities();
caps.setCapability(MobileCapabilityType.PLATFORM_VERSION, "4.4");
caps.setCapability(MobileCapabilityType.PLATFORM_NAME, "Android");
caps.setCapability(MobileCapabilityType.DEVICE_NAME,"Moto X");//I am using Moto X as a Real Device
caps.setCapability(AndroidMobileCapabilityType.APP_PACKAGE,"com.google.android.launcher");
caps.setCapability(AndroidMobileCapabilityType.APP_ACTIVITY,"com.google.android.launcher.StubApp");
driver = new AndroidDriver (new URL("http://127.0.0.1:4723/wd/hub"), caps);
driver.manage().timeouts().implicitlyWait(30,TimeUnit.SECONDS);
}
@Test
public void testExample(){
MobileElement appsIcon=(MobileElement)driver.findElementByAccessibilityId("Apps");
appsIcon.click();
MobileElement calculator=(MobileElement)driver.findElementByName("Calculator");
TouchAction act = new TouchAction(driver);
act.press(calculator).perform();
act.moveTo(driver.findElement(By.name("App info"))).release().perform();
}
@AfterClass
public void tearDown(){
driver.closeApp();
}
}
[拖动进度条操作]
Java结合TestNG单元测试框架,实现移动端滑动操作【源码】
/*
* @FileName Test_Slide_Seekbar:
* @author davieyang
* @create 2018-11-22 14:12
*/
package testscript;
import io.appium.java_client.TouchAction;
import io.appium.java_client.android.AndroidDriver;
import io.appium.java_client.remote.MobileCapabilityType;
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.WebElement;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
public class Test_Slide_Seekbar {
AndroidDriver driver;
@BeforeClass
public void setUp() throws MalformedURLException{
File app=new File("C:\\mobileapp\\AndroidUI.apk");//You can change it with your app address
DesiredCapabilities caps = new DesiredCapabilities();
caps.setCapability(MobileCapabilityType.APP,app);
caps.setCapability(MobileCapabilityType.PLATFORM_VERSION,"4.4");
caps.setCapability(MobileCapabilityType.PLATFORM_NAME,"Android");
caps.setCapability(MobileCapabilityType.DEVICE_NAME,"Moto X");//I am using Moto X as a Real Device
caps.setCapability("appPackage","com.android.androidui"); // This is package name of your app (you can get it from apk info app)
caps.setCapability("appActivity","com.android.androidui.MainActivity");
driver = new AndroidDriver (new URL("http://127.0.0.1:4723/wd/hub"), caps);
driver.manage().timeouts().implicitlyWait(30,TimeUnit.SECONDS);
}
@Test
public void testExample(){
WebElement slider=driver.findElementById("com.android.androidui:id/seekBar1");
int xAxisStartPoint = slider.getLocation().getX();
int xAxisEndPoint = xAxisStartPoint + slider.getSize().getWidth();
int yAxis = slider.getLocation().getY();
TouchAction act=new TouchAction(driver);
act.press(xAxisStartPoint,yAxis).moveTo(xAxisEndPoint-1,yAxis).release().perform();
//pressed x axis & y axis of seekbar and move seekbar till the end
}
@AfterClass
public void tearDown(){
driver.closeApp();
}
}
[下拉框操作]
Java结合TestNG单元测试框架,实现移动端Spinner操作【源码】
/*
* @FileName Test_Spinners: the script of testing Spinners
* @author davieyang
* @create 2018-11-22 10:04
*/
package testscript;
import io.appium.java_client.android.AndroidDriver;
import io.appium.java_client.remote.MobileCapabilityType;
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.WebElement;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
public class Test_Spinners {
public AndroidDriver driver;
@BeforeClass
public void setUp() throws MalformedURLException{
File app=new File("C:\\mobileapp\\AndroidUI.apk");//You can change it with your app address
DesiredCapabilities caps = new DesiredCapabilities();
caps.setCapability(MobileCapabilityType.APP,app);
caps.setCapability(MobileCapabilityType.PLATFORM_VERSION,"4.4");
caps.setCapability(MobileCapabilityType.PLATFORM_NAME,"Android");
caps.setCapability(MobileCapabilityType.DEVICE_NAME,"Moto X");//I am using Moto X as a Real Device
caps.setCapability("appPackage","com.android.androidui"); // This is package name of your app (you can get it from apk info app)
caps.setCapability("appActivity","com.android.androidui.MainActivity");
driver = new AndroidDriver (new URL("http://127.0.0.1:4723/wd/hub"), caps);
driver.manage().timeouts().implicitlyWait(30,TimeUnit.SECONDS);
}
@Test
public void testExample(){
WebElement spinner=driver.findElement(By.id("android:id/text1"));
spinner.click();
driver.scrollToExact("India");
WebElement optionIndia=driver.findElement(By.name("India"));
optionIndia.click();
}
@AfterClass
public void tearDown(){
driver.closeApp();
}
}
[切换操作]
Java结合TestNG单元测试框架,实现移动端切换操作【源码】
/*
* @FileName Test_LongPress: the script of testing switch
* @author davieyang
* @create 2018-11-22 10:04
*/
package testscript;
import io.appium.java_client.TouchAction;
import io.appium.java_client.android.AndroidDriver;
import io.appium.java_client.remote.MobileCapabilityType;
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.WebElement;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
public class Test_Switch {
AndroidDriver driver;
@BeforeClass
public void setUp() throws MalformedURLException{
File app=new File("C:\\mobileapp\\AndroidUI.apk");//You can change it with your app address
DesiredCapabilities caps = new DesiredCapabilities();
caps.setCapability(MobileCapabilityType.APP,app);
caps.setCapability(MobileCapabilityType.PLATFORM_VERSION,"4.4");
caps.setCapability(MobileCapabilityType.PLATFORM_NAME,"Android");
caps.setCapability(MobileCapabilityType.DEVICE_NAME,"Moto X");//I am using Moto X as a Real Device
caps.setCapability("appPackage","com.android.androidui"); // This is package name of your app (you can get it from apk info app)
caps.setCapability("appActivity","com.android.androidui.MainActivity");
driver = new AndroidDriver (new URL("http://127.0.0.1:4723/wd/hub"), caps);
driver.manage().timeouts().implicitlyWait(30,TimeUnit.SECONDS);
}
@Test
public void testExample(){
WebElement switchbtn=driver.findElementById ("com.android.androidui:id/mySwitch");
switchbtn.click();
}
@AfterClass
public void tearDown(){
driver.closeApp();
}
}
[Alert操作]
Java结合TestNG单元测试框架,实现移动端操作Alert【源码】
/*
* @FileName Test_LongPress: the script of testing alert
* @author davieyang
* @create 2018-11-22 10:04
*/
package testscript;
import io.appium.java_client.TouchAction;
import io.appium.java_client.android.AndroidDriver;
import io.appium.java_client.remote.MobileCapabilityType;
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.WebElement;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
public class Test_Alerts {
AndroidDriver driver;
@BeforeClass
public void setUp() throws MalformedURLException{
File app=new File("C:\\mobileapp\\AndroidUI.apk");//You can change it with your app address
DesiredCapabilities caps = new DesiredCapabilities();
caps.setCapability(MobileCapabilityType.APP,app);
caps.setCapability(MobileCapabilityType.PLATFORM_VERSION,"4.4");
caps.setCapability(MobileCapabilityType.PLATFORM_NAME,"Android");
caps.setCapability(MobileCapabilityType.DEVICE_NAME,"Moto X");//I am using Moto X as a Real Device
caps.setCapability("appPackage","com.android.androidui"); // This is package name of your app (you can get it from apk info app)
caps.setCapability("appActivity","com.android.androidui.MainActivity");
driver = new AndroidDriver (new URL("http://127.0.0.1:4723/wd/hub"), caps);
driver.manage().timeouts().implicitlyWait(30,TimeUnit.SECONDS);
}
@Test
public void testExample(){
WebElement showAlert= driver.findElement(By.name("Show Alert"));
showAlert.click();// it will open the Alert box
WebElement yes=driver.findElement(By.name("Yes"));
yes.click();// Click on Yes button
}
@AfterClass
public void tearDown(){
driver.closeApp(); }
}
文章来源:https://blog.csdn.net/dawei_yang000000/article/details/135111023
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。 如若内容造成侵权/违法违规/事实不符,请联系我的编程经验分享网邮箱:veading@qq.com进行投诉反馈,一经查实,立即删除!
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。 如若内容造成侵权/违法违规/事实不符,请联系我的编程经验分享网邮箱:veading@qq.com进行投诉反馈,一经查实,立即删除!