A new session could not be created. (Original error: The following desired capabilities are required, but were not provided: platformName,deviceName)" - appium

Here is my Java code:
import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;
import io.appium.java_client.android.AndroidDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.testng.annotations.Test;
public class AppiumCapabilities {
#Test
public void testAppium() throws MalformedURLException{
AndroidDriver driver;
DesiredCapabilities capability = new DesiredCapabilities();
capability.setCapability("platformName", "Android");
capability.setCapability("deviceName", "Nexus 5");
capability.setCapability("udid","039e8ae813aa787d");
capability.setCapability("platformVersion", "6.0.1");
File file = new File("C:\\Users\\ShahreenMushtaq\\workspace1\\Appium\\zameenapk\\app-zameen-
live-release.apk");
capability.setCapability("app", file.getAbsolutePath());
driver = new AndroidDriver(new URL("http://192.168.100.12:4723/wd/hub"),capability);
}
}
Error:
info: [debug] Responding to client with error: {"status":33,"value":{"message":"A new session could not be created. (Original error: The following desired capabilities are required, but were not provided: platformName, deviceName)","origValue":"The following desired capabilities are required, but were not provided: platformName, deviceName"},"sessionId":null}
info: <-- POST /wd/hub/session 500 3.228 ms - 314
I have already restarted the Appium with Override Existing Session. My Appium settings are shown below:

Related

Appium parallel test threadlocal

I am trying to run parallel test through selenium grid.
I know I have to use "thread local" for parallel execution,
but I have a problem with my code.
Cannot invoke "io.appium.java_client.android.AndroidDriver.findElementByAccessibilityId(String)" because "driver" is null
can you please solve it
package appiumset;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Parameters;
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.AndroidMobileCapabilityType;
import io.appium.java_client.remote.MobileCapabilityType;
public class _2_Deviceinfo {
public ThreadLocal<AppiumDriver> driver = new ThreadLocal<>();
public void setDriver(AppiumDriver driver) {
this.driver.set(driver);
}
public AppiumDriver getDriver() {
return this.driver.get();
}
#Parameters({"device", "apppackage", "activity","version","appiumServer" , "systemPort", "platformName"})
#BeforeMethod
public synchronized void deviceSetUp(String device, String apppackage, String activity, String version, String appiumServer, String systemPort, String platformName) throws InterruptedException, MalformedURLException {
System.out.println("****************************************");
System.out.println("Setting up device and desired capabilities");
DesiredCapabilities cap = new DesiredCapabilities();
URL url = new URL(appiumServer);
setDriver(new AndroidDriver<>(url, cap));
cap.setCapability(MobileCapabilityType.DEVICE_NAME, device);
cap.setCapability(MobileCapabilityType.UDID, device);
cap.setCapability(AndroidMobileCapabilityType.SYSTEM_PORT, systemPort);
cap.setCapability(MobileCapabilityType.NEW_COMMAND_TIMEOUT, 120);
cap.setCapability(MobileCapabilityType.PLATFORM_NAME, platformName);
//cap.setCapability(MobileCapabilityType., BrowserType.ANDROID);
cap.setCapability(MobileCapabilityType.PLATFORM_VERSION, version);
cap.setCapability(AndroidMobileCapabilityType.APP_PACKAGE, apppackage);
cap.setCapability(AndroidMobileCapabilityType.APP_ACTIVITY, activity);
cap.setCapability("automationName", "UiAutomator2");
cap.setCapability("noReset","false");
cap.setCapability("FullReset","true");
cap.setCapability("APP_WAIT_ACTIVITY", "*");
cap.setCapability("autowebview","false");
}
#AfterMethod
public void closeDriver() {
getDriver().quit();
}
}
I can't find the driver (AppiumDriver)
package appiumset;
import java.net.MalformedURLException;
import java.util.concurrent.TimeUnit;
import org.testng.annotations.Test;
import io.appium.java_client.AppiumDriver;
import io.appium.java_client.MobileElement;
import io.appium.java_client.android.AndroidDriver;
public class _3_Onboarding extends _1_Appstart {
#Test
public void onboarding() throws MalformedURLException, InterruptedException {
System.out.println("_3_Onboarding Start");
MobileElement arrow = driver.findElementByAccessibilityId("next");
arrow.click();
System.out.println("next-done");
}
}
call getdriver() method of your class _2_Deviceinfo to your test class.
Appiumdriver<?> driver = getDriver();
MobileElement arrow = driver.findElementByAccessibilityId("next");
arrow.click();

spring-data-elasticsearch latest version throws NullPointer exception with Elasticsearch 8.1 version

Planning to use elasticsearch 8.1 version and use 'org.springframework.boot:spring-boot-starter-data-elasticsearch' in
our project.
Repository.save() throws following exception.
java.lang.NullPointerException: null
at java.base/java.util.Objects.requireNonNull(Objects.java:221)
at org.elasticsearch.action.DocWriteResponse.(DocWriteResponse.java:116)
at org.elasticsearch.action.index.IndexResponse.(IndexResponse.java:43)
The same code with Elasticsearch 7.15.2 works fine.
I see the supported matrix here https://docs.spring.io/spring-data/elasticsearch/docs/current/reference/html/#preface.requirements
Where to see the road map of Spring boot elasticsearch data plugin? When Do we get the plugin support for the 8.1 version of
Elasticsearch?
Thanks in advance
Adding the following headers resolved the issue.
Accept: "application/vnd.elasticsearch+json;compatible-with=7"
Content-Type: "application/vnd.elasticsearch+json;compatible-with=7"
Its better to change your code which is used for creating client
package com.search.elasticsearchapp.config;
import org.apache.http.Header;
import org.apache.http.HttpHost;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.apache.http.message.BasicHeader;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestClientBuilder;
import org.elasticsearch.client.RestHighLevelClient;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.elasticsearch.repository.config.EnableElasticsearchRepositories;
import org.springframework.http.HttpHeaders;
#Configuration
#EnableElasticsearchRepositories(basePackages = "*")
public class TestClient {
#Value("${elasticsearch.host}")
private String host;
#Value("${elasticsearch.port}")
private int port;
#Value("${elasticsearch.protocol}")
private String protocol;
#Value("${elasticsearch.username}")
private String userName;
#Value("${elasticsearch.password}")
private String password;
#Bean(destroyMethod = "close")
public RestHighLevelClient restClient() {
final CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(userName, password));
RestClientBuilder builder = RestClient.builder(new HttpHost(host, port, protocol))
.setHttpClientConfigCallback(httpClientBuilder -> httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider))
.setDefaultHeaders(compatibilityHeaders());
return new RestHighLevelClient(builder);
}
private Header[] compatibilityHeaders() {
return new Header[]{new BasicHeader(HttpHeaders.ACCEPT, "application/vnd.elasticsearch+json;compatible-with=7"), new BasicHeader(HttpHeaders.CONTENT_TYPE, "application/vnd.elasticsearch+json;compatible-with=7")};
}
}

How can i launch an iOS app which installed already using appium

In order to test the duplication in registering,I need to close and relaunch the iOS application.Can any one provide the best and simple scripts to close an application and relaunch the same in iOS using Appium and JAVA?
You can achieve the following way :
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;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;
public class SampleTest {
AppiumDriver<?> appiumDriver;
final String URL_STRING = "http://127.0.0.1:4723/wd/hub";
URL url = new URL(URL_STRING);
public SampleTest() throws MalformedURLException {
}
#BeforeTest
public void beforeTest() {
File appDir = new File("path to ipa file");
File app = new File(appDir, "app.ipa");
DesiredCapabilities iOSCapabilities = new DesiredCapabilities();
iOSCapabilities.setCapability(MobileCapabilityType.PLATFORM, "iOS");
iOSCapabilities.setCapability(MobileCapabilityType.AUTOMATION_NAME, "XCUITest");
iOSCapabilities.setCapability(MobileCapabilityType.DEVICE_NAME, "iPhone6s");
iOSCapabilities.setCapability(MobileCapabilityType.PLATFORM_VERSION, "11.2.1");
iOSCapabilities.setCapability(MobileCapabilityType.UDID, "udid");
iOSCapabilities.setCapability(MobileCapabilityType.APP, app.getAbsolutePath());
appiumDriver = new IOSDriver(url, iOSCapabilities);
}
#Test
public void test() {
//perform some test
}
#AfterTest
public void afterTest() {
// following command will close and restart the app
appiumDriver.resetApp();
}
}
You may want to check this for more details.
use the driver driver.resetApp(), like this
try{
LOG.debug("reset app");
driver.resetApp();
} catch (WebDriverException e){
LOG.error("ERROR on reset app, e: ",e);
}
Appium allows you to start any pre-installed app on the device, so you can use closeApp first and then launchApp with your app bundle id:
// closing app
driver.closeApp();
// launch app again
HashMap<String, Object> args = new HashMap<>();
args.put("bundleId", APP_BUNDLE_ID);
driver.executeScript("mobile: launchApp", args);

I want to scroll down to Specific element using appium in Android, java client v-5.0.4 and appium v-1.7.1

I am trying to scroll down to an element, looked and searched everywhere and no code is helping to scroll down, i have tried with the code below which seems to be not working, anyone gives me the solution to scroll down perfectly.
As swipe and scrollTo functions are depreciated in the latest java client version, a perfect code will help me to solve my TASK
package mobileapp.com.example;
import java.net.MalformedURLException;
import java.net.URL;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
import io.appium.java_client.android.AndroidDriver;
import io.appium.java_client.pagefactory.AndroidFindBy;
public class Day5 {
AndroidDriver<WebElement> driver;
#AndroidFindBy (uiAutomator = "new UiScrollable(new UiSelector()).scrollIntoView(new UiSelector().textContains(\"PHP\"))")
public WebElement scrollStepOne;
#BeforeTest
public void setup() throws MalformedURLException {
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability("BROWSER_NAME", "Android");
capabilities.setCapability("VERSION", "6.0.1");
capabilities.setCapability("deviceName","Nexus 5");
capabilities.setCapability("platformName","Android");
capabilities.setCapability("appPackage", "com.vector.guru99");
capabilities.setCapability("appActivity","com.vector.guru99.BaseActivity");
driver= new AndroidDriver<WebElement>(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);
}
#Test
public void StartTest() throws InterruptedException {
//Verify Homepage
if(driver.findElement(By.id("android:id/action_bar_title")).isDisplayed())
System.out.println("Home page is displayed");
else
System.out.println("Home page is not displayed");
//step2 - click on Course List tab
driver.findElement(By.name("Course List")).click();
System.out.println("Courses list are : ");
Thread.sleep(3000);
//Step 3 - darg until PHP course found and click on it
scrollStepOne.click();
Thread.sleep(3000);
// driver.findElement(By.xpath("//android.widget.TextView[#text()='PHP']")).click();
// Thread.sleep(3333);
//Step 4 - Click on lesson 1 and verify
driver.findElement(By.xpath("//android.widget.TextView[#text='What is PHP? Write your first PHP Program']")).click();
Thread.sleep(3333);
if(driver.findElement(By.id("com.vector.guru99:id/lesson_title")).isDisplayed())
System.out.println("First Lesson is displayed");
else
System.out.println("First lesson not opened");
}
#AfterTest
public void tearDown() {
driver.quit();
}
}
for scrolling to a element have a text as App_Settings, you can use the below
androidDriver.findElementByAndroidUIAutomator("new UiScrollable(new UiSelector().scrollable(true).instance(0))" +
".scrollIntoView(new UiSelector().description(\"App_Settings\"));");

How to start appium test on iOS simulator?

I have the following setup and trying to start the test on iOS Simulator. I am a beginner and don't know how to start the test. I have already imported and installed appium from tutorials.
The questions are:
Is this setup correct?
How to run the tests?
import java.io.File;
import java.net.URL;
import java.util.HashMap;
import java.util.List;
import org.openqa.selenium.By;
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 org.testng.Assert;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.remote.CapabilityType;
import org.openqa.selenium.remote.DesiredCapabilities;
public class AppiumDriver {
private static final String JavascriptExecutor = null;
public WebDriver driver = null;
#BeforeMethod
public void setUp() throws Exception {
// set up appium
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability(CapabilityType.BROWSER_NAME, "iOS");
capabilities.setCapability(CapabilityType.VERSION, "8.1");
capabilities.setCapability(CapabilityType.PLATFORM, "Mac");
capabilities.setCapability("device", "iPhone");
capabilities.setCapability("app", "path here, i have started it with appium inspector and it works");
driver = new RemoteWebDriver(new URL("http://127.0.0.1:4725/wd/hub"), capabilities);
System.out.println("App launched");
}
#Test
public void test01() throws InterruptedException {
driver.findElement(By.name("Guest")).click();
Thread.sleep(5000);
}
#AfterMethod
public void tearDown() throws Exception {
driver.quit();
}
}
Yes, your setup is correct. Looks like you are using testng framework so just run it as "testng" and you should be good to go.

Resources