I have seperate class with called CommonAndroidDriver where capabilites and driver are. I want to be able to add device udid as parameter seperately for each test.
public class CommonAndroidDriver {
public static AndroidDriver driver;
private URL serverUrl;
private DesiredCapabilities capabilities = new DesiredCapabilities();
public CommonAndroidDriver(ITestContext context) throws MalformedURLException {
this.serverUrl = new URL("https:");
capabilities.setCapability("appium:platformName", MobilePlatform.ANDROID);
capabilities.setCapability("appium:automationName", "UiAutomator2");
capabilities.setCapability("appium:appPackage", "blabla");
capabilities.setCapability("appium:appActivity", "blabla");
capabilities.setCapability("uftm:oauthClientId", "blabla");
capabilities.setCapability("uftm:oauthClientSecret", "blabla");
capabilities.setCapability("uftm:tenantId", "blabla");
capabilities.setCapability("appium:udid", context.getCurrentXmlTest().getParameter("udid"));
capabilities.setCapability("appium:noReset", true);
capabilities.setCapability("appium:autoGrantPermissions", true);
this.driver = new AndroidDriver(serverUrl, capabilities);
}
public AndroidDriver getDriver() {
return this.driver;
}
}
Example test start, I want this udid to be entered UP in CommonAndroidDriver:
public class LoginTest extends BaseAppium {
public LoginTest() throws IOException {
}
@Test
@Parameters({"login", "password", "udid"})
public void Login(String login, String password, String udid) throws Exception {
//some test steps
}
}
I started combining it with ITestContext getCurrentXmlTest().getParameter, but couldn't find a way to pass udid to capabilities.
Testngxml
<suite name="AndroidAppiumTest">
<test name="LoginTests">
<parameter name="loginmp" value="test@test.com"/>
<parameter name="passwordtest" value="Test1234"/>
<parameter name="udid" value="DeviceId"/>
<classes>
<class name="tests.LoginTest"/>
<methods>
<include name="Login"/>
</methods>
</classes>
</test>
</suite>
BaseAppium
public class BaseAppium {
public AndroidDriver driver;
public BaseAppium() throws IOException {
}
@BeforeClass
public void setUp(ITestContext context) throws IOException {
this.driver = new CommonAndroidDriver(context).getDriver();
}
@BeforeTest
public void setTest() {
}
@AfterClass
public void tearDown() throws IOException, InterruptedException {
this.driver.terminateApp("app.name");
this.driver.quit();
}
}
There are a couple of issues in your code.
CommonAndroidDriver
at a constructor level in BaseAppium
. We should instead be creating it via setup()
method.Here's a cleaned-up code that shows how the fixes/changes would look like
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.testng.ITestContext;
import java.net.MalformedURLException;
import java.net.URL;
public class CommonAndroidDriver {
// Intentionally using RemoteWebDriver to avoid my test project having to add dependency on appium client
// just for illustration purposes.
public RemoteWebDriver driver;
private final URL serverUrl;
public CommonAndroidDriver(ITestContext context) throws MalformedURLException {
this.serverUrl = new URL("https:");
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability("appium:platformName", "Android");
capabilities.setCapability("appium:automationName", "UiAutomator2");
capabilities.setCapability("appium:appPackage", "blabla");
capabilities.setCapability("appium:appActivity", "blabla");
capabilities.setCapability("uftm:oauthClientId", "blabla");
capabilities.setCapability("uftm:oauthClientSecret", "blabla");
capabilities.setCapability("uftm:tenantId", "blabla");
capabilities.setCapability("appium:udid", context.getCurrentXmlTest().getParameter("udid"));
capabilities.setCapability("appium:noReset", true);
capabilities.setCapability("appium:autoGrantPermissions", true);
this.driver = new RemoteWebDriver(serverUrl, capabilities);
}
public RemoteWebDriver getDriver() {
return this.driver;
}
}
import org.openqa.selenium.remote.RemoteWebDriver;
import org.testng.ITestContext;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import java.io.IOException;
public class BaseAppium {
public RemoteWebDriver driver;
@BeforeClass
public void setUp(ITestContext context) throws IOException {
this.driver = new CommonAndroidDriver(context).getDriver();
}
@AfterClass
public void tearDown() {
this.driver.quit();
}
}