I have installed Appium on ubuntu and make my first test using java command in an android studio while running my test I get an error
A new session could not be created. (Original error: Requested a new
session but one was in progress) (WARNING: The server did not provide
any stacktrace information) Command duration or timeout: 603.92
seconds
public class MyTest {
AndroidDriver driver;
#Before
public void testCaseSetup()throws Exception {
//service.start();
//reader.readFile();
DesiredCapabilities cap = new DesiredCapabilities();
cap.setCapability(MobileCapabilityType.PLATFORM_NAME, "Android");
cap.setCapability(MobileCapabilityType.DEVICE_NAME, "Nexus_5");
cap.setCapability(MobileCapabilityType.PLATFORM_VERSION, "5.0.2");
cap.setCapability(MobileCapabilityType.APPIUM_VERSION, "v1.4.7");
cap.setCapability(MobileCapabilityType.APP_PACKAGE, "com.example.nitish.myappium");
cap.setCapability(MobileCapabilityType.APP_ACTIVITY, ".MainActivity");
driver = new AndroidDriver<MobileElement>(new URL("http://0.0.0.0:4723/wd/hub"), cap);
}
#Test
public void testcase1()throws Exception
{
driver.findElement(By.id("com.example.nitish.myappium:id/front")).click();
}
#After
public void testCaseTearDown()
{
driver.quit();
}
}
Try this code snippet
#Before
public void setUp() throws MalformedURLException {
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability("platformName", "Android");
capabilities.setCapability("VERSION", "5.0.2");
capabilities.setCapability("deviceName", "Device Name");
capabilities.setCapability("appPackage", "com.example.nitish.myappium");
capabilities.setCapability("appActivity", "ui.activity.SplashScreenActivity");
driver = new AndroidDriver(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);
}
This error means the server seeion is already there for port 4273 try changing server port number and give the same port number in your code
driver = new AndroidDriver<MobileElement>(new URL("http://0.0.0.0:**4723**/wd/hub"), cap);
Related
I want to implement a method to detect type of device connected and run it's specified script any help?
This will get you the device for Android and iOS. Do whatever you want to do from there.
In Android:
String manufacturer = Build.MANUFACTURER;
String model = Build.MODEL;
In iOS:
https://stackoverflow.com/a/26962452/8200290
I created enum and method to differentiate between both types
public static OS detectOperatingSystem ()
{if (OSName.contains("android")) {
return OS.ANDROID;
}
if (OSName.contains("ios")) {
return OS.IOS;
}
else return null;
}
String = OSName;
public enum OS {IOS,ANDROID}
}
#BeforeClass
public void getDesiredCapabilites() throws MalformedURLException {
switch (detectOperatingSystem()) {
case ANDROID:
capabilities.setCapability(MobileCapabilityType.DEVICE_NAME, androidVirtualDeviceVersion);
capabilities.setCapability(MobileCapabilityType.PLATFORM_NAME, androidMobilePlatform);
capabilities.setCapability(MobileCapabilityType.PLATFORM_VERSION, androidVirtualDeviceVersion);
capabilities.setCapability(MobileCapabilityType.AUTOMATION_NAME, androidAutomationName);
capabilities.setCapability(MobileCapabilityType.APP, androidFilePath);
capabilities.setCapability("eventTimings", true);
//capabilities.setCapability("unicodeKeyboard", true);
setDriver();
break;
case IOS:
capabilities.setCapability(MobileCapabilityType.DEVICE_NAME, IOSDeviceName);
capabilities.setCapability(MobileCapabilityType.PLATFORM_NAME, IOSMobilePlatform);
capabilities.setCapability(MobileCapabilityType.PLATFORM_VERSION, IOSPlatformVersion);
capabilities.setCapability(MobileCapabilityType.AUTOMATION_NAME, IOSAutomationName);
capabilities.setCapability(MobileCapabilityType.APP, IOSFilePath);
setDriver();
//IOSdriver = new IOSDriver(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);
break;
}
Just want to get clarity on the driver type that I need to use for testing mobile app on Android and iOS devices. My main goal is to keep the same code-base for the two platforms - although, keeping them separate would be easier to implement; but difficult to maintain.
Having said that, I am curious if the following code snippet will work in this situation.
AppiumDriver<MobileElement> driver = null;
if ("iOS".equals(os)) {
driver = new IOSDriver<MobileElement>(new URL(urlString), capabilities);
} else {
driver = new AndroidDriver<MobileElement>(new URL(urlString), capabilities);
}
Yes the code snippet which you have mentioned will work for your situation.
AppiumDriver<MobileElement> driver = null;
if ("iOS".equals(os)) {
driver = new IOSDriver<MobileElement>(new URL(urlString), capabilities);
} else {
driver = new AndroidDriver<MobileElement>(new URL(urlString), capabilities);
}
The driver instance created will be of type MobileElement.
private AppiumDriver<AppiumWebElement> _driver;
public readonly string platform = "iOS";
switch (platform)
{
case MobilePlatform.Android:
_driver = new AndroidDriver<AppiumWebElement>(new
Uri("http://localhost:4723/wd/hub"), driverOptions,
TimeSpan.FromSeconds(300));
break;
case MobilePlatform.IOS:
_driver = new IOSDriver<AppiumWebElement>(new
Uri("http://127.0.0.1:4723/wd/hub"), driverOptions,
TimeSpan.FromSeconds(300));
break;
}
In Mobile App automation, Eclipse displayed error "Unable to create new remote session."
Here is my code :
public class MyFirstAppiumClass {
AppiumDriver<WebElement> driver;
#BeforeClass
public void setup() throws MalformedURLException
{
DesiredCapabilities cap = new DesiredCapabilities();
//cap.setCapability("BROWSER_NAME", "Android");
cap.setCapability("platformVersion","6.0.1");
cap.setCapability("deviceName","Nexus 5");
cap.setCapability("platformName","Android");
cap.setCapability("appPackage", "com.android.calculator2");
cap.setCapability("appActivity", "com.android.calculator2.Calculator");
driver = new AndroidDriver<WebElement> (new URL("http://0.0.0.0:4723/wd/hub"),cap);
}
#Test
public void testCal() throws Exception
{
WebElement two = driver.findElement(By.name("4"));
two.click();
WebElement plus = driver.findElement(By.name("+"));
plus.click();
WebElement four = driver.findElement(By.name("4"));
four.click();
WebElement equalto = driver.findElement(By.name("="));
equalto.click();
DesiredCapabilities cap = new DesiredCapabilities();
cap.setCapability(InternetExplorerDriver.IE_ENSURE_CLEAN_SESSION, true);
WebElement results = driver.findElement(By.tagName("EditText"));
assert results.getText().equals("8"):"Actual Value is : "+results.getText()+" did not match with expected value: 8";
}
}
And Here is the Error :
org.openqa.selenium.SessionNotCreatedException: Unable to create new remote session.
desired capabilities = Capabilities [{appPackage=com.android.calculator2, appActivity=com.android.calculator2.Calculator, platformVersion=6.0.1, platformName=Android, deviceName=Nexus 5}], required capabilities = Capabilities [{}]
Can you try changing the below line:
driver = new AndroidDriver (new URL("http://0.0.0.0:4723/wd/hub"),cap);
to
driver = new AndroidDriver (new URL("http://127.0.0.1:4723/wd/hub"),cap);
I am trying to use Appium to automate a test case on my app.
I managed to run a simple script, but I do NOT understand the logic of of the multiple testcases running process like android life-cycle.
What is the cycle for a testcase?
Because when I run the code below it does not run in order of: firstTest, secondTest, thirdTest...
How do we tell the testCase what to run first and in what order ? thanks
public class LoginTest {
AndroidDriver driver;
#BeforeClass
public void setUp() throws MalformedURLException{
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability("device", "Android");
capabilities.setCapability(CapabilityType.BROWSER_NAME, ""); //Name of mobile web browser to automate. Should be an empty string if automating an app instead.
capabilities.setCapability(CapabilityType.VERSION, "5.0.2");
capabilities.setCapability(CapabilityType.PLATFORM, "Android");
capabilities.setCapability("app-package", "com.myapp"); //Replace with your app's package
capabilities.setCapability("app-activity", ".myapp"); //Replace with app's Activity
driver = new RemoteWebDriver(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);
}
#Test
public void firstTest() throws InterruptedException
{
List<WebElement> textFieldsList = driver.findElements(By.className("android.widget.EditText"));
int size = textFieldsList.size();
textFieldsList.get(0).sendKeys("test#test.com");
textFieldsList.get(1).sendKeys("12345");
Thread.sleep(1000);
WebElement btnLogin=driver.findElement(By.name("Login"));
String login = btnLogin.getText();
Assert.assertTrue(login.contains("Login"));
System.out.println(login);
btnLogin.click();
Thread.sleep(1000);
}
#Test
public void secondTest() throws InterruptedException {
WebElement btnHome=driver.findElement(By.name("Home"));
String login_1 = btnHome.getText();
Assert.assertTrue(login_1.contains("Home"));
System.out.println(login_1);
btnHome.click();
Thread.sleep(1000);
}
#Test
public void thirdTest() throws InterruptedException {
WebElement btnSecond=driver.findElement(By.name("Second"));
String login_2 = btnSecond.getText();
Assert.assertTrue(login_2.contains("Second"));
System.out.println(login_2);
btnSecond.click();
Thread.sleep(1000);
}
#AfterClass
public void tearDown() {
driver.quit();
}
Thank you
Well the answer to this depends on the Test framework you are using to for your Test.
If you are using Junit for your tests, then you might not be able to prioritise them in a user defined order.
On the other end using if you are using TestNG framework, adding parameter to Test annotation would solve your problem. e.g.
#Test(groups = {"checklist1"}, priority = 1, testName = "firstTest", description = "My First Test")
Though I would suggest, you go through and follow this.
You can use dependsOnMethods with #Test annotation to make that flow:
#Test(dependsOnMethods = "methodName")
I want to use appium to automate browser on android phone,but I don't know how to set the capability.
First, I have enabled USB debugging on my Android device in the developer options.
Second, adb was working well, i can see the device id.
Third, I started Appium.exe from Appium for windows and writed some code by JAVA, but I don't know how to set the capability on Android browser.
public class Test {
private WebDriver driver;
#Before
public void setUp() throws Exception {
DesiredCapabilities capabilities = new DesiredCapabilities();
//------------I don't know how to set the capability------------//
capabilities.setCapability(CapabilityType.VERSION, "2.3.7");
capabilities.setCapability(CapabilityType.PLATFORM, "WINDOWS");
//--------------------------------------------------------------//
driver = new SwipeableWebDriver(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);
}
#Test
public void testcase_001() throws Exception{
driver.get("http://www.google.com");
WebDriverWait wait = new WebDriverWait(driver, 30);
wait.until(ExpectedConditions.presenceOfElementLocated(By.id("lst-ib")));
WebElement keyword = driver.findElement(By.name("lst-ib"));
keyword.sendKeys("appium");
driver.findElement(By.id("btnK")).click();
Thread.sleep(5000);
}
#After
public void tearDown() throws Exception {
driver.quit();
}
public class SwipeableWebDriver extends RemoteWebDriver implements HasTouchScreen {
private RemoteTouchScreen touch;
public SwipeableWebDriver(URL remoteAddress, Capabilities desiredCapabilities) {
super(remoteAddress, desiredCapabilities);
touch = new RemoteTouchScreen(getExecuteMethod());
}
public TouchScreen getTouch() {
return touch;
}
}
}
Many thanks.
Try this code using Android Driver:
import io.appium.java_client.android.AndroidDriver;
public class Test {
private AndroidDriver;
#Before
public void setUp() throws Exception {
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability("deviceName", "MOTO G 2");
capabilities.setCapability("platformName", "Android");
capabilities.setCapability(CapabilityType.BROWSER_NAME, "Chrome");
capabilities.setCapability("platformVersion", "5.0.2");
capabilities.setCapability("appPackage", "com.android.chrome");
capabilities.setCapability("appActivity","com.google.android.apps.chrome.ChromeTabbedActivity");
driver = new AndroidDriver(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);
}
#After
public void tearDown() throws Exception {
driver.quit();
}
Note: Use selenium version 2.48.2 (latest version) to make the android driver run without any error.
Try something as next:
#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.3");
capabilities.setCapability(CapabilityType.PLATFORM, "WINDOWS");
driver = new RemoteWebDriver(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);
}
It should lunch Chrome driver.
You can try with this capability type:
CapabilityType.BROWSER_NAME --- "Browser"
This code is working on my end:
#BeforeMethod
public void setUp() throws Exception {
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability("deviceName", "Samsung Galaxy S4");
capabilities.setCapability("platformName", "Android");
capabilities.setCapability(CapabilityType.BROWSER_NAME, "Browser");
capabilities.setCapability("platformVersion", "4.4.4");
capabilities.setCapability(MobileCapabilityType.TAKES_SCREENSHOT, "true");
driver = new AndroidDriver<WebElement>(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
driver.get("http://www.google.com");
}
Try this
DesiredCapabilities mobCapabilities = new DesiredCapabilities();
mobCapabilities.SetCapability(CapabilityType.BrowserName,
AppConfig.MobileBrowser.Equals("chrome") ? MobileBrowserType.Chrome : MobileBrowserType.Browser);
mobCapabilities.SetCapability(MobileCapabilityType.DeviceName, AppConfig.DeviceName);
mobCapabilities.SetCapability(MobileCapabilityType.PlatformName, AppConfig.Platform);
mobCapabilities.SetCapability(MobileCapabilityType.PlatformVersion, AppConfig.PlatformVersion);
// init driver
driver = new AndroidDriver<AndroidElement>(
new Uri("http://127.0.0.1:4723/wd/hub"),
mobCapabilities);
driver.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(15));