I've set up an Android phone emulator that contains the app komoot which I seek to perform testing on using Appium and IntelliJ to write the test. I'm working out of a macbook air. After having: started up my Android emulator, launched the Appium server, and written my test (in java) in IntelliJ, I run my test and it produces the error seen in the attached pictures. I have also copied/pasted my simple testing script and the error log messages that result from running my test "komootTest".
I am confused about the error being thrown. As per line 44 I am instantiating a variable that is an element in komoot (the login button) with the correct id. Why would it throw a null exception in this case? I can provide more details as needed, thank you!
error log image
komootTest.java code image
Here is the code for my test komootTest.java:
import io.appium.java_client.AppiumDriver;
import io.appium.java_client.android.AndroidDriver;
import org.junit.After;
import org.junit.Before;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.testng.annotations.Test;
import java.net.URL;
public class komootTest
{
AppiumDriver driver;
#Before
public void setUp() throws Exception
{
//set desired capabilities and specify device name
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability("deviceName", "BigBrother Emulator");
//capabilities.setCapability("appPackage", "de.komoot.android");
//capabilities.setCapability("appActivity", "com.google.android.gms.auth.api.signin.internal.SignInHubActivity");
//establish a connection with the server
driver = new AndroidDriver(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);
}
#After
public void end() throws Exception
{
//kill connection with server after test has been executed
driver.quit();
}
#Test
public void logInHereButton()
{
//reference UI element by ID and click it
WebElement logInHere = driver.findElement(By.id("de.komoot.android:id/textview_login"));
logInHere.click();
}
}
Here is the error message when I run komootTest.java:
[TestNG] Running:
/Users/coracoleman/Library/Caches/IdeaIC2016.1/temp-testng-customsuite.xml
java.lang.NullPointerException
at komootTest.logInHereButton(komootTest.java:44)
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:497)
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:121)
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:497)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144)
===============================================
Default Suite
Total tests run: 1, Failures: 1, Skips: 0
===============================================
Process finished with exit code 0
Problem solved! I rewrote the project by beginning a new project in IntelliJ, instead selecting Maven versus Java. I then imported the dependencies in the pom.xml file as I have included below. I had not fully specified my capabilities and I had not included my dependencies in the pom.xml file. I made sure to include the java-client-4.0.0.jar and selenium standalone server .jar file in my Project Structure/Project Settings/Library.
Here are the contents of my pom.xml file:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>AppiumTest2</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>2.53.0</version>
</dependency>
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>6.1.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.appium</groupId>
<artifactId>java-client</artifactId>
<version>4.0.0</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
Here is the simple code that I was able to successfully run:
package scenarios;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.testng.annotations.Test;
import java.net.URL;
import java.util.concurrent.TimeUnit;
public class AndroidSetup
{
WebDriver driver;
#Test
public void setUp() throws Exception
{
//create object of DesiredCapabilities class
DesiredCapabilities capabilities = new DesiredCapabilities();
//set android deviceName desired capability
capabilities.setCapability("deviceName", "BigBrother Emulator");
//set browserName desired capability
capabilities.setCapability("browserName", "Android");
//set android platformVersion desired capability
capabilities.setCapability("platformVersion", "5.1");
//set android platformName desired capability
capabilities.setCapability("platformName", "Android");
//set android appPackage desired capability
capabilities.setCapability("appPackage", "de.komoot.android");
//set android appActivity desired capability
capabilities.setCapability("appActivity", "de.komoot.android.app.InspirationActivity");
//set appium server address and port number in URL string
//this will launch app in emulator
driver = new RemoteWebDriver(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);
driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);
//click on LogIn button
driver.findElement(By.id("de.komoot.android:id/textview_login")).click();
driver.quit();
}
}
Here is the resulting execution log:
[TestNG] Running:
/Users/coracoleman/Library/Caches/IdeaIC2016.1/temp-testng-customsuite.xml
===============================================
Default Suite
Total tests run: 1, Failures: 0, Skips: 0
===============================================
Process finished with exit code 0
Related
I am having this error when I am trying to use selenium via github actions. There is no problem with the local tests, probably I am not installing the chrome properly.
Could not start a new session. Response code 500. Message: unknown error: Chrome failed to start: exited abnormally.
I checked the versions of the driver and the chrome they are compatible, both using version 103. I searched and I couldn't find the problem.
ci.yml file
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout#v3
- name: Set up JDK 17
uses: actions/setup-java#v3
with:
java-version: '17'
distribution: 'adopt'
- name: Install Google Chrome # Using shell script to install Google Chrome
run: |
chmod +x ./scripts/InstallChrome.sh
./scripts/InstallChrome.sh
- name: Test with Maven
run: mvn clean test
The test
import connect.Connect;
import io.github.bonigarcia.wdm.WebDriverManager;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
public class DummyTest {
WebDriver driver;
#org.junit.Test
public void test() throws InterruptedException {
WebDriverManager.chromedriver().setup();
ChromeOptions opt = new ChromeOptions();
opt.addArguments("--no-sandbox");
opt.addArguments("--disable-dev-shm-usage");
opt.addArguments("--headless");
driver = new ChromeDriver(opt);
WebDriver driver = new ChromeDriver();
driver.get("https://www.amazon.com.tr/");
driver.findElement(By.xpath("/html/body/div[1]/header/div/div[4]/div[2]/div/div/a[5]")).click();
}
InstallChrome.sh
#!/bin/bash
set -ex
wget https://dl.google.com/linux/direct/google-chrome-stable_current_amd64.deb
sudo apt install ./google-chrome-stable_current_amd64.deb
Dependencies
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-chrome-driver</artifactId>
<version>4.3.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/io.github.bonigarcia/webdrivermanager -->
<dependency>
<groupId>io.github.bonigarcia</groupId>
<artifactId>webdrivermanager</artifactId>
<version>5.2.1</version>
</dependency>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>4.2.1</version>
</dependency>
I have a maven project where in I am running testng.xml from pom.xml file.
My testng.xml file looks like below:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Suite" parallel="tests">
<test name="FirefoxTest">
<parameter name="browser" value="firefox"/>
<classes>
<class name="tests.PM_User_Test"/>
</classes>
</test> <!-- Test -->
<test name="ChromeTest">
<parameter name="browser" value="chrome"/>
<classes>
<class name="tests.PM_User_Test"/>
<class name="tests.PM_Extension_Test"/>
</classes>
</test> <!-- Test -->
</suite> <!-- Suite -->
and below is my class:
package base;
import java.io.IOException;
import java.lang.reflect.Method;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.UnexpectedAlertBehaviour;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.remote.CapabilityType;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.ITestResult;
import org.testng.annotations.AfterClass;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.AfterSuite;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.BeforeSuite;
import org.testng.annotations.Optional;
import org.testng.annotations.Parameters;
import utilities.ExcelReadAndWrite;
import utilities.Take_Screenshot;
public class ConfigClass
{
public WebDriver driver = null;
public static String excelPath = null;
public static ExcelReadAndWrite loginData;
public static ExcelReadAndWrite pmTests;
public static ExcelReadAndWrite snmTests;
public static ExcelReadAndWrite ipData;
#BeforeSuite
public void beforeSuite()
{
excelPath = "C://Users//mallikar//git//PM//PM//src//main//java//myTestData//TestData.xlsx";
loginData = new ExcelReadAndWrite("logindata", excelPath);
pmTests = new ExcelReadAndWrite("PMTestData", excelPath);
snmTests = new ExcelReadAndWrite("SNMTestData", excelPath);
ipData = new ExcelReadAndWrite("IP", excelPath);
}
#AfterSuite
public void afterSuite()
{
}
#Parameters({"browser"})
#BeforeClass
public void beforeClass(String browser)
{
DesiredCapabilities dc = new DesiredCapabilities();
dc.setCapability(CapabilityType.UNEXPECTED_ALERT_BEHAVIOUR, UnexpectedAlertBehaviour.ACCEPT);
if(browser.equalsIgnoreCase("firefox"))
{
driver = new FirefoxDriver(dc);
}
else if(browser.equalsIgnoreCase("chrome"))
{
driver = new ChromeDriver();
}
}
/*#Parameters("browser")
#BeforeClass
public void beforeClass()
{
DesiredCapabilities dc = new DesiredCapabilities();
dc.setCapability(CapabilityType.UNEXPECTED_ALERT_BEHAVIOUR, UnexpectedAlertBehaviour.ACCEPT);
driver = new FirefoxDriver(dc);
}*/
#AfterClass
public void afterClass()
{
driver.close();
}
#BeforeMethod
public void beforeMethod()
{
// driver.get("https://sqa.stackexchange.com/questions/36253/taking-screenshot-on-test-failure-selenium-webdriver-testng");
// System.out.println(driver);
driver.manage().window().maximize();
driver.manage().deleteAllCookies();
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
}
#AfterMethod
public void afterMethod(ITestResult result) throws IOException
{
if(result.getStatus() == 2)
{
String methodName = result.getMethod().getMethodName();
new Take_Screenshot().get_Screenshot(driver, methodName);
}
}
}
But I am getting the "Parameter 'browser' is required by BeforeClass on method beforeClass but has not been marked #Optional or defined" error in maven project.
However when I run only testng.xml file it is absolutely working fine. The pro
blem comes when I run testng.xml from pom.xml file.
Below is the content of POM.xml file where I have defined testng.xml:
<profiles>
<profile>
<id>Regression</id>
<build>
<plugins>
<plugin>
<inherited>true</inherited>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.12.2</version>
<configuration>
<suiteXmlFiles>
<suiteXmlFile>src/test/resources/testng.xml</suiteXmlFile>
</suiteXmlFiles>
</configuration>
</plugin>
</plugins>
</build>
</profile>
</profiles>
I have gone through lot of threads about this on internet but I haven't found any solution. Please somebody help me with this.
And below are the console logs when I run mvn clean install command
Running TestSuite
Configuring TestNG with: org.apache.maven.surefire.testng.conf.TestNG652Configurator#ba4d54
Tests run: 10, Failures: 2, Errors: 0, Skipped: 8, Time elapsed: 2.368 sec <<< FAILURE!
beforeClass(tests.PM_Extension_Test) Time elapsed: 2.098 sec <<< FAILURE!
org.testng.TestNGException:
Parameter 'browser' is required by BeforeClass on method beforeClass but has not been marked #Optional or defined
at org.testng.internal.Parameters.createParams(Parameters.java:289)
at org.testng.internal.Parameters.createParametersForMethod(Parameters.java:377)
at org.testng.internal.Parameters.createParameters(Parameters.java:721)
at org.testng.internal.Parameters.createConfigurationParameters(Parameters.java:164)
at org.testng.internal.ConfigInvoker.invokeConfigurations(ConfigInvoker.java:275)
at org.testng.internal.TestMethodWorker.invokeBeforeClassMethods(TestMethodWorker.java:176)
at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:122)
at java.util.ArrayList.forEach(ArrayList.java:1257)
at org.testng.TestRunner.privateRun(TestRunner.java:770)
at org.testng.TestRunner.run(TestRunner.java:591)
at org.testng.SuiteRunner.runTest(SuiteRunner.java:402)
at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:396)
at org.testng.SuiteRunner.privateRun(SuiteRunner.java:355)
at org.testng.SuiteRunner.run(SuiteRunner.java:304)
at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:53)
at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:96)
at org.testng.TestNG.runSuitesSequentially(TestNG.java:1180)
at org.testng.TestNG.runSuitesLocally(TestNG.java:1102)
at org.testng.TestNG.runSuites(TestNG.java:1032)
at org.testng.TestNG.run(TestNG.java:1000)
at org.apache.maven.surefire.testng.TestNGExecutor.run(TestNGExecutor.java:77)
at org.apache.maven.surefire.testng.TestNGDirectoryTestSuite.executeMulti(TestNGDirectoryTestSuite.java:159)
at org.apache.maven.surefire.testng.TestNGDirectoryTestSuite.execute(TestNGDirectoryTestSuite.java:99)
at org.apache.maven.surefire.testng.TestNGProvider.invoke(TestNGProvider.java:106)
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.apache.maven.surefire.util.ReflectionUtils.invokeMethodWithArray(ReflectionUtils.java:189)
at org.apache.maven.surefire.booter.ProviderFactory$ProviderProxy.invoke(ProviderFactory.java:165)
at org.apache.maven.surefire.booter.ProviderFactory.invokeProvider(ProviderFactory.java:85)
at org.apache.maven.surefire.booter.ForkedBooter.runSuitesInProcess(ForkedBooter.java:115)
at org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:75)
beforeClass(tests.PM_User_Test) Time elapsed: 0.002 sec <<< FAILURE!
org.testng.TestNGException:
Parameter 'browser' is required by BeforeClass on method beforeClass but has not been marked #Optional or defined
at org.testng.internal.Parameters.createParams(Parameters.java:289)
at org.testng.internal.Parameters.createParametersForMethod(Parameters.java:377)
at org.testng.internal.Parameters.createParameters(Parameters.java:721)
at org.testng.internal.Parameters.createConfigurationParameters(Parameters.java:164)
at org.testng.internal.ConfigInvoker.invokeConfigurations(ConfigInvoker.java:275)
at org.testng.internal.TestMethodWorker.invokeBeforeClassMethods(TestMethodWorker.java:176)
at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:122)
at java.util.ArrayList.forEach(ArrayList.java:1257)
at org.testng.TestRunner.privateRun(TestRunner.java:770)
at org.testng.TestRunner.run(TestRunner.java:591)
at org.testng.SuiteRunner.runTest(SuiteRunner.java:402)
at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:396)
at org.testng.SuiteRunner.privateRun(SuiteRunner.java:355)
at org.testng.SuiteRunner.run(SuiteRunner.java:304)
at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:53)
at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:96)
at org.testng.TestNG.runSuitesSequentially(TestNG.java:1180)
at org.testng.TestNG.runSuitesLocally(TestNG.java:1102)
at org.testng.TestNG.runSuites(TestNG.java:1032)
at org.testng.TestNG.run(TestNG.java:1000)
at org.apache.maven.surefire.testng.TestNGExecutor.run(TestNGExecutor.java:77)
at org.apache.maven.surefire.testng.TestNGDirectoryTestSuite.executeMulti(TestNGDirectoryTestSuite.java:159)
at org.apache.maven.surefire.testng.TestNGDirectoryTestSuite.execute(TestNGDirectoryTestSuite.java:99)
at org.apache.maven.surefire.testng.TestNGProvider.invoke(TestNGProvider.java:106)
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.apache.maven.surefire.util.ReflectionUtils.invokeMethodWithArray(ReflectionUtils.java:189)
at org.apache.maven.surefire.booter.ProviderFactory$ProviderProxy.invoke(ProviderFactory.java:165)
at org.apache.maven.surefire.booter.ProviderFactory.invokeProvider(ProviderFactory.java:85)
at org.apache.maven.surefire.booter.ForkedBooter.runSuitesInProcess(ForkedBooter.java:115)
at org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:75)
Results :
Failed tests: beforeClass(tests.PM_Extension_Test): (..)
beforeClass(tests.PM_User_Test): (..)
Tests run: 10, Failures: 2, Errors: 0, Skipped: 8
[WARNING] Could not delete temp direcotry C:\Users\mallikar\git\PM\PM\target\surefire because Directory C:\Users\mallikar\git\PM\PM\target\surefire unable to be deleted.
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 10.374 s
[INFO] Finished at: 2019-09-26T16:39:22+05:30
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.12.4:test (default-test) on project PM: There are test failures.
[ERROR]
[ERROR] Please refer to C:\Users\mallikar\git\PM\PM\target\surefire-reports for the individual test results.
[ERROR] -> [Help 1]
[ERROR]
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR]
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoFailureException
Please try doing one of the following (which should fix your problem )
Make sure that the annotation #Parameters is imported from the package org.testng.annotations
Make sure you adhere to one of the following, when running the test:
If you are running from the IDE, by right clicking on the class and choosing run as TestNG test then make sure that you edit your run configuration and provide the JVM argument -Dbrowser=firefox
If you would like to run the entire suite, then you would need to make sure that you right click on the suite and then run it.
If you are running from the commandline using maven, then make sure that you are using mvn clean test -P Regression (This would ensure that the profile Regression gets activated and through that, the test execution happens via the suite file)
For me adding configuration for xml suite inside maven-surefire-plugin worked.
I have added the below configuration.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.12.2</version>
<configuration>
<suiteXmlFiles>
<suiteXmlFile>location-to-testng-xml-file</suiteXmlFile>
</suiteXmlFiles>
</configuration>
</plugin>
Hi I am creating an appium framework with testNG and I was copying a drag n drop android test to use in my framework as a basic example.
For some reason 'driver' has an error message next to it (see below) and I'm confused why I would get that as I have selenium in my pom.xml file ( see below). I also imported selenium (see below too). I'm really confused why I have this message. I am also adding the test case with this question.
I am wondering:
Did I forget to import something into my test?
Did I forget to add something to my .pom xml file?
Why would error message appear when
the test it originates from works perfectly (I created that test
too)?
Pom xml file
<dependencies>
<!-- https://mvnrepository.com/artifact/io.appium/java-client -->
<dependency>
<groupId>io.appium</groupId>
<artifactId>java-client</artifactId>
<version>5.0.4</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-java -->
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>3.11.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.testng/testng -->
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>6.11</version>
<scope>test</scope>
</dependency>
</dependencies> <artifactId>testng</artifactId>
<version>6.11</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
Test
package android;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
import java.net.MalformedURLException;
import java.time.Duration;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.interactions.touch.TouchActions;
import io.appium.java_client.TouchAction;
import io.appium.java_client.android.AndroidDriver;
import io.appium.java_client.android.AndroidElement;
import org.openqa.selenium.WebDriver;
public class Dragndroptest extends invoke {
//Given I am on the Home screen
//When I tap Views
//Then I will see the View menu
//When I am in the 'view menu' I will select 'drag n drop'
//Then I will see 3 dots
//When I select a dot and drag it
//Then I will drop it on the dot below
#BeforeTest
public void Cookies() throws MalformedURLException
{
AndroidDriver<AndroidElement> driver =Capabilities();
driver.findElementByAccessibilityId("Views").click();
}
#Test
public void dragnddrop()
{
//tap syntax using TOUCHACTIONS
TouchAction t = new TouchAction (driver);
t.tap(driver.findElementByAccessibilityId("Drag and Drop")).perform();
//Drag & Drop Syntax
t.longPress(driver.findElementById("io.appium.android.apis:id/drag_dot_1")).
moveTo(driver.findElementById("io.appium.android.apis:id/drag_dot_3")).release().perform();
}
#AfterTest
public void Testtitle here()
{
}
}
You must define your AppiumDriver outside of your method. You must define desired capabilities. Using desiredCapabilities, it will know which app to open and in which device we should run it. You must enable appium server defined in your driver.
public class Dragndroptest extends invoke {
AppiumDriver<MobileElement> driver;
#BeforeSuite(alwaysRun=true)
public void setupDesiredCaps(){
DesiredCapabilities caps = new DesiredCapabilities();
caps.setCapability("deviceName", "Any name");
caps.setCapability("udid", "your device udid");
caps.setCapability("platformName", "Android");
caps.setCapability("platformVersion", "your device platform version");
caps.setCapability("appPackage", "your app Package name");
caps.setCapability("appActivity", "your app's main package name");
driver = new AndroidDriver<MobileElement>(new URL(
"http://127.0.0.1:4723/wd/hub"), caps);
}
//your other code after it
}
I'm writing a test class for an Android App, but I keep getting the error code "The project was not built since its build path is incomplete. Cannot find the class file for org.openqa.selenium.html5.BrowserConnection. Fix the build path then try building this project." each time I try to add the selenium Android driver jar file to the project. I need the Android driver to use the TestNG with Appium properly, otherwise Appium can't find elements on the page when testing. The Code:
package appiumTest;
import java.net.MalformedURLException;
import java.net.URL;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.android.*;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.testng.annotations.*;
public class TestCase {
public class NewTest {
WebDriver driver = new AndroidDriver();
#BeforeClass
protected void setUp() throws MalformedURLException {
//Tells the program what device is used
DesiredCapabilities capabilities = new
DesiredCapabilities();
capabilities.setCapability("BROWSER_NAME", "Android");
capabilities.setCapability("VERSION", "4.2.2");
capabilities.setCapability("deviceName","Emulator");
capabilities.setCapability("platformName","Android");
//Tells the program what app is being run
capabilities.setCapability("appPackage",
"com.android.testapp2");
capabilities.setCapability("appActivity",
"com.android.testapp2.MainActivity ");
driver = (AndroidDriver) new RemoteWebDriver(new
URL("http://127.0.0.1:4723/wd/hub"), capabilities);
}
#Test
public void test() {
WebElement userName = driver.findElement(By.id("txtUserName"));
userName.sendKeys("a");
WebElement button = driver.findElement(By.id("btnEnter"));
button.click();
};
#AfterClass
protected void tearDown() throws Exception {
driver.quit();
}
}
}
Here's the code of the pom.xml file I'm trying to edit:
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>TestProject</groupId>
<artifactId>TestProject</artifactId>
<version>0.0.1-SNAPSHOT</version>
<build>
<sourceDirectory>src</sourceDirectory>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.3</version>
<configuration>
<source/>
<target/>
</configuration>
</plugin>
</plugins>
</build>
</project>
I don't see anywhere to put a dependency, and I'm not even sure if that's what I'm supposed to be doing.
Ok, this should get you going again, I think.
The improvements made in the pom.xml:
No dependencies where definded, so added the required onces
testng v6.9.8
selenium-java v2.39.0
selenium-android-driver v2.39.0
Remark 1: The Selenium Java and Android libraries, should be of the same version (for as far as I know, otherwise it can give issues).
_Remark 2:_The Selenium Android library, is an older library. It should actually be replaced by the newer one Selendroid. Therefor also some import would change.
Improved the <groupid> and <artifactid>, these are standard in lower case, and should represent some logical and unique thing. Maybe you store you project on bitbucket, or github, then a logical group id would start with com.github.<your-github-account-id>.<name-of-the-project>.
Removed the <sourceDirectory>src</sourceDirectory>
Created a directory src/test/java
Added the package name appiumTest to that test source directory
added the class TestCase in that package.
The Maven project file pom.xml:
Remark Below is the correct Maven starting tag <project> shown, as it should be used in the pom.xml. But StackOverflow, does not show it correctly, when the starting tag has attributes.
project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
Below the corrected pom.xml, but without the correct starting tag.
<project>
<modelVersion>4.0.0</modelVersion>
<groupId>org.organisation.project</groupId>
<artifactId>android-selenium</artifactId>
<version>0.1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>6.9.8</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<!--
<version>2.48.2</version>
-->
<version>2.39.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-android-driver</artifactId>
<version>2.39.0</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.3</version>
<configuration>
<source />
<target />
</configuration>
</plugin>
</plugins>
</build>
</project>
The improvements made in the class TestCase:
No need to use of inner class NewTest, so removed it.
Removed initialization of field driver, as it gets initialized in the #BeforeClass method.
Added check in the #AfterClass, only call driver.quit(), when driver is initalize.
The class TestCase:
package appiumTest;
import java.net.MalformedURLException;
import java.net.URL;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.android.*;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.testng.annotations.*;
public class TestCase {
WebDriver driver; // = new AndroidDriver();
#BeforeClass
protected void setUp() throws MalformedURLException {
// Tells the program what device is used
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability("BROWSER_NAME", "Android");
capabilities.setCapability("VERSION", "4.2.2");
capabilities.setCapability("deviceName", "Emulator");
capabilities.setCapability("platformName", "Android");
// Tells the program what app is being run
capabilities.setCapability("appPackage", "com.android.testapp2");
capabilities.setCapability("appActivity",
"com.android.testapp2.MainActivity");
driver = (AndroidDriver) new RemoteWebDriver(new URL(
"http://127.0.0.1:4723/wd/hub"), capabilities);
}
#Test
public void test() {
WebElement userName = driver.findElement(By.id("txtUserName"));
userName.sendKeys("a");
WebElement button = driver.findElement(By.id("btnEnter"));
button.click();
};
#AfterClass
protected void tearDown() throws Exception {
if (driver != null) {
driver.quit();
}
}
}
Possible Improvements
Correct the package name (currently appiumTest), but that is not inline with the nameing convention, all lower case, seperate with a dot .. And something meaningful, similar as to the Maven group-id. Sp start with com.github.<your-github-account-id>.<name-of-the-project>
Give the class TestCase a better name
Update to Selendroid, then also update the Selenium Java version
I'm just starting out writing out Java tests to automate native Android apps using Appium and JUnit. Getting a NoClassDefFound error even if the required jars are on class path in Eclipse GUI. I've narrowed it down to this line :
driver = new RemoteWebDriver(new URL("appium typical server url here"), capabilities);
The following jars are in the Java build path in Eclipse under Libraries: java-client-1.5.0.jar, selenium-java-2.42.2.jar, selenium-java-2.42.2-srcs.jar, along with JUnit
Here is my complete code:
package com.chinmay.automation;
import java.io.File;
import java.net.URL;
import org.openqa.selenium.By;
import org.openqa.selenium.Capabilities;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.remote.CapabilityType;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;
import static org.junit.Assert.*;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
public class ChessTest {
private WebDriver driver=null;
#Before
public void setUp() throws Exception{
File appDir= new File("/Users/csathe/Desktop/Java");
File app = new File(appDir, "ChessFree.apk");
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability(CapabilityType.BROWSER_NAME, "");
capabilities.setCapability(CapabilityType.VERSION, "4.3");
capabilities.setCapability(CapabilityType.PLATFORM, "Mac");
capabilities.setCapability("device", "android");
capabilities.setCapability("app-package", "uk.co.aifactory.chessfree");
capabilities.setCapability("app-activity", "ChessFreeActivity");
capabilities.setCapability("app", app.getAbsolutePath());
driver= new RemoteWebDriver(new URL("//appium typical server url here"), capabilities);
}
#Test
public void testFirst(){
// do something
}
#After
public void tearDown(){
// driver.quit();
}
}
If I run this using JUnit with that line commented, it runs fine (does nothing however).
The stacktrace with the line:
java.lang.NoClassDefFoundError: com/google/common/base/Function
at com.chinmay.automation.ChessTest.setUp(ChessTest.java:39)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
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.RunBefores.evaluate(RunBefores.java:27)
at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:31)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:71)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:49)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184)
at org.junit.runners.ParentRunner.run(ParentRunner.java:236)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
at
Caused by: java.lang.ClassNotFoundException: com.google.common.base.Function
at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
... 25 more
Running this one class outside JUnit leads to the same error.
This error clearly indicates that any jar dependency is missing from the build path.
Please add all the jars available here to the class path :
http://selenium-release.storage.googleapis.com/2.42/selenium-java-2.42.2.zip
OR
Download the Selenium-server-standalone.jar from here and add to the class path:
http://selenium-release.storage.googleapis.com/2.42/selenium-server-standalone-2.42.2.jar
You can retry by selecting mentioned JAR files under Order and Export tab. Can be accessed in the way mentioned below:
Right click on the project -> Build Path -> Configure Build Path -> Order and Export -> Select required jars (e.g. Selenium,java etc.) and click on OK.
Lemme know if this works for you!