I am beginner to Apium and java, i was looking for a solution to driver.scrollTo("Views"), i tried the below code , while running i was getting the following error:
A new session could not be created. (Original error: Activity used to start app doesn't exist or cannot be launched! Make sure it exists and is a launchable activity) (WARNING: The server did not provide any stacktrace information)
package Android;
import static org.junit.Assert.assertNotNull;
import io.appium.java_client.android.AndroidDriver;
import java.net.URL;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
public class ScrollingToText {
AndroidDriver driver;
#BeforeTest
public void setUp() throws Exception {
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability("deviceName", "0123456789ABCDEF");
capabilities.setCapability("browserName", "Android");
capabilities.setCapability("platformVersion", "5.0");
capabilities.setCapability("platformName", "Android");
capabilities.setCapability("appPackage", "com.hmh.api");
capabilities.setCapability("appActivity","com.hmh.ApiDemos");
driver = new AndroidDriver(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);
driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);
}
#Test
public void findScrollable() throws InterruptedException {
//Scroll till element which contains "Views" text If It Is not visible on screen.
// driver.scrollTo("Views");
driver.findElementByAccessibilityId("Views").click();
WebElement radioGroup = driver.findElementByAndroidUIAutomator("new UiScrollable(new UiSelector()"+ ".resourceId(\"android:id/list\")).scrollIntoView("
+ "new UiSelector().text(\"Radio Group\"));");
assertNotNull(radioGroup.getLocation());
// Click on Views/.
driver.findElement(By.name("Views")).click();
System.out.println("Scrolling has been started to find text -> Tabs.");
// Scroll till element which contains Tabs text.
// driver.scrollTo("Tabs");
driver.findElementByAccessibilityId("Tabs").click();
System.out.println("Tabs text has been found and now clicking on It.");
// Click on Tabs.
driver.findElement(By.name("Tabs")).click();
}
#AfterTest
public void End() {
driver.quit();
}
}
(Original error: Activity used to start app doesn't exist or cannot be launched! Make sure it exists and is a launchable activity)
Above error come when you don't have application installed on your device.
Please install it first and then run your test script it should run.
Hope these help you.
Related
I am trying to run a mini appium project, I have the emulator and an appium server running and here's my code, it says that .getBinaryPath() is undefined for type WebDriverManager "caps.setCapability("chromedriverExecutable", WebDriverManager.chromedriver().getBinaryPath());"
package appiumBasics;
import java.net.MalformedURLException;
import java.net.URL;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.testng.annotations.Test;
import io.appium.java_client.android.AndroidDriver;
import io.appium.java_client.remote.MobileCapabilityType;
import io.github.bonigarcia.wdm.WebDriverManager;
#Test
public class RubWebApplicationAndroidEmulator {
public void OpenWebApplication() throws MalformedURLException {
DesiredCapabilities caps = new DesiredCapabilities();
caps.setCapability(MobileCapabilityType.BROWSER_NAME, "chrome");
caps.setCapability(MobileCapabilityType.DEVICE_NAME, "HaidyEmulator");
WebDriverManager.chromedriver().setup();
caps.setCapability("chromedriverExecutable", WebDriverManager.chromedriver().getBinaryPath());
AndroidDriver driver = new AndroidDriver(new URL("http://127.0.0.1:4723/wd/hub"),caps);
}
}
That is because if you actually go to the repository and open the problematic class, you find there is no such method defined for WebDriverManager:
https://github.com/bonigarcia/webdrivermanager/blob/master/src/main/java/io/github/bonigarcia/wdm/WebDriverManager.java
Presumably this was changed at some point. Possibly you need WebDriverManager#getDownloadedDriverPath():
#Test
public class RubWebApplicationAndroidEmulator {
public void OpenWebApplication() throws MalformedURLException {
DesiredCapabilities caps = new DesiredCapabilities();
caps.setCapability(MobileCapabilityType.BROWSER_NAME, "chrome");
caps.setCapability(MobileCapabilityType.DEVICE_NAME, "HaidyEmulator");
WebDriverManager.chromedriver().setup();
caps.setCapability("chromedriverExecutable", WebDriverManager.chromedriver().getDownloadedDriverPath());
AndroidDriver driver = new AndroidDriver(new URL("http://127.0.0.1:4723/wd/hub"),caps);
}
}
This question already exists:
How to run a Java Selenium test on an incognito Google Chrome profile
Closed 2 years ago.
I hope that you're fine. I'm new at Java Selenium automation, what I want to do is I want to run a test on a certain website but not in a clean session of ChromeDriver I would like to use a certain profile of mine I name it "test" its shortcut in the Desktop is "test.lnk", here what I did:
import static org.testng.Assert.assertEquals;
import org.openqa.selenium.By;
import org.openqa.selenium.By.ByXPath;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
//import org.openqa.selenium.edge.EdgeDriver;
import org.testng.annotations.Test;
public class AutomationTest {
WebDriver driver;
#BeforeTest
public void setUp() {
ChromeOptions options = new ChromeOptions();
options.addArguments("user-data-dir=C:/Users/user_name/AppData/Local/Google/Chrome/User Data/test");
options.addArguments("--start-maximized");
driver = new ChromeDriver(options);
// driver = new ChromeDriver();
driver.get("https://www.rapidtables.com/tools/click-counter.html");
driver.manage().window().maximize();
}
#Test
public void testRegister() throws InterruptedException {
do {
Thread.sleep(3000);
driver.findElement(By.id("addbtn")).click();
} while(true);
}
#AfterTest
public void tearDown() {
// driver.close();
}
}
```
chrom_options.add_argument("user-data-dir=C:/Users/user_name/AppData/Local/Google/Chrome/User Data")
chrom_options.add_argument("profile-directory=test)
specify only the user data folder in userdata dir argument
Pass profile directory separately
I want to automate test a basic Hybrid Mobile Application build on top of Cordova running in Android. I used Apppium for that. I followed the tutorial video to get started. I downloaded and Added Selenum, selendroid and java client .jar files to the build path of the Application.
Below is my code,
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Iterator;
import java.util.Set;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
import io.appium.java_client.AppiumDriver;
import io.appium.java_client.MobileElement;
import io.appium.java_client.android.AndroidDriver;
import io.appium.java_client.remote.MobileCapabilityType;
public class AppiumTest {
public static void main(String[] args) throws MalformedURLException, InterruptedException {
URL serverAddress = new URL("http://0.0.0.0:4723/wd/hub");
WebDriver driver = new AndroidDriver<MobileElement>(serverAddress, getDesiredCapabilities());
try{
Set<String> contextNames = ((AppiumDriver<MobileElement>) driver).getContextHandles();
for (final String contextName : contextNames) {
System.out.println(contextName);
if (contextName.contains("WEBVIEW_0")) {
Thread.sleep(3000);
driver.switchTo().window("WEBVIEW_0");
}
}
//Change color to Red
driver.findElement(By.cssSelector("p.recieved")).click();
Thread.sleep(2000);
//Change color to Red
driver.findElement(By.cssSelector("recieved")).click();
Thread.sleep(2000);
driver.get("http//appium.io/");
Thread.sleep(2000);
}
finally {
driver.quit();
}
System.out.println("Driver "+driver);
}
public static DesiredCapabilities getDesiredCapabilities() {
DesiredCapabilities capability = new DesiredCapabilities();
capability.setCapability(MobileCapabilityType.AUTOMATION_NAME, "selendroid");
capability.setCapability(MobileCapabilityType.PLATFORM_NAME, "Android");
capability.setCapability("platformVersion", "4.4.4");
capability.setCapability("deviceName", "Android-Dev");
capability.setCapability("app",
"C:/Joseph/Appium/HybridProject/AppiumTest/platforms/android/build/outputs/apk/android-debug.apk");
capability.setCapability("appPackage", "com.joseph.appiumtest");
capability.setCapability("appActivity", ".MainActivity");
return capability;
}
}
I can able to get the Capabilities and Contexts. On switching the window (driver.switchTo().window("WEBVIEW_0")), I am getting error like Exception in thread "main" org.openqa.selenium.WebDriverException: CATCH_ALL: java.lang.VerifyError: io/selendroid/server/model/SelendroidWebDriver
Versions :
Android : 4.4.4
Appium : 1.6.3
Selenium : 3.0.1
Selendroid: 0.17.0
After lots of tries, got the Automation testing in Hybrid Mobile Application using Appium worked.
Basically in Capabilities, its not necessary to set the package name and activity name. Instead get the file path of the Application package name(.apk).
File app= new File("project/platforms/android/build/outputs/apk/android-debug.apk");
DesiredCapabilities capabilities= new DesiredCapabilities();
capabilities.setCapability("deviceName", "Android-Dev");
capabilities.setCapability("platformVersion", "4.4.4");
capabilities.setCapability(MobileCapabilityType.PLATFORM_NAME, "MobilePlatform.ANDROID");
capabilities.setCapability("app", app.getAbsolutePath());
Then do not explicitly switch the window to WEBVIEW. Get the context and set the dynamic context of Webview particular for the Application.
try {
Set<String> contextNames = ((AppiumDriver) driver).getContextHandles();
for (String contextName : contextNames) {
System.out.println(contextName);
if (contextName.contains("WEBVIEW")) {
((AppiumDriver<MobileElement>) driver).context(contextName);
}
}
}
catch(Exception e){
e.printStackTrace();
}
And finally with the driver set, do the action.
driver.findElement(By.xpath("//*[#id='login']")).click();
Performing automation test on android emulator using appium. Browser in emulator is not opening when it is automated via code. I have copied my code below kindly look into it and help me out. Thanks in advance
package report;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.List;
import java.util.concurrent.TimeUnit;
import javax.swing.JOptionPane;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxProfile;
import org.openqa.selenium.remote.CapabilityType;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
public class emulator {
WebDriver driver;
public void setUp() throws MalformedURLException
{
DesiredCapabilities capabilities= new DesiredCapabilities();
capabilities.setCapability(CapabilityType.BROWSER_NAME,"browser");
capabilities.setCapability(CapabilityType.VERSION,"4.4");
capabilities.setCapability(CapabilityType.PLATFORM,"windows");
capabilities.setCapability("platformName","Android");
capabilities.setCapability("devices","Android");
capabilities.setCapability("avd","nexus");
capabilities.setCapability("deviceName","");
capabilities.setCapability("appPackage", "com.android.browser");
capabilities.setCapability("appActivity", "com.android.browser.BrowserActivity");
driver=new RemoteWebDriver(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);
}
public void cal(){
driver.get("http://www.google.com");
}
public static void main(String[] args) throws MalformedURLException
{
emulator a=new emulator();
a.setUp();
a.cal();
}
}
The lock screen will deactivate the other script of our program. So I tried open the emulator manually and disable the lock screen (Settings -> Security -> None). Then close the emulator. Now open the emulator automatically and run the script.
public void setUp(int p) throws MalformedURLException {
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability(CapabilityType.BROWSER_NAME, "browser");
capabilities.setCapability(CapabilityType.VERSION, "");
capabilities.setCapability(CapabilityType.PLATFORM, "windows");
capabilities.setCapability("platformName", "Android");
capabilities.setCapability("udid", "emulator-" + p);
capabilities.setCapability("devices", "Android");
capabilities.setCapability("avd", "Nexus7");
capabilities.setCapability("deviceName", "");
capabilities.setCapability("appPackage", "com.android.browser");
capabilities.setCapability("appActivity", "com.android.browser.BrowserActivity");
driver = new RemoteWebDriver(new URL("http://127.0.0.1:" + this.port + "/wd/hub"),
capabilities);
}
When working on mobile browser we need not to pass all the above desiredcapabilities like apppackage and appactivitiy. You can check the details of required capabilities # http://qaautomationcafe.blogspot.in/2015/09/mobile-web-automation-using-appium.html
I have 2 desktop machines in which Appium script is saved in machine 1 and Appium got installed in machine 2 and android devices also connected in machine 2. now i want to take the script from the machine 1 and execute it in the devices connected in the machine 2. how to achieve this. please suggest me.
The below code i am able to run same machine.i want to run on android device which is connected in another machine.Both machine ip segment is same segment.
package com.appiumproj.test;
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.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.annotations.*;
public class Appium {
AppiumDriver driver;
#BeforeClass
public void setUp() throws MalformedURLException{
//Set up desired capabilities and pass the Android app-activity and app-package to Appium
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability(MobileCapabilityType.VERSION, "5.0.2");
capabilities.setCapability(MobileCapabilityType.PLATFORM_NAME, "Android");
capabilities.setCapability(MobileCapabilityType.DEVICE_NAME,"ZX1D62FPVQ");
capabilities.setCapability(MobileCapabilityType.APP_PACKAGE, "com.android.calculatord");
capabilities.setCapability(MobileCapabilityType.APP_ACTIVITY, "com.android.calculator2.Calculator");
driver = new AndroidDriver(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);
}
#Test
public void testCal(){
WebElement two=driver.findElement(By.name("2"));
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();
WebElement results=driver.findElement(By.className("android.widget.EditText"));
assert results.getText().equals("6"):"Actual value is : "+results.getText()+" did not match with expected value: 6";
System.out.println("Inside Test Function");
terClass
public void teardown(){
driver.closeApp();
}
}
package com.appiumproj.test;
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.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.annotations.*;
public class Appium {
AppiumDriver driver;
#BeforeClass
public void setUp() throws MalformedURLException{
//Set up desired capabilities and pass the Android app-activity and app-package to Appium
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability(MobileCapabilityType.VERSION, "5.0.2");
capabilities.setCapability(MobileCapabilityType.PLATFORM_NAME, "Android");
capabilities.setCapability(MobileCapabilityType.DEVICE_NAME,"ZX1D62FPVQ");
capabilities.setCapability(MobileCapabilityType.APP_PACKAGE, "com.android.calculatord"); // This is package name of your app (you can get it from apk info app
capabilities.setCapability(MobileCapabilityType.APP_ACTIVITY, "com.android.calculator2.Calculator"); // This is Launcher activity of your app (you can get it from apk info app)
//Create AndroidDriver instance and connect to the Appium server.
//It will launch the Calculator App in Android Device using the configurations specified in Desired Capabilities
driver = new AndroidDriver(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);
}
#Test
public void testCal(){
//locate the Text on the calculator by using By.name()
WebElement two=driver.findElement(By.name("2"));
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();
//locate the edit box of the calculator by using By.className()
WebElement results=driver.findElement(By.className("android.widget.EditText"));
//Check the calculated value on the edit box
assert results.getText().equals("6"):"Actual value is : "+results.getText()+" did not match with expected value: 6";
/*
System.out.println("Inside Test Function");
driver.findElement(By.partialLinkText("More")).click();
driver.findElement(By.xpath("//EditText[#text='Email Address']")).sendKeys("tester#gmail.com");
driver.findElement(By.xpath("//LinearLayout/EditText[2]")).sendKeys("Testerpwd");
driver.findElement(By.xpath("//CheckBox")).click();
driver.findElement(By.xpath("//Button[#text='Login']")).click();
WebDriverWait wait = new WebDriverWait(driver,80);
wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath("//Button[#text='Logout']")));
driver.findElement(By.xpath("//Button[#text='Logout']")).click();
*/
}
#AfterClass
public void teardown(){
//close the app
driver.closeApp();
}
}
Give the exact Ip address on the Appium Instance running on Machine 2. By default it is 127.0.0.1, remove this and give its own IP address explicitly in Appium settings. This will work. Let me know if not.