Install Android APK on emulator using appium Node server - android

I have an appium server created in NODE.JS. I am working out to make the appium test to run an emulator and install the apk. Not able to find any particular examples online that has example on how to do it using Node server. Mostly examples are with the desktop installed appium server. I need some guidelines on how to do that. To further break it down, I want to perform following things using the appium Node server (Not to right any test case in the application source code)
Start the emulator or possible if can do it on real device
Install the APK on the emulator/device
Fire an intent that launches the app on emulator/device . Intent also contains data in bundle
Perform a click on a button inside the app.

start appium server in your terminal
appium
Output of above command like below on your terminal
Sample code here :
public class AppTest {
AppiumDriver driver;
MobileElement appTitle;
#Before
public void setup() throws MalformedURLException {
DesiredCapabilities desiredCapabilities = new DesiredCapabilities();
desiredCapabilities.setCapability(MobileCapabilityType.PLATFORM_NAME, "Android");
desiredCapabilities.setCapability(MobileCapabilityType.PLATFORM_VERSION, "7.0");
desiredCapabilities.setCapability(MobileCapabilityType.NO_RESET, true);
desiredCapabilities.setCapability(MobileCapabilityType.DEVICE_NAME, "Moto");
desiredCapabilities.setCapability(AndroidMobileCapabilityType.APP_PACKAGE, "com.android.vending");
desiredCapabilities.setCapability(AndroidMobileCapabilityType.APP_ACTIVITY, "com.google.android.finsky.activities.MainActivity");
driver = new AndroidDriver(new URL("http://0.0.0.0:4723/wd/hub"), desiredCapabilities);
}
#Test
public void testGooglePlayApp() throws InterruptedException {
String appName = "Amazon Now - Grocery Shopping";
//How to scroll to specific text
MobileElement scrollToText = (MobileElement) driver.findElement(MobileBy.AndroidUIAutomator("new UiScrollable(new UiSelector()).scrollIntoView(new UiSelector().text(\"" + appName + "\"));"));
scrollToText.click();
// Verifying the app detail page
appTitle = (MobileElement) driver.findElementById("com.android.vending:id/title_title");
Assert.assertTrue(appName.equals(appTitle.getText().trim()));
driver.navigate().back();
//Clicking the search bar icon
MobileElement scrollToElement = (MobileElement) driver.findElement(MobileBy.AndroidUIAutomator("new UiScrollable(new UiSelector()).scrollIntoView(new UiSelector().description(\"Search\"));"));
scrollToElement.click();
MobileElement editText = (MobileElement) driver.findElementById("com.android.vending:id/search_box_text_input");
editText.sendKeys(appName);
Thread.sleep(1000);
List<MobileElement> listOfSuggestedResults = driver.findElementsById("com.android.vending:id/suggest_text");
for (MobileElement element : listOfSuggestedResults) {
if (appName.equals(element.getText().trim())) {
element.click();
break;
}
}
appTitle = (MobileElement) driver.findElementById("com.android.vending:id/title_title");
Assert.assertTrue(appName.equals(appTitle.getText().trim()));
}
#After
public void tearDown() {
if (driver != null) {
driver.quit();
}
}
}
Sample code in github : https://github.com/tech-tock-tech/apptest
hope above will help if you still facing issue please check below video link -
https://www.youtube.com/watch?v=jP2NAY8ylp8

Related

Using Appium to reboot android device results in Illegal argument exception

I am using Appium to test the App in the Android mobile phone with Android 12 version. There i have a case where i want to restart the mobile phone.
My setup is where my c# code lies in windows which in turn connected with Macbook throuh WLAN and macbook controls the Android phone through Appium. That is the reason why I cannot execute adb commands directly on the Android mobile phone.
For that i am using the Executescript function from Appium as following
driver.ExecuteScript("mobile:shell", new AdbCommand("reboot"));
But this command throws an illegal argument exception as follows
Exception thrown: 'System.ArgumentException' in WebDriver.dll
An exception of type 'System.ArgumentException' occurred in WebDriver.dll but was not handled in user code
Argument is of an illegal type{"command":"reboot","args":[]}
Relaxed security is ON and adb commands should work in this case.
Any help appreciated regarding this problem.
Regarding Adb Class which does nothing but provides a provision to enter arguments in json format in C# and this has been copied from an example from automatetheplanet.com
public class AdbCommand
{
public AdbCommand(string command)
{
Command = command;
Args = new List();
}
public AdbCommand(string command, params string[] args)
{
Command = command;
Args = new List(args);
}
[JsonProperty("command")]
public string Command { get; set; }
[JsonProperty("args")]
public List Args { get; set; }
public override string ToString()
{
return JsonConvert.SerializeObject(this);
}
}

Chrome driver not performing any actions in webview Appium

I am using appium for my android app . I have some native and some webviews in my app . I am trying to automate the testing . I made a sample app and it is working for native but not working for webviews. Appium is able to detect the object but not able to perform the clicks and sendkeys . Below is my code
#Test
public void runMessagesTest() throws InterruptedException {
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability(MobileCapabilityType.AUTOMATION_NAME, "UiAutomator2");
capabilities.setCapability(MobileCapabilityType.PLATFORM_NAME, "Android");
capabilities.setCapability(MobileCapabilityType.DEVICE_NAME, "Phone");
capabilities.setCapability("goog:chromeOptions", ImmutableMap.of("w3c", false));
capabilities.setCapability("chromedriverExecutable", System.getProperty("webdriver.chrome.driver"));
driver = new AndroidDriver(getAppiumServerUrl(), capabilities);
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(60));
System.out.println("Created AppiumDriver");
((SupportsContextSwitching) driver).getContextHandles().forEach(context -> {
System.out.println("in here " + context);
if (context.contains("WEBVIEW")) {
((SupportsContextSwitching) driver).context(context);
System.out.println("Switched to " + context);
}
});
WebElement first_name = driver.findElement(By.xpath("//*[#id=\"firstname_input\"]"));
first_name.click();
first_name.sendKeys("First Name");
}
The test is passing as it is able to detect the WebElement but not performing any action. Even I printed the tagName . It is also printed correctly . Can somebody help me on this ?

Appium testing automatization: locate element with #ref id in Android Studio

I have the element in Android:
<android.support.design.widget.TextInputLayout
android:layout_width="-1"
android:layout_height="-2">
<AutoCompleteTextView
android:id="#ref/0x7f0c007c"
android:layout_width="-1"
android:layout_height="-2"
android:hint="#ref/0x7f060035"
android:maxLines="1"
android:singleLine="true"
android:inputType="0x21" />
</android.support.design.widget.TextInputLayout>
the name for 0x7f0c007c is password inside the ids.xml file.
This TextInputLayout is in the LoginActivity which starts right after SplashScreenActivity.
This is my class for the test:
public class test02 {
WebDriver driver;
#Before
public void testApp() throws MalformedURLException, InterruptedException {
String apkpath = "C:\\Users\\0013498\\Desktop\\qa-XXX-2-2-4-2.apk";
File app = new File(apkpath);
DesiredCapabilities capabilities= new DesiredCapabilities();
capabilities.setCapability(CapabilityType.BROWSER_NAME,"Android");
capabilities.setCapability("deviceName","Emulator");
capabilities.setCapability("platformName","Android");
capabilities.setCapability("app",app.getAbsolutePath());
capabilities.setCapability("appPackage", "com.XXX.XXX");
capabilities.setCapability("appActivity", "com.XXX.XXX.SplashScreenActivity");
driver = new AppiumDriver(new URL("http://127.0.0.1:4725/wd/hub"),capabilities);
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
//int KEYCODE_BACK Back key. Constant Value: 4
}
#Test
public void appiumExampleTest() throws Exception {
//WebElement emailEV=driver.findElement(By.id("com.XXX.XXX:id/email"));
//WebElement emailEV=driver.findElement(By.id("com.XXX.XXX.android:id/email"));
WebElement emailEV=driver.findElement(By.xpath(".//*[#id='email']"));
emailEV.sendKeys("#");
driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
}
}
As you can see, I'm simply trying to set a text using the id of the AutoCompleteTextView.
My problem is that my test is unable to locate the element, and I have tried this 3 ways:
WebElement emailEV=driver.findElement(By.id("com.XXX.XXX:id/email"));
WebElement emailEV=driver.findElement(By.id("com.XXX.XXX.android:id/email"));
WebElement emailEV=driver.findElement(By.xpath(".//*[#id='email']"));
but none of them works.
the error message is:
org.openqa.selenium.NoSuchElementException: An element could not be located on the page using the given search parameters.
I'm beginner on automatization testing with Appium & Android, and I'm a bit lost.
Please, any help?
Thx
EDIT:
as suggested in an answer to my question, i started using UI Automator Viewer. It is a .bat file located in the /sdk/tools folder.
I started an android emulator device and run the UI automator Viewer. Then, when clicked on Device Screenshot, i got the all the elements and their attributes.
That way i could see the correct id:
I finally used:
AndroidElement emailEV=driver.findElement(By.id("com.mobgen.interview.mobgeninterviewtest:id/email"));
emailEV.setValue("#"); // although i'm stuck here now
thanks for the help!
Step 1: element is the xpath=//android.widget.EditText[contains(#resource-id,"email")]
Step 2:public void click(String element) {
WebElement webElement = appiumDriver.findElement(By.xpath(element));
webElement.click();
System.out.println("Click element: "+element+" index = "+index);
}
Step 3:
public void elementSendText(String element, String text) {
WebElement webElement = appiumDriver.findElement(By.xpath(element));
webElement.sendKeys(text);
}

Run parallel test appium with physical devices

I've seen a bit everywhere the Selenium Grid solution to run parallel test but I don't know if this works with physical devices or only virtual and what I need is to test on multiple physical devices.
So I tried to do it "my way" for the moment what I have done is creating a selenium server & driver for each of my physical devices and run my tests but the main issue I encounter is that the test are not really parallel ...
Step 1 Server one open & driver one is created
Step 2 Server two open & driver two is created
Step 3 test1 start on driver one until it finish
Step 4 test1 start on driver two until it finish
Step 5 test2 start on driver one
....
Is there a way to make it really parallel ?
Here is my code :
Create server
public static AppiumServer startServer (String deviceName){
String port="";
String boostrapPort="";
switch (deviceName){
case ONEPLUS_ONE :
port = ONEPLUS_ONE_PORT;
boostrapPort = ONEPLUS_ONE_BOOTSTRAP_PORT;
break;
.....
}
ServerArguments serverArguments = new ServerArguments();
serverArguments.setArgument("--address", "127.0.0.1");
serverArguments.setArgument("--port", port);
serverArguments.setArgument("--bootstrap-port", boostrapPort);
AppiumServer appiumServer = new AppiumServer(serverArguments);
appiumServer.startServer();
return appiumServer;
}
Create driver :
#BeforeTest(alwaysRun = true)
public void setUp(){
AppiumServer samsung_S6_server = AppiumServerFactory.startServer(AppiumServerFactory.SAMSUNG_S6);
if(samsung_S6_server.isServerRunning()) {
AndroidDriver samsung_S6 = AndroidDriverFactory.getAndroidDriver(appPackage, appActivity, AppiumServerFactory.SAMSUNG_S6_PORT, AppiumServerFactory.SAMSUNG_S6);
serverList.add(samsung_S6_server);
driverList.add(samsung_S6);
} else {
System.out.println("Server running issue");
}
AppiumServer oneplus_one_server = AppiumServerFactory.startServer(AppiumServerFactory.ONEPLUS_ONE);
if(oneplus_one_server.isServerRunning()) {
AndroidDriver oneplus_one = AndroidDriverFactory.getAndroidDriver(appPackage, appActivity, AppiumServerFactory.ONEPLUS_ONE_PORT, AppiumServerFactory.ONEPLUS_ONE);
serverList.add(oneplus_one_server);
driverList.add(oneplus_one);
} else {
System.out.println("Server running issue");
}
The tests :
#Test
public void openArticle (){
for(AndroidDriver driver : driverList){
WebElement el = driver.findElements(By.id("..."));
el.click();
}
}
.....

Can i use only an app's .apk to automate using appium? Or i need the app in my workspace?

I am asking this because when i automate an app(TestApp.apk) present in my workspace is working fine. But when i use another app(WnG.apk) which is not present in my workspace it doesn't works. At this time appium fails to locate the element. An error ("An element could not be located on the page using the given search parameters.") occurs.
Below is my code:
public class Appium {
WebDriver driver;
#BeforeClass
public void setUp() throws MalformedURLException{
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability("deviceName", "Android Emulator");
capabilities.setCapability(CapabilityType.BROWSER_NAME, ""); //Name of mobile web browser to automate. Should be an empty string if automating an app instead.
capabilities.setCapability("platformVersion", "4.4");
capabilities.setCapability(CapabilityType.PLATFORM, "Windows");
capabilities.setCapability("platformName", "Android");
capabilities.setCapability("app", "C:/Appium/AppiumForWindows-1.2.4.1/WishAndGreet.apk");
capabilities.setCapability("app-package", "com.example.wishandgreet"); //Replace with your app's package
capabilities.setCapability("app-activity", ".LoginActivity"); //Replace with app's Activity
driver = new RemoteWebDriver(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);
}
#Test
public void loginTest(){
System.out.println("*************************************************************");
WebElement e1 = driver.findElement(By.id("com.example.wishandgreet:id/usernameEditText"));
System.out.println(e1.getText());
Assert.assertTrue(e1.getText() == "email / mobile number", "Username Text incorrect");
//Assert.assertEquals(e1.getText(),"email / mobile number");
e1.sendKeys("test#gmail.com");
WebElement e2 = driver.findElement(By.id("com.example.wishandgreet:id/passwordEditText"));
Assert.assertTrue(e2.getText() == "Password", "Pass text incorrect");
e2.sendKeys("1234");
System.out.println(e2);
WebElement e3 = driver.findElement(By.id("com.example.wishandgreet:id/loginButton"));
e3.click();
System.out.println(e1);
}
#AfterClass
public void tearDown(){
driver.quit();
}
In Android case, you need only .apk file to automate using Appium.
Looks like the issue you are facing is related to identifying an element. Please double check you are using right locators to identify required element and make sure that element appears on the view.

Categories

Resources