Mobile Native App/Web App testing using Appium and Selendroid(for Android)
Created by Kavan Sheth
Best viewed in Chrome, Firefox, Opera or A browser supporting HTML5.
Use arrow keys ( up ↑, down ↓, left ←, right ↑) to navigate. Uses Reveal.js by Hakim El Hattab
Press ESC to enter the slide overview.
npm install -g appium(run from command prompt(cmd))
Setup your AVD(Android virtual device)
//setup the web driver and launch the webview app.
DesiredCapabilities desiredCapabilities = new DesiredCapabilities();
desiredCapabilities.setCapability(MobileCapabilityType.BROWSER_NAME, "Safari");
URL url = new URL("http://127.0.0.1:4723/wd/hub");
AppiumDriver driver = new AppiumDriver(url, desiredCapabilities);
// Navigate to the page and interact with the elements on the guinea-pig page using id.
driver.get("http://saucelabs.com/test/guinea-pig");
WebElement div = driver.findElement(By.id("i_am_an_id"));
Assert.assertEquals("I am a div", div.getText()); //check the text retrieved matches expected value
driver.findElement(By.id("comments")).sendKeys("My comment"); //populate the comments field by id.
//close the app.
driver.quit();
AppiumDriver driver = new AppiumDriver(url, desiredCapabilities);
as following
AndroidDriver driver = new AndroidDriver(url, desiredCapabilities);
After removing compilation errors your code should look like following:
import io.appium.java_client.AppiumDriver;
import io.appium.java_client.android.AndroidDriver;
import io.appium.java_client.remote.MobileCapabilityType;
import java.net.MalformedURLException;
import java.net.URL;
import org.junit.Assert;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.remote.DesiredCapabilities;
public class androidAuto {
public static void main(String[] args)
{
//setup the web driver and launch the webview app.
DesiredCapabilities desiredCapabilities = new DesiredCapabilities();
desiredCapabilities.setCapability(MobileCapabilityType.BROWSER_NAME, "Safari");
URL url=null;
try {
url = new URL("http://127.0.0.1:4723/wd/hub");
} catch (MalformedURLException e) {
e.printStackTrace();
}
AndroidDriver driver = new AndroidDriver(url, desiredCapabilities);
// Navigate to the page and interact with the elements on the guinea-pig page using id.
driver.get("http://saucelabs.com/test/guinea-pig");
WebElement div = driver.findElement(By.id("i_am_an_id"));
Assert.assertEquals("I am a div", div.getText()); //check the text retrieved matches expected value
driver.findElement(By.id("comments")).sendKeys("My comment"); //populate the comments field by id.
//close the app.
driver.quit();
}
}
Now we will run this piece of code and remove run time errors.
info: Appium REST http interface listener started on 0.0.0.0:4723
desiredCapabilities.setCapability(MobileCapabilityType.DEVICE_NAME, "Android Emulator");
desiredCapabilities.setCapability(MobileCapabilityType.BROWSER_NAME, "Browser");
Hmm. Error doesn't say much, let's check cmd logs. If you get something like following:
It is a strange error possibly a bug. which I could not resolve, but while searching found that this might be an issue with chromedriver.exe used by appium.
At https://discuss.appium.io/t/did-not-get-session-redirect-from-chromedriver/1086
@ramshankar_testing has provided chromedriver_dev.exe. which is compiled from source to ignore chrome version. Follow the step given in discussion and replace your chromedriver.exe with downloaded exe.
Note 1: If you have installed appium using npm then you will find appium at C:\Users\XXXXX\AppData\Roaming\npm\node_modules.
Note 2: Before running your test again make sure that no instance of chromedriver.exe is running in your task manager.
But Appium logs are not having error for chrome version. but just following:
error: Chromedriver create session did not work. Status was 200 and body was
{"sessionId":"ca8c519a79d5ed140bbfc153bdd8ec3f","status":100,"value":{"message":"chrome not reachable\n
(Driver info: chromedriver=2.10.267521,platform=Windows NT 6.1 SP1 x86_64)"}}
info: [debug] Cleaning up appium session
error: Failed to start an Appium session, err was: Error: Did not get session redirect from Chromedriver
info: [debug] Error: Did not get session redirect from Chromedriver
at null. (d:\Job\appium git\appium\lib\devices\android\chromedriver.js:220:12)
In this case, you may try replacing chromedriver.exe provided by appium, with latest chromedriver version which is 2.12 as of now, as suggested in this post .
Note 1: Make sure you restart your AVD or atleast make sure that browser working properly after earlier failure.(otherwise browser will keep on crashing)
Note 2: Path for chromedriver.exe is appium\build\chromedriver\windows under your appium installation.