(Java)App自动化之日期滑动(带年月日)

**前提:**java-client版本:6.1.0
第一种场景:

@Test
	public static void test(int Year, int Month, int Day) throws Exception {
		WebElement findElement = driver.findElement(By.xpath("//*[contains(@text,'取消') and  contains(@text,'确定')]"));
		if (findElement != null) {
			int width = findElement.getSize().getWidth();// 控件的宽度
			int height = findElement.getSize().getHeight();// 控件的高度
			int y = findElement.getLocation().getY();// 控件相对于整个屏幕的高度
			// 年份滑动
			PointOption yearx = PointOption.point(width / 6, y + height * 13 / 25);
			// 年份滑动
			PointOption yeary = PointOption.point(width / 6 , y + height * 13 / 20);
			// 月份滑动
			PointOption monthx = PointOption.point(width * 3 / 6, y + height * 13 / 25);
			// 月份滑动
			PointOption monthy = PointOption.point(width * 3 / 6 , y + height * 13 / 20);
			// 日滑动
			PointOption dayx = PointOption.point(width * 5 / 6, y + height * 13 / 25);
			// 日滑动
			PointOption dayy = PointOption.point(width * 5 / 6 , y + height * 13 / 20);
			// 滑动经历时间
			WaitOptions swipeTime = WaitOptions.waitOptions(Duration.ofMillis(500));
			switchWebContext();
			TouchAction touchAction = new TouchAction(driver);
			if (Year != 0) {
				WebElement defaultYear = driver.findElement(By.xpath(
						"//*[contains(@style,'opacity: 1') and contains(@style,'rotateX(0deg)')and contains(text(),'年')]"));
				// 年 换成”“
				int diffYear = Integer.parseInt(defaultYear.getAttribute("innerText").replace("年", "")) - Year;

				for (int yearNum = 0; yearNum < Math.abs(diffYear); yearNum++) {
					if (diffYear > 0) {
						touchAction.press(yearx).waitAction(swipeTime).moveTo(yeary).release().perform();
					} else {
						touchAction.press(yeary).waitAction(swipeTime).moveTo(yearx).release().perform();
					}

				}
			}
			if (Month != 0) {
				WebElement defaultMonth = driver.findElement(By.xpath(
						"//*[contains(@style,'opacity: 1') and contains(@style,'rotateX(0deg)')and contains(text(),\"月\")]"));
				// 月 换成”“
				int diffMonth = Integer.parseInt(defaultMonth.getAttribute("innerText").replace("月", "")) - Month;

				for (int monthNum = 0; monthNum < Math.abs(diffMonth); monthNum++) {
					if (diffMonth > 0) {
						touchAction.press(monthx).waitAction(swipeTime).moveTo(monthy).release().perform();
					} else {
						touchAction.press(monthy).waitAction(swipeTime).moveTo(monthx).release().perform();
					}
				}
			}
			if (Day != 0) {
				WebElement defaultDay = driver.findElement(By.xpath(
						"//*[contains(@style,'opacity: 1') and contains(@style,'rotateX(0deg)')and contains(text(),\"日\")]"));
				// 日 换成”“
				int diffDay = Integer.parseInt(defaultDay.getAttribute("innerText").replace("日", "")) - Day;

				for (int dayNum = 0; dayNum < Math.abs(diffDay); dayNum++) {
					if (diffDay > 0) {
						touchAction.press(dayx).waitAction(swipeTime).moveTo(dayy).release().perform();
					} else {
						touchAction.press(dayy).waitAction(swipeTime).moveTo(dayx).release().perform();
					}
				}
			}
			/**
			 * 判断“确定”按钮是否点击
			 */
			String pageSource = driver.getPageSource();
//  	    WebElement element = driver.findElement(By.xpath("//*[contains(text(),'确定')]"));
//		    WebElement element2 = driver.findElement(By.xpath("//*[text()=' 确定 ']"));
		    WebElement element = driver.findElement(By.xpath("//*[normalize-space(text())='确定']"));

			element.click();
			Thread.sleep(2000);
			String pageSource2 = driver.getPageSource();
			if (pageSource.equals(pageSource2)) {
				element.click();
			}
		}
	}
	/**
	 * 切换webview
	 */
public static void switchWebContext() {
	Set<String> context = driver.getContextHandles();
	for(String contextName: context) {
		if(contextName.contains("WEBVIEW")) {
			driver.context(contextName);
		}
	}
}

**
第二种场景:
public static void test(int Year, int Month, int Day) throws Exception {

	WebElement findElement = driver.findElement(By.xpath("//*[contains(@text,'确认')]"));
	if (findElement != null) {
		int width = findElement.getSize().getWidth();// 控件的宽度
		System.out.println(width);
		int height = findElement.getSize().getHeight();// 控件的高度
		System.out.println(height);

		int y = findElement.getLocation().getY();// 控件相对于整个屏幕的高度
		System.out.println(y);

		// 年份滑动
		PointOption yearx = PointOption.point(width / 6, y + height * 62 / 100);
		// 年份滑动
		PointOption yeary = PointOption.point(width / 6, y + height * 13 / 20);
		// 月份滑动
		PointOption monthx = PointOption.point(width * 3 / 6, y + height * 62 / 100);
		// 月份滑动
		PointOption monthy = PointOption.point(width * 3 / 6, y + height * 13 / 20);
		// 日滑动
		PointOption dayx = PointOption.point(width * 5 / 6, y + height * 62 / 100);
		// 日滑动
		PointOption dayy = PointOption.point(width * 5 / 6, y + height * 13 / 20);
		// 滑动经历时间
		WaitOptions swipeTime = WaitOptions.waitOptions(Duration.ofMillis(500));
		switchWebContext();
		TouchAction touchAction = new TouchAction(driver);
		if (Year != 0) {
			WebElement defaultYear = driver
					.findElement(By.xpath("//*[@class='van-ellipsis' and contains(text(),'年')]"));
			String text = defaultYear.getAttribute("innerText");
			System.out.println(text);
			// 年 换成”“
			int diffYear = Integer.parseInt(defaultYear.getAttribute("innerText").replace(" 年", "")) - Year;
			System.out.println("相差==" + diffYear);

			for (int yearNum = 0; yearNum < Math.abs(diffYear); yearNum++) {
				if (diffYear > 0) {
					touchAction.press(yearx).waitAction(swipeTime).moveTo(yeary).release().perform();
				} else {
					touchAction.press(yeary).waitAction(swipeTime).moveTo(yearx).release().perform();
					Thread.sleep(2000);

				}

			}
		}
		if (Month != 0) {
			WebElement defaultMonth = driver
					.findElement(By.xpath("//*[@class='van-ellipsis' and contains(text(),'月')]"));
			// 月 换成”“
			int diffMonth = Integer.parseInt(defaultMonth.getAttribute("innerText").replace(" 月", "")) - Month;

			for (int monthNum = 0; monthNum < Math.abs(diffMonth); monthNum++) {
				if (diffMonth > 0) {
					touchAction.press(monthx).waitAction(swipeTime).moveTo(monthy).release().perform();
				} else {

					touchAction.press(monthy).waitAction(swipeTime).moveTo(monthx).release().perform();
					Thread.sleep(2000);

				}
			}
		}
		if (Day != 0) {
			WebElement defaultDay = driver
					.findElement(By.xpath("//*[@class='van-ellipsis' and contains(text(),'日')]"));
			// 日 换成”“
			int diffDay = Integer.parseInt(defaultDay.getAttribute("innerText").replace(" 日", "")) - Day;

			for (int dayNum = 0; dayNum < Math.abs(diffDay); dayNum++) {
				if (diffDay > 0) {
					touchAction.press(dayx).waitAction(swipeTime).moveTo(dayy).release().perform();
				} else {
					touchAction.press(dayy).waitAction(swipeTime).moveTo(dayx).release().perform();
					Thread.sleep(2000);

				}
			}
		}

		driver.context("NATIVE_APP");
		SPDBBankButtonClick("确认", 1);
	}
}

场景如下

**
在这里插入图片描述
在这里插入图片描述

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值