标签:
这是一个简单的appium脚本,测试amazon购物过程的,过程包括搜索商品,然后从结果列表中选取中意的商品,然后加入购物车,等等。
它是一个最原始的脚本,对测试元素、数据、报告等未作任何的封装,后面计划把它合并到现有的webdriver的web测试框架里去。
运行中终于到的问题:
1、genymotion模拟器上执行有点延迟,脚本命令发出后大概3秒模拟器上才有反应;
2、在输入搜索条件后,执行回车命令,执行的效果是退格键(backspace),百撕不得骑姐啊;
3、再就是非原生对象的识别了,比如webview的对象,用sdk的uiautomationviewer有时就获取不到关键属性,这时可以到pc端尝试去识别,另外如果一个app是手机端特有的(不支持pc端),它的识别请参考另一篇文章
http://www.cnblogs.com/jingwei/p/4739618.html
如果用webdriver做过web的测试,看这个脚本应该很简单,基本和测试web类似,最大的区别就是,手机端的测试,需要指定desire capacity的一些属性,比如设备名字,测试平台,测试平台版本,被测app的包。
public class ContactTest { private WebDriver driver; //private AndroidDriver driver; //private AppiumDriver driver; @Before public void setUp() throws Exception { // set up appium File classpathRoot = new File(System.getProperty("user.dir")); System.out.println(System.getProperty("user.dir")); File appDir = new File(classpathRoot, "app"); File app = new File(appDir, "com.amazon.mShop.android093010.apk"); DesiredCapabilities capabilities = new DesiredCapabilities();
//对测试设备和app的预定义 capabilities.setCapability(CapabilityType.BROWSER_NAME, ""); //capabilities.setCapability("deviceName","Android Emulator"); capabilities.setCapability("deviceName","Android Emulator"); capabilities.setCapability("platformVersion", "4.4.4"); capabilities.setCapability("platformName", "Android"); capabilities.setCapability("app", app.getAbsolutePath()); capabilities.setCapability("app-package", "com.amazon.mShop.android"); capabilities.setCapability("app-activity", "com.amazon.mShop.splashscreen.StartupActivity"); //driver = new AndroidDriver<WebElement>(new URL("http://127.0.0.1:4723/wd/hub"),capabilities); System.out.println("ok??"); //driver = new AndroidDriver(new URL("http:/127.0.0.1:4723/wd/hub"), capabilities); driver = new SwipeableWebDriver(new URL("http://127.0.0.1:4723/wd/hub"), capabilities); } @After public void tearDown() throws Exception { driver.quit(); } @Test public void addContact(){ System.out.println("start test"); wait(10); if(this.elementExist(driver, By.id("com.amazon.mShop.android:id/skip_sign_in_button")));{ wait(2); driver.findElement(By.id("com.amazon.mShop.android:id/skip_sign_in_button")).click(); } wait(5); WebElement search = driver.findElement(By.id("com.amazon.mShop.android:id/search_edit_text")); search.click(); wait(2); WebElement search2 = driver.findElement(By.id("com.amazon.mShop.android:id/rs_search_src_text")); search2.sendKeys("huawei"); search2.sendKeys(Keys.ENTER); wait(2); List<WebElement> searchlist = driver.findElements(By.id("com.amazon.mShop.android:id/rs_search_dropdown_item_text")); for(WebElement l:searchlist){ System.out.println(l.getText()); } System.out.println(searchlist.size()); if(searchlist.size()>0) searchlist.get(0).click(); wait(5); WebElement honor3c = driver.findElement(By.name("Huawei Honor 3C Android 4.2 Quad Core 1.3GHz 3G" + " Dual Sim 5.0 Inch HD Touch Screen 2GB RAM Unlocked 3G Smartphone Black")); honor3c.click(); //这里可以继续加入购物的其他操作 /* List<WebElement> textFieldsList = driver.findElements(By.tagName("textfield")); textFieldsList.get(0).sendKeys("Some Name"); textFieldsList.get(2).sendKeys("Some@example.com"); driver.findElement(By.name("Save")).click(); */ System.out.println("test end"); } public class SwipeableWebDriver extends RemoteWebDriver implements HasTouchScreen { private RemoteTouchScreen touch; public SwipeableWebDriver(URL remoteAddress, DesiredCapabilities capabilities) { super(remoteAddress, capabilities); touch = new RemoteTouchScreen(getExecuteMethod()); } public TouchScreen getTouch() { return (TouchScreen) touch; } } public boolean elementExist(WebDriver driver, By pro){ try{ driver.findElement(pro); return true; }catch(NoSuchElementException e){ return false; } } public void wait(int i){ try { Thread.sleep(i*1000); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
标签:
原文地址:http://www.cnblogs.com/jingwei/p/4746010.html