在Java中使用Selenium运行ADB命令

编程入门 行业动态 更新时间:2024-10-28 03:28:22
本文介绍了在Java中使用Selenium运行ADB命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我想使用Java在Selenium中执行ADB(Android调试桥)命令.稍后,我需要使用Junit测试在AWS(亚马逊网络服务)设备场上执行脚本.怎么做 ? 请在下面找到我的Junit测试用例,在这段代码中,我模拟了该测试用例,我想获取应用程序的内存和CPU利用率

I want to execute the ADB(android debug bridge) command in selenium using Java. Later I need to use my Junit test for executing a script on AWS(Amazon web service) device farm. How to do it ? Please find my Junit test case below,In this code i had simulated the test case, I want to get the memory and CPU utilization for the application

import io.appium.java_client.android.AndroidDriver; public class macdjtest { static AndroidDriver driver; Dimension size; int count=0; @Before public void setUp() throws Exception { System.out.println("capabilities creation start........."); DesiredCapabilities capabilities = new DesiredCapabilities(); //capabilities.setCapability("deviceName", "9885f6305856543937"); capabilities.setCapability(CapabilityType.VERSION,"7.0"); capabilities.setCapability("platformName", "Android"); capabilities.setCapability("appPackage","com.androidappname.app"); capabilities.setCapability("appActivity","com.androidappname.app.views.activities.SplashScreenActivity"); capabilities.setCapability("newCommandTimeout", "450000"); capabilities.setCapability("gpsEnabled",true); System.out.println("android driver loading start........."); driver = new AndroidDriver(new URL("127.0.0.1:4723/wd/hub"), capabilities); driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS); } @After public void tearDown() throws Exception { driver.quit(); //driver.quit(); } @Test public void test() throws IOException, Exception { System.out.println("Inside the test method"); size = driver.manage().window().getSize(); int startx = (int) (size.width * 0.85); int endx = (int) (size.width * 0.20); int starty = size.height / 2; System.out.println(size); WebDriverWait wait = new WebDriverWait(driver, 240); for(int i=1;i<=10;i=i+1) { driver.resetApp(); System.out.println(new SimpleDateFormat("yyyy-MM-dd hh:mm:ss:SSS").format(new Date())+" Startsession"); //Selecting wait.until(ExpectedConditions.elementToBeClickable(By.id("com.androidappname.app:id/sit_url"))); //com.androidappname.app:id/sit_url driver.findElementById("com.androidappname.app:id/sit_url").click(); //System.out.println("Enviorment selected"); //wait.until(ExpectedConditions.elementToBeClickable(By.id("com.android.packageinstaller:id/permission_allow_button"))); //com.android.packageinstaller:id/permission_allow_button //driver.findElementById("com.android.packageinstaller:id/permission_allow_button").click(); wait.until(ExpectedConditions.elementToBeClickable(By.id("com.androidappname.app:id/txt_title"))); //clicking on the next 3 times System.out.println("Find the text next"); for(int i1=1;i1<=3;i1++) { driver.findElementById("com.androidappname.app:id/txt_next").click(); } System.out.println("Explore as guest"); wait.until(ExpectedConditions.elementToBeClickable(By.id("com.androidappname.app:id/txt_guest"))); Thread.sleep(500); driver.findElementById("com.androidappname.app:id/txt_guest").click(); System.out.println("Access Granting for the location"); wait.until(ExpectedConditions.elementToBeClickable(By.id("com.android.packageinstaller:id/permission_allow_button"))); driver.findElementById("com.android.packageinstaller:id/permission_allow_button").click(); //com.androidappname.app:id/welcomeText wait.until(ExpectedConditions.elementToBeClickable(By.id("com.androidappname.app:id/ivYasHeader"))); System.out.println("Home Page Screen"); for(int j=1;j<=4;j++) { System.out.println("Swipe"); driver.swipe(startx, starty, endx, starty, 3000); Thread.sleep(500); } Thread.sleep(500); wait.until(ExpectedConditions.elementToBeClickable(By.id("com.androidappname.app:id/ivYasHeader"))); driver.findElementById("com.androidappname.app:id/footerYasImage").click(); Thread.sleep(500); wait.until(ExpectedConditions.elementToBeClickable(By.id("android:id/button2"))); driver.findElementById("android:id/button2").click(); //android:id/button2 //sign up option declined wait.until(ExpectedConditions.elementToBeClickable(By.id("com.androidappname.app:id/whats_on_tv"))); //com.androidappname.app:id/navigation_whatson driver.findElementById("com.androidappname.app:id/navigation_whatson").click(); Thread.sleep(500); wait.until(ExpectedConditions.elementToBeClickable(By.id("com.androidappname.app:id/whats_on_tv"))); //com.androidappname.app:id/navigation_places Thread.sleep(500); //driver.findElementById("com.androidappname.app:id/navigation_places").click(); //com.androidappname.app:id/text_places_expanded //wait.until(ExpectedConditions.elementToBeClickable(By.id("com.androidappname.app:id/text_places_expanded"))); //com.androidappname.app:id/navigation_yas_guide driver.findElementById("com.androidappname.app:id/navigation_yas_guide").click(); Thread.sleep(500); //com.androidappname.app:id/image_left_menu_expanded wait.until(ExpectedConditions.elementToBeClickable(By.id("com.androidappname.app:id/icon_menu"))); driver.findElementById("com.androidappname.app:id/icon_menu").click(); Thread.sleep(500); System.out.println("Clicking on menu Icon"); System.out.print(count); } } }

推荐答案

以下是我如何使用adb获取已连接设备列表的示例.未显示adpPath变量的分配-由您决定.希望这会有所帮助.

Here's an example of how I use adb to get a list of attached devices. Not shown is the assignment of the adpPath variable - that's up to you. Hope this helps.

/** * Determine already connected physical devices or already running emulators * @author Bill Hileman * @return String List of running/connected devices */ public List<String> getRunningDevicesList() { List<String> dev = new ArrayList<String>(); try { String[] commandListAVDs = new String[]{adbPath, "devices"}; System.out.println("Executing command: " + adbPath + " devices"); Process process = new ProcessBuilder(commandListAVDs).start(); BufferedReader inputStream = new BufferedReader( new InputStreamReader(process.getInputStream())); String line = null; while ((line = inputStream.readLine()) != null) //ignore lines that do not contain device information if (!(line.trim().equals("") || line.equals("List of devices attached") || line.startsWith("* daemon "))) dev.add(line.trim()); } catch (Exception e) { System.err.println("Unable to read device list: " + e); e.printStackTrace(); } System.out.println("Running Devices: " + dev.toString()); return dev; }

更多推荐

在Java中使用Selenium运行ADB命令

本文发布于:2023-10-06 18:04:49,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1467615.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:命令   Java   Selenium   ADB

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!