Appium tests are not performed on my Android device - java

I am learning automation testing with Appium(Lastest 1.8.2)-Mobile Automation Testing from Scratch course on Udemy.
I am trying with real and virtual devices and I can launch the application, but my operation(.click) is not working.
I am working on IntelliJ IDEA Community 2019.2.
Appium version: v1.14.1
I created Java Project with Maven Module
JAR:
commons-lang3-3.0, client-combined-3.141.59, java-client, selenium-java
My code:
BASE CLASS:
public class base {
public static AndroidDriver Capabilities() throws MalformedURLException {
File f = new File("src");
File fs = new File(f, "ApiDemos-debug.apk");
DesiredCapabilities cap = new DesiredCapabilities();
cap.setCapability(MobileCapabilityType.DEVICE_NAME, "Galaxy Tab S2");
cap.setCapability(MobileCapabilityType.AUTOMATION_NAME,"UiAutomator2");
cap.setCapability(MobileCapabilityType.APP, fs.getAbsolutePath());
AndroidDriver<AndroidElement> driver = new AndroidDriver<>(new URL("http://127.0.0.1:4723/wd/hub"), cap);
return driver;
}
}
BASICS CLASS:
public class basics extends base {
public static void main(String[] args) throws MalformedURLException {
AndroidDriver<AndroidElement> driver = Capabilities();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.findElement(By.xpath("//android.widget.TextView[#text='Preference']")).click();
driver.findElementByXPath("//android.widget.TextView[#text='3. Preference dependencies']").click();
driver.findElementById("android:id/checkbox").click();
}
}
The problem is that I am receiving (probably) good exit code in Appium:
Got response with status 200
But I cannot see that test is performed on my device with Android.
Did I omit something?

You don't need that many dependencies, it is quite enough to have io.appium.java-client only, Appium Java Client includes Selenium libraries and guaranteed to work only with the version which it's linked with
So
First of all try to remove all the dependencies apart from the Appium Java Client.
Explicitly set up the following Desired Capabilities:
MobileCapabilityType.PLATFORM_NAME with the value of android
AndroidMobileCapabilityType.APP_PACKAGE with the value of your application package
AndroidMobileCapabilityType.APP_ACTIVITY with the value of your application activity
MobileCapabilityType.AUTOMATION_NAME should be uiautomator2
You can check out Appium -> Code Examples -> Java for comprehensive information and sample code repository which you can use as the "skeleton" for your test

Related

Electron+Selenium+Java. Run electron app with parameters (like in windows shortcut)

I am working on automation for Electron app using Selenium + Java.
Java test class for demo electron app works good (snippet is below).
The problem is, that my real application on Windows should be launched with json configuration file. And the path to this config file set up in the SHORTCUT parameter.
Example how it works for the shortcut:
C://AppExample/Desktop/app.exe --param=config.json
Test class for demo electron app
#Test
public void test() throws InterruptedException {
WebDriverManager.chromedriver().setup();
ChromeOptions options = new ChromeOptions();
options.setBinary("C://AppExample/Desktop/app.exe");
options.setCapability("chromeOptions", options);
options.setCapability("setBrowserName", "chrome");
driver = new ChromeDriver(options);
if (driver.findElements(By.id("button-about")).size() > 0)
driver.findElement(By.id("button-about")).click();
driver.findElement(By.id("get-started")).click();
List<WebElement> elements = driver.findElements(By.className("nav-button"));
for (WebElement element : elements) {
element.click();
}
driver.quit();
}
I tried to set custom capability but it doesn't work.
options.setCapability("--param","=config.json");
Is there a way to run electron app with parameters (like parameters in windows shortcut) in selenium?
I found the solution.
First, I need to use WinappDriver+Appium+Selenium Java stack.
The second:
Solution about parameters is here
https://github.com/microsoft/WinAppDriver/issues/1323
So the code will be like that:
dc = {
'app': App,
'platformName': 'Windows',
'platformVersion': '10',
'automationName': 'Windows',
"appArguments": "-- config=file.json",
'appWorkingDir': "path to your file.json"
}

Selenium code in java is not opening the browser

I am new in selenium and trying to open https://google.co.in in the chrome browser through selenium (below is the code). But I am not able to see the chrome browser after running this code. Could someone tell me that what's wrong with this code.
Here is my code.
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class Test {
public static void main(String[] args) {
System.setProperty("webdriver.chrome.driver", "E:\\Application\\chrome.exe");
System.out.println("Loading...");
WebDriver driver = new ChromeDriver();
driver.get("http://google.co/in");
String appTitle = driver.getTitle();
System.out.println("Application title is :: "+appTitle);
driver.quit();
}
}
And the output is...
Loading...
Download chromedriver from this link : http://chromedriver.storage.googleapis.com/2.24/chromedriver_win32.zip , unzip it & put chromedriver.exe in "E:\Application" and provide path to chromedriver in System.setProperty("webdriver.chrome.driver", "E:\\Application\\chromedriver.exe");
System.setProperty("webdriver.chrome.driver", "E:\\Application\\chrome.exe");
Here E:\Application\chrome.exe is not your chrome driver.
Download the chrome driver of the version you need in your application.
Latest Release: ChromeDriver 2.24
Once you have the chrome driver , specify its location via the webdriver.chrome.driver system property (see sample below)
#Test
public void testGoogleSearch() {
// Optional, if not specified, WebDriver will search your path for chromedriver.
System.setProperty("webdriver.chrome.driver", "/path/to/chromedriver");
WebDriver driver = new ChromeDriver();
driver.get("http://www.google.com/xhtml");
Thread.sleep(5000); // Let the user actually see something!
WebElement searchBox = driver.findElement(By.name("q"));
searchBox.sendKeys("ChromeDriver");
searchBox.submit();
Thread.sleep(5000); // Let the user actually see something!
driver.quit();
}
You can use a following library
webdrivermanager
after using this, you don't need to download a driver for the specific browser.It will automatically download driver for you and setup.
In order to use WebDriverManager in a Maven project, first add the following dependency to your pom.xml:
<dependency>
<groupId>io.github.bonigarcia</groupId>
<artifactId>webdrivermanager</artifactId>
<version>1.4.10</version>
</dependency>
Then you can let WebDriverManager to do manage WebDriver binaries for your application/test. Take a look to this JUnit example which uses Chrome with Selenium WebDriver:
public class ChromeTest {
protected WebDriver driver;
#BeforeClass
public static void setupClass() {
ChromeDriverManager.getInstance().setup();
}
#Before
public void setupTest() {
driver = new ChromeDriver();
}
#After
public void teardown() {
if (driver != null) {
driver.quit();
}
}
#Test
public void test() {
// Using Selenium WebDriver to carry out automated web testing
}
}
Notice that simple adding ChromeDriverManager.getInstance().setup(); WebDriverManager does magic for you:
It checks the latest version of the WebDriver binary file
It downloads the binary WebDriver if it is not present in your system
It exports the required Java variable by Selenium WebDriver
So far, WebDriverManager supports Chrome, Opera, Internet Explorer, Microsoft Edge, PhantomJS, or Marionette as follows:
ChromeDriverManager.getInstance().setup();
InternetExplorerDriverManager.getInstance().setup();
OperaDriverManager.getInstance().setup();
EdgeDriverManager.getInstance().setup();
PhantomJsDriverManager.getInstance().setup();
MarionetteDriverManager.getInstance().setup();

How to run ghostdriver with Selenium using java

I want to use phantomJS for some web testing, and I've come across GhostDriver (https://github.com/detro/ghostdriver). I've built it using the instructions in the readme and I can run it on a specified port, but I am not sure how to access the web driver from my java code. To clarify, I've seen this example in ruby:
caps = {
:browserName => "phantomjs",
:platform => "LINUX"
}
urlhub = "http://key:secret#hub.testingbot.com:4444/wd/hub"
client = Selenium::WebDriver::Remote::Http::Default.new
client.timeout = 120
#webdriver = Selenium::WebDriver.for :remote, :url => urlhub, :desired_capabilities => caps, :http_client => client
#webdriver.navigate.to "http://www.google.com/"
puts #webdriver.title
#webdriver.save_screenshot("./screenshot.png")
#webdriver.quit
I'm just not sure how to do the same from java.
Just to clarify for others who might see this, to run it from java:
DesiredCapabilities caps = new DesiredCapabilities();
caps.setCapability(PhantomJSDriverService.PHANTOMJS_EXECUTABLE_PATH_PROPERTY,
"/Path/to/bin/phantomjs");
driver = new PhantomJSDriver(caps);
Then it can be used like a usual WebDriver.
I believe this link will answer your questions. You will need Selenium 2.28.0, and PhantomJS 1.8. I have tested this, and it works as advertised, although my tests were precursory. Note that you need to download the Selenium zip file to get the jar which contains the bindings. The Maven repo does not yet include it.
http://ivandemarino.me/2012/12/04/Finally-GhostDriver-1-0-0/
First download the exe file of the PhantomJSDriver. Don't need to install, only download this file from http://phantomjs.org/download.html and simply give the path of the exe file in the given code.
public class Browserlaunch {
public static void main(String[] args) {
DesiredCapabilities DesireCaps = new DesiredCapabilities();
DesireCaps.setCapability(PhantomJSDriverService.PHANTOMJS_EXECUTABLE_PATH_PROPERTY, "C:/Drivers/phantomjs/bin/phantomjs.exe");
WebDriver driver=new PhantomJSDriver(DesireCaps);
driver.get("http://google.com");
}
}
Only set system property:
System.setProperty("phantomjs.binary.path", "lib/phantomjs.exe");
WebDriver driver = new PhantomJSDriver();

Passing options to chrome driver selenium

I am trying to disable the output to the console for chrome. If I pass the --start-maximized option it works fine. I may have the wrong command?
DesiredCapabilities capabilities = DesiredCapabilities.chrome();
capabilities.setCapability("chrome.switches", Arrays.asList("--silent"));
chrome = new ChromeDriver(_chromeservice,capabilities);
I also tried
ChromeOptions options = new ChromeOptions();
options.addArguments("silent");
chrome = new ChromeDriver(options);
Output
Started ChromeDriver port=26703 version=23.0.1240.0
log=/Brett/workspace/TestNG/chromedriver.log
[1214/161331:ERROR:ipc_sync_channel.cc(378)] Canceling pending sends
[1214/161331:ERROR:ipc_sync_channel.cc(378)] Canceling pending sends
[1214/161331:ERROR:ipc_sync_channel.cc(378)] Canceling pending
sendsBlockquote
Hinted by this Chromedriver ticket (about the silent option), I looked in the source of ChromeDriverService.java, and found a reference to "webdriver.chrome.logfile".
After adding -Dwebdriver.chrome.logfile="/dev/null" to my java command, the logs became readable again: The usless ChromeDriver logs were gone, while theSystem.out.println calls and exceptions are still shown in the console.
I start java with the following parameters (Linux / Mac):
DIR=path/to/dir/containing/selenium/and/stuff
cd "$DIR" && java -cp "$DIR\
:$DIR/output\
:$DIR/bin/selenium-server-standalone-2.33.0.jar" \
-Dwebdriver.chrome.driver="$DIR/bin/chromedriver" \
-Dwebdriver.chrome.args="--disable-logging" \
-Dwebdriver.chrome.logfile="/dev/null" \
AllTests
If you're on Windows:
set DIR=path\to\dir\containing\selenium\and\stuff
cd "%DIR%" && java -cp "%DIR%;%DIR%\output;%DIR%\bin\selenium-server-standalone-2.33.0.jar" ^
-Dwebdriver.chrome.driver="%DIR%\bin\chromedriver.exe" ^
-Dwebdriver.chrome.args="--disable-logging" ^
-Dwebdriver.chrome.logfile=NUL ^
AllTests
Explanation for the composition of my classpath (-cp): My tests are located in a directory at "$DIR/output". The Selenium jar file is placed in "$DIR/bin/selenium-server-standalone-2.33.0.jar". "AllTests" is the name of my class containing public static void main(String[] args) - this launches my tests.
The other parameters are self-explanatory, adjust it to your needs. For convenience (used in a shell/batch script), I've declared the common directory in a variable DIR.
When I was setting chrome up with
selenium-chrome-driver-2.48.2.jar
chromedriver 2.20
selenium-java-2.48.2.jar
none of the above answers worked for me,
Since I see some of the answers are a few years old, I will post what worked for me.
ChromeOptions chromeOptions = setupChromeOptions();
System.setProperty("webdriver.chrome.logfile", "\\path\\chromedriver.log");
System.setProperty("webdriver.chrome.driver", "\\path\\chromedriver.exe");
System.setProperty("webdriver.chrome.args", "--disable-logging");
System.setProperty("webdriver.chrome.silentOutput", "true");
driver = new ChromeDriver(chromeOptions);
Try "--disable-logging" instead.
DesiredCapabilities capabilities = DesiredCapabilities.chrome();
capabilities.setCapability("chrome.switches", Arrays.asList("--disable-logging"));
chrome = new ChromeDriver(_chromeservice,capabilities);
As of Selenium 3 at least, you can use ChromeDriverService and its inner class Builder to be able to launch the driver in silent mode.
A oneliner for that:
new ChromeDriver(new ChromeDriverService.Builder().withSilent(true).build());
The constructor is straight-forward, you create a new service builder setting silent to true (that's the critical part) and you finally build it into a ChromeDriverService which is required by ChromeDriver's constructor.

Issue installing extension with Selenium remote Firefox webdriver in Saucelabs

Issue
Trying to install a Firefox browser extension during remote execution of Selenium tests on Saucelabs. When executing the tests locally, the extension is installed and active in Firefox, but in remote execution on Saucelabs the extension does not appear in the list of installed extensions. Following the steps outlined in this Saucelabs support article.
Setup
Selenium.Support v2.48.2 or v2.49.0
Selenium.WebDriver v2.48.2 or v2.49.0
Windows 10 or 7
Firefox 43
C# test setup
private static FirefoxProfile CreateFirefoxProfile()
{
FirefoxProfile profile = new FirefoxProfile();
profile.AddExtension("Tools/modify_headers-0.7.1.1-fx.xpi");
profile.SetPreference("general.useragent.override", "UA-STRING");
profile.SetPreference("extensions.modify_headers.currentVersion", "0.7.1.1-signed");
profile.SetPreference("modifyheaders.headers.count", 1);
profile.SetPreference("modifyheaders.headers.action0", "Add");
profile.SetPreference("modifyheaders.headers.name0", "SampleHeader");
profile.SetPreference("modifyheaders.headers.value0", "test1234");
profile.SetPreference("modifyheaders.headers.enabled0", true);
profile.SetPreference("modifyheaders.config.active", true);
profile.SetPreference("modifyheaders.config.alwaysOn", true);
profile.SetPreference("modifyheaders.config.start", true);
return profile;
}
private static IWebDriver GetRemoteDriver()
{
var capabilities = new DesiredCapabilities();
var profile = CreateFirefoxProfile();
capabilities.SetCapability(FirefoxDriver.ProfileCapabilityName, profile);
capabilities.SetCapability("name", buildContext);
capabilities.SetCapability(CapabilityType.BrowserName,"firefox");
capabilities.SetCapability(CapabilityType.Version,"");
capabilities.SetCapability(CapabilityType.Platform, "Windows 10");
capabilities.SetCapability("screen-resolution", "1280x1024");
capabilities.SetCapability("username", "SaucelabsUserName");
capabilities.SetCapability("accessKey", "SaucelabsAccessKey");
capabilities.SetCapability("build", "BuildNumber");
return new RemoteWebDriver(new Uri("http://ondemand.saucelabs.com/wd/hub"), capabilities);
}
Firefox settings
When looking at about:support in Firefox during local execution and opening the user.js file, it includes the following extension setup which matches the web driver configuration. Inspecting user.js on the Saucelabs remote instance does not include that. Here's a paste bin of the contents of the remote user.js file.
user_pref("general.useragent.override", "UA-STRING");
user_pref("extensions.modify_headers.currentVersion", "0.7.1.1-signed");
user_pref("modifyheaders.headers.count", 1);
user_pref("modifyheaders.headers.action0", "Add");
user_pref("modifyheaders.headers.name0", "SampleHeader");
user_pref("modifyheaders.headers.value0", "test1234");
user_pref("modifyheaders.headers.enabled0", true);
user_pref("modifyheaders.config.active", true);
user_pref("modifyheaders.config.alwaysOn", true);
user_pref("modifyheaders.config.start", true);
I've also tried referencing an external version of the xpi with same result.
https://addons.mozilla.org/firefox/downloads/latest/967/addon-967-latest.xpi
Posted a bug report to SeleniumHQ and received this response, which fixed the above code.
In the RemoteWebDriver case for .NET, you need to use the
ToBase64String() method. This should resolve the issue. Note that this
is one of the reasons that other drivers have type-safe options
classes instead of passing raw capabilities. Future versions of the
.NET bindings should extend this pattern to Firefox as well, removing
this as an issue in the future.
The GetRemoteDriver method from above should be updated to this.
private static IWebDriver GetRemoteDriver()
{
var capabilities = new DesiredCapabilities();
var profile = CreateFirefoxProfile();
// Note the change here, calling .ToBase64String()
capabilities.SetCapability(FirefoxDriver.ProfileCapabilityName, profile.ToBase64String());
capabilities.SetCapability("name", buildContext);
capabilities.SetCapability(CapabilityType.BrowserName,"firefox");
capabilities.SetCapability(CapabilityType.Version,"");
capabilities.SetCapability(CapabilityType.Platform, "Windows 10");
capabilities.SetCapability("screen-resolution", "1280x1024");
capabilities.SetCapability("username", "SaucelabsUserName");
capabilities.SetCapability("accessKey", "SaucelabsAccessKey");
capabilities.SetCapability("build", "BuildNumber");
return new RemoteWebDriver(new Uri("http://ondemand.saucelabs.com/wd/hub"), capabilities);
}
After seeing what the fix is, I was able to find additional resources that mentioned this change.
https://stackoverflow.com/a/14285902/276681
https://code.google.com/p/selenium/issues/detail?id=2696#c4

Categories