Is there any way we can cast RemoteWebDriver as AndroidDriver

I am using the RemoteWebDriver to lauch the application on mobile but at its lacks some inbuilt methods which are provided by AndroidDriver.

So is there any way we can cast the RemoteWebDriver as AndroidDriver or Vice verse please let me know what could be done.

Declare androiddriver globally…

or
in your capabilities mention it as android driver…

driver = new AndroidDriver(new URL(“http://127.0.0.1:4723/wd/hub/”), capabilities);

Actually I need to use RemoteWebDriver Method only as new RemoteWebDriver(new URL(“http://127.0.0.1:4723/wd/hub/”), capabilities);

as working on a framework which can be used to run both Android and IOS apps using RemoteWebDriver is only way to make it generic is there a way to type cast RemoteWebDriver as AndroidDriver

Hi, @NishantSingh

This might help…

    driver = (AndroidDriver) new RemoteWebDriver(new URL("http://127.0.0.1:4723/wd/hub"),
            capabilities);

Try it out And say me if it works…

Thankyou

Initially declare

private AndroidDriver driver;

thanks sudhanvam,
But i am getting exception while using this code

Can i know what is the exception?

this is the error I am geting

@BeforeClass setUp
java.lang.ClassCastException: org.openqa.selenium.remote.RemoteWebDriver cannot be cast to io.appium.java_client.android.AndroidDriver

But its working fine for me…

But I am getting error message is there any other solution you have… If not could be able to send in which this casting method working for you

Hello, I’m facing the same issue. Here is what I’m doing in my code.

  • Creating a page in which declaring driver as RemoteDriver.
  • Calling a function where again I’m declaring a driver as RemoteDriver.
  • Running a test where I’m declaring driver as RemoteDriver.
  • Trying to access the AppiumDriver method in my test method.

My question is, where should I cast this method? My driver initialization is in common function. Also where should I declare androiddriver publicly and where should I declare remotedriver privately.

Thanks in advance.

This is Java. If you initialize something (using the ‘new’ keyword to call a class’s constructor to initialize the object), the type of that object will always be whatever class whose constructor you used to initialize the object. That is, if you use RemoteWebDriver’s constructor, then the driver object will always be a RemoteWebDriver object.

You can always cast up the class inheritance tree. e.g. casting the RemoteWebDriver object reference into a WebDriver or Object reference. Casting down the inheritance tree will require that the type you’re casting to be the same type or a parent type of the object. If you initialize something as a RemoteWebDriver, then you can not cast it down to an AndroidDriver, IOSDriver, nor AppiumDriver, as the object was not initialized as one of the child types.

If you want to use an AndroidDriver, IOSDriver, or AppiumDriver, then you have to set up your driver object at the very beginning during its construction. You can not change an object type once the object is already created (at least not in Java). If you want generality for both your Android and iOS tests, then you will also have to determine whether you’ll be running on an iOS or Android test at the beginning of your test runs. If this can not be done with your current framework, then you’ll have to redesign your framework.

2 Likes

Hi Nishant,

have u getting solution of your problem?
I am also having same situation. I also want to use both RemoteWebDriver and IOSDriver/AndroidDriver.

Please help me out if you got the solution.

Thanks

What you can do is define the driver using AndroidDriver or iOSDriver and later assign it to RemoteDriver which most probably resolve your issue.

e.g AndroidDriver androidDriver = new AndroidDriver(new URL(…),cap);
RemoteWebDriver driver = androidDriver;

Hi Nishant

I want to use Tap method of iOSDriver Like

driver.tap(1, (int) screenWidth,(int) screenHeight, 5);

following is my code snippet

static WebDriver driver =null;

DesiredCapabilities dc= new DesiredCapabilities();
dc.setCapability(“platformName”, “iOS”);
dc.setCapability(“platformVersion”, “8.4”);
dc.setCapability(“deviceName”, “iPhone 6”);
dc.setCapability(“app”, “App Path”);
dc.setCapability(“acceptAllAlert”, true);
driver = new RemoteWebDriver(new URL(“http://127.0.0.1:4723/wd/hub”),dc);

driver.findElement(By.xpath("//UIAApplication[1]/UIAWindow[1]/UIATableView[1]/UIATableCell[1]/UIAButton[1]")).click();
Thread.sleep(2000);
driver.findElement(By.xpath("//UIAApplication[1]/UIAWindow[1]/UIANavigationBar[1]/UIAButton[2]")).click();
driver.findElement(By.xpath("//UIAApplication[1]/UIAWindow[1]/UIATextField[1]")).sendKeys(“apple”);

Dimension dimensions =driver.manage().window().getSize();
float screenWidth = (dimensions.getWidth())/4;
float screenHeight = 3*(dimensions.getHeight())/4;
System.out.println(screenWidth+" "+screenHeight);

Now I want to use Tap method which serve by AppiumDriver*******
But here below code not work with Remote web driver so I need to create the driver for AppiumDriver or iOSDriver

driver.tap(1, (int) screenWidth,(int) screenHeight, 5);

Please provide me if you have solution

Hi,

I have modified your code it should work just resolve if any syntax error come:

static RemoteWebDriver driver =null;
AndroidDriver androidDriver=null;
DesiredCapabilities dc= new DesiredCapabilities();
dc.setCapability(“platformName”, “iOS”);
dc.setCapability(“platformVersion”, “8.4”);
dc.setCapability(“deviceName”, “iPhone 6”);
dc.setCapability(“app”, “App Path”);
dc.setCapability(“acceptAllAlert”, true);
androidDriver= new AndroidDriver(new URL(“http://127.0.0.1:4723/wd/hub”),dc);
driver = androidDriver;
driver.findElement(By.xpath("//UIAApplication[1]/UIAWindow[1]/UIATableView[1]/UIATableCell[1]/UIAButton[1]")).click();
Thread.sleep(2000);
driver.findElement(By.xpath("//UIAApplication[1]/UIAWindow[1]/UIANavigationBar[1]/UIAButton[2]")).click();
driver.findElement(By.xpath("//UIAApplication[1]/UIAWindow[1]/UIATextField[1]")).sendKeys(“apple”);

Dimension dimensions =driver.manage().window().getSize();
float screenWidth = (dimensions.getWidth())/4;
float screenHeight = 3*(dimensions.getHeight())/4;
System.out.println(screenWidth+" "+screenHeight);
androidDriver.tap(1, (int) screenWidth,(int) screenHeight, 5);

Thank you very much Nishant, Now its working form me by the way you suggested

1 Like

Thanks for detailed answer. Currently I’m designing framework and still has below queries, can you please clarify

  1. which scenario do you use RemoteWebDriver or AppiumDriver ?
  2. Once device OS is found as Android or iOS then we can directly create respective driver type. I’m not sure if there is any particular need for step 1 driver types.

Regards,
Vikram

Hi NishantSingh,

Greetings to you.

I am facing the same issue so i tried with your code. But for me it is not working simply throwing null pointer exception at very 1st Remote WebElement it self.

Could you please review my code and guide me in proper way.

static RemoteWebDriver driver =null;
AndroidDriver androidDriver=null;

@Test
public void setUp() throws Exception {
	
	

	DesiredCapabilities Capabilities = new DesiredCapabilities();

	
	Capabilities.setCapability("deviceName", "Android");

	
	Capabilities.setCapability("browserName", "Android");

	
	Capabilities.setCapability("platformVersion", "6.0");

	
	Capabilities.setCapability("platformName", "Android");

	
	Capabilities.setCapability("appPackage", "PATH");

	
	Capabilities.setCapability("appActivity", "PATH");
	

	androidDriver = new AndroidDriver(new URL("http://127.0.0.1:4444/wd/hub"), Capabilities);
	driver = androidDriver;
	
	driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);

	WebElement element = driver.findElement(By.xpath("//*[@text='Skip to find your library or school now']"));
	

	
Error Line =>    driver.findElement(By.xpath("//*[@text='Skip to find your library or school now']")).click();
	
	
	WebDriverWait Wait = new WebDriverWait(driver, 10);
	Wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//*[@text='Welcome to Axis 360!']")));
	WebElement input = driver.findElement(By.xpath("//*[@text='Search for My Library']"));

You can try it
1:-First of all Initialize this variable as
private AndroidDriver androidDriver;
2:-then cast the RemoteWebDriver as
androidDriver=(AndroidDriver) new RemoteWebDriver(new URL(“http://127.0.0.1:4723/wd/hub”),
capabilities);