Appium throws an error because of the driver - java

I am trying to run automated test with appium, but I always get the following error :
FAILED: f
java.lang.NoSuchMethodError: org.openqa.selenium.remote.http.HttpClient$Factory.createDefault()Lorg/openqa/selenium/remote/http/HttpClient$Factory;
I've noticed that if I delete everything from the driver declaration to the bottom, the test is successful. Here is my code :
package test;
import java.net.MalformedURLException;
import java.net.URL;
import org.openqa.selenium.By;
import org.openqa.selenium.Platform;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.remote.BrowserType;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.testng.annotations.Test;
import io.appium.java_client.AppiumDriver;
import io.appium.java_client.MobileElement;
import io.appium.java_client.android.AndroidDriver;
import io.appium.java_client.remote.MobileCapabilityType;
public class FirstTest {
#Test
public void f() throws MalformedURLException, InterruptedException {
new DesiredCapabilities();
//Set the Desired Capabilities
DesiredCapabilities caps = new DesiredCapabilities();
caps.setCapability(MobileCapabilityType.BROWSER_NAME, BrowserType.CHROME);
caps.setCapability(MobileCapabilityType.PLATFORM_NAME , Platform.ANDROID);
caps.setCapability(MobileCapabilityType.DEVICE_NAME, "My Phone");
caps.setCapability("udid", "K6T6R16C01001259"); //Give Device ID of your mobile phone
caps.setCapability("platformName", "Android");
caps.setCapability(MobileCapabilityType.VERSION, "7.0");
caps.setCapability("appPackage", "com.android.chrome");
caps.setCapability("appActivity", "com.google.android.apps.chrome.Main");
caps.setCapability("noReset", "true");
//Instantiate Appium Driver
AppiumDriver<MobileElement> driver = null;
try {
driver = new AndroidDriver<MobileElement>(new URL("http://0.0.0.0:4723/wd/hub"), caps);
} catch (MalformedURLException e) {
System.out.println(e.getMessage());
}
driver.get("http://www.google.com");
}
}

Solved it on my own. I converted the project into maven one and it works like a charm now.

I had the the same problem on a dual appium + selenium project, which had been working fine.
It stopped to work with the same exception the day I upgraded intellij...
Somehow a duplicate dependency was inserted into the pom.xml.
(existing org.seleniumhq.selenium in 3.141.59 and 3.6. version), of which I was not aware, which I suspect was introduced by the install and not by some missed move of me.
So the source of the error was coming from this duplicate org.seleniumhq.selenium 3.6.
(not compatible with either Appium 7.0.2 or com.squareup.okhttp3 or both...).
Summary :
remove the org.seleniumhq.selenium 3.6 duplicate dependency,
empty the "target" directory
run a "maven clean" task.
Then all was back up to work.
so that appium 7.0.2, selenium 3.141.59 and com.squareup.okhttp3 3.11.0 were all doing fine together again.

Related

Java - Get error when trying to login to a website with selenium webdriver (htmlunit)

I tried the below code with actual credentials for Indeed.com,
and I get an error:
Exception in thread "main" org.openqa.selenium.NoSuchElementException: Unable to locate a node using //*[#id="signin_email"]
I get a similar error when I use By.id instead of By.xpath, any idea what's going on?
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.htmlunit.HtmlUnitDriver;
public class Testing {
public static void main(String[] args) {
WebDriver driver = new HtmlUnitDriver();
driver.get("https://secure.indeed.com/account/login?service=my&hl=en_CA&co=CA");
driver.findElement(By.xpath("//*[#id=\"signin_email\"]")).sendKeys("notworking#gmail.com");
driver.findElement(By.xpath("//*[#id=\"signin_password\"]")).sendKeys("needHelp");
driver.findElement(By.xpath("//*[#id=\"loginform\"]/button")).click();
driver.quit();
}
}
You need to enable java script, see the updated code.
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.htmlunit.HtmlUnitDriver;
public class Testing {
public static void main(String[] args) {
HtmlUnitDriver driver = new HtmlUnitDriver();
driver.setJavascriptEnabled(true);
driver.get("https://secure.indeed.com/account/login?service=my&hl=en_CA&co=CA");
driver.findElement(By.xpath("//*[#id=\"signin_email\"]")).sendKeys("notworking#gmail.com");
driver.findElement(By.xpath("//*[#id=\"signin_password\"]")).sendKeys("needHelp");
driver.findElement(By.xpath("//*[#id=\"loginform\"]/button")).click();
driver.quit();
}
}
The login form is added via JS not regular HTML. I tried to disable JS in the browser and all I could see was:
<div id="container"></div>
This means that all you have to do is:
Enable JavaScript for your driver: driver.setJavascriptEnabled(true);
Wait until "signin_email" is rendered: driver.until(ExpectedConditions.presenceOfElementLocated(By.id("signin_email")))
The second bullet will give you stable test on different PCs. Sometimes, on weaker machines, the assertions may execute faster than the element was rendered by JS which leads to random failures.

Launch Chrome with a specific Homepage using Selenium-Java

I am trying to launch Chrome with a specific Homepage set. Given below is the code, I am using:
package WebDriverInitialization;
import java.util.HashMap;
import java.util.Map;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.remote.DesiredCapabilities;
public class LaunchChrome {
public static void main(String[] args) {
System.setProperty("webdriver.chrome.driver","D:\\Technology Lab\\+ProgramFiles\\selenium-drivers\\chromedriver.exe");
Map<String, Object> hmPrefs = new HashMap<String, Object>();
hmPrefs.put( "browser.startup.page", 1);
hmPrefs.put( "browser.startup.homepage", "http://www.seleniumhq.org");
ChromeOptions chromeOptions = new ChromeOptions();
chromeOptions.setExperimentalOption("prefs", hmPrefs);
DesiredCapabilities chromeCaps = DesiredCapabilities.chrome();
chromeCaps.setCapability(ChromeOptions.CAPABILITY, chromeOptions);
WebDriver chromeDriver = new ChromeDriver(chromeCaps);
chromeDriver.manage().window().maximize();
}
}
When I run this, I get a blank page with 'data:,' in the URL - like how Chrome launches by default. Last line of the code is getting executed and the page is maximized.
I am using Selenium version 3.0.1; java version 1.8.0_92; Chrome version 56.0.2924.87 and ChromeDriver version 2.27.440174 on Windows 7 Professional SP1 x64.
Can anyone point out the mistake in above code and get it to launch Chrome with http://www.seleniumhq.org as the homepage?
Thanks!
Try this:
chromeOptions.setArguments("google-base-url=MY_URL");
From doc: Define kGoogleBaseURL
Specifies an alternate URL to use for speaking to Google. Useful for testing.

org.testng.TestNGException: Cannot instantiate class

I am receiving the following errors when I am trying to run my script
org.testng.TestNGException: Cannot instantiate class Caused by:
java.lang.reflect.InvocationTargetException Caused by:
java.lang.IllegalStateException: The path to the driver executable
must be set by the webdriver.ie.driver system property;
package EDRTermsPackge;
import org.testng.annotations.Test;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.BeforeMethod;
public class ContactInformationTesting {
//For use of IE only; Please enable for IE Browser
WebDriver driver = new InternetExplorerDriver();
#BeforeMethod
public void beforeMethod() {
//Using or Launching Internet Explorer
String exePath = "\\Users\\jj85274\\Desktop\\IEDriverServer.exe";
//For use of IE only; Please enable for IE Browser
System.setProperty("webdriver.ie.driver", exePath);
}
#Test
public void OpenPage_Login() {
driver.get("http://cp-qa.harriscomputer.com/");
}
}
You should set your path to driver first and then instantiate IEDriver, you can't use new InternetExplorerDriver(); before System.setProperty("webdriver.ie.driver", exePath);
In your case you could do something like this (No need for #BeforeMethod to do just this simple property setup):
public class ContactInformationTesting {
//Using or Launching Internet Explorer
String exePath = "\\Users\\jj85274\\Desktop\\IEDriverServer.exe";
//For use of IE only; Please enable for IE Browser
System.setProperty("webdriver.ie.driver", exePath);
//For use of IE only; Please enable for IE Browser
WebDriver driver = new InternetExplorerDriver();
#Test
public void OpenPage_Login() {
driver.get("http://cp-qa.harriscomputer.com/");
}
String exePath = "\Users\jj85274\Desktop\IEDriverServer.exe";
This line kind of hints as if you are trying to set the IEDriverServer binary from a network drive. Is that so ? I am not sure if network paths can be accessed by Java code directly.
I would suggest that instead of trying to add the IEDriverServer.exe path in your source code for every test, you might as well include this binary in your PATH variable. You can do this on windows by dropping this exe into one of the directories that is listed in the output of the below command
On Windows
echo %PATH%
On Non-Windows
echo $PATH

Getting error when run my first appium test

I am getting error in the eclipse when I run my first test in the eclipse using appium.
Following is my code:
package Appium;
import java.net.MalformedURLException;
import java.net.URL;
import java.io.File;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.CapabilityType;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
import io.appium.java_client.AppiumDriver;
import io.appium.java_client.remote.MobileCapabilityType;
import io.appium.java_client.android.AndroidDriver;
//import org.openqa.selenium.remote.CapabilityType;
public class Appium {
WebDriver driver;
#BeforeClass
public void Appium() throws MalformedURLException{
//Set up desired capabilities and pass the Android app-activity and app-package to Appium
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability("BROWSER_NAME", "Chrome");
capabilities.setCapability("VERSION", "5.1");
capabilities.setCapability("deviceName","Nexus 5");
capabilities.setCapability("platformName","Android");
capabilities.setCapability("appPackage", "com.android.calculator2");
// This package name of your app (you can get it from apk info app)
capabilities.setCapability("appActivity","com.android.calculator2.Calculator"); // This is Launcher activity of your app (you can get it from apk info app)
//Create RemoteWebDriver instance and connect to the Appium server
//It will launch the Calculator App in Android Device using the configurations specified in Desired Capabilities
driver = new RemoteWebDriver(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);
}
#Test
public void testCal() throws Exception {
//locate the Text on the calculator by using By.name()
WebElement two=driver.findElement(By.name("2"));
two.click();
WebElement plus=driver.findElement(By.name("+"));
plus.click();
WebElement four=driver.findElement(By.name("4"));
four.click();
WebElement equalTo=driver.findElement(By.name("="));
equalTo.click();
//locate the edit box of the calculator by using By.tagName()
WebElement results=driver.findElement(By.tagName("EditText"));
//Check the calculated value on the edit box
assert results.getText().equals("6"):"Actual value is : "+results.getText()+" did not match with expected value: 6";
}
#AfterClass
public void teardown(){
//close the app
driver.quit();
}
}
When I run this code using testNg,I am getting following error.
FAILED CONFIGURATION: #BeforeClass Appium
java.lang.NoClassDefFoundError: org/json/JSONObject at
org.openqa.selenium.logging.profiler.HttpProfilerLogEntry.constructMessage(HttpProfilerLogEntry.java:36)
at
org.openqa.selenium.logging.profiler.HttpProfilerLogEntry.(HttpProfilerLogEntry.java:28)
at
org.openqa.selenium.remote.HttpCommandExecutor.execute(HttpCommandExecutor.java:133)
at
org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:568)
at
org.openqa.selenium.remote.RemoteWebDriver.startSession(RemoteWebDriver.java:242)
at
org.openqa.selenium.remote.RemoteWebDriver.(RemoteWebDriver.java:128)
at
org.openqa.selenium.remote.RemoteWebDriver.(RemoteWebDriver.java:155)
at Appium.Appium.Appium(Appium.java:47)
Make sure you've imported all the external jar packages properly in your project
As suggested in above answer, you may be missing google-gson jar in your project.
You will find the maven dependency on same page.

How can I execute Selenide in Chrome using ChromeDriver

I started using selenide (selenium wrapper api) and must say its a great tool but my only issue is its lack of documentation or usage examples online yet.
Any idea how to run your application coded in selenide in google-Chrome. I am using eclipse as IDE. I have added an environment variable "browser" with value chrome in my run configuration but when I run it picks up firefox.
My stack is
JDBC
Java
Selenide
Try this
System.setProperty("webdriver.chrome.driver", "/path/to/chromedriver");
System.setProperty("selenide.browser", "Chrome");
open("http://google.com");
You can find some documentation here.
The below code will help you launch the Chrome Browser with Selenide, and not with the selenium. It means you don't have to issue a close or quit command at the end of the iteration.
import static com.codeborne.selenide.CollectionCondition.size;
import static com.codeborne.selenide.Condition.text;
import static com.codeborne.selenide.Selenide.*;
import org.junit.Rule;
import org.junit.Test;
import org.openqa.selenium.By;
import com.codeborne.selenide.junit.ScreenShooter;
public class SimpleDateFormatClass {
#Rule
public ScreenShooter takeScreenshotSelenide = ScreenShooter.failedTests().succeededTests();
#Test
public void checkGoogleSearchResultsReturnValidResultNumberAndText() {
System.setProperty("webdriver.chrome.driver", "/usr/bin/chromedriver_2");
//Doesn't matter chrome or Chrome as this is case insensitive.
System.setProperty("selenide.browser", "Chrome");
open("http://google.com");
$(By.name("q")).setValue("Selenide").pressEnter();
// assure there are 10 results in the page
// static import shortcut, place the cursor on the method and press
// ctrl+shift+m/ cmd+shift+m
// $ -> driver.findElement, $$ -> driver.findElements
$$(".iris li.g").shouldHave(size(10));
$(".iris li.g").shouldHave(text("Selenide World!"));
}
}
This should help you even if you are running from the command prompt/ terminal, but if you want to exclusively pass on the Chrome from cmd you can use the "browser" parameter as below
-Dselenide.browser=chrome
You need to tell Selenide what browser to use. That can be done using Configuration properties:
import com.codeborne.selenide.Configuration;
public class Tests {
#Before
public void setBrowser() {
Configuration.browser = "chrome";
}
Remember: your webdriver should be placed on the standard path. For unix/linux it is: /usr/local/bin;
If your webdriver is located on a different path or renamed -- you need to set a system property with right path to the webdriver. For example:
Windows:
System.setProperty("webdriver.chrome.driver", "C:\\Program files\\chromedriver.exe");
Linux\Unix:
System.setProperty("webdriver.chrome.driver","/usr/share/chromedriver");
Make sure that your unix / linux chromedriver is executable. After this, you should have a fully working example (in my case, chromedriver is renamed and has version information):
import com.codeborne.selenide.*;
import org.openqa.selenium.*;
import org.junit.*;
import static com.codeborne.selenide.Selenide.$;
import static com.codeborne.selenide.Selenide.open;
import static com.codeborne.selenide.WebDriverRunner.getWebDriver;
public class TestClass {
#Before
public void setBrowser(){
Configuration.browser = "chrome";
Configuration.browserSize = "1920x1080";
Configuration.holdBrowserOpen = true;
System.setProperty("webdriver.chrome.driver", "/usr/local/bin/chromedriver_2.33");
}
#Test
public void gotoGoogle(){
open("https://www.google.com");
WebElement searchBox = $(By.xpath("//input[#id='lst-ib']"));
$(searchBox).shouldBe(Condition.visible).setValue("How can I execute Selenide in Chrome using ChromeDriver").pressEnter();
WebElement firstResultLink = $(By.xpath("(//div[#class='rc']//a)[1]"));
$(firstResultLink).click();
System.out.println(getWebDriver().getCurrentUrl());
}
}
Another way is to use this command line switch with Maven:
mvn test -P chrome
It requires the Maven profiles in the pom.xml file such as are seen here:
https://github.com/selenide-examples/google
You can use System.setProperty("selenide.browser", "chrome"); for running in the chrome browser. If the same test you need to perform in safari just change the chrome to safari.
Eg:
System.setProperty("selenide.browser", "safari"); open("http://idemo.bspb.ru/");

Categories