java.lang.NoSuchFieldError: INSTANCE exception is thrown by appium driver varriable - java

I am trying to write this java class that opens an apk file in an android device and presses some buttons through appium,using the code below:
package new_appium_test;
import io.appium.java_client.android.AndroidDriver;
import io.appium.java_client.remote.MobileCapabilityType;
import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.concurrent.TimeUnit;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
public class new_appium_test {
public MobileDriver driver;
#Before
public void setUp() throws MalformedURLException, InterruptedException, Exception {
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability(MobileCapabilityType.PLATFORM_NAME,"Android");
capabilities.setCapability(MobileCapabilityType.DEVICE_NAME, "GT-I9300"); //specify your cellphone name
capabilities.setCapability(MobileCapabilityType.PLATFORM_VERSION,"4.3"); //specify the platform version
capabilities.setCapability("platformName", "Android");
capabilities.setCapability("appium-version", "1.2.4.1");
capabilities.setCapability("appPackage","wizzo.mbc.net");
capabilities.setCapability("appActivity","wizzo.mbc.net.activities.SplashActivity");
driver = new AndroidDriver(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);
}
driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
}
#Test
public void chooseEnglish() throws Exception
{
driver.findElement(By.name("English")).click();
}
#After
public void tearDown() throws Exception {
driver.quit();
}
}
although this failure trace appears:
java.lang.NoSuchFieldError: INSTANCE
at org.apache.http.conn.ssl.SSLConnectionSocketFactory.<clinit>(SSLConnectionSocketFactory.java:144)
at org.openqa.selenium.remote.internal.HttpClientFactory.getClientConnectionManager(HttpClientFactory.java:71)
at org.openqa.selenium.remote.internal.HttpClientFactory.<init>(HttpClientFactory.java:57)
at org.openqa.selenium.remote.internal.HttpClientFactory.<init>(HttpClientFactory.java:60)
at org.openqa.selenium.remote.internal.ApacheHttpClient$Factory.getDefaultHttpClientFactory(ApacheHttpClient.java:251)
at org.openqa.selenium.remote.internal.ApacheHttpClient$Factory.<init>(ApacheHttpClient.java:228)
at org.openqa.selenium.remote.HttpCommandExecutor.getDefaultClientFactory(HttpCommandExecutor.java:89)
at org.openqa.selenium.remote.HttpCommandExecutor.<init>(HttpCommandExecutor.java:63)
at org.openqa.selenium.remote.HttpCommandExecutor.<init>(HttpCommandExecutor.java:58)
at org.openqa.selenium.remote.RemoteWebDriver.<init>(RemoteWebDriver.java:155)
at io.appium.java_client.DefaultGenericMobileDriver.<init>(DefaultGenericMobileDriver.java:22)
at io.appium.java_client.AppiumDriver.<init>(AppiumDriver.java:202)
at io.appium.java_client.android.AndroidDriver.<init>(AndroidDriver.java:50)
at new_appium_test.new_appium_test.setUp(new_appium_test.java:34)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.junit.internal.runners.BeforeAndAfterRunner.invokeMethod(BeforeAndAfterRunner.java:74)
at org.junit.internal.runners.BeforeAndAfterRunner.runBefores(BeforeAndAfterRunner.java:50)
at org.junit.internal.runners.BeforeAndAfterRunner.runProtected(BeforeAndAfterRunner.java:33)
at org.junit.internal.runners.TestMethodRunner.runMethod(TestMethodRunner.java:75)
at org.junit.internal.runners.TestMethodRunner.run(TestMethodRunner.java:45)
at org.junit.internal.runners.TestClassMethodsRunner.invokeTestMethod(TestClassMethodsRunner.java:71)
at org.junit.internal.runners.TestClassMethodsRunner.run(TestClassMethodsRunner.java:35)
at org.junit.internal.runners.TestClassRunner$1.runUnprotected(TestClassRunner.java:42)
at org.junit.internal.runners.BeforeAndAfterRunner.runProtected(BeforeAndAfterRunner.java:34)
at org.junit.internal.runners.TestClassRunner.run(TestClassRunner.java:52)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
the problem is located on the command driver = new AndroidDriver(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);.
Can anyone please tell me why this is happening?

NoSuchfield exception is thrown if an application tries to access or modify a specified field of an object, and that object no longer has that field. If this error is happening at the Android driver instantiation, then it could be that some of capabilites you have may not be right. There is no such capability as appium-version - Link. Also device_name is ignored for Android. Try out below capabilities
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability(MobileCapabilityType.AUTOMATION_NAME,
"Selendroid");
capabilities.setCapability(MobileCapabilityType.PLATFORM_NAME, "Android");
capabilities.setCapability(MobileCapabilityType.PLATFORM_VERSION, "4.3");
capabilities.setCapability(MobileCapabilityType.APP_PACKAGE: "wizzo.mbc.net");
capabilities.setCapability(MobileCapabilityType.APP_ACTIVITY: "wizzo.mbc.net.activities.SplashActivity");

Related

How to solve: cucumber.runtime.CucumberException: Failed to instantiate class AppiumdemoDj.SignUpDj

I have checked answers to other similar questions in stack overflow but none of that helped. Could you please help me resolve the issue. I am trying to automate a mobile app. Learning the android app automation stuff. My code is below:
The project structure
Feature File:
Feature: Register for Yammer
Scenario: Sign Up for Yammer App
Given The App is launched
When I click on SignUp button
Then I should see FirstName Lastname EmailId Password field
When I click on SignUp button
Then I should see error
StringsDj.java class
package AppiumdemoDj;
public class StringsDj {
public static String signUpButton = "com.yammer.v1:id/signup_button";
public static String firstName = "com.yammer.v1:id/first_name_edittext";
public static String lastName = "com.yammer.v1:id/last_name_edittext";
public static String emailId = "com.yammer.v1:id/work_email_edittext";
public static String password = "com.yammer.v1:id/password_edittext";
}
SignUpDj.java class
package AppiumdemoDj;
import java.io.File;
import java.net.URL;
import java.util.concurrent.TimeUnit;
import org.apache.commons.io.FileUtils;
import org.openqa.selenium.By;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.remote.CapabilityType;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;
import cucumber.api.java.en.Given;
import cucumber.api.java.en.Then;
import cucumber.api.java.en.When;
import io.appium.java_client.AppiumDriver;
public class SignUpDj {
private WebDriver driver;
File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE); //Taking screenshot
#Given("^The App is launched$")
public void the_App_is_launched() throws Throwable
{
DesiredCapabilities capabilities = new DesiredCapabilities();
//capabilities.setCapability("webdriver.gecko.driver", "/Applications/Automation/dependencies/geckodriver");
capabilities.setCapability(CapabilityType.BROWSER_NAME, "");
capabilities.setCapability("deviceName", "Pixel");
capabilities.setCapability("platformVersion", "8.0.0");
capabilities.setCapability("platformName", "Android");
capabilities.setCapability("appPackage", "com.yammer.v1");
capabilities.setCapability("appActivity", "com.yammer.droid.ui.LauncherActivity");
//AppiumDriver wd = new AppiumDriver(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);
//wd.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);
driver = new RemoteWebDriver(new URL("http://127.0.0.1:4723/wd/hub"),capabilities);
driver.manage().timeouts().implicitlyWait(80, TimeUnit.SECONDS);
}
#When("^I click on SignUp button$")
public void i_click_on_SignUp_Button() throws Throwable
{
driver.findElement(By.id(StringsDj.signUpButton)).click();
FileUtils.copyFile(scrFile, new File("./ScreenShots/"));
}
#Then("^I should see FirstName Lastname EmailId Password field$")
public void i_should_see_FirstName_Lastname_EmailId_Password_field() throws Throwable
{
driver.findElement(By.id(StringsDj.firstName)).sendKeys("Test");
FileUtils.copyFile(scrFile, new File("./ScreenShots/"));
driver.findElement(By.id(StringsDj.lastName)).sendKeys("Dj");
FileUtils.copyFile(scrFile, new File("./ScreenShots/"));
driver.findElement(By.id(StringsDj.emailId)).sendKeys("reachdipanjan#gmail.com");
FileUtils.copyFile(scrFile, new File("./ScreenShots/"));
driver.findElement(By.id(StringsDj.password)).sendKeys("TestDj");
FileUtils.copyFile(scrFile, new File("./ScreenShots/"));
}
#Then("^I should see error$")
public void i_should_see_error() throws Throwable
{
System.out.println("error displayed");
}
}
The error received:
cucumber.runtime.CucumberException: Failed to instantiate class AppiumdemoDj.SignUpDj
at cucumber.runtime.java.DefaultJavaObjectFactory.cacheNewInstance(DefaultJavaObjectFactory.java:47)
at cucumber.runtime.java.DefaultJavaObjectFactory.getInstance(DefaultJavaObjectFactory.java:33)
at cucumber.runtime.java.JavaStepDefinition.execute(JavaStepDefinition.java:38)
at cucumber.runtime.StepDefinitionMatch.runStep(StepDefinitionMatch.java:37)
at cucumber.runtime.Runtime.runStep(Runtime.java:300)
at cucumber.runtime.model.StepContainer.runStep(StepContainer.java:44)
at cucumber.runtime.model.StepContainer.runSteps(StepContainer.java:39)
at cucumber.runtime.model.CucumberScenario.run(CucumberScenario.java:44)
at cucumber.runtime.junit.ExecutionUnitRunner.run(ExecutionUnitRunner.java:102)
at cucumber.runtime.junit.FeatureRunner.runChild(FeatureRunner.java:63)
at cucumber.runtime.junit.FeatureRunner.runChild(FeatureRunner.java:18)
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 cucumber.runtime.junit.FeatureRunner.run(FeatureRunner.java:70)
at cucumber.api.junit.Cucumber.runChild(Cucumber.java:95)
at cucumber.api.junit.Cucumber.runChild(Cucumber.java:38)
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 cucumber.api.junit.Cucumber.run(Cucumber.java:100)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:678)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)
Caused by: java.lang.reflect.InvocationTargetException
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
at cucumber.runtime.java.DefaultJavaObjectFactory.cacheNewInstance(DefaultJavaObjectFactory.java:41)
... 32 more
Caused by: java.lang.NullPointerException
at AppiumdemoDj.SignUpDj.<init>(SignUpDj.java:25)
... 37 more
The driver object is null on the line 25 of the SignUpDj class because it has not been initialized:
File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE); //Taking screenshot
You need to invoke that code whenever you want to take a screenshot, not on the class initialization. I am not sure what you are trying to achieve with the code below:
FileUtils.copyFile(scrFile, new File("./ScreenShots/"));
But this will not take a screenshot. You need to wrap up the call to take a screenshot in a method and call that method instead.

"org.openqa.selenium.WebDriverException" Error displayed in the code

I am unable to find what is the cause of the error "org.openqa.selenium.WebDriverException" in my code, Please help me find and understand the cause much better. Firstly I would like to explain my code, It is a simple code where I go to a website click on "Sign In" link and then click on a text "Email" text field for registration purpose. I have used Page Factory in this code.
I have 3 files.
- Base file (Before and After methods)
- Registration File (Object repository and methods)
- Registration_Testcase File (Calling the methods)
Note: Base and Registration Testcase classes are in one package and Registration is in another package.
**Below is my code:**
Base Class
package testcases;
import org.testng.annotations.BeforeTest;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxProfile;
import org.openqa.selenium.firefox.internal.ProfilesIni;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.annotations.AfterTest;
public class Base {
public WebDriver driver;
public WebDriverWait wait;
#BeforeTest
public void beforeTest() {
ProfilesIni pro = new ProfilesIni();
FirefoxProfile ffProfile = pro.getProfile("vishvesh");
ffProfile.setAcceptUntrustedCertificates(true);
ffProfile.setAssumeUntrustedCertificateIssuer(false);
String base_url = "http://automationpractice.com/index.php";
System.setProperty("webdriver.gecko.driver", "G:/Workplace/AutomationSetupFiles/Geckdriver/geckodriver.exe");
driver = new FirefoxDriver();
driver.get(base_url);
}
#AfterTest
public void afterTest() {
driver.close();
}
}
Registration Class
package pages;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.PageFactory;
import org.openqa.selenium.support.ui.ExpectedConditions;
import testcases.Base;
public class Registration extends Base{
#FindBy(xpath="//*[#id='header']/div[2]//div[1]/a")
WebElement SignIn;
#FindBy(xpath="//input[#id='email_create']")
WebElement Registration_Email;
public Registration(WebDriver driver){
this.driver=driver;
PageFactory.initElements(driver, this);
}
public void ClickSignIn(){
//wait.until(ExpectedConditions.visibilityOf(SignIn)).click();
SignIn.click();
}
public void EnterEmail(String y){
//wait.until(ExpectedConditions.visibilityOf(Registration_Email)).click();
Registration_Email.click();
Registration_Email.sendKeys(y);
}
}
Registration TestCase Class
package testcases;
import org.testng.annotations.Test;
import pages.Registration;
public class Registatration_testcase extends Base{
#Test
public void f() {
Registration regobj = new Registration(driver);
regobj.ClickSignIn();
regobj.EnterEmail("amdv1991#gmail.com");
}
}
Error What I get:
FAILED CONFIGURATION:
#BeforeTest beforeTest
org.openqa.selenium.WebDriverException:
Build info: version: 'unknown', revision: '8c03df6', time: '2017-03-02 09:32:39 -0800'
System info: host: 'LAPTOP-SCJ4OHK5', ip: '192.168.1.3', os.name: 'Windows 10', os.arch: 'amd64', os.version: '10.0', java.version: '1.8.0_131'
Driver info: org.openqa.selenium.firefox.FirefoxDriver
Capabilities [{moz:profile=C:\Users\VISHVE~1\AppData\Local\Temp\rust_mozprofile.XaA5YbSD8jeh, rotatable=false, timeouts={implicit=0, pageLoad=300000, script=30000}, pageLoadStrategy=normal, platform=ANY, specificationLevel=0, moz:accessibilityChecks=false, acceptInsecureCerts=false, browserVersion=53.0.3, platformVersion=10.0, moz:processID=7440, browserName=firefox, platformName=windows_nt}]
Session ID: 1ad0b6c1-bb5c-4306-afa6-b4e3dcb62ac1
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at org.openqa.selenium.remote.http.W3CHttpResponseCodec.createException(W3CHttpResponseCodec.java:127)
at org.openqa.selenium.remote.http.W3CHttpResponseCodec.decode(W3CHttpResponseCodec.java:93)
at org.openqa.selenium.remote.http.W3CHttpResponseCodec.decode(W3CHttpResponseCodec.java:42)
at org.openqa.selenium.remote.HttpCommandExecutor.execute(HttpCommandExecutor.java:163)
at org.openqa.selenium.remote.service.DriverCommandExecutor.execute(DriverCommandExecutor.java:82)
at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:604)
at org.openqa.selenium.remote.RemoteWebDriver.get(RemoteWebDriver.java:325)
at testcases.Base.beforeTest(Base.java:30)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:108)
at org.testng.internal.Invoker.invokeConfigurationMethod(Invoker.java:523)
at org.testng.internal.Invoker.invokeConfigurations(Invoker.java:224)
at org.testng.internal.Invoker.invokeConfigurations(Invoker.java:146)
at org.testng.TestRunner.beforeRun(TestRunner.java:626)
at org.testng.TestRunner.run(TestRunner.java:594)
at org.testng.SuiteRunner.runTest(SuiteRunner.java:380)
at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:375)
at org.testng.SuiteRunner.privateRun(SuiteRunner.java:340)
at org.testng.SuiteRunner.run(SuiteRunner.java:289)
at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
at org.testng.TestNG.runSuitesSequentially(TestNG.java:1301)
at org.testng.TestNG.runSuitesLocally(TestNG.java:1226)
at org.testng.TestNG.runSuites(TestNG.java:1144)
at org.testng.TestNG.run(TestNG.java:1115)
at org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:132)
at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:230)
at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:76)
I have seemed to have resolved this issue, Looked like there was some compatibility issues. Now I am using the following configurations
selenium 3.4
Firefox browser v48
geckodriver 17

First test fails on IntelliJ IDEA + Appium

I'm a new one to automation. I've set up an environment for test automation, but I'm unable to run my first test, errors occurs on each run. Have no idea what it is, please help me
Here is my code:
import io.appium.java_client.android.AndroidDriver;
import org.junit.After;
import org.junit.Before;
import org.openqa.selenium.By;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.testng.annotations.Test;
public class FirstTest {
AndroidDriver androidDriver;
#Before
public void setUp() throws Exception {
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability("devicename", "Testdevice");
androidDriver = new AndroidDriver(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);
}
#Test
public void logInCheck() throws Exception {
androidDriver.findElement(By.id("com.example.mariia.testing:id/button")).click();
}
#After
public void tearDown() throws Exception {
androidDriver.quit();
}
}
Here are the errors that i'm receiving:
java.lang.NullPointerException
at FirstTest.logInCheck(FirstTest.java:34)
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 org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:85)
at org.testng.internal.Invoker.invokeMethod(Invoker.java:639)
at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:816)
at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1124)
at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:125)
at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:108)
at org.testng.TestRunner.privateRun(TestRunner.java:774)
at org.testng.TestRunner.run(TestRunner.java:624)
at org.testng.SuiteRunner.runTest(SuiteRunner.java:359)
at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:354)
at org.testng.SuiteRunner.privateRun(SuiteRunner.java:312)
at org.testng.SuiteRunner.run(SuiteRunner.java:261)
at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
at org.testng.TestNG.runSuitesSequentially(TestNG.java:1215)
at org.testng.TestNG.runSuitesLocally(TestNG.java:1140)
at org.testng.TestNG.run(TestNG.java:1048)
at org.testng.IDEARemoteTestNG.run(IDEARemoteTestNG.java:74)
at org.testng.RemoteTestNGStarter.main(RemoteTestNGStarter.java:124)
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 com.intellij.rt.execution.application.AppMain.main(AppMain.java:147)
You haven't set capabilities platformName, platformVersion,app and deviceName. You're testing Android app, so You need to provide additionally capabilites appActivity and appPackage.
DesiredCapabilities caps = new DesiredCapabilities();
caps.SetCapability("app-package", "com.myapp.test");
caps.SetCapability("browserName", "");
caps.SetCapability("device", "Android");
caps.SetCapability("app-activity", "com.myapp.SplashActivity");
caps.SetCapability("version", "4.1.2");
caps.SetCapability("device ID", "uniquedeviceid");
caps.SetCapability("app", "apk-path");
To narrow down your issue, please provide your testng.xml file.

Nullpointerexception in chekcing page source with JavascriptExecutor

I need to check if text in on the website. According to driver.getPageSource() converts signs < to < I tried to avoid getPageSource(). I need to use my function in various files, so I thought I can make it in the different file and write a code just once.
This is a file, which I wrote just for checking page source:
package pl.mainfolder.tests.selenium;
import static org.junit.Assert.fail;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
public class CheckSource {
private WebDriver driver;
public void CheckSite(String textToBeChecked, String error)
throws Exception {
Object test = (((JavascriptExecutor) driver)
.executeScript("return document.body.innerHTML;"));
if (!((String) test).contains(textToBeChecked)) {
fail(error);
}
}
}
I tried to start that code with:
package pl.mainfolder.tests.selenium;
import static org.junit.Assert.fail;
import java.util.concurrent.TimeUnit;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import pl.mainfolder.tests.selenium.utils.WebDriverUtil;
public class LoginTest {
private WebDriver driver;
private String baseUrl;
private final StringBuffer verificationErrors = new StringBuffer();
public CheckSource Source = new CheckSource();
#Before
public void setUp() throws Exception {
driver = WebDriverUtil.getFirefoxWebDriver();
driver.manage().deleteAllCookies();
baseUrl = "https://google.com/";
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
}
#Test
public void Login() throws Exception {
driver.get(baseUrl);
Source.CheckSite("Log in", "Error1");
(...)
And still got error NullPointerExcepiton
java.lang.NullPointerException
at pl.mainfolder.tests.selenium.CheckSource.CheckSite(CheckSource.java:14)
at pl.mainfolder.tests.selenium.LoginPLTest.Login(LoginPLTest.java:33)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)
at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:31)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:263)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:69)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:48)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:231)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:60)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:50)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:222)
at org.junit.runners.ParentRunner.run(ParentRunner.java:292)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)
I tried with try-catch, but the code for checking wasn't working (everything was great and no problems, absolutely, even when on the site wasn't text "Logggginnnnn") and try to mix it with "if" (again the same)
Trying adding else{} made the NPE error again.
When I paste a code
public void CheckSite(String textToBeChecked, String error)
throws Exception {
Object test = (((JavascriptExecutor) driver)
.executeScript("return document.body.innerHTML;"));
if (!((String) test).contains(textToBeChecked)) {
fail(error);
}
}
directly to all files, it works perfectly.
How can I handle it?
Yeah the error is indeed because driver is not getting initialized in class CheckSource. You may want to do below change.
Change in LoginTest
#Test
public void Login() throws Exception {
driver.get(baseUrl);
Source.CheckSite("Log in", "Error1",driver);// pass the instantiated driver object.
}
In class CheckSource
public void CheckSite(String textToBeChecked, String error,WebDriver driver)
throws Exception {
Object test = (((JavascriptExecutor) driver)
.executeScript("return document.body.innerHTML;"));
if (!((String) test).contains(textToBeChecked)) {
fail(error);
}
}
This will not give you NPE, in addition it is passing test case for Text=Sign in, offered.
Note: It will fail the test if you are checking Log in as you have mentioned in your question. This is working for me. Give it a try.

java.lang.ClassNotFoundException is comming when running appium script

I am very new to appium:
i wanted to run a code where in my device i open chrome and open a google.com:
#BeforeMethod
public void setUp() throws Exception {
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability("browserName", "Browser");
capabilities.setCapability("device", "Android");
capabilities.setCapability("deviceName", "TA9330416L");
capabilities.setCapability("platformVersion", "5.1");
capabilities.setCapability("platformName", "Android");
capabilities.setCapability("appPackage", "com.android.chrome");
driver = new RemoteWebDriver(new URL("http://127.0.0.1:4723/wd/hub"),capabilities);
}
#AfterMethod
public void tearDown() throws Exception {
driver.quit();
}
#Test
public void launchWebsite()throws InterruptedException {
driver.get("http://www.google.com");
}
but getting the following error:
?* FAILED CONFIGURATION: #BeforeMethod setUp
java.lang.NoClassDefFoundError: com/google/common/base/Function
at WhatsApp.setUp(WhatsApp.java:36)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:86)
at org.testng.internal.Invoker.invokeConfigurationMethod(Invoker.java:514)
at org.testng.internal.Invoker.invokeConfigurations(Invoker.java:215)
at org.testng.internal.Invoker.invokeMethod(Invoker.java:589)
at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:820)
at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1128)
at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:129)
at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:112)
at org.testng.TestRunner.privateRun(TestRunner.java:782)
at org.testng.TestRunner.run(TestRunner.java:632)
at org.testng.SuiteRunner.runTest(SuiteRunner.java:366)
at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:361)
at org.testng.SuiteRunner.privateRun(SuiteRunner.java:319)
at org.testng.SuiteRunner.run(SuiteRunner.java:268)
at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
at org.testng.TestNG.runSuitesSequentially(TestNG.java:1246)
at org.testng.TestNG.runSuitesLocally(TestNG.java:1171)
at org.testng.TestNG.run(TestNG.java:1066)
at org.testng.remote.RemoteTestNG.run(RemoteTestNG.java:113)
at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:206)
at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:177)
Caused by: java.lang.ClassNotFoundException: com.google.common.base.Function
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
... 27 more
SKIPPED CONFIGURATION: #AfterMethod tearDown
SKIPPED: launchWebsite*/
You need to add this dependendcy jar https://code.google.com/p/guava-libraries/
Have to created any test class? Error says no class definition found. You have to create on test class first and define functions in the class. I have not tried on the android browser but I have automated android hybrid and native apps using appium.
package test;
import java.net.URL;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
import io.appium.java_client.android.AndroidDriver;
public class TestBrowser {
public AndroidDriver driver;
public TestBrowser() {
//To do
}
#BeforeMethod
public void setUp() throws Exception {
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability("appium-version", "1.0");
capabilities.setCapability("browserName", "Browser");
capabilities.setCapability("platformName", "Android");
capabilities.setCapability("deviceName", "TA9330416L");
capabilities.setCapability("platformVersion", "5.1");
capabilities.setCapability("appPackage", "com.android.chrome");
driver = new AndroidDriver(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);
}
#AfterMethod
public void tearDown() throws Exception {
if(driver!=null)
driver.quit();
}
#Test
public void launchWebsite()throws InterruptedException {
driver.get("http://www.google.com");
}
}

Categories