I trying to automate a hybrid app using the appium. i had completed the total setup ready and also testing using sample apk file. I facing problem in getting the object properties for my hybrid app. I am not able to inspect ids using Appium inspector or uiautomatorviewer. It shows only one class for my app .
i also need to enable WebView debugging, for making
setWebContentsDebugging Enabled
to true on the WebView class.can any one help me how to do that?
some of the blogs are saying to keep driver.context("web_view"); but i not clear how to get that. please help to to solve this.
thanks.
this is my java class
public class myMavenTest {
private WebDriver driver;
//i think is not the way to do this.so i comented this.
/*public void onCreate(){
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT){
if(0 != (getApplicationInfo().flags = ApplicationInfo.FLAG_DEBUGGABLE)){
WebView.setWebContentsDebuggingEnabled(true);
}
}
}*/
#BeforeTest
public void setUp() throws Exception
{
System.out.println("in the setup function");
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability(CapabilityType.BROWSER_NAME, "");
capabilities.setCapability("deviceName","My Android");
capabilities.setCapability("platformVersion","5.1");
capabilities.setCapability("platformName","Android");
capabilities.setCapability("appPackage","com.mysoftware.testapp");
capabilities.setCapability("appActivity","com.mysoftware.testapp.MainActivity");
try
{
driver = new AndroidDriver(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);
driver.manage().timeouts().implicitlyWait(5,TimeUnit.SECONDS);
//Thread.sleep(10000);
}
catch(MalformedURLException e)
{
e.printStackTrace();
}
}
#Test
public void Loginforsample() throws Exception
{
System.out.println("in the login() function");
//i tried using classname of my app. but it is not recognizing
driver.findElement(By.className("ink-dark")).click();
//After the button clicks need to enter the text
driver.findElement(By.xpath("//need to find xpath'")).sendKeys("My first Automation");
//tried using selendroid.apk works fine here.
/*driver.findElement(By.id("io.selendroid.testapp:id/startUserRegistration")).click();*/
Thread.sleep(10000);
}
#AfterTest
public void tearDown() throws InterruptedException
{
Thread.sleep(10000);
driver.quit();
}
}
Use below the sample Hybrid App code which I have written for a combination of WEBView Native View. Hope it helps for you!
public class Hybrid_App {
public static void main(String[] args) throws MalformedURLException, InterruptedException{
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability("deviceName", "Atom 2x");
capabilities.setCapability("platformName", "Android");
capabilities.setCapability("platformVersion", "5.1");
capabilities.setCapability("appPackage","***YourHydridAppPkg****");
capabilities.setCapability("appActivity", "****YourlauchableActivity***");
AndroidDriver driver= new AndroidDriver(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);
Thread.sleep(4000);
Set<String> contextHandles = driver.getContextHandles();
Map<String,String> hashMap= new HashMap<String,String>();
for(String contextname:contextHandles){
if(contextname.contains("NATIVE")){
hashMap.put("native", contextname);
}else{
hashMap.put("webview", contextname);
}
}
//native page - Native
driver.context(hashMap.get("native"));
WebDriverWait wait= new WebDriverWait(driver, 50);
WebElement ele_voucher = wait.until(ExpectedConditions.visibilityOfElementLocated(
By.xpath("//*[#class='android.view.View'][#index='9'][#content-desc='VOUCHERS']")));
System.out.println(ele_voucher.isDisplayed());
Thread.sleep(6000);
ele_voucher.click();
Thread.sleep(9000);
//second page - Native
driver.context(hashMap.get("native"));
Thread.sleep(5000);
driver.findElementByXPath("//*[#class='android.view.View'][#index='17'][#content-desc='REDEEM']").click();
Thread.sleep(8000);
System.out.println("----Third page----"+" uname,pwd element");
//third page - Webview
driver.context(hashMap.get("webview"));
Thread.sleep(6000);
driver.findElementByXPath("//input[#class='x-input-email'][#type='email'][#name='email']").sendKeys("descbatch#gmail.com");
WebElement ele_pwd = driver.findElementByXPath("//input[#class='x-input-password'][#type='password'][#name='password']");
ele_pwd.click();
Thread.sleep(4000);
ele_pwd.sendKeys("12345");
Thread.sleep(6000);
System.out.println("----Third page----"+" Sign in element");
//fourth page - Native
driver.context(hashMap.get("native"));
Thread.sleep(6000);
driver.findElementByXPath("//*[#class='android.view.View'][#index='69'][#content-desc='SIGN IN']").click();
Thread.sleep(6000);
driver.sendKeyEvent(AndroidKeyCode.BACK);
}
}
Related
I am automating Amazon app through appium. In product description page it uses webview.
But when using getContextHandles() in appium it only returns NATIVE_APP as context.
Even switching directly by using driver.context("WEBVIEW_1") throws exception saying no such context present.
How to switch to webview in amazon app.
public class AmazonTest {
AndroidDriver<MobileElement> driver;
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("VERSION", "9");
capabilities.setCapability("deviceName","Mi A2");
capabilities.setCapability("platformName","Android");
capabilities.setCapability("appPackage", "com.amazon.mShop.android.shopping");
capabilities.setCapability("appActivity", "com.amazon.mShop.home.HomeActivity");
//Url where appium server is running
URL url = new URL("http://0.0.0.0:4723/wd/hub");
driver = new AndroidDriver<>(url, capabilities);
}
public void amazonTest() throws InterruptedException {
//Skip sign in
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
MobileElement skipSignInButton = (MobileElement) driver.findElement(By.id("com.amazon.mShop.android.shopping:id/skip_sign_in_button"));
skipSignInButton.click();
//Search for IPhone
driver.manage().timeouts().implicitlyWait(7, TimeUnit.SECONDS);
MobileElement searchTextElement = (MobileElement) driver.findElement(By.id("com.amazon.mShop.android.shopping:id/rs_search_src_text"));
searchTextElement.sendKeys("iPhone X"+"\n");
//click on first result
driver.manage().timeouts().implicitlyWait(7, TimeUnit.SECONDS);
MobileElement firstResult = (MobileElement) driver.findElement(By.xpath("//android.widget.TextView[contains(#text,'Apple iPhone X (64GB) - Space Grey')]"));
firstResult.click();
//Click on Enter Pincode button
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
MobileElement pincodeButton = (MobileElement) driver.findElement(By.id("com.amazon.mShop.android.shopping:id/loc_ux_gps_enter_pincode"));
pincodeButton.click();
//Enter pincode
driver.manage().timeouts().implicitlyWait(7, TimeUnit.SECONDS);
MobileElement pinTextBox = (MobileElement) driver.findElement(By.id("com.amazon.mShop.android.shopping:id/loc_ux_pin_code_text_pt1"));
pinTextBox.sendKeys("248002");
MobileElement applyButton = (MobileElement) driver.findElement(By.id("com.amazon.mShop.android.shopping:id/loc_ux_update_pin_code"));
applyButton.click();
driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
//Swipe up to show the buy now button
TouchAction ta = new TouchAction(driver);
ta.press(PointOption.point(540, 1420)).moveTo(PointOption.point(540, 200)).release().perform();
Set<String> con = driver.getContextHandles(); //Get all context associated with this app
for(String s : con) { //only showing native app
System.out.println(s);
}
}
//close the application
public void teardown(){
driver.quit();
}
public static void main(String args[]) {
AmazonTest d = new AmazonTest();
//simulating TestNg Flow
try {
d.setUp();
d.amazonTest();
d.teardown();
}catch(Exception e) {
e.printStackTrace();
}
}
}
For hybrid application, you will need a debug application to run automated tests using appium. If you use the release build for hybrid app, you will be able to automate native context. But to run the non native content you will need a debug app.
Native app : Debug app not needed.
Hybrid app : Debug app is needed to detect web elements, native content can be used both ways.
I am facing some problem with PhantomJS reg locating the webelements. I have gone through previously answered questions in which 2 possible solutions was given:
Sync Issue-give Implicit wait
Css selector don't work in phantomjs.
I have tried both the solution but my code is still not working.
Code:
public class Headless_phantomJS {
#Test
public void googlesearch() throws InterruptedException
{
File path=new File("C:/Third party softwares/phantomJS/phantomjs-2.1.1-windows/phantomjs-2.1.1-windows/bin/phantomjs.exe");
System.setProperty("phantomjs.binary.path",path.getAbsolutePath());
WebDriver driver= new PhantomJSDriver();
driver.manage().window().maximize();
driver.navigate().to("https://www.google.co.in/");
System.out.println("inside Test");
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
driver.findElement(By.xpath("//input[#id='lst-ib']")).isEnabled();
driver.findElement(By.xpath("//input[#id='lst-ib']")).sendKeys("lol");
driver.findElement(By.xpath(".//*[#id='tsf']/div[2]/div[3]/center/input[1]")).click();
}
}
Here is your own code block which executes well with PhantomJS 2.1.1 with some tweaks and some added Sysouts for your convenience:
#Test
public void googlesearch()
{
File path=new File("C:\\Utility\\phantomjs-2.1.1-windows\\bin\\phantomjs.exe");
System.setProperty("phantomjs.binary.path",path.getAbsolutePath());
WebDriver driver= new PhantomJSDriver();
driver.manage().window().maximize();
driver.navigate().to("https://www.google.co.in/");
System.out.println("inside Test");
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
System.out.println("Checking if the Search field is Enabled");
driver.findElement(By.name("q")).isEnabled();
System.out.println("Sending lol to Search field");
driver.findElement(By.name("q")).sendKeys("lol");
System.out.println("Clicking on Google Search button Next");
driver.findElement(By.name("btnG")).click();
System.out.println("Google Search Completed");
}
I am new to Appium.
I am trying to automate an application which on launch asks to allow to access Contact List. I did research on various websites for the solution but couldn`t resole this issue.
Code:
public class TestSuiteBase {
WebDriver driver = null;
//Element Variables
String deviceIdTextbox = "et_Registration_DeviceID";
#BeforeClass
public void setup() throws MalformedURLException, InterruptedException {
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability("platformName", "Android");
capabilities.setCapability("platformVersion", "4.4.2");
capabilities.setCapability("deviceName", "0ef3c26f");
capabilities.setCapability("browserName", "");
capabilities.setCapability("app", "D:\\Ashish\\AppiumProject\\Mobile Framework\\apk\\androidApplication.apk");
capabilities.setCapability("appPackage", "com.atyati.rbl.mfi");
capabilities.setCapability("appActivity", "com.atyati.rbl.mfi.Activity.SplashScreenActivity");
AndroidDriver driver = new AndroidDriver(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);
driver.manage().timeouts().implicitlyWait(80, TimeUnit.SECONDS);
Thread.sleep(10000);
}
#Test
public void Test() throws Exception {
Thread.sleep(2000);
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability("appWaitPackage", "com.android.packageinstaller");
capabilities.setCapability("appWaitActivity", ".permission.ui.GrantPermissionsActivity");
System.out.println("Trying to click Allow");
driver.findElement(MobileBy.id("permission_allow_button")).click();
driver.findElement(By.id(deviceIdTextbox)).sendKeys("123456789");
}
#AfterClass
public void tearDown() throws Exception {
driver.quit();
}
Output: My code launches the app but when permission pop-up surfaces, it is unable to click.
Issue: Not able click on permission pop-up.
Query: Do I need to specify "appActivity" before moving to each screen?
Let me know if any other details needed.
I have figured it out where I was going wrong.
Solution: I have to specify the complete resource id for the Edit Textbox
String deviceIdTextbox = "com.atyati.rbl.mfi:id/et_Registration_DeviceID";
Also, it is not needed to specify the activity.
#Test
public void Test() throws Exception {
Thread.sleep(1000);
if(androidDriver!=null){
System.out.println("driver does not == null");
System.out.println("Trying to click Allow");
androidDriver.findElement(By.id(contactListAllowPermissionBtn)).click();
Thread.sleep(1000);
androidDriver.findElement(By.id(deviceIdTextbox)).sendKeys("123456789");
androidDriver.findElement(By.id(registerUserBtn)).click();
} else {
System.out.println("driver == null");
}
}
You can use this JAVA CODE for clicking on allow app permission button as many times it appears. just add the method and in your class file and call the method in your test case. This code is taken from example on THIS POST
public void allowAppPermission(){
while (driver.findElements(MobileBy.xpath("//*[#class='android.widget.Button'][2]")).size()>0)
{ driver.findElement(MobileBy.xpath("//*[#class='android.widget.Button'][2]")).click();
}
}
I am trying to automate the naukri.com application and as a part of that when I launch the site it basically displays some set of pop up windows which needs to be closed before I proceed to login to the application.This specific functionality has been handled by the code where it closes all the pop up windows and when I proceed to click on the login button, the login button link is not identified and script fails.If i comment the pop up window code then the login button is identified.Please find the below code for the same and kindly help me to resolve the issue
public class naukri {
WebDriver driver = new FirefoxDriver();
#Test
public void pagelaunch() throws InterruptedException{
driver.get("http://www.naukri.com");
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
String parenthandle = driver.getWindowHandle();
String parent = driver.getWindowHandle();
//close all the pop ups
Set<String> pops=driver.getWindowHandles();
Iterator<String> it =pops.iterator();
while (it.hasNext()){
String popupHandle=it.next().toString();
if(!popupHandle.contains(parent))
{
driver.switchTo().window(popupHandle);
System.out.println("Pop Up Title: "+ driver.switchTo().window(popupHandle).getTitle());
driver.close();
}
}
System.out.println("the system handle is"+parenthandle);
//to click on login button and proceed to login to the application
driver.findElement(By.xpath("//a[#title='Jobseeker Login']")).click();
Thread.sleep(5000);
for(String winhandle:driver.getWindowHandles())
{
driver.switchTo().window(winhandle);
}
driver.findElement(By.xpath("//a[#id='uSel']")).click();
driver.findElement(By.xpath("html/body/div[8]/div[2]/div[2]/form/div[4]/div[2]/input")).sendKeys("anand_qa");
driver.findElement(By.xpath("html/body/div[8]/div[2]/div[2]/form/div[5]/div[2]/input")).sendKeys("test1234");
driver.findElement(By.xpath("//div[8]/div[2]/div[2]/form/div[7]/button")).click();
driver.switchTo().window(parenthandle);
}
}
public class naukri {
WebDriver driver = new FirefoxDriver();
#Test
public void pageLaunch() throws InterruptedException {
WebDriver driver = new FirefoxDriver();
driver.get("http://www.naukri.com");
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
String parenthandle = driver.getWindowHandle();
String parent = driver.getWindowHandle();
//close all the pop ups
Set<String> pops=driver.getWindowHandles();
{
Iterator<String> it =pops.iterator();
while (it.hasNext()) {
String popupHandle=it.next().toString();
if(!popupHandle.contains(parent))
{
driver.switchTo().window(popupHandle);
System.out.println("Pop Up Title: "+ driver.switchTo().window(popupHandle).getTitle());
driver.close();
}
}
}
System.out.println("the system handle is"+ driver.switchTo().window(parenthandle).getTitle());
//to click on login button and proceed to login to the application
driver.findElement(By.xpath("//a[#id='login_Layer']")).click();
Thread.sleep(5000);
/*for (String winhandle:driver.getWindowHandles())
{
driver.switchTo().window(winhandle);
}*/
driver.findElement(By.xpath("//a[#id='uSel']")).click();
driver.findElement(By.xpath("//form[#id ='lgnFrm']/div[4]/div[2]/input[#id='uLogin']")).sendKeys("anand_qa");
driver.findElement(By.xpath("//form[#id ='lgnFrm']/div[5]/div[2]/input[#id='pLogin']")).sendKeys("test1234");
driver.findElement(By.xpath("//form[#id='lgnFrm']/div[7]/button")).click();
}
}
#AK17:
Your code had 2 problems
1.You were not switching to parenthandle after closing the pop-up windows, I added the code
driver.switchTo().window(parenthandle);
2.Your locators for username,password and login button were not correct
Working code, try this:
public class naukri {
WebDriver driver = new FirefoxDriver();
#Test
public void pagelaunch() throws InterruptedException{
driver.get("http://www.naukri.com");
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
String parenthandle = driver.getWindowHandle();
String parent = driver.getWindowHandle();
//close all the pop ups
Set<String> pops=driver.getWindowHandles();
Iterator<String> it =pops.iterator();
while (it.hasNext()){
String popupHandle=it.next().toString();
if(!popupHandle.contains(parent))
{
driver.switchTo().window(popupHandle);
System.out.println("Pop Up Title: "+ driver.switchTo().window(popupHandle).getTitle());
driver.close();
}
}
System.out.println("the system handle is"+parenthandle);
driver.switchTo().window(parenthandle);
WebDriverWait wait = new WebDriverWait(driver,10);
WebElement login = driver.findElement(By.xpath("//a[#title='Jobseeker Login']"));
wait.until(ExpectedConditions.elementToBeClickable(login));
//to click on login button and proceed to login to the application
driver.findElement(By.xpath("//a[#title='Jobseeker Login']")).click();
Thread.sleep(3000);
for(String winhandle:driver.getWindowHandles())
{
System.out.println("login: "+winhandle);
driver.switchTo().window(winhandle);
}
Thread.sleep(3000);
driver.findElement(By.xpath("//a[#id='uSel']")).click();
driver.findElement(By.xpath(".//*[#id='uLogin']")).sendKeys("anand_qa");
driver.findElement(By.xpath(".//*[#id='pLogin']")).sendKeys("test1234");
driver.findElement(By.xpath(".//*[#id='lgnFrm']/div[7]/button")).click();
//driver.switchTo().window(parenthandle);
}
}
I'm automating a php web page using webdriver and java language. My code was executing and I was able to perform actions on web page, but today only the login method is executing and my second method gets failed everytime I run. I'm so worried why it is happening. Please help, I'm new in automation testing.
public class TestNGClass {
private String baseUrl = "http://test.com/test2/1.4.6.3/public/admin/";
private WebDriver driver = new FirefoxDriver();
#Test
public void login() {
driver.manage().window().maximize();
driver.get(baseUrl);
driver.findElement(By.name("username")).sendKeys("abc");
driver.findElement(By.name("password")).sendKeys("123");
driver.findElement(By.name("login")).submit();
System.out.print("\nCongrats..You have successfully logged in.");
}
#Test
public void createUser() {
String expectedTitle = "User";
String actualTitle = driver.getTitle();
Assert.assertEquals(actualTitle, expectedTitle,"Title Not Found!");
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
driver.findElement(By.xpath("//body/div[3]/div[2]/ul/li[2]/a/img")).click();
Error :
java.lang.AssertionError: Title Not Found! expected [User] but found []
This is because you are using Assert.
Assert.assertEquals(actualTitle, expectedTitle,"Title Not Found!");
Make use of Try catch in-order to proceed next step.
try{
Assert.assertEquals(actualTitle, expectedTitle,"Title Not Found!");
}catch (Exception e)
{
}