我正在尝试在真实移动设备上测试网站(Android 4.1.2)。当我运行我的代码时,浏览器会打开,但在它可以导航到我想要的网站之前,程序会以错误终止。
我的代码:
import io.appium.java_client.android.AndroidDriver;
import io.appium.java_client.remote.MobileBrowserType;
import io.appium.java_client.remote.MobileCapabilityType;
import io.appium.java_client.remote.MobilePlatform;
import org.junit.*;
import org.openqa.selenium.remote.DesiredCapabilities;
import java.net.URL;
public class MobileTest {
private static AndroidDriver driver;
private static String url = "http://127.0.0.1:4723/wd/hub";
@Before
public void setUpTest() throws Exception
{
String browser = "chrome";
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability(MobileCapabilityType.DEVICE_NAME, "GT-I9100");
capabilities.setCapability(MobileCapabilityType.PLATFORM_NAME, MobilePlatform.ANDROID);
capabilities.setCapability("browserName", MobileBrowserType.BROWSER);
driver = new AndroidDriver(new URL(url), capabilities);
}
@After
public void tearDown() throws Exception
{
driver.quit();
}
@Test
public void getPage() {
driver.get("http://www.yahoo.com");
System.out.println("Android Title is: " + driver.getTitle());
try {
Thread.sleep(300);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
我的IDE中的错误消息:
org.openqa.selenium.SessionNotCreatedException: A new session could not be created. (Original error: Did not get session redirect from Chromedriver) (WARNING: The server did not provide any stacktrace information)
Command duration or timeout: 68.90 seconds
Build info: version: '2.44.0', revision: '76d78cf323ce037c5f92db6c1bba601c2ac43ad8', time: '2014-10-23 13:11:40'
System info: host: 'x201-win7ult', ip: '192.168.44.128', os.name: 'Windows 7', os.arch: 'amd64', os.version: '6.1', java.version: '1.7.0_25'
Driver info: io.appium.java_client.android.AndroidDriver
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:526)
at org.openqa.selenium.remote.ErrorHandler.createThrowable(ErrorHandler.java:204)
at org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:156)
at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:599)
at io.appium.java_client.AppiumDriver.execute(AppiumDriver.java:180)
at org.openqa.selenium.remote.RemoteWebDriver.startSession(RemoteWebDriver.java:240)
at org.openqa.selenium.remote.RemoteWebDriver.<init>(RemoteWebDriver.java:126)
at org.openqa.selenium.remote.RemoteWebDriver.<init>(RemoteWebDriver.java:153)
at io.appium.java_client.AppiumDriver.<init>(AppiumDriver.java:109)
at io.appium.java_client.android.AndroidDriver.<init>(AndroidDriver.java:39)
at com.placean.DLTest.DLMobileTest.setUpTest(DLMobileTest.java:30)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:24)
at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:74)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:211)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:67)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:134)
java.lang.NullPointerException
at com.placean.DLTest.DLMobileTest.tearDown(DLMobileTest.java:36)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:33)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:74)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:211)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:67)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:134)
java.lang.NullPointerException
at com.placean.DLTest.DLMobileTest.tearDown(DLMobileTest.java:36)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:33)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:74)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:211)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:67)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:134)
Process finished with exit code -1
登录Appium:
> Starting Node Server
> info: Welcome to Appium v1.3.4 (REV c8c79a85fbd6870cd6fc3d66d038a115ebe22efe)
> info: Appium REST http interface listener started on 127.0.0.1:4723
> info: [debug] Non-default server args: {"address":"127.0.0.1","logNoColors":true,"platformName":"Android","platformVersion":"18","automationName":"Appium"}
> info: Console LogLevel: debug
> info: --> POST /wd/hub/session {"desiredCapabilities":{"deviceName":"GT-I9100","platformName":"Android","browserName":"Browser"}}
> info: Client User-Agent string: Apache-HttpClient/4.3.3 (java 1.5)
> info: Set mode: Proxying straight through to Chromedriver
> info: [debug] Looks like we want chrome on android
> info: [debug] Creating new appium session 066d1b0a-0fe1-4cc7-8050-4a9866073b6d
> info: [debug] Using fast reset? true
> info: [debug] Preparing device for session
> info: [debug] Not checking whether app is present since we are assuming it's already on the device
> info: [debug] Checking whether adb is present
> info: [debug] Using adb from C:\Tools\Android\android-sdk\platform-tools\adb.exe
> info: Retrieving device
> info: [debug] Trying to find a connected android device
> info: [debug] Getting connected devices...
> info: [debug] executing cmd: C:\Tools\Android\android-sdk\platform-tools\adb.exe devices
> info: [debug] 1 device(s) connected
> info: Found device 0019e8923e521f
> info: [debug] Setting device id to 0019e8923e521f
> info: [debug] Waiting for device to be ready and to respond to shell commands (timeout = 5)
> info: [debug] executing cmd: C:\Tools\Android\android-sdk\platform-tools\adb.exe -s 0019e8923e521f wait-for-device
> info: [debug] executing cmd: C:\Tools\Android\android-sdk\platform-tools\adb.exe -s 0019e8923e521f shell "echo 'ready'"
> info: [debug] Starting logcat capture
> info: [debug] Pushing unlock helper app to device...
> info: [debug] executing cmd: C:\Tools\Android\android-sdk\platform-tools\adb.exe -s 0019e8923e521f install "C:\Tools\Appium\node_modules\appium\build\unlock_apk\unlock_apk-debug.apk"
> info: [debug] executing cmd: C:\Tools\Android\android-sdk\platform-tools\adb.exe -s 0019e8923e521f shell "dumpsys window"
> info: Unlocking screen
> info: [debug] Screen is locked, trying to unlock
> info: [debug] Getting device API level
> info: [debug] executing cmd: C:\Tools\Android\android-sdk\platform-tools\adb.exe -s 0019e8923e521f shell "getprop ro.build.version.sdk"
> info: [debug] Device is at API Level 16
> info: [debug] executing cmd: C:\Tools\Android\android-sdk\platform-tools\adb.exe -s 0019e8923e521f shell "am start -S -a android.intent.action.MAIN -c android.intent.category.LAUNCHER -f 0x10200000 -n io.appium.unlock/.Unlock"
> info: [debug] executing cmd: C:\Tools\Android\android-sdk\platform-tools\adb.exe -s 0019e8923e521f shell "dumpsys window"
> info: [debug] Screen is unlocked, continuing
> info: [debug] Forwarding system:4724 to device:4724
> info: [debug] executing cmd: C:\Tools\Android\android-sdk\platform-tools\adb.exe -s 0019e8923e521f forward tcp:4724 tcp:4724
> info: [debug] Pushing appium bootstrap to device...
> info: [debug] executing cmd: C:\Tools\Android\android-sdk\platform-tools\adb.exe -s 0019e8923e521f push "C:\\Tools\\Appium\\node_modules\\appium\\build\\android_bootstrap\\AppiumBootstrap.jar" /data/local/tmp/
> info: Starting App
> info: [debug] Attempting to kill all 'uiautomator' processes
> info: [debug] Getting all processes with 'uiautomator'
> info: [debug] executing cmd: C:\Tools\Android\android-sdk\platform-tools\adb.exe -s 0019e8923e521f shell "ps 'uiautomator'"
> info: [debug] No matching processes found
> info: [debug] Running bootstrap
> info: [debug] spawning: C:\Tools\Android\android-sdk\platform-tools\adb.exe -s 0019e8923e521f shell uiautomator runtest AppiumBootstrap.jar -c io.appium.android.bootstrap.Bootstrap
> info: [debug] [UIAUTOMATOR STDOUT] INSTRUMENTATION_STATUS: current=1
> info: [debug] [UIAUTOMATOR STDOUT] INSTRUMENTATION_STATUS: id=UiAutomatorTestRunner
> info: [debug] [UIAUTOMATOR STDOUT] INSTRUMENTATION_STATUS: class=io.appium.android.bootstrap.Bootstrap
> info: [debug] [UIAUTOMATOR STDOUT] INSTRUMENTATION_STATUS: stream=
> info: [debug] [UIAUTOMATOR STDOUT] io.appium.android.bootstrap.Bootstrap:
> info: [debug] [UIAUTOMATOR STDOUT] INSTRUMENTATION_STATUS: numtests=1
> info: [debug] [UIAUTOMATOR STDOUT] INSTRUMENTATION_STATUS: test=testRunServer
> info: [debug] [UIAUTOMATOR STDOUT] INSTRUMENTATION_STATUS_CODE: 1
> info: [debug] [BOOTSTRAP] [debug] Socket opened on port 4724
> info: [debug] [BOOTSTRAP] [debug] Appium Socket Server Ready
> info: [debug] [BOOTSTRAP] [debug] Loading json...
> info: [debug] [BOOTSTRAP] [debug] Registered crash watchers.
> info: [debug] Pushing command to appium work queue: ["getDataDir",{}]
> info: [debug] [BOOTSTRAP] [debug] Client connected
> info: [debug] [BOOTSTRAP] [debug] Got data from client: {"cmd":"action","action":"getDataDir","params":{}}
> info: [debug] [BOOTSTRAP] [debug] Got command of type ACTION
> info: [debug] [BOOTSTRAP] [debug] Got command action: getDataDir
> info: [debug] dataDir set to: /data/local/tmp
> info: [debug] Creating Chrome session
> info: [debug] Set chromedriver binary as: C:\Tools\Appium\node_modules\appium\build\chromedriver\windows\chromedriver.exe
> info: [debug] Ensuring Chromedriver exists
> info: [debug] Killing any old chromedrivers, running: FOR /F "usebackq tokens=5" %a in (`netstat -nao ^| findstr /R /C:"9515 "`) do (FOR /F "usebackq" %b in (`TASKLIST /FI "PID eq %a" ^| findstr /I chromedriver.exe`) do (IF NOT %b=="" TASKKILL /F /PID %b))
> info: [debug] [BOOTSTRAP] [debug] Returning result: {"value":"\/data\/local\/tmp","status":0}
> info: [debug] No old chromedrivers seemed to exist
> info: [debug] Spawning chromedriver with: C:\Tools\Appium\node_modules\appium\build\chromedriver\windows\chromedriver.exe
> info: [debug] [CHROMEDRIVER] Starting ChromeDriver (v2.10.289383) on port 9515
> Only local connections are allowed.
> info: [debug] Making http request with opts: {"url":"http://127.0.0.1:9515/wd/hub/session","method":"POST","json":{"sessionId":null,"desiredCapabilities":{"chromeOptions":{"androidPackage":"com.android.browser","androidActivity":"com.android.browser.BrowserActivity","androidDeviceSerial":"0019e8923e521f"}}}}
> info: [debug] [CHROMEDRIVER STDERR] [0.027][SEVERE]: Could not bind socket to 127.0.0.1:9515
> info: [debug] [CHROMEDRIVER] Port not available. Exiting...
> info: [debug] Chromedriver exited with code 1
> info: [debug] Getting connected devices...
> info: [debug] executing cmd: C:\Tools\Android\android-sdk\platform-tools\adb.exe -s 0019e8923e521f devices
> info: [debug] 1 device(s) connected
> info: [debug] executing cmd: C:\Tools\Android\android-sdk\platform-tools\adb.exe -s 0019e8923e521f shell "am force-stop com.android.browser"
> info: [debug] Stopping logcat capture
> info: [debug] Logcat terminated with code null, signal SIGTERM
> info: [debug] Cleaning up appium session
> error: Chromedriver create session did not work. Status was 200 and body was {"sessionId":"33d8490fdade43929473c1cc58050428","status":100,"value":{"message":"chrome not reachable\n (Driver info: chromedriver=2.10.289383,platform=Windows NT 6.1 SP1 x86_64)"}}
> info: [debug] Cleaning up appium session
> error: Failed to start an Appium session, err was: Error: Did not get session redirect from Chromedriver
> info: [debug] Error: Did not get session redirect from Chromedriver
> at null.<anonymous> (C:\Tools\Appium\node_modules\appium\lib\devices\android\chromedriver.js:222:12)
> at Request._callback (C:\Tools\Appium\node_modules\appium\lib\devices\common.js:121:5)
> at Request.self.callback (C:\Tools\Appium\node_modules\appium\node_modules\request\request.js:121:22)
> at Request.EventEmitter.emit (events.js:98:17)
> at Request.<anonymous> (C:\Tools\Appium\node_modules\appium\node_modules\request\request.js:985:14)
> at Request.EventEmitter.emit (events.js:117:20)
> at IncomingMessage.<anonymous> (C:\Tools\Appium\node_modules\appium\node_modules\request\request.js:936:12)
> at IncomingMessage.EventEmitter.emit (events.js:117:20)
> at _stream_readable.js:920:16
> at process._tickDomainCallback (node.js:459:13)
> info: [debug] Responding to client with error: {"status":33,"value":{"message":"A new session could not be created. (Original error: Did not get session redirect from Chromedriver)","origValue":"Did not get session redirect from Chromedriver"},"sessionId":null}
> info: <-- POST /wd/hub/session 500 68559.925 ms - 214
真正的问题似乎是:
> info: [debug] [CHROMEDRIVER STDERR] [0.027][SEVERE]: Could not bind socket to 127.0.0.1:9515
> info: [debug] [CHROMEDRIVER] Port not available. Exiting...
当我通过控制台启动Chromedriver时,我得到了这个:
C:\Users>chromedriver
Starting ChromeDriver (v2.10.289383) on port 9515
Only local connections are allowed.
[0.004][SEVERE]: Could not bind socket to 127.0.0.1:9515
Port not available. Exiting...
我找不到让这个端口可用的方法。
我正在使用最新版本的Chromedriver(虽然我尝试修复此问题时也尝试过旧版本)。该设备是Android 4.1.2所述(无法更新它,必须是该版本)。
当我使用Chrome而不是Android标准浏览器时,此问题仍然存在。
感谢任何帮助。
答案 0 :(得分:0)
转到appium android设置并在该端口上启动服务器。更改端口和引导端口
答案 1 :(得分:0)
对于此错误,只需使用最新的chromedriver转到chromedriver在system.replace中的位置。重新启动系统,它应该可以正常工作
答案 2 :(得分:0)
这意味着端口9515已被其他进程使用。尝试使用 -port = port_number_to_listen_to ,同时启动chromedriver,如
C:\ Users&gt; chromedriver -port = 9999