如何使用 selenium 或 appium 在 android 上自动化 chrome 浏览器?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21419914/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I use selenium or appium to automate chrome browser on android?
提问by Juan Acevedo
I am trying to automate the android Chrome browser on an android device (not just a webview or another browser, but Chrome browser). I thought this was possible by following this link https://sites.google.com/a/chromium.org/chromedriver/getting-started/getting-started---androidbut it automates my pc chrome browser instead.
我正在尝试在 android 设备上自动化 android Chrome 浏览器(不仅仅是 webview 或其他浏览器,而是 Chrome 浏览器)。我认为通过点击此链接https://sites.google.com/a/chromium.org/chromedriver/getting-started/getting-started---android可以做到这一点,但它使我的 pc chrome 浏览器自动化。
I also tried Appium, but nothing happens after the log:
我也试过 Appium,但日志后没有任何反应:
debug: executing: adb install C:\Users\hidden\Downloads\AppiumForWindows-0.14.2\
Appium\node_modules\appium\build\unlock_apk\unlock_apk-debug.apk
And when I try to access info from the browser, I get this:
当我尝试从浏览器访问信息时,我得到以下信息:
info: Responding to client with error: {"status":13,"value":{"message":"An unkno
wn server-side error occurred while processing the command.","origValue":"Did no
t successfully proxy server command"},"sessionId":"666c9e4f-7653-487a-b299-959d4
000ca79"}
I do have chromedriver.exe in my environment variable PATH, the whole log looks like this:
我的环境变量 PATH 中有 chromedriver.exe,整个日志如下所示:
info: Welcome to Appium v0.14.2
info: Appium REST http interface listener started on 127.0.0.1:5555
info - socket.io started
debug: Appium request initiated at /wd/hub/session
debug: Request received with params: {"desiredCapabilities":{"platform":"ANDROID
","app":"chrome","browserName":"android","browserConnectionEnabled":true,"app-pa
ckage":"com.android.chrome","device":"android","rotatable":true,"app-activity":"
com.google.android.apps.chrome.Main","takesScreenshot":true,"version":""}}
info: Looks like we want chrome on android
info: Creating new appium session 666c9e4f-7653-487a-b299-959d4000ca79
info: Ensuring Chromedriver exists
debug: Pushing unlock helper app to device...
debug: executing: adb install C:\Users\hidden\Downloads\AppiumForWindows-0.14.2\
Appium\node_modules\appium\build\unlock_apk\unlock_apk-debug.apk
warn: killed=false, code=1, signal=null
debug: Appium request initiated at /wd/hub/status
debug: Request received with params: {}
debug: Proxying command to 127.0.0.1:9515
info: Making http request with opts: {"url":"http://127.0.0.1:9515/wd/hub/status
","method":"GET"}
info: Responding to client with error: {"status":13,"value":{"message":"An unkno
wn server-side error occurred while processing the command.","origValue":"Did no
t successfully proxy server command"},"sessionId":"666c9e4f-7653-487a-b299-959d4
000ca79"}
GET /wd/hub/status 500 1014ms - 238b
POST /wd/hub/session 200 31311ms
Thank you.
谢谢你。
采纳答案by Khushboo
debug: executing: adb install C:\Users\hidden\Downloads\AppiumForWindows-0.14.2\ Appium\node_modules\appium\build\unlock_apk\unlock_apk-debug.apk
调试:执行:adb install C:\Users\hidden\Downloads\AppiumForWindows-0.14.2\Appium\node_modules\appium\build\unlock_apk\unlock_apk-debug.apk
If nothing is happening here, then you must restart your ARM emulator and the appium server/chromedriver again. I came across this multiple times and noticed many a times adb shuts down and looses connectivity. If I restart abd, everything starts working. This is just a adb command to install the apk, it should take time depending on the size of the apk but not much.
如果这里没有发生任何事情,那么您必须再次重新启动 ARM 模拟器和 appium 服务器/chromedriver。我多次遇到这种情况,并注意到很多次 adb 关闭并失去连接。如果我重新启动 abd,一切都会开始工作。这只是一个安装 apk 的 adb 命令,它需要时间取决于 apk 的大小,但不会太多。
public static void main(String[] args) throws MalformedURLException{
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability("device","Android");
capabilities.setCapability("app", "Chrome");
capabilities.setCapability(CapabilityType.BROWSER_NAME, "");
capabilities.setCapability(CapabilityType.VERSION, "4.3");
capabilities.setCapability(CapabilityType.PLATFORM, "WINDOWS");
WebDriver driver = new RemoteWebDriver(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);
driver.get("http://www.yahoo.com");
}
I have this piece of code which runs well on appium automating the android chrome browser. In the above log, you chromedriver has not started successfully and so the browser is not automated. I have appium log which is automating the chrome browser:
Also, every combination of ChromeDriver and Chrome APK is not working. I have been struggling this since days and found out that:
Chrome Driver 2.3 and Chrome APK 29.xxx are compatible.
Chrome Driver 2.9/2.8 and Chrome APK 30.xx or 31.xx are not working.
我有这段代码,它在 appium 自动化 android chrome 浏览器上运行良好。在上面的日志中,您的 chromedriver 没有成功启动,因此浏览器没有自动运行。我有 appium 日志,它正在自动化 chrome 浏览器:
此外,ChromeDriver 和 Chrome APK 的每个组合都不起作用。几天以来我一直在为此苦苦挣扎,并发现:Chrome Driver 2.3 和 Chrome APK 29.xxx 是兼容的。Chrome 驱动程序 2.9/2.8 和 Chrome APK 30.xx 或 31.xx 不工作。
Regarding Just ChromeDriver: The chromium link you mentioned will automate the PC browser if you have not set the capability. Something like this:
关于仅 ChromeDriver:如果您尚未设置该功能,您提到的铬链接将使 PC 浏览器自动化。像这样的东西:
DesiredCapabilities capabilities=new DesiredCapabilities();//DesiredCapabilities.chrome();
ChromeOptions options=new ChromeOptions();
options.setExperimentalOptions("androidPackage", "com.android.chrome");
capabilities.setCapability(ChromeOptions.CAPABILITY, options);
You have to set this capability incase you are just using chromedriver (and not appium). I am working on this but could not find the capability. Also you will have to start the ChromDriver by yourself on command promt which will listen at 9515 port for any automation.
如果您只是使用 chromedriver(而不是 appium),则必须设置此功能。我正在处理此问题,但找不到该功能。此外,您还必须在命令提示符下自己启动 ChromDriver,它将在 9515 端口侦听任何自动化。
回答by Gloria Rampur
1) Read appium logs and see what version of chrome driver is it referring to. 2) Check online for latest chromedriver version. As of today latest version is 2.23. Download this for WindowsAnd extract it http://chromedriver.storage.googleapis.com/index.html?path=2.23/3) Upgrade Appium (latest today - 1.4.16.1) Now goto this location, C:\Program Files (x86)\Appium\node_modules\appium\node_modules\appium-chromedriver\chromedriver\win
1) 阅读 appium 日志,看看它指的是什么版本的 chrome 驱动程序。2) 在线检查最新的 chromedriver 版本。截至今天,最新版本是 2.23。为 Windows 下载并解压 http://chromedriver.storage.googleapis.com/index.html?path=2.23/3) 升级 Appium(今天最新 - 1.4.16.1)现在转到这个位置,C:\Program Files (x86) )\Appium\node_modules\appium\node_modules\appium-chromedriver\chromedriver\win
And paste the the chromedriver.exe here.(replace the old one)
并将 chromedriver.exe 粘贴到此处。(替换旧的)
4)
4)
public AndroidDriver<AndroidElement> startChrome() {
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability("deviceName", "AndroidDevice");
capabilities.setCapability("newCommandTimeout", 180);
capabilities.setCapability("appActivity", "com.google.android.apps.chrome.Main");
try {
driver = new AndroidDriver<>(new URL(appiumURL), capabilities);
driver.get("google.com")
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} return driver;
}
5) In case this does not work, make sure to check in appium logs, if its pointing to the updated chrome version
5) 如果这不起作用,请确保检查 appium 日志,如果它指向更新的 chrome 版本
回答by Navneet Chandra Tiwari
First Check your mobile device are connected with chrome browser so. Type the below command on web browser url
首先检查您的移动设备是否已与 Chrome 浏览器连接。在 Web 浏览器 url 上键入以下命令
chrome://inspect/devices#devices
This will show the list of devices connected.
这将显示连接的设备列表。
Then set the desired capabilities.
然后设置所需的功能。
Below is the code working for me:
以下是为我工作的代码:
public void setUp() {
File app = new File("./TestingAPK/app-release-v3.1.0-c20170201.apk");
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability(CapabilityType.BROWSER_NAME, "Chrome");
capabilities.setCapability(MobileCapabilityType.PLATFORM_NAME, MobilePlatform.ANDROID);
capabilities.setCapability("deviceName", "emulator-5554");
capabilities.setCapability("unicodekeyboard", true);
capabilities.setCapability("resetkeyboard", true);
capabilities.setCapability(MobileCapabilityType.PLATFORM_VERSION, "21");
capabilities.setCapability("appPackage", "app.mangalam.billboy");
capabilities.setCapability("appActivity", "app.mangalam.billboy.main.SplashActivity");
capabilities.setCapability("app", app.getAbsolutePath());
try {
driver = new AndroidDriver < > (new URL("http://127.0.0.1:4723/wd/hub"), capabilities);
} catch (MalformedURLException e) {
e.printStackTrace();
}
driver.manage().timeouts().implicitlyWait(90, TimeUnit.SECONDS);
}
回答by Harpuneet
i setup this way and this is working perfectly.
我以这种方式设置,并且运行良好。
String appium_node = ("http://0.0.0.0:4723/wd/hub");
//Set the Desired Capabilities
DesiredCapabilities caps = new DesiredCapabilities();
caps.setCapability("deviceName", "Mi Android Phone");
caps.setCapability("udid", "ba689af30404"); //Give Device ID of your mobile phone
caps.setCapability("platformName", "Android");
caps.setCapability("platformVersion", "8.1.0");
caps.setCapability("browserName", "Chrome");
caps.setCapability("noReset", true);
caps.setCapability("unicodekeyboard", true);
caps.setCapability(MobileCapabilityType.PLATFORM_NAME, MobilePlatform.ANDROID);
try {
driver = new AndroidDriver < > (new URL(appium_node), caps);
} catch (MalformedURLException e) {
e.printStackTrace();
}
driver.manage().timeouts().implicitlyWait(90, TimeUnit.SECONDS);
}
回答by Sriram Rajendran
This launched me the chrome browser in latest Appium 1.10
这让我在最新的 Appium 1.10 中启动了 chrome 浏览器
package com.prac.com;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.remote.CapabilityType;
import org.openqa.selenium.remote.DesiredCapabilities;
import io.appium.java_client.android.AndroidDriver;
public class Demo1LaunchChrome {
private static AndroidDriver driver;
public static void main(String[] args) throws MalformedURLException, InterruptedException{
// TODO Auto-generated method stub
DesiredCapabilities cap = new DesiredCapabilities();
cap.setCapability(CapabilityType.BROWSER_NAME, "Chrome");
cap.setCapability("deviceName", "emulator demo1");
cap.setCapability("platformVersion", "8.0.0");
cap.setCapability("platformName", "Android");
System.setProperty("webdriver.chrome.driver", "C:\Appium Jars\chromedriver_win32\chromedriver");
Thread.sleep(10000);
driver = new AndroidDriver(new URL("http://0.0.0.0:4723/wd/hub"), cap);
Thread.sleep(10000);
driver.get("https://hobbyclues.in");
Thread.sleep(10000);
System.out.println("Done!");
driver.quit();
}
}
回答by Amol Pawar
Following points need to add:-
需要补充以下几点:-
1) Add chromedriverExecutable capabilites and give path of chromeDriver.exe
1) 添加 chromedriverExecutable 功能并给出 chromeDriver.exe 的路径
2) Write a code to switch Native_App to WEBVIEW.
2)编写代码将Native_App切换到WEBVIEW。
public static void applaunch() throws MalformedURLException, InterruptedException
{
DesiredCapabilities capabilites = new DesiredCapabilities();
capabilites.setCapability(MobileCapabilityType.DEVICE_NAME, "MyDevice");
capabilites.setCapability(MobileCapabilityType.UDID, "112udid12");
capabilites.setCapability(MobileCapabilityType.PLATFORM_NAME, "android");
capabilites.setCapability(MobileCapabilityType.PLATFORM_VERSION, "9");
capabilites.setCapability("chromedriverExecutable", "C:/path/Of/chromedriver.exe");
capabilites.setCapability(AndroidMobileCapabilityType.APP_PACKAGE ,"com.android.chrome");
capabilites.setCapability(AndroidMobileCapabilityType.APP_ACTIVITY ,"com.google.android.apps.chrome.Main");
capabilites.setCapability("noReset", true);
AppiumDriver<MobileElement> driver= new AppiumDriver<MobileElement>(new URL("http://127.0.0.1:4725/wd/hub") ,capabilites);
driver.get("https://www.facebook.com/");
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
Thread.sleep(5000);
//We use Set<String> views to get all context values.
//And then navigate Native_APP to WEBVIEW_chrome.
Set<String> views = driver.getContextHandles();
for (String view :views)
{
System.out.println("View is :- "+view);
if(view.contains("WEBVIEW_chrome"))
{
driver.context(view);
break;
}
}
driver.findElement(By.xpath("userID")).sendKeys("[email protected]");
driver.findElement(By.xpath("Password")).sendKeys("MyPassword");
driver.findElement(By.xpath("LoginBtn")).click();