Testproject Java SDK + Appium + Browserstack Integration Issue

Description:
I have written my Appium web automation test in my Eclipse IDE with Testproject Java Open SDK and trying to integrate it with Browserstack for devices, but I am getting some issue while doing this, it successfully launches the browser in the specified device in browserstack and after that it fails with below error. It also show execution progress in testproject but it gets stuck. Kindly find my code and console error below for your reference and please suggest some solution for the same.

Code snippet:

import java.net.URL;
import java.util.HashMap;
import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

import com.browserstack.local.Local;

import io.testproject.sdk.drivers.TestProjectCapabilityType;
import io.testproject.sdk.drivers.web.RemoteWebDriver;

public class TestprojectBrowserstackApiumLocalTest {

	public static void main(final String[] args) throws Exception {

		// perparing URL for remote driver
		String AUTOMATE_USERNAME = "browserstack_username";
		String AUTOMATE_ACCESS_KEY = "browserstack_accesskey";
		String URL = "https://" + AUTOMATE_USERNAME + ":" + AUTOMATE_ACCESS_KEY + "@hub-cloud.browserstack.com/wd/hub";

		DesiredCapabilities caps = new DesiredCapabilities();
		caps.setCapability("browserName", "Android");
		caps.setCapability("device", "Samsung Galaxy S9 Plus");
		caps.setCapability("realMobile", "true");
		caps.setCapability("os_version", "9.0");
		caps.setCapability("name", "BStack-[Java] Sample Test"); // test name
		caps.setCapability("build", "BStack Build Number 1"); // CI/CD job or build name

		// setting up driver
		ChromeOptions chromeOptions = new ChromeOptions();
		chromeOptions.setCapability(TestProjectCapabilityType.CLOUD_URL, URL);

		caps.setCapability(ChromeOptions.CAPABILITY, chromeOptions);
		WebDriver driver = new RemoteWebDriver(caps,"Mobile Test Project","MobileCWR");


		driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);

		try {
			driver.get("https://bstackdemo.com/");
			final WebDriverWait wait = new WebDriverWait(driver, 10);
			wait.until(ExpectedConditions.titleIs("StackDemo"));
			// getting name of the product
			String product_name = wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//*[@id=\'1\']/p"))).getText();
			// waiting for the Add to cart button to be clickable
			WebElement cart_btn = wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//*[@id=\'1\']/div[4]")));
			// clicking the 'Add to cart' button
			cart_btn.click();
			// checking if the Cart pane is visible
			wait.until(ExpectedConditions.visibilityOfElementLocated(By.className("float-cart__content")));
			// getting the product's name added in the cart
			final String product_in_cart = wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//*[@id=\'__next\']/div/div/div[2]/div[2]/div[2]/div/div[3]/p[1]"))).getText();
			// verifying whether the product added to cart is available in the cart
			if (product_name.equals(product_in_cart)) {
				markTestStatus("passed", "Product has been successfully added to the cart!", driver);
			}
		} catch (Exception e) {
			markTestStatus("failed", "Some elements failed to load", driver);
		}
		driver.quit();
		System.out.println("Executed");
		


	}

	// This method accepts the status, reason and WebDriver instance and marks the test on BrowserStack
		public static void markTestStatus(String status, String reason, WebDriver driver) {
			final JavascriptExecutor jse = (JavascriptExecutor) driver;
			jse.executeScript("browserstack_executor: {\"action\": \"setSessionStatus\", \"arguments\": {\"status\": \""+ status + "\", \"reason\": \"" + reason + "\"}}");
		}
}

**Error:**
SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
Exception in thread "main" java.lang.ClassCastException: class com.google.gson.internal.LinkedTreeMap cannot be cast to class java.lang.String (com.google.gson.internal.LinkedTreeMap is in unnamed module of loader 'app'; java.lang.String is in module java.base of loader 'bootstrap')
	at org.openqa.selenium.AbstractCapabilities.setCapability(AbstractCapabilities.java:77)
	at org.openqa.selenium.MutableCapabilities.setCapability(MutableCapabilities.java:100)
	at org.openqa.selenium.MutableCapabilities.lambda$new$0(MutableCapabilities.java:54)
	at java.base/java.util.Map.forEach(Map.java:661)
	at org.openqa.selenium.MutableCapabilities.<init>(MutableCapabilities.java:52)
	at io.testproject.sdk.internal.rest.AgentClient.startSession(AgentClient.java:750)
	at io.testproject.sdk.internal.rest.AgentClient.<init>(AgentClient.java:293)
	at io.testproject.sdk.internal.rest.AgentClient.getClient(AgentClient.java:573)
	at io.testproject.sdk.drivers.web.RemoteWebDriver.<init>(RemoteWebDriver.java:884)
	at io.testproject.sdk.drivers.web.RemoteWebDriver.<init>(RemoteWebDriver.java:764)
	at io.testproject.sdk.drivers.web.RemoteWebDriver.<init>(RemoteWebDriver.java:281)
	at TestprojectBrowserstackApiumLocalTest.main(TestprojectBrowserstackApiumLocalTest.java:51)

Testproject SS:

Hi @ssabat ,
First of all welcome to our community :slight_smile:

Regarding the issue, which java version are you using?
If you are using Java 12 and above, please change to Java 11 and reproduce the test.
To change the execution environment you can do it following this:
Click on the Window tab in Eclipse, go to Preferences and when that window comes up, go to JavaInstalled JREsExecution Environment and choose JavaSE-11

1 Like

Thanks for the reply. I tried it by changing Installed JREsExecution Environment and by choosing JavaSE-11 but still it didn’t worked for me and getting same error.

SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
Exception in thread "main" java.lang.ClassCastException: class com.google.gson.internal.LinkedTreeMap cannot be cast to class java.lang.String (com.google.gson.internal.LinkedTreeMap is in unnamed module of loader 'app'; java.lang.String is in module java.base of loader 'bootstrap')
	at org.openqa.selenium.AbstractCapabilities.setCapability(AbstractCapabilities.java:77)
	at org.openqa.selenium.MutableCapabilities.setCapability(MutableCapabilities.java:100)
	at org.openqa.selenium.MutableCapabilities.lambda$new$0(MutableCapabilities.java:54)
	at java.base/java.util.Map.forEach(Map.java:661)
	at org.openqa.selenium.MutableCapabilities.<init>(MutableCapabilities.java:52)
	at io.testproject.sdk.internal.rest.AgentClient.startSession(AgentClient.java:750)
	at io.testproject.sdk.internal.rest.AgentClient.<init>(AgentClient.java:293)
	at io.testproject.sdk.internal.rest.AgentClient.getClient(AgentClient.java:573)
	at io.testproject.sdk.drivers.web.RemoteWebDriver.<init>(RemoteWebDriver.java:884)
	at io.testproject.sdk.drivers.web.RemoteWebDriver.<init>(RemoteWebDriver.java:764)
	at io.testproject.sdk.drivers.web.RemoteWebDriver.<init>(RemoteWebDriver.java:281)
	at TestprojectBrowserstackApiumLocalTest.main(TestprojectBrowserstackApiumLocalTest.java:51)

Hi @ssabat ,
This warning , i.e. not an error, message is reported when no SLF4J providers could be found on the class path. Placing one (and only one) of slf4j-nop.jar slf4j-simple.jar , slf4j-log4j12.jar , slf4j-jdk14.jar or logback-classic.jar on the class path should solve the problem. Note that these providers must target slf4j-api 1.8 or later.
If you are still facing the issue we can jump on a call and troubleshoot it together.

1 Like

SLF4J issue is resolved after adding dependency and was not the trouble maker but main issue was the below highlighted exception in bold due to which browser is opening but after that it does nothing. This is a ClassCastException.

[main] INFO io.testproject.sdk.internal.rest.AgentClient - Initializing new session...
[main] INFO io.testproject.sdk.internal.rest.AgentClient - Session [5fed1ddf5b070a2dff09b3665403120bef015071] initialized
Ex**ception in thread "main" java.lang.ClassCastException: class com.google.gson.internal.LinkedTreeMap cannot be cast to class java.lang.String (com.google.gson.internal.LinkedTreeMap is in unnamed module of loader 'app'; java.lang.String is in module java.base of loader 'bootstrap')**
**	at o**rg.openqa.selenium.AbstractCapabilities.setCapability(AbstractCapabilities.java:77)
	at org.openqa.selenium.MutableCapabilities.setCapability(MutableCapabilities.java:100)
	at org.openqa.selenium.MutableCapabilities.lambda$new$0(MutableCapabilities.java:54)
	at java.base/java.util.Map.forEach(Map.java:661)
	at org.openqa.selenium.MutableCapabilities.<init>(MutableCapabilities.java:52)
	at io.testproject.sdk.internal.rest.AgentClient.startSession(AgentClient.java:750)
	at io.testproject.sdk.internal.rest.AgentClient.<init>(AgentClient.java:293)
	at io.testproject.sdk.internal.rest.AgentClient.getClient(AgentClient.java:573)
	at io.testproject.sdk.drivers.web.RemoteWebDriver.<init>(RemoteWebDriver.java:884)
	at io.testproject.sdk.drivers.web.RemoteWebDriver.<init>(RemoteWebDriver.java:764)
	at io.testproject.sdk.drivers.web.RemoteWebDriver.<init>(RemoteWebDriver.java:281)
	at TestprojectBrowserstackMobileLocalTest.main(TestprojectBrowserstackMobileLocalTest.java:51)

hi @alex.ivanov,
I have imported the jar files which resolved the SLF4J warnings but still we are facing ClassCastException issue as shown in below error message, can you please have look whether I am using correct remoteWebdriver() constructor to connect with browserstack cloud.

[main] INFO io.testproject.sdk.internal.rest.AgentClient - Initializing new session...
[main] INFO io.testproject.sdk.internal.rest.AgentClient - Session [5fed1ddf5b070a2dff09b3665403120bef015071] initialized
**Exception in thread "main" java.lang.ClassCastException: class com.google.gson.internal.LinkedTreeMap cannot be cast to class java.lang.String (com.google.gson.internal.LinkedTreeMap is in unnamed module of loader 'app'; java.lang.String is in module java.base of loader 'bootstrap')**
	at org.openqa.selenium.AbstractCapabilities.setCapability(AbstractCapabilities.java:77)
	at org.openqa.selenium.MutableCapabilities.setCapability(MutableCapabilities.java:100)
	at org.openqa.selenium.MutableCapabilities.lambda$new$0(MutableCapabilities.java:54)
	at java.base/java.util.Map.forEach(Map.java:661)
	at org.openqa.selenium.MutableCapabilities.<init>(MutableCapabilities.java:52)
	at io.testproject.sdk.internal.rest.AgentClient.startSession(AgentClient.java:750)
	at io.testproject.sdk.internal.rest.AgentClient.<init>(AgentClient.java:293)
	at io.testproject.sdk.internal.rest.AgentClient.getClient(AgentClient.java:573)
	at io.testproject.sdk.drivers.web.RemoteWebDriver.<init>(RemoteWebDriver.java:884)
	at io.testproject.sdk.drivers.web.RemoteWebDriver.<init>(RemoteWebDriver.java:764)
	at io.testproject.sdk.drivers.web.RemoteWebDriver.<init>(RemoteWebDriver.java:281)
	at TestprojectBrowserstackMobileLocalTest.main(TestprojectBrowserstackMobileLocalTest.java:51)

Hi @ssabat ,

The best solution is to share your code to our support@testproject.io
And we’ll investigate it and update you with a solution.

1 Like