Automating android emulator and browser using Appium script - java

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

Related

When running appium script to automate chrome the browser is getting stuck at data

I have tried or solutions from Stack Overflow. I am using latest chrome browser with latest chrome browser web driver version
I am new to Appium.
When I am running command in terminal
npm show appium-chromedriver version
I am getting 5.0.1
When I am running npm show chromedriver version (without appium)
I am getting version 98.0.1
P.S I am using latest Chrome browser version in my device which is 98.0.4758
import java.net.MalformedURLException;
import java.net.URL;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.remote.DesiredCapabilities;
import io.appium.java_client.android.AndroidDriver;
import io.appium.java_client.remote.MobileCapabilityType;
import io.appium.java_client.service.local.AppiumDriverLocalService;
import io.appium.java_client.service.local.AppiumServiceBuilder;
import io.github.bonigarcia.wdm.WebDriverManager;
public class browse {
public static void main(String[] args) throws MalformedURLException {
System.getProperty("webdriver.chrome.driver", "C:\\Automation\\chromedriver_win32\\chromedriver.exe");
AndroidDriver<WebElement> driver;
DesiredCapabilities cap = new DesiredCapabilities();
cap.setCapability("deviceName", "RealMe");
cap.setCapability(MobileCapabilityType.AUTOMATION_NAME, "UiAutomator1");
cap.setCapability(MobileCapabilityType.BROWSER_NAME, "Chrome");
cap.setCapability(MobileCapabilityType.NO_RESET, true);
cap.setCapability("udid", "MBEITORSYTQOPBAE");
cap.setCapability("platformName", "Android");
cap.setCapability("platformVersion", "9.0");
// cap.setCapability("appPackage", "com.android.Chrome");
// cap.setCapability("appActivity", "com.android.calculator2.Calculator");
// cap.setCapability(MobileCapabilityType.BROWSER_NAME, "Chrome");
cap.setCapability("skipDeviceInitialization", true);
cap.setCapability("skipServerInstallation", true);
// cap.setCapability("chromedriverExecutable", "\Automation\chromedriver_win32");
ChromeOptions options = new ChromeOptions();
options.setExperimentalOption("androidKeepAppDataDir", true);
cap.setCapability(ChromeOptions.CAPABILITY, options);
driver = new AndroidDriver<>(new URL("http://127.0.0.1:4723/wd/hub"), cap);
// driver.get("https://www.youtube.com/");
}
}```

The method getBinaryPath() is undefined for the type WebDriverManager error shows in eclipse while i am trying to run appium automation project

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);
}
}

IOSDriver can't resolve constructor, wants to cast DesiredCapabilities to Capabilities

package AppiumTest;
import java.net.URL;
import java.net.MalformedURLException;
import org.testng.annotations.Test;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.CapabilityType;
import io.appium.java_client.ios.IOSDriver;
public class TestIOSSafariBrowser{
#Test
public void startBrowser() throws MalformedURLException {
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability("deviceName","iPhone 8");
capabilities.setCapability("platformName", "iOS");
capabilities.setCapability("platformVersion", "11.4.1");
capabilities.setCapability(CapabilityType.BROWSER_NAME, "safari");
IOSDriver driver = new IOSDriver(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);
driver.get("http://www.google.com");
System.out.println("Page title is " + driver.getTitle());
}
}
I'm relatively new to using Selenium and Appium.
I'm trying to open Safari to access Google through Xcode and Appium. When I instantiate IOSDriver as driver down below, IOSDriver does not allow me to pass in a DesiredCapabilities object (capabilities), instead expecting me to pass a Capabilities object. How would I try to open Safari and access the Google homepage if I use the Capabilities object instead of DesiredCapabilities, or is there another way to go about this?

Looking for solution to driver.scrollTo("Views")

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.

How to run appium script in remote machine?

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.

Categories