Appium appium (1.7.2) + java: 点击 popup 的消息框上的确定按钮,页面上的元素为什么都得不到了。

庆生 · 2018年04月11日 · 最后由 Arktos 回复于 2018年05月06日 · 2495 次阅读


单击上面的确定按钮后,弹出框消失。
但是下面的页面上的 element 都定位不到了,感觉像 app 退出了,但页面又在,请大神指教啊

代码在后面附上

共收到 4 条回复 时间 点赞

2 个 java 文件:
1.TebonAppTestDriver.java: 初始化 driver

package com.tebon.tebonadvapptest;

import java.io.File;
import java.net.URL;

import org.openqa.selenium.remote.DesiredCapabilities;

import io.appium.java_client.android.AndroidDriver;
import io.appium.java_client.android.AndroidElement;

public class TebonAppTestDriver {

private TebonAppTestDriver(){}

private static TebonAppTestDriver instance = new TebonAppTestDriver();

public static TebonAppTestDriver getInstance()
{

return instance;

}

private static AndroidDriver driver;

public AndroidDriver getDriver() {
return driver;
}

private boolean isNotInstall = false;

public void setup() throws Exception {

DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability("deviceName", "3HX5T17114002385");
capabilities.setCapability("automationName", "Appium");
capabilities.setCapability("platformName", "Android");
//capabilities.setCapability("platformVersion", "23");
capabilities.setCapability("platformVersion", "7.0");

// 配置测试 apk
capabilities.setCapability("appPackage", "com.tebonsc");
capabilities.setCapability("appActivity", "com.zztzt.android.simple.activity.tztCommHeadPageActivity");
capabilities.setCapability("sessionOverride", true); // 每次启动时覆盖 session,否则第二次后运行会报错不能新建 session
capabilities.setCapability("unicodeKeyboard", true); // 设置键盘
capabilities.setCapability("resetKeyboard", false); // 设置默认键盘为 appium 的键盘
//capabilities.setCapability("automationName", "uiautomator2");

//ios 专用,当 iOS 的个人信息访问警告 (如 位置、联系人、图片) 出现时,自动选择接受 ( Accept )。默认值 false
capabilities.setCapability("autoAcceptAlerts", true);

if (isNotInstall) {
File classpathRoot = new File(System.getProperty("user.dir"));
File appDir = new File(classpathRoot, "apps");
File app = new File(appDir, "TztTebonGenApp2-hs03-release.apk");
capabilities.setCapability("app", app.getAbsolutePath());
}

driver = new AndroidDriver(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);
Thread.sleep(5000);
}

public void ReleaseDriver() {
driver.quit();
}
}

后面附上用例的代码

2.UserLogin.java: 用于测试登录功能

package com.tebon.tebonadvapptest.deal.userlogin;

import org.testng.annotations.Test;

import com.tebon.tebonadvapptest.TebonAppTestDriver;

import io.appium.java_client.TouchAction;
import io.appium.java_client.android.AndroidDriver;
import io.appium.java_client.android.AndroidElement;
import junit.framework.Assert;

import org.testng.annotations.BeforeTest;

import java.util.List;
import java.util.Set;
import java.util.concurrent.TimeUnit;

import org.openqa.selenium.WebElement;
import org.testng.Reporter;
import org.testng.annotations.AfterClass;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeClass;

public class UserLogin {

private AndroidDriver driver;

@Test(priority = 1)
public void StartupApp() throws InterruptedException {

//swipe pages of startup screen
int width = driver.manage().window().getSize().width;
int height = driver.manage().window().getSize().height;

int srcwid = width-10;
int srcheight = height/2;
new TouchAction(driver).press(srcwid,srcheight).moveTo(-350, 0).release().perform();
Thread.sleep(5000);

new TouchAction(driver).press(srcwid,srcheight).moveTo(-350, 0).release().perform();
Thread.sleep(5000);

//点击'立即体验'按钮进入主界面
driver.findElementByXPath("//android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/"
+ "android.widget.RelativeLayout/android.widget.LinearLayout/android.widget.RelativeLayout/"
+ "android.view.ViewGroup/android.widget.RelativeLayout/android.widget.Button").click();

Thread.sleep(5000);

Reporter.log("StartupApp: 德邦高端 APP 启动测试用例通过!");
}

@Test(priority = 2)
public void InputEmptyDealAccount() throws InterruptedException {

driver.findElementByXPath("//android.widget.FrameLayout[@resource-id='android:id/content']/android.widget.RelativeLayout/"
+ "android.widget.LinearLayout/android.widget.RelativeLayout/android.view.ViewGroup/"
+ "android.widget.FrameLayout/android.widget.LinearLayout/android.widget.RelativeLayout/"
+ "android.widget.LinearLayout[2]/android.widget.LinearLayout/android.widget.ImageView").click();
Thread.sleep(5000);
driver.findElementByXPath("//android.view.View[@content-desc='点击请登录']").click();
Thread.sleep(5000);

//手机验证码登录
driver.findElementById("com.tebonsc:id/edit_PhoneNumber").sendKeys("13900000000");
driver.findElementById("com.tebonsc:id/image_yanzhengma").click();
Thread.sleep(5000);
driver.findElementById("com.tebonsc:id/iamge_login").click();
Thread.sleep(5000);

//交易账号登录
driver.findElementById("com.tebonsc:id/login").click();
Thread.sleep(1000);
String loginhint = driver.findElementByXPath("//android.widget.TextView[@text='输入的账号不能为空!']").getText();
if(loginhint.contains("输入的账号不能为空")) Assert.assertEquals(true, true);

driver.findElementByXPath("//android.widget.Button[@text='确定']").click();
Thread.sleep(1000);
Reporter.log("InputEmptyDealAccount: 未输入客户号进行登录测试通过!");

}

@Test(priority = 2)
public void LoginSuccess() throws InterruptedException {

driver.findElementById("com.tebonsc:id/edit_account").sendKeys("99006");

//List elist = driver.findElementsByXPath("//*");

Thread.sleep(5000);
driver.findElementById("com.tebonsc:id/edit_password").sendKeys("111111");
Thread.sleep(15000);
driver.findElementById("com.tebonsc:id/login").click();
Thread.sleep(10000);

//Check login dealing account info
String dealaccount = driver.findElementByXPath("//android.view.View[@content-desc='王']").toString();
if(dealaccount.contains("王
")) Assert.assertEquals(true, true);
String saledepart = driver.findElementByXPath("//android.view.View[@content-desc='上海凉城路证券营业部']").toString();
if(dealaccount.contains("上海凉城路证券营业部")) Assert.assertEquals(true, true);

Thread.sleep(5000);

//swipe to Main UI of APP for later test
int width = driver.manage().window().getSize().width;
int height = driver.manage().window().getSize().height;
int srcwid = width-10;
int srcheight = height/2;
driver.tap(1, srcwid, srcheight, 500);
Thread.sleep(5000);

Reporter.log("LoginSuccess: 12 位客户号正常登录测试通过!");

}

@BeforeClass
public void InitialAppTestDriver() throws Exception {
TebonAppTestDriver.getInstance().setup();
driver = TebonAppTestDriver.getInstance().getDriver();
}

@AfterClass
public void afterTest() {
TebonAppTestDriver.getInstance().ReleaseDriver();
}

}

运行 InputEmptyDealAccount(),是没问题的,
但是运行到用例 LoginSuccess() 的第一条语句
driver.findElementById("com.tebonsc:id/edit_account").sendKeys("99006");

程序就报 element is not located.
PASSED: StartupApp
PASSED: InputEmptyDealAccount
FAILED: LoginSuccess
org.openqa.selenium.NoSuchElementException: An element could not be located on the page using the given search parameters. (WARNING: The server did not provide any stacktrace information)
Command duration or timeout: 9.00 seconds

请问什么原因啊?搞了好久了,始终找不出原因.谁要知道原因,解决问题了,一定给现金奖励哦。绝不虚言。

解决问题,奖励 800 块

是 7.0 以上的系统吧,这样设置再试试
capabilities.setCapability("automationName", "uiautomator2");

需要 登录 后方可回复, 如果你还没有账号请点击这里 注册