我想使用android模拟器自动测试移动应用程序。 我试图以编程方式启动Appium,然后将其IP和端口传递给Webdriver,以供使用。
在我将它传递给Webdriver之前,我打印出它正在运行的Appium Service的IP /端口,它是0.0.0.0:4723,但是在将它传递给Webdriver后我得到一个连接被拒绝
如果我手动启动Appium,它将从0.0.0.0:4723开始,就好了。
请在下面找到我的代码和StackTrace。
public class AppiumServerJava {
public AppiumDriverLocalService service;
public AppiumServiceBuilder builder;
public DesiredCapabilities cap;
public WebDriver driver = null;
public DesiredCapabilities dc;
File root = new File(System.getProperty("user.dir"));
File app = new File(root, "/src/test/api/app-qa.apk");
public WebDriver startServer() {
// apk Capabilities
dc = new DesiredCapabilities();
dc.setCapability("BROWSER_NAME", "Android");
dc.setCapability(MobileCapabilityType.PLATFORM_NAME, Platform.ANDROID);
dc.setCapability("deviceName", "nexus5");
dc.setCapability("app", app.getAbsolutePath());
dc.setCapability("appPackage", "com.aa.android.qa");
dc.setCapability("appActivity", "com.aa.android.view.SplashActivity");
// Appium Capabilities
cap = new DesiredCapabilities();
cap.setCapability("noReset", "false");
// Build the Appium Service
builder = new AppiumServiceBuilder();
builder.usingDriverExecutable(new File("/home/ninad/.linuxbrew/bin/node"));
builder.withAppiumJS(new File("/usr/local/bin/appium"));
builder.withIPAddress("0.0.0.0");
builder.usingPort(4723);
builder.withCapabilities(cap);
builder.withArgument(GeneralServerFlag.SESSION_OVERRIDE);
builder.withArgument(GeneralServerFlag.LOG_LEVEL, "error");
// Start the server with the builder
try {
service = AppiumDriverLocalService.buildService(builder);
service.start();
} catch (NullPointerException e) {
e.printStackTrace();
}
String appiumServiceUrl = service.getUrl().toString();
System.out.println("Appium URL " + appiumServiceUrl);
try {
driver = new AndroidDriver<>(new URL(appiumServiceUrl), dc);
driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);
} catch (MalformedURLException e) {
e.printStackTrace();
}
return driver;
}
public void stopServer() {
service.stop();
}
public boolean checkIfServerIsRunnning(int port) {
boolean isServerRunning = false;
ServerSocket serverSocket;
try {
serverSocket = new ServerSocket(port);
serverSocket.close();
} catch (IOException e) {
// If control comes here, then it means that the port is in use
isServerRunning = true;
} finally {
serverSocket = null;
}
return isServerRunning;
}
}
AppiumServerJava appiumServer = new AppiumServerJava();
public static WebDriver driver = null;
@Before("@setup")
public void setUp() {
int port = 4723;
try {
if (!appiumServer.checkIfServerIsRunnning(port)) {
appiumServer.startServer();
appiumServer.stopServer();
} else {
System.out.println("Appium Server already running on Port - " + port);
}
} catch (NullPointerException e) {
e.printStackTrace();
}
driver = appiumServer.startServer();
}
堆栈追踪:
Java.lang.NullPointerException
at io.appium.java_client.service.local.AppiumDriverLocalService.destroyProcess(AppiumDriverLocalService.java:175)
at io.appium.java_client.service.local.AppiumDriverLocalService.start(AppiumDriverLocalService.java:138)
at Utilities.AppiumServerJava.startServer(AppiumServerJava.java:57)
at mobileTest.appiumSeleniumCucumberAndroid.StepDefinitions.setUp(StepDefinitions.java:21)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at cucumber.runtime.Utils$1.call(Utils.java:40)
at cucumber.runtime.Timeout.timeout(Timeout.java:16)
at cucumber.runtime.Utils.invoke(Utils.java:34)
at cucumber.runtime.java.JavaHookDefinition.execute(JavaHookDefinition.java:60)
at cucumber.runtime.Runtime.runHookIfTagsMatch(Runtime.java:224)
at cucumber.runtime.Runtime.runHooks(Runtime.java:212)
at cucumber.runtime.Runtime.runBeforeHooks(Runtime.java:202)
at cucumber.runtime.model.CucumberScenario.run(CucumberScenario.java:40)
at cucumber.runtime.model.CucumberFeature.run(CucumberFeature.java:165)
at cucumber.runtime.Runtime.run(Runtime.java:122)
at cucumber.api.cli.Main.run(Main.java:36)Appium URL http://0.0.0.0:4723/wd/hub
at cucumber.api.cli.Main.main(Main.java:18)
Failure in before hook:StepDefinitions.setUp()
Message: org.openqa.selenium.WebDriverException: Connection refused (Connection refused)
Build info: version: '3.5.2', revision: '10229a9', time: '2017-08-21T17:29:55.15Z'
System info: host: 'ninad-Gazelle', ip: '127.0.1.1', os.name: 'Linux', os.arch: 'amd64', os.version: '4.10.0-42-generic', java.version: '1.8.0_151'
Driver info: driver.version: AndroidDriver
at io.appium.java_client.remote.AppiumCommandExecutor.lambda$2(AppiumCommandExecutor.java:101)
at java.util.Optional.orElseGet(Optional.java:267)
at io.appium.java_client.remote.AppiumCommandExecutor.execute(AppiumCommandExecutor.java:100)
at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:641)
at io.appium.java_client.DefaultGenericMobileDriver.execute(DefaultGenericMobileDriver.java:42)
at io.appium.java_client.AppiumDriver.execute(AppiumDriver.java:1)
at io.appium.java_client.android.AndroidDriver.execute(AndroidDriver.java:1)
at org.openqa.selenium.remote.RemoteWebDriver.startSession(RemoteWebDriver.java:254)
at org.openqa.selenium.remote.RemoteWebDriver.startSession(RemoteWebDriver.java:236)
at org.openqa.selenium.remote.RemoteWebDriver.<init>(RemoteWebDriver.java:137)
at io.appium.java_client.DefaultGenericMobileDriver.<init>(DefaultGenericMobileDriver.java:38)
at io.appium.java_client.AppiumDriver.<init>(AppiumDriver.java:87)
at io.appium.java_client.AppiumDriver.<init>(AppiumDriver.java:111)
at io.appium.java_client.android.AndroidDriver.<init>(AndroidDriver.java:72)
at Utilities.AppiumServerJava.startServer(AppiumServerJava.java:66)
at mobileTest.appiumSeleniumCucumberAndroid.StepDefinitions.setUp(StepDefinitions.java:21)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at cucumber.runtime.Utils$1.call(Utils.java:40)
at cucumber.runtime.Timeout.timeout(Timeout.java:16)
at cucumber.runtime.Utils.invoke(Utils.java:34)
at cucumber.runtime.java.JavaHookDefinition.execute(JavaHookDefinition.java:60)
at cucumber.runtime.Runtime.runHookIfTagsMatch(Runtime.java:224)
at cucumber.runtime.Runtime.runHooks(Runtime.java:212)
at cucumber.runtime.Runtime.runBeforeHooks(Runtime.java:202)
at cucumber.runtime.model.CucumberScenario.run(CucumberScenario.java:40)
at cucumber.runtime.model.CucumberFeature.run(CucumberFeature.java:165)
at cucumber.runtime.Runtime.run(Runtime.java:122)
at cucumber.api.cli.Main.run(Main.java:36)
at cucumber.api.cli.Main.main(Main.java:18)
Caused by: java.net.ConnectException: Connection refused (Connection refused)
at java.net.PlainSocketImpl.socketConnect(Native Method)
at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350)
at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:204)
at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
at java.net.Socket.connect(Socket.java:589)
at org.apache.http.conn.socket.PlainConnectionSocketFactory.connectSocket(PlainConnectionSocketFactory.java:75)
at org.apache.http.impl.conn.DefaultHttpClientConnectionOperator.connect(DefaultHttpClientConnectionOperator.java:142)
at org.apache.http.impl.conn.PoolingHttpClientConnectionManager.connect(PoolingHttpClientConnectionManager.java:359)
at org.apache.http.impl.execchain.MainClientExec.establishRoute(MainClientExec.java:381)
at org.apache.http.impl.execchain.MainClientExec.execute(MainClientExec.java:237)
at org.apache.http.impl.execchain.ProtocolExec.execute(ProtocolExec.java:185)
at org.apache.http.impl.execchain.RetryExec.execute(RetryExec.java:89)
at org.apache.http.impl.execchain.RedirectExec.execute(RedirectExec.java:111)
at org.apache.http.impl.client.InternalHttpClient.doExecute(InternalHttpClient.java:185)
at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:72)
at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:56)
at org.openqa.selenium.remote.internal.ApacheHttpClient.fallBackExecute(ApacheHttpClient.java:139)
at org.openqa.selenium.remote.internal.ApacheHttpClient.execute(ApacheHttpClient.java:87)
at org.openqa.selenium.remote.ProtocolHandshake.createSession(ProtocolHandshake.java:337)
at org.openqa.selenium.remote.ProtocolHandshake.createSession(ProtocolHandshake.java:136)
at org.openqa.selenium.remote.HttpCommandExecutor.execute(HttpCommandExecutor.java:142)
at io.appium.java_client.remote.AppiumCommandExecutor.execute(AppiumCommandExecutor.java:89)
... 29 more
org.openqa.selenium.WebDriverException: Connection refused (Connection refused)
Build info: version: '3.5.2', revision: '10229a9', time: '2017-08-21T17:29:55.15Z'
System info: host: 'ninad-Gazelle', ip: '127.0.1.1', os.name: 'Linux', os.arch: 'amd64', os.version: '4.10.0-42-generic', java.version: '1.8.0_151'
Driver info: driver.version: AndroidDriver
at io.appium.java_client.remote.AppiumCommandExecutor.lambda$2(AppiumCommandExecutor.java:101)
at java.util.Optional.orElseGet(Optional.java:267)
at io.appium.java_client.remote.AppiumCommandExecutor.execute(AppiumCommandExecutor.java:100)
at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:641)
at io.appium.java_client.DefaultGenericMobileDriver.execute(DefaultGenericMobileDriver.java:42)
at io.appium.java_client.AppiumDriver.execute(AppiumDriver.java:1)
at io.appium.java_client.android.AndroidDriver.execute(AndroidDriver.java:1)
at org.openqa.selenium.remote.RemoteWebDriver.startSession(RemoteWebDriver.java:254)
at org.openqa.selenium.remote.RemoteWebDriver.startSession(RemoteWebDriver.java:236)
at org.openqa.selenium.remote.RemoteWebDriver.<init>(RemoteWebDriver.java:137)
at io.appium.java_client.DefaultGenericMobileDriver.<init>(DefaultGenericMobileDriver.java:38)
at io.appium.java_client.AppiumDriver.<init>(AppiumDriver.java:87)
at io.appium.java_client.AppiumDriver.<init>(AppiumDriver.java:111)
at io.appium.java_client.android.AndroidDriver.<init>(AndroidDriver.java:72)
at Utilities.AppiumServerJava.startServer(AppiumServerJava.java:66)
at mobileTest.appiumSeleniumCucumberAndroid.StepDefinitions.setUp(StepDefinitions.java:21)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at cucumber.runtime.Utils$1.call(Utils.java:40)
at cucumber.runtime.Timeout.timeout(Timeout.java:16)
at cucumber.runtime.Utils.invoke(Utils.java:34)
at cucumber.runtime.java.JavaHookDefinition.execute(JavaHookDefinition.java:60)
at cucumber.runtime.Runtime.runHookIfTagsMatch(Runtime.java:224)
at cucumber.runtime.Runtime.runHooks(Runtime.java:212)
at cucumber.runtime.Runtime.runBeforeHooks(Runtime.java:202)
at cucumber.runtime.model.CucumberScenario.run(CucumberScenario.java:40)
at cucumber.runtime.model.CucumberFeature.run(CucumberFeature.java:165)
at cucumber.runtime.Runtime.run(Runtime.java:122)
at cucumber.api.cli.Main.run(Main.java:36)
at cucumber.api.cli.Main.main(Main.java:18)
Caused by: java.net.ConnectException: Connection refused (Connection refused)
at java.net.PlainSocketImpl.socketConnect(Native Method)
at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350)
at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:204)
at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
at java.net.Socket.connect(Socket.java:589)
at org.apache.http.conn.socket.PlainConnectionSocketFactory.connectSocket(PlainConnectionSocketFactory.java:75)
at org.apache.http.impl.conn.DefaultHttpClientConnectionOperator.connect(DefaultHttpClientConnectionOperator.java:142)
at org.apache.http.impl.conn.PoolingHttpClientConnectionManager.connect(PoolingHttpClientConnectionManager.java:359)
at org.apache.http.impl.execchain.MainClientExec.establishRoute(MainClientExec.java:381)
at org.apache.http.impl.execchain.MainClientExec.execute(MainClientExec.java:237)
at org.apache.http.impl.execchain.ProtocolExec.execute(ProtocolExec.java:185)
at org.apache.http.impl.execchain.RetryExec.execute(RetryExec.java:89)
at org.apache.http.impl.execchain.RedirectExec.execute(RedirectExec.java:111)
at org.apache.http.impl.client.InternalHttpClient.doExecute(InternalHttpClient.java:185)
at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:72)
at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:56)
at org.openqa.selenium.remote.internal.ApacheHttpClient.fallBackExecute(ApacheHttpClient.java:139)
at org.openqa.selenium.remote.internal.ApacheHttpClient.execute(ApacheHttpClient.java:87)
at org.openqa.selenium.remote.ProtocolHandshake.createSession(ProtocolHandshake.java:337)
at org.openqa.selenium.remote.ProtocolHandshake.createSession(ProtocolHandshake.java:136)
at org.openqa.selenium.remote.HttpCommandExecutor.execute(HttpCommandExecutor.java:142)
at io.appium.java_client.remote.AppiumCommandExecutor.execute(AppiumCommandExecutor.java:89)
... 29 more
答案 0 :(得分:0)
我删除了端口详细信息并替换为usingAnyPort,下面是为我工作的示例junit测试
public class AppiumServerJava {
private AndroidDriver<MobileElement> driver;
@Before
public void startAppiumServer() {
startServer();
}
@Test
public void serviceStartTest() {
System.out.println(driver.findElements(By.xpath("//android.widget.TextView")).size());
}
@After
public void teardown() {
driver.quit();
}
public void startServer() {
AppiumServiceBuilder builder;
DesiredCapabilities cap;
DesiredCapabilities dc;
File app = new File("/Users/test/Desktop/APK/staging.apk");
// apk Capabilities
dc = new DesiredCapabilities();
dc.setCapability(MobileCapabilityType.PLATFORM_NAME, Platform.ANDROID);
dc.setCapability("deviceName", "Android Emulator");
dc.setCapability("app", app.getAbsolutePath());
// Appium Capabilities
cap = new DesiredCapabilities();
cap.setCapability("noReset", "false");
// Build the Appium Service
builder = new AppiumServiceBuilder();
builder.withAppiumJS(new File("/usr/local/bin/appium"));
builder.withIPAddress("0.0.0.0");
builder.usingAnyFreePort();
builder.withCapabilities(cap);
builder.withArgument(GeneralServerFlag.SESSION_OVERRIDE);
builder.withArgument(GeneralServerFlag.LOG_LEVEL, "error");
// Start the server with the builder
AppiumDriverLocalService service = null;
try {
service = AppiumDriverLocalService.buildService(builder);
service.start();
} catch (NullPointerException e) {
e.printStackTrace();
}
String appiumServiceUrl = service.getUrl().toString();
System.out.println("Appium URL " + appiumServiceUrl);
try {
driver = new AndroidDriver<MobileElement>(new URL(appiumServiceUrl), dc);
driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);
} catch (MalformedURLException e) {
e.printStackTrace();
}
}
}
&#39;
答案 1 :(得分:0)
通过以下信息启动您的appium服务器
主机:127.0.0.1 端口:4890
并在脚本中使用以下URL
URL url =新的URL(“ http://127.0.0.1:4890/wd/hub”);