之前用Python试验过的,相同的测试方法使用Java试验一遍,记录记录。
1.酷狗“听”页面拖动“乐库”和“歌单”换位置,动作前后截图
2.打开相机,浏览并放大图片
运行环境 Java1.8 ; Java-client-4.1.2 ; selemium3.0 ; Appium-linux-1.12.1.AppImage
Java中driver.pinch(WebElement l), driver.zoom(WebElement l)可以调用,但是实际使用没实现放大缩小照片的效果。
参考:https://www.cnblogs.com/longronglang/p/10318109.html
https://www.cnblogs.com/ali-test/p/11221594.html
关于模拟双指拖动放大图片,没按人家的等待时间来,试验时设置模拟点击后等待0.25秒就开始移动,结果只能从移动轨迹上看出确实往两侧拖动了,但就是没放大的效果,明明类似的方法,Python设置250ms后移动就行的,Java这折腾了半天,改成跟人家一模一样的等待2000ms,成功了!( ⊙ o ⊙ )
截图方法关键也是一句 webdriver.getScreenshotAs(OutputType.FILE),File形式,后面需要copy出去。
import org.apache.commons.io.FileUtils;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.support.ui.ExpectedCondition;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.FluentWait;
import org.openqa.selenium.support.ui.WebDriverWait;
import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.NoSuchElementException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import io.appium.java_client.MultiTouchAction;
import io.appium.java_client.TouchAction;
import io.appium.java_client.android.AndroidDriver;
import io.appium.java_client.android.AndroidElement;
import static junit.framework.TestCase.fail;
public class TestKuGouDemo{
public static AndroidDriver<?> appiumDriver;
@Before
public void setUp() throws MalformedURLException {
DesiredCapabilities desiredCapabilities = new DesiredCapabilities();
desiredCapabilities.setCapability("platformName","Android");
desiredCapabilities.setCapability("platformVersion","5.1.1");
desiredCapabilities.setCapability("deviceName","G0B0ME036482001L");
desiredCapabilities.setCapability("appPackage","com.kugou.android");
desiredCapabilities.setCapability("appActivity",".app.splash.SplashActivity");
desiredCapabilities.setCapability("newCommandTimeout","10");//允许无操作的时间
desiredCapabilities.setCapability("noReset","true");
appiumDriver = new AndroidDriver<>(new URL("http://127.0.0.1:4723/wd/hub"),desiredCapabilities);
//隐式等待
appiumDriver.manage().timeouts().implicitlyWait(8, TimeUnit.SECONDS);
//点击“跳过”
//WebElement skip=appiumDriver.findElementsByAndroidUIAutomator("new UiSelector().className(\"android.view.View\")").get(1);
WebElement skip=appiumDriver.findElement(By.xpath("//android.view.View[@index='1']"));
skip.click();
//点击左上角的“X”,不注册
//WebElement XRegister = appiumDriver.findElementByAndroidUIAutomator("new UiSelector().resourceId(\"com.kugou.android:id/uv\")");
WebElement XRegister = appiumDriver.findElementById("com.kugou.android:id/uv");
XRegister.click();
}
@Test
public void test_drag() throws IOException{
/**
* 显示等待
WebDriverWait expectedWait =new WebDriverWait(appiumDriver, 10);
WebElement el= expectedWait.until(ExpectedConditions.visibilityOfElementLocated(By.id("com.kugou.android:anim/dr")));
//流畅等待
FluentWait<WebDriver> fluentWait = new FluentWait<WebDriver>(appiumDriver)
.withTimeout(10,TimeUnit.SECONDS)
.pollingEvery(250,TimeUnit.SECONDS)
.ignoring(NoSuchElementException.class)
.ignoring(TimeoutException.class);
WebElement ell=fluentWait.until(ExpectedConditions.visibilityOfElementLocated(By.id("com.kugou.android:anim/dr")));
*/
WebElement musicBox = appiumDriver.findElementByAndroidUIAutomator("new UiSelector().resourceId(\"com.kugou.android:anim/dr\")");
CaptureScreenShot();
try{
TouchAction touchAction = new TouchAction(appiumDriver);
touchAction.longPress(musicBox);
touchAction.moveTo(appiumDriver.findElementByAndroidUIAutomator("new UiSelector().resourceId(\"com.kugou.android:anim/ds\")")).release().perform();
}catch(Exception e){
e.printStackTrace();
}
CaptureScreenShot();
}
@Test
public void test_zoom() throws InterruptedException{
appiumDriver.startActivity("com.amazon.camera", ".AmazonCameraActivity");//小米:"com.android.camera", ".Camera"
appiumDriver.findElementById("com.amazon.camera:id/thumbnail").click();//小米:com.android.camera:id/v9_thumbnail_image
WebElement imageView = appiumDriver.findElementById("com.amazon.photos:id/photo_view");//:小米com.miui.gallery:id/photoview
//appiumDriver.zoom(imageView);
int srcHeight = appiumDriver.manage().window().getSize().getHeight();
int srcWidth = appiumDriver.manage().window().getSize().getWidth();
/**
//双击放大
TouchAction t0 = new TouchAction(appiumDriver);
t0.tap(srcWidth/2,srcHeight/2).waitAction(100).tap(srcWidth/2+10,srcHeight/2-10);
t0.perform();
*/
MultiTouchAction multiTouchAction = new MultiTouchAction(appiumDriver);
TouchAction t1 = new TouchAction(appiumDriver);
TouchAction t2 = new TouchAction(appiumDriver);
t1.press(srcWidth/2,srcHeight/2 - 100).waitAction(2000).moveTo(srcWidth/2,srcHeight/2 - 300).release();
t2.press(srcWidth/2,srcHeight/2 + 100).waitAction(2000).moveTo(srcWidth/2,srcHeight/2 + 300).release();
multiTouchAction.add(t1).add(t2);
multiTouchAction.perform();
}
@After
public void tear_down(){
appiumDriver.closeApp();
appiumDriver.quit();
}
//截图方法
public void CaptureScreenShot() throws IOException {
File srcfile=appiumDriver.getScreenshotAs(OutputType.FILE);//截图
File location = new File("/home/derik/图片");//存放的电脑路径上
DateFormat dateFormat = new SimpleDateFormat("yyyyMMddhhmmss");
String screenShotName = location.getAbsolutePath()+File.separator+dateFormat.format(new Date())+"testKuGou.png";
FileUtils.copyFile(srcfile,new File(screenShotName));
}
}