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 ?
Related
Below program hangs completely, after trying to find elements. Doesn't look like anything wrong with xpath either. I need to terminate the program manually each time. Is there anything which i need to change in code or should i need to add something?
public class Test123 {
public static void main(String[] args) throws MalformedURLException, InterruptedException {
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability("noReset", "true");
capabilities.setCapability(MobileCapabilityType.AUTOMATION_NAME, "Appium");
capabilities.setCapability(MobileCapabilityType.PLATFORM_NAME, "Android");
capabilities.setCapability(MobileCapabilityType.PLATFORM_VERSION, "8.1.0");
capabilities.setCapability(MobileCapabilityType.DEVICE_NAME,"Android");
capabilities.setCapability(MobileCapabilityType.APP, app.getAbsolutePath());
capabilities.setCapability(AndroidMobileCapabilityType.APP_PACKAGE, "com.talentpace.substk");
capabilities.setCapability(AndroidMobileCapabilityType.APP_ACTIVITY, "com.talentpace.substk.MainActivity");
URL url=new URL("http://127.0.0.1:4723/wd/hub");
AndroidDriver driver=new AndroidDriver(url,capabilities);
Thread.sleep(5000);
driver.findElementByXPath("//node[#class='android.widget.EditText']").click();
}
}
Change your AUTOMATION_NAME in desired capabilites from Appium to uiautomator2
capabilities.setCapability(MobileCapabilityType.AUTOMATION_NAME, "uiautomator2");
Seems that you have given the wrong xpath
the correct one should look like this
//android.widget.EditText[#class='Enter Class name here..']
A new session could not be created.
[error: No app set; either start appium with --app or pass in an 'app' value in desired capabilities, or set android Package to launch pre-existing app on device)
While running code with appium using eclipse, I had this error.
public class FirstAppiumProgram {
public static void main(String[] args) throws MalformedURLException {
DesiredCapabilities dc = new DesiredCapabilities();
dc.setCapability("BROWSER_NAME", "");
dc.setCapability(MobileCapabilityType.PLATFORM_NAME,Platform.ANDROID);
dc.setCapability(MobileCapabilityType.VERSION, "5.1.1");
dc.setCapability("APP_PACKAGE", "com.android.calculator2");
dc.setCapability("APP_ACTIVITY", "com.android.calculator2.Calculator");
dc.setCapability(MobileCapabilityType.DEVICE_NAME,"Nexus 5");
WebDriver driver= new RemoteWebDriver(new
URL("http://127.0.0.1:4723/wd/hub"),dc);
System.out.println(dc.toString());
driver.quit();
}
}
I think your error is because your capabilities are set wrongly.
At least appPackage and appActivity.
Read how capabilities are. Your possible capabilities:
//Your capabilities should be something like this:
DesiredCapabilities dc = new DesiredCapabilities();
dc.setCapability("platformName", "Android");
dc.setCapability("platformVersion", "5.1.1");
dc.setCapability("deviceName", "Nexus 5");
dc.setCapability("appPackage", "com.android.calculator2");
dc.setCapability("appActivity", "com.android.calculator2.Calculator");
Give it a try and let me know if you made it work
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
I am stuck with a game application where the appium returns 200 status code for a spin click(none of the clicks on game page is working though appium returns 200 code) but the click is not performed on the device. It is a web application and I am using the real device. Any help would be greatly appreciated.
I have tried using implicit, explicit waits, wait using Thread, Javascript, coordinates based click but no luck.
I am using the below code:
public class AndriodDriver {
AndroidDriver<WebElement> driver;
#Test
public void testFirstCalculator() throws IOException, InterruptedException {
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities = new DesiredCapabilities();
capabilities.setCapability("deviceName", "Samsung Galaxy S7");
capabilities.setCapability("platformName", "Android");
capabilities.setCapability(CapabilityType.BROWSER_NAME, "Chrome");
capabilities.setCapability("platformVersion", "7.0");
driver = new AndroidDriver<WebElement>(new
URL("http://0.0.0.0:4723/wd/hub"), capabilities);
driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
driver.get("https://pa03-mob.wi-gameserver.com/resource-service/test-
lobby/index.html");
driver.manage().timeouts().implicitlyWait(200, TimeUnit.SECONDS);
System.out.println("main url");
driver.findElement(By.xpath(".//*[#id='Open Lobby with debug properties']")).click();
System.out.println("lobby opened");
driver.findElement(By.xpath(".//*[#id='brucelee']")).click();
System.out.println("game loading");
WebDriverWait wait= new WebDriverWait(driver, 30);
//wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath(".//*
[#id='spin-button']")));
Thread.sleep(3500);
driver.findElementByXPath(".//*[#id='spin-button']").click();
}
}
First step:
Please add the following capability :
cap.setCapability("automationName","MyTest");
Second step:
Please replace the line :
capabilities.setCapability(CapabilityType.BROWSER_NAME, "Chrome");
by this:
cap.setCapability("browserName","Chrome");
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.