I have windows 7 which is connected to two android devices and I am using Selenium and Appium to automate an App but not able to run the test simultaneously in both the devices. below is the code I am using along with contents from testng.xml. let me know where I am wrong. The below code runs fine but it installs the app on first device then on second device what I want to acheive is install the app simultaneously on both the devices. Any help appreciated.
package ca.automation.com;
import org.testng.annotations.Test;
import com.relevantcodes.extentreports.ExtentReports;
import com.relevantcodes.extentreports.ExtentTest;
import com.relevantcodes.extentreports.LogStatus;
import io.appium.java_client.android.AndroidDriver;
import org.testng.annotations.BeforeSuite;
import org.testng.annotations.BeforeTest;
import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.List;
import java.util.concurrent.TimeUnit;
import org.junit.Assert;
import org.openqa.selenium.By;
import org.openqa.selenium.NoSuchElementException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
public class StackOverflow {
WebDriver driver1;
WebDriver driver2;
// ExtentReports report;
// ExtentTest logger;
// Boolean present;
File app = new File("App\\app_US_IT_Ananta.apk");
#BeforeSuite
public void startReport(){
// report=new ExtentReports("C:\\Anuj\\MobileAppResults.html");
}
#Test (priority =0)
public void installapp() {
// logger=report.startTest("VerifyAppInstalltion");
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability("udid", "1015fadb1a274005");
// capabilities.setCapability("udid", "ee92ba92");
capabilities.setCapability("deviceName","Android Emulator");
capabilities.setCapability("platformVersion", "4.4");
capabilities.setCapability("autoAcceptAlerts", true);
capabilities.setCapability("app", app.getAbsolutePath());
try {
driver1 = new AndroidDriver(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);
} catch (MalformedURLException e) {
e.printStackTrace();
}
}
#Test (priority =0)
public void installapp1() {
DesiredCapabilities capabilities1 = new DesiredCapabilities();
capabilities1.setCapability("udid", "ee92ba92");
capabilities1.setCapability("deviceName","Android Emulator");
capabilities1.setCapability("platformVersion", "4.4");
capabilities1.setCapability("autoAcceptAlerts", true);
capabilities1.setCapability("app", app.getAbsolutePath());
try {
driver2 = new AndroidDriver(new URL("http://127.0.0.1:4730/wd/hub"), capabilities1);
} catch (MalformedURLException e) {
e.printStackTrace();
}
}
}
Testng.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Suite" parallel="tests" thread-count="2">
<test name="Test">
<classes>
<class name="ca.automation.com.StackOverflow"/>
</classes>
</test> <!-- Test -->
Change parallel="tests" to parallel="methods" because you have to execute the methods in parallel, as its in your case.
Also, running tests in parallel wont be exactly 100% simultaneous execution. There would be some lag between execution in both devices. Try out a complete script with few more additional steps. That way we can easily make out that the tests are running simultaneously.
Related
I'm trying to explore Mobile App Automation, i'm running into this issue.
package sample;
import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
import io.appium.java_client.android.AndroidDriver;
public class AndroidTest {
static WebDriver driver;
public static void main(String[] args) throws MalformedURLException, InterruptedException
{
File app= new File("C:\\Users\\ASSOUND\\Downloads\\DevOps\\mymbfs60_simulator_0301.apk");
DesiredCapabilities capabilities= new DesiredCapabilities();
capabilities.setCapability("deviceName", "Android Emulator");
capabilities.setCapability("platformName", "Android");
capabilities.setCapability("app", app.getAbsolutePath());
driver = new AndroidDriver(new URL("http://127.0.0.1:4723/wd/hub"),capabilities);
driver.findElement(By.id("com.android.packageinstaller:id/permission_allow_button")).click();
driver.quit();
}
}
This code Installs & Launches the App, but when i try to locate the Element and click, i'm getting this error:
Exception in Thread "main" org.openga.selenium.WebDriverException: Method has not yet been implemented (WARNING: The server did not Provide..
Note: i tried to include enough time-wait before locating the element, but its still the same.
import java.net.URL;
import org.openqa.selenium.Capabilities;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.testng.annotations.BeforeClass;
import io.appium.java_client.AppiumDriver;
import io.appium.java_client.android.AndroidDriver;
import io.appium.java_client.remote.MobileCapabilityType;
public class LaunchAppium {
AndroidDriver driver;
// driver = new AndroidDriver("http://127.0.0.1:4723/wd/hub", test);
#BeforeClass
public void setup() throws MalformedURLException
{
DesiredCapabilities test=new DesiredCapabilities();
test.setCapability(MobileCapabilityType.DEVICE_NAME, "Androidemulator");
test.setCapability(MobileCapabilityType.APP, "Ebutor 15_dec_2016.apk");
//test.setCapability(MobileCapabilityType., value);
driver = new AndroidDr`enter code here`iver(new URL("http://127.0.0.1:4723/wd/hub"), test);
}
In the above pice of code i could not find the APP_PACKAGE and APP_ACTIVITY for the mobilecapabilitytpe instance which is essential for the android settings.
PLEASE HELP!!!
From 4.0, this is moved to specific to android package:
public interface AndroidMobileCapabilityType extends CapabilityType {
/**
* Activity name for the Android activity you want to launch from your package.
* This often needs to be preceded by a . (e.g., .MainActivity instead of MainActivity).
*/
String APP_ACTIVITY = "appActivity";
/**
* Java package of the Android app you want to run.
*/
String APP_PACKAGE = "appPackage";
/**
* Activity name for the Android activity you want to wait for.
*/
String APP_WAIT_ACTIVITY = "appWaitActivity";
}
So you have to use
AndroidMobileCapabilityType.APP_ACTIVITY
Please check the above it should work or you can see the see # link
package com.appium.android;
import java.net.MalformedURLException;
import java.net.URL;
import org.openqa.selenium.By;
import org.openqa.selenium.Capabilities;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.remote.CapabilityType;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
import io.appium.java_client.AppiumDriver;
import io.appium.java_client.android.AndroidDriver;
import io.appium.java_client.remote.MobileCapabilityType;
public class LaunchAppium {
static AndroidDriver driver;
// driver = new AndroidDriver("http://127.0.0.1:4723/wd/hub", test);
#SuppressWarnings("deprecation")
#BeforeClass
public void setup() throws MalformedURLException
{
DesiredCapabilities test=new DesiredCapabilities();
//test.setCapability(MobileCapabilityType.APP_PACKAGE, "com.veronicaapps.veronica.simplecalculator");
//test.setCapability(MobileCapabilityType.APP_ACTIVITY, "com.veronicaapps.veronica.simplecalculator.MainActivity");
test.setCapability(MobileCapabilityType.DEVICE_NAME, "emulator");
test.setCapability(MobileCapabilityType.VERSION, "4.2.2");
test.setCapability(MobileCapabilityType.PLATFORM_NAME, "android");
//test.setCapability("appPackage",
"com.veronicaapps.veronica.simplecalculator");
//test.setCapability("appActivity", ".MainActivity");
//driver = new AndroidDriver(new URL("http://127.0.0.1:4723/wd/hub"),
test);
driver = new AndroidDriver(new URL("http://127.0.0.1:4723/wd/hub"), test);
}
public interface AndroidMobileCapabilityType extends CapabilityType {
String APP_ACTIVITY = "com.veronicaapps.veronica.simplecalculator.MainActivity";
String APP_PACKAGE = "com.veronicaapps.veronica.simplecalculator";
}
#Test
public void atest() throws Exception
{
WebDriver two=(WebDriver) driver.findElement(By.linkText("2"));
((WebElement) two).click();
WebDriver plus= (WebDriver) driver.findElement(By.linkText("+"));
((WebElement) plus).click();
WebDriver five= (WebDriver) driver.findElement(By.linkText("5"));
((WebElement) five).click();
WebDriver equal= (WebDriver) driver.findElement(By.linkText("="));
((WebElement) equal).click();
}
}
I am not able to connect or runb a script on a real android device from android studio and geeting the following error:
The code is as follows:
import org.junit.Before;
import java.net.URL;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.remote.CapabilityType;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;
import io.appium.java_client.android.AndroidDriver;
public class Sample {
private static RemoteWebDriver driver;
#Before
public void setUp() throws Exception {
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability("device","Android");
capabilities.setCapability("app", "Chrome");
capabilities.setCapability(CapabilityType.BROWSER_NAME, "");
capabilities.setCapability(CapabilityType.VERSION, "4.4.2");
capabilities.setCapability(CapabilityType.PLATFORM, "WINDOWS");
driver = new RemoteWebDriver(new URL("http://127.0.0.1:4723/wd/hub"),
capabilities);
}
#Test
public void testcase_001() throws Exception{
driver.get("http://www.google.com");
WebElement keyword = driver.findElementByName("q");
keyword.sendKeys("appium");
driver.findElement(By.id("btnK")).click();
Thread.sleep(5000);
}
}
But the device is listed in Android device monitor
And shown connected in cmd
And the things that I tried is changing the following settings:
Android studio > Run > Edit Configuration > In Gerenal tab > set Target as USB DEVICE.
Any Idea what is wrong
Remove the last apk if you already installed before and try to install new apk.
I try to automate tests for a native android app with Appium. The feature I want to test is that certain user settings, for example, favorite search result, of the app can retain after updating to a newer version of the app. I have two version of the app and manually tested that this feature works. But when I automate the test, all the settings are cleared/reset.
My automation is done in two steps. First, in one session, install the old version, and set the favorite settings; second, start a new session, install the new version.
First step works just fine. In my second step
If I set full reset to my capability before starting new session, the app is reinstalled with new version but all the settings are cleared.
If I set no reset to my capability before starting new session, then I found the app is NOT reinstalled with new version at all.
How should I set the appium server flag so that in my second step, I can get my new version installed while still keeping the settings? I am guessing this has something to do with plist file of the app?
Here is my code.
settingOldVersion.java
package RR.upgradeTest;
import java.io.File;
import java.net.URL;
import org.junit.AfterClass;
import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.Point;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.remote.DesiredCapabilities;
import RR.utility.RRhelper;
import io.appium.java_client.android.AndroidDriver;
import io.appium.java_client.remote.MobileCapabilityType;
import io.appium.java_client.remote.MobilePlatform;
public class settingOldVersion{
public static AndroidDriver DRIVER;
public static String APK_file = "oldversion.apk";
#BeforeClass
public static void setUpBeforeClass() throws Exception {
File appDir = new File("src");
File app = new File(appDir, APK_file);
DesiredCapabilities cap=new DesiredCapabilities();
cap.setCapability(MobileCapabilityType.PLATFORM_NAME, MobilePlatform.ANDROID);
cap.setCapability(MobileCapabilityType.DEVICE_NAME, "Android Emulator");
cap.setCapability(MobileCapabilityType.PLATFORM_VERSION, "5.0.1");
cap.setCapability(MobileCapabilityType.APP, app.getAbsolutePath());
System.out.println("\n\n******************************************************");
System.out.println("Installation and startup app "+APK_file);
//Android Driver
DRIVER=new AndroidDriver(new URL("http://127.0.0.1:4723/wd/hub"), cap);
System.out.println("Driver obtained");
Thread.sleep(2000);
}
#AfterClass
public static void tearDownAfterClass() throws Exception {
DRIVER.quit();
}
#Test
public void settingFavorite() throws Exception
{
System.out.println("setting favorite");
//Add search result into favorite
....
//Take a screenshot of the favorite page
....
String image_path=".//result//UpgradeTest/OldVersion//";
String name="FavoriteSetting"+APK_file;
RRhelper.takeScreenshot(DRIVER, name, image_path);
}
}
Here is my code for checking new version: verifyNewVersion.java
package RR.upgradeTest;
import java.io.File;
import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.List;
import org.junit.AfterClass;
import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.Point;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.remote.DesiredCapabilities;
import RR.utility.RRhelper;
import io.appium.java_client.android.AndroidDriver;
import io.appium.java_client.remote.MobileCapabilityType;
import io.appium.java_client.remote.MobilePlatform;
public class varifyNewVersion{
public static AndroidDriver DRIVER;
public static String APK_file = "newversion.apk";
#BeforeClass
public static void setUpBeforeClass() throws Exception {
File appDir = new File("src");
File app = new File(appDir, APK_file);
DesiredCapabilities cap=new DesiredCapabilities();
cap.setCapability(MobileCapabilityType.PLATFORM_NAME, MobilePlatform.ANDROID);
cap.setCapability(MobileCapabilityType.DEVICE_NAME, "Android Emulator");
cap.setCapability(MobileCapabilityType.PLATFORM_VERSION, "5.0.1");
cap.setCapability(MobileCapabilityType.APP, app.getAbsolutePath());
// cap.setCapability("fullReset", true);
// cap.setCapability("noReset", false);
System.out.println("\n\n******************************************************");
System.out.println("Installation and startup app "+app.getAbsolutePath());
//Android Driver
DRIVER=new AndroidDriver(new URL("http://127.0.0.1:4723/wd/hub"), cap);
System.out.println("Driver obtained");
Thread.sleep(2000);
}
#AfterClass
public static void tearDownAfterClass() throws Exception {
DRIVER.quit();
}
#Test
public void checkingFavorite() throws Exception
{
//Come to Favorite page
Assert.assertTrue(RRhelper.ComeToMenuItem(DRIVER, "Favoritter"));
Thread.sleep(300);
String image_path=".//result//UpgradeTest/NewVersion//";
String name="FavoriteSetting_"+APK_file;
RRhelper.takeScreenshot(DRIVER, name, image_path);
....
}
}
Using two different apks you cannot reserve the package settings unless they are synced by the application using the deviceID you are installing it on(application end handling.) For an upgrade, usually the applications have an option of updating the app which could be done using the playstore/linking to a local server.
Also fullReset and noReset works on the package specified, if it would already be there noReset would not override the existing and fullReset would uninstall and then install the application.
More or so, it depends on your application how is the state of the flag/settings reserved, do you want a user to keep using the same settings while he is trying to force download another apk version or do you want to restore default settings in that case.
With appium server setting, either fullreset or noreset can server my purpose of testing the retaining of user setting after upgrade the app. As described in the question. Now I found a solution, which is to install the app with adb install -r, which means reinstall an exisiting app, keeping its data.
This command line can be wrapped up in my JAVA test code. After installation with adb, start the session with appium without installation of the app,
cap.setCapability(MobileCapabilityType.APP_PACKAGE, "package name");
cap.setCapability(MobileCapabilityType.APP_ACTIVITY, "activity name");
Then I can see the settings are retained.
I ran the following code for Automating a simple Test for Android via Appium:
package appiumtest;
import io.appium.java_client.AppiumDriver;
import io.appium.java_client.*;
import io.appium.java_client.android.AndroidDriver;
import io.appium.java_client.AppiumSetting;
import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.List;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.remote.*;
import org.testng.*;
//import org.junit.*;
import org.openqa.selenium.By;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.testng.annotations.*;
public class AppiumTest {
public static void main(String[] args) throws InterruptedException, MalformedURLException{
// TODO Auto-generated method stub
AppiumTest testcases = new AppiumTest();
testcases.launchtest();
}
public void launchtest() throws InterruptedException, MalformedURLException{
File app = new File("D:\\Radio\\ebayTest.apk");
DesiredCapabilities capabilities = new DesiredCapabilities();
AndroidDriver driver = new AndroidDriver(new URL("http://127.0.0.1:4723/wd/hub"),capabilities);
capabilities.setCapability(CapabilityType.BROWSER_NAME,"");
capabilities.setCapability("platformname", "Android");
capabilities.setCapability(CapabilityType.VERSION,"4.4.2");
capabilities.setCapability("deviceName", "07d4bb0a00caa300");
capabilities.setCapability("app", app.getAbsolutePath());
capabilities.setCapability("appPackage", "com.ebay.mobile");
capabilities.setCapability("appActivity", ".activities.eBay");
Thread.sleep(3000);
System.out.println("App launched");
WebElement signin = driver.findElement(By.id("button_sign_in"));
signin.click();
Thread.sleep(4000);
}
}
Now after running the test, I continuously see the error message shown below in the logs and the Appium Server Window:
Exception in thread "main" org.openqa.selenium.SessionNotCreatedException: A new session could not be created. (Original error: The following desired capabilities are required, but were not provided: deviceName) (WARNING: The server did not provide any stacktrace information)
Command duration or timeout: 200 milliseconds
Why am I seeing this error message? I have provided the correct device name parameter in Desired Capabilities as seen in 'adb devices'.
You initialized the driver with the capabilities before all the capabilities were set.
Move this line AndroidDriver driver = new AndroidDriver(new URL("http://127.0.0.1:4723/wd/hub"),capabilities);
after this line capabilities.setCapability("appActivity", ".activities.eBay");