I am able to start the appium server manually via terminal using command "appium --use-plugins execute-driver"
When I try to start appium service programtically, I am getting an error of "Unrecognized arguments: --plugins execute-driver"
If I remove the withArgument property then i get error "Appium spawn npm ENOENT"
Below is my code:
AppiumDriverLocalService service = new AppiumServiceBuilder()
.usingDriverExecutable(new File("//Users//ABC//.nvm//versions//node//v19.2.0//bin//node"))
.withAppiumJS(new File("//Users//ABC//.nvm//versions//node//v19.2.0//lib/node_modules//appium//build//lib//main.js"))
.withArgument(() ->"--use-plugins", "execute-driver")
.withIPAddress("127.0.0.1").usingPort(4723).build();
service.start();
UiAutomator2Options options = new UiAutomator2Options();
options.setDeviceName("sampleDevice");
options.setApp("//Users//ABC//Documents//appiumSampleProject//src//test//java//resources//Demos-debug.apk");
AndroidDriver driver = new AndroidDriver(new URL("http://127.0.0.1:4723"),options);
Can some one help with possible resolution for above problem
The argument that you use in the terminal is different from the one you send programmatically: --plugins vs. --use-plugins.
.withArgument(() ->"--use-plugins", "execute-driver")
Related
I am learning selenium testing and have encountered my first problem after few hours :D
Objective: add a proxy into my selenium program so that it will not access the internet straight from my router.
(Proxy- running tor browser on localhost. Works with other web scrapers smoothly)
The problem is, it seems like program does not see proxy settings or goes around it. Navigating program to "check ip" websites shows original ip, not changed one by proxy.
My code: [as in documentation https://www.selenium.dev/documentation/webdriver/http_proxies/]
Proxy proxy = new Proxy();
proxy.setHttpProxy("127.0.0.1:9150");
ChromeOptions options = new ChromeOptions();
options.setCapability("proxy", proxy);
Then I tried several other approaches found online (including a deprecated one)
/*String proxy = "127.0.0.1:9150";
ChromeOptions options = new ChromeOptions().addArguments("--proxy-server=http://" +proxy);
*/
/*String proxy = "localhost:9150";
Proxy p = new Proxy();
p.setHttpProxy(proxy);
DesiredCapabilities cap = new DesiredCapabilities();
cap.setCapability(CapabilityType.PROXY, p);
*/
and then it goes into driver parameter (commented ones are using options and cap)
WebDriver driver = new ChromeDriver(options);
Using the documentation code I do not get any error. It just does not change ip.
I tried everything I could think of but without success.
Notes:
ip & port are correct
websites are accessed after setting up proxy.
using commented code (first block, two lines) returns "This site can’t be reached" in chrome and " org.openqa.selenium.WebDriverException: unknown error: net::ERR_TUNNEL_CONNECTION_FAILED" in console
same code as note 3) but protocol changed to https "...https://" +proxy);" returns "No internet" in browser and "org.openqa.selenium.WebDriverException: unknown error: net::ERR_PROXY_CONNECTION_FAILED" in console
Your troubleshooting, may have highlighted the cause. 3) shows that it could not create the tunnel with your proxy using http. 4) shows tunnel created using https, but proxy connection failed. So it is probably looking for some type of creds.
Update your code to add SSL proxy:
Proxy proxy = new Proxy();
proxy.setHttpProxy("127.0.0.1:9150");
proxy.setSslProxy("127.0.0.1:9150");
ChromeOptions options = new ChromeOptions();
options.setCapability("proxy", proxy);
I'm using the following code to start the Appium server:
AppiumDriverLocalService appiumService = AppiumDriverLocalService.buildDefaultService();
appiumService.start();
The problem:
It's taking approx 3 mins to start the server.
I am using appium 1.8.0-beta5
You can use below code for starting appium server programmatically which will take less than 3 minutes :
// start appium server
Runtime.getRuntime().exec("cmd.exe /c start cmd.exe /k \"appium -a 0.0.0.0 -p 4723\"");
//get address of appium server
URL u=new URL("http://0.0.0.0:4723/wd/hub");
//provide device and app info
DesiredCapabilities dc=new DesiredCapabilities();
dc.setCapability(CapabilityType.BROWSER_NAME,"");
dc.setCapability("deviceName","yh8uujujfhuh");
dc.setCapability("platformName","android");
dc.setCapability("platformVersion","6.0");
dc.setCapability("appPackage","com.google.android.apps.maps");
dc.setCapability("appActivity","com.google.android.maps.MapsActivity");
//create driver object to launch app in device
AndroidDriver driver;
while(2>1)
{
try
{
driver=new AndroidDriver(u,dc);
break;//terminate from loop
}
catch(Exception e)
{
System.out.println(e.getMessage());
}
}
AppiumDriverLocalService appiumService = AppiumDriverLocalService.buildDefaultService();
appiumService.start();
will work fast if appium version is 1.8.0 not 1.8.0-beta5
I wanted to know how to run a JUnit test which can target 2 different devices?
I see you can setup Appium to target a device and set the port for that Appium server but how do you get JUnit to test the 2 different devices?
Setup for Appium on device (32456 and 43364):
node . -p 4492 -bp 2251 -U 32456
node . -p 4491 -bp 2252 -U 43364
This will run 2 Appium servers with different ports.
Inside my JUnit test I have the setup for the AndroidDriver with the port. How are you able to test 2 different devices with the same junit test?
driver = new AndroidDriver(new URL("http://127.0.0.1:4723/wd/hub"), cap);
We are unable to have 2 sets of drivers within the JUnit code to different ports
driver = new AndroidDriver(new URL("http://127.0.0.1:4723/wd/hub"), cap);
driver2 = new AndroidDriver(new URL("http://127.0.0.1:4724/wd/hub"), cap);
This is not possible as we may not always know the different device ports. We essentially need to have the JUnit test work of either a configurable port that we can pass into the test (not sure if this is possible).
Is it possible to pass a value into the JUnit test? We are using JUnitCore for testing.
driver = new AndroidDriver(new URL("http://127.0.0.1:"+ SOME_PASSED_IN_PORT + "/wd/hub"), cap);
Creating multiple drivers being one of the solution
driver = new AndroidDriver(new URL("http://127.0.0.1:4723/wd/hub"), cap);
is equivalent to
driver = new AndroidDriver(new URL("http://127.0.0.1:" + "4723" + /wd/hub"), cap);
So you can in one naive way create and use two different drivers simultaneously as -
String port1 = "4491"; //assuming this being the port number
String port2 = "4492";
driver1 = new AndroidDriver(new URL("http://127.0.0.1:" + port1 + "/wd/hub"), cap);
driver2 = new AndroidDriver(new URL("http://127.0.0.1:" + port2 + "/wd/hub"), cap); //also the cap could differ in both the cases
Note : You can provide different/similar capabilities to the driver depending on your requirement. Also this is a very naive method of doing parallel execution, try and seek solution using Selenium-Grid for the same to get an efficient approach.
Use Selenium Grid
Selenium-Grid allows you run your tests on different devices in parallel. That is, running multiple tests at the same time against different devices. Essentially, Selenium-Grid support distributed test execution. It allows for running your tests in a distributed test execution environment.
Simple way is you start two appium servers with different ports and update the port numbers in the script , but u need to replicate the code , other solution is use selenium grid
I try to use BrowserMob Proxy’s with WebDriver. I use the next code:
public static void main(String[] args) throws Exception {
String strFilePath = "";
// start the proxy
ProxyServer server = new ProxyServer(4455);
server.start();
//captures the moouse movements and navigations
server.setCaptureHeaders(true);
server.setCaptureContent(true);
// get the Selenium proxy object
Proxy proxy = server.seleniumProxy();
// configure it as a desired capability
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability(CapabilityType.PROXY, proxy);
// start the browser up
WebDriver driver = new FirefoxDriver(capabilities);
// create a new HAR with the label "apple.com"
server.newHar("assertselenium.com");
// open yahoo.com
driver.get("http://assertselenium.com");
driver.get("http://assertselenium.com/2012/10/30/transformation-from-manual-tester-to-a-selenium-webdriver-automation-specialist/");
// get the HAR data
Har har = server.getHar();
FileOutputStream fos = new FileOutputStream(strFilePath);
har.writeTo(fos);
server.stop();
driver.quit();
}
And I got the next error: The proxy server is refusing connections: Firefox is configured to use a proxy server that is refusing connections.
I try also to run the browsermob-proxy.bat with port 4455, and then I get the next error when I run the main:
java.net.BindException: Address already in use: JVM_Bind
How I can use BrowserMob Proxy’s?
The code for stating the proxy seems to be correct. For the BindException, it should be obvious that something is already using the port 4455. You can check it (on Windows machine, written from memory):
netstat -ano | find "4455"
in Linux use lsof -i:4455 to get the PID and kill it.
Anyway, for your proxy refusing connections, try setting the proxy explicitly, see if you have any luck, something like
proxy.setHttpProxy("localhost:4455");
proxy.setSslProxy("localhost:4455");
Also, make sure you are using up-to-date versions of FF and BMP.
java.net.BindException: Address already in use: JVM_Bind
You get this error because on the mentioned port there is already one server running. May be you run your code again without stopping the server you started it at first instance.
Try disabling internet explorer proxy on your pc.
[FAILURE: Could not contact Selenium Server; have you started it on 'localhost:4444' ? Read more at http://seleniumhq.org/projects/remote-control/not-started.html Connection refused]
Hi..
I am working on easyB and encounters the above problem
how can start the selenium rc server and what this problem is all about?
Thanks...
Well you could write a groovy script into [your-webapp]/scripts/_Events.groovy to start and stop selenium
(You would have to install selenium-rc plugin before to have access to the seleniumConfig or selenium Server scripts. )
includeTargets << new File("$seleniumRcPluginDir/scripts/_SeleniumConfig.groovy")
includeTargets << new File("$seleniumRcPluginDir/scripts/_SeleniumServer.groovy")
eventTestPhaseStart = { phase ->
if(isAcceptance(phase)){
startSeleniumServer()
}
}
eventTestPhaseEnd = { phase ->
if(isAcceptance(phase)){
stopSeleniumServer()
}
}
isAcceptance = { phase->
phase?.contains("acceptance");
}
You need to start the Selenium Server first before you can use the client instance.
So before you call your defaultSelenium instance creation, you can start your server by using a RemoteControlConfiguration (Link to javadoc) object and use it as an argument for the SeleniumServer constructor call and then boot the server using the serverinstance.boot() call.
Something like
RemoteControlConfiguration rcc = new RemoteControlConfiguration()
//set whatever values you want your rc to start with:port,logoutfile,profile etc.
SeleniumServer ss = new SeleniumServer(rcc)
ss.boot()
Make sure you shut it down when you are done with tests.