📜 ⬆️ ⬇️

Translation of the Appium Essentials book. Chapter 6

Good evening, Habr. This is where the Appium Essentials translation comes to an end. What has already been completed:


In this chapter:




Before the start


Before starting work, once again make sure that everything is set up:
Requirements for AndroidIOS requirements
Java (version 7 and up)Mac OS (version 10.7 and higher)
Android SDK API, version 17 and higherXcode (version 4.6.3 and higher; 5.1 recommended)
Android deviceIOS profile [these and other incomprehensible words are explained below]
Chrome browser on deviceIOS device
Eclipse [or Idea]SafariLauncher application
Testngios-webkit-debug-proxy
Appium serverJava (version 7 and up)
Appium client library (we still have Java)Eclipse [or Idea]
Selenium Server and WebDriver Java LibraryTestng
Apk Info appSelenium Server and WebDriver Java Library
Appium server
Appium client library (we still have Java)

Make sure that the Android device is in developer mode and debugging is enabled on USB.
To check that the device is connected, type in the command line
')
adb devices 

You will receive a list of Android devices. If not, try restarting the adb server:

 adb kill-server adb start-server 

Desired capabilities for Android for native and hybrid applications


In Chapter 1, we discussed the options and why. So here, let's go straight to the code.
First, import the packages:

 import java.io.File; import org.openqa.selenium.remote.DesiredCapabilities; import io.appium.java_client.remote.MobileCapabilityType; 

Now DC:

 DesiredCapabilities caps = new DesiredCapabilities(); File app=new File("path of the apk");//    apk caps.setCapability(MobileCapabilityType.APP,app);//   ,      caps.setCapability(MobileCapabilityType.PLATFORM_VERSION, "4.4");// Android caps.setCapability(MobileCapabilityType.PLATFORM_NAME, "Android");// OS caps.setCapability(MobileCapabilityType.DEVICE_NAME, "Moto X");//    caps.setCapability(MobileCapabilityType.APP_PACKAGE, "   (  ,  apk info app)"); caps.setCapability(MobileCapabilityType.APP_ACTIVITY, ",     (    apk info app)"); 

Desired capabilities for Android for web applications


When working with web applications, we will not need some of the options mentioned above: APP, APP PACKAGE and APP ACTIVITY, since we work with a browser.
First, import the packages:

 import java.io.File; import org.openqa.selenium.remote.DesiredCapabilities; import io.appium.java_client.remote.MobileCapabilityType; 

Now DC:

 DesiredCapabilities caps = new DesiredCapabilities(); caps.setCapability(MobileCapabilityType.PLATFORM_VERSION, "4.4");// Android caps.setCapability(MobileCapabilityType.PLATFORM_NAME, "Android");// OS caps.setCapability(MobileCapabilityType.DEVICE_NAME, "Moto X");//    caps.setCapability(MobileCapabilityType.BROWSER_NAME, "Chrome"); //  Chrome 

We are done with the configuration, now we can initialize the driver.
Import:

 import io.appium.java_client.android.AndroidDriver; import java.net.URL; 

Initialization:

 AndroidDriver driver = new AndroidDriver (new URL("http://127.0.0.1:4723/wd/hub"), caps);// ,   Appium- 

Everything is ready to work with your Android device.

Understanding the provisional profile, SafariLauncher and ios-webkit-debug-proxy


Before setting up a driver for iOS, you need to follow a few steps.

Provisional profile

Profile is needed in order to install your applications on iOS devices. To do this, you need to join the iOS Developer Program

After registering, visit this page to generate a profile.

This profile will need to be installed on the device:

  1. Download the generated profile
  2. Connect iOS device to Mac
  3. Open Xcode (version 6) and go to Window -> Devices



  4. Call the context menu for the connected device and click on Show Provisional Profiles ...



  5. Click +, select the downloaded profile, click Done.


SafariLauncher and ios-webkit-debug-proxy application

SafariLauncher is used to launch Safari browser on the device. You need to build and fire SafariLauncher on an iOS device in order to work with Safari browser:

  1. Download the source
  2. Launch Xcode and open the SafariLauncher project
  3. Select the device for deployment and click the build button
  4. Then you will need to replace SafariLauncher in Appium.dmg; for this you need:
    1. Call the context menu for Appium.dmg
    2. Click on Show Package Contents and go to Contents / Resources / node_modules / appium / build / SafariLauncher
    3. Unzip SafariLauncher.zip
    4. Go to submodules / SafariLauncher / build / Release-iphoneos and replace the SafariLauncher application with your own.
    5. Archive the submodules and rename to SafariLauncher.zip

Now you need to put ios-webkit-debug-proxy on Mac to establish a connection and access the web-view. To set up a proxy, you can use brew and execute the command

 brew install ios-webkit-debug-proxy 

Desired capabilities for iOS for native and hybrid applications


In Chapter 1, we discussed the options and why. So here, let's go straight to the code.
First, import the packages:

 import java.io.File; import org.openqa.selenium.remote.DesiredCapabilities; import io.appium.java_client.remote.MobileCapabilityType; 

Now DC:

 DesiredCapabilities caps = new DesiredCapabilities(); File app=new File("path of the .app"); caps.setCapability(MobileCapabilityType.APP,app); caps.setCapability(MobileCapabilityType.PLATFORM_VERSION, "8.1"); caps.setCapability(MobileCapabilityType.PLATFORM_NAME, "iOS");// caps.setCapability(MobileCapabilityType.DEVICE_NAME, "iPad");// caps.setCapability("udid","Id  ");//UDID 

Desired capabilities for iOS for web applications


Some of the options we do not need. These options include: APP, APP PACKAGE and APP ACTIVITY. Now to the point.

First, import the packages:

 import java.io.File; import org.openqa.selenium.remote.DesiredCapabilities; import io.appium.java_client.remote.MobileCapabilityType; 

Now DC:

 DesiredCapabilities caps = new DesiredCapabilities(); caps.setCapability(MobileCapabilityType.PLATFORM_VERSION, "8.1"); caps.setCapability(MobileCapabilityType.PLATFORM_NAME, "iOS"); caps.setCapability(MobileCapabilityType.DEVICE_NAME,"iPad"); caps.setCapability("udid","UDID  "); caps.setCapability(MobileCapabilityType.BROWSER_NAME,"Safari"); //  Safari 

We are done with the configuration, now we can initialize the driver. Import:

 import io.appium.java_client.ios.IOSDriver; import java.net.URL; 

Initialization:
 IOSDriver driver = new IOSDriver (new URL("http://127.0.0.1:4723/wd/hub"),caps); //,  Appium- 

Everything is ready to work with your iOS device.

Now you can add TestNG and try to start everything together:

 import io.appium.java_client.ios.IOSDriver; import io.appium.java_client.remote.MobileCapabilityType; import java.io.File; import java.net.MalformedURLException; import java.net.URL; import java.util.concurrent.TimeUnit; import org.openqa.selenium.remote.DesiredCapabilities; import org.testng.annotations.AfterClass; import org.testng.annotations.BeforeClass; import org.testng.annotations.Test; public class TestAppIication { IOSDriver driver; @BeforeClass //      public void setUp() throws MalformedURLException{ DesiredCapabilities caps = new DesiredCapabilities(); File app=new File("path of the .app"); caps.setCapability(MobileCapabilityType.APP,app); caps.setCapability(MobileCapabilityType.PLATFORM_VERSION, "8.1"); caps.setCapability(MobileCapabilityType.PLATFORM_NAME, "iOS"); caps.setCapability(MobileCapabilityType.DEVICE_NAME,"iPad"); caps.setCapability("udid","UDID  "); caps.setCapability(MobileCapabilityType.BROWSER_NAME, "Safari");// ,    - driver = new IOSDriver (new URL("http://127.0.0.1:4723/wd/hub"), caps); driver.manage().timeouts().implicitlyWait(30,TimeUnit.SECONDS); } @Test public void testExample(){ //   } @AfterClass public void tearDown(){ driver.closeApp(); //driver.quit(); // ,    - } } 

Automation of native applications


Native Android Applications


In the case of Android automation, the same code will work as in the previous chapter (tested on the emulator), only you need to delete the information about avd.

Let's analyze an example of dialing on Android:


  1. Install the following desired capabilities to launch the dialer application.

     caps.setCapability(MobileCapabilityType.APP_PACKAGE, "com.android.dialer"); caps.setCapability(MobileCapabilityType.APP_ACTIVITY, "com.android.dialer.DialtactsActivity"); 

  2. Now you need to find the dial pad. We will search by AccessibityId

     WebElement dialPad= driver.findElementByAccessibilityId("dial pad")); 

  3. Cry

     dialPad.click(); 

  4. It is necessary to find the keys with numbers. Let's use some logic to find the 0-9 keys by name and click each one of them.

     for(int n=0;n<10;n++){ driver.findElement(By.name(""+n+"")).click(); } 

  5. In this case, we use an incorrect phone number, so the phone will not call anywhere.

  6. Let's find the call button. We will search by AccessibilityId:

     WebElement dial= driver.findElementByAccessibilityId("dial")); 

  7. Cry:

     dial.click(); 

  8. The whole script, using TestNG, will look like this:

     public class TestAppIication { AndroidDriver driver; @BeforeClass public void setUp() throws MalformedURLException{ DesiredCapabilities caps = new DesiredCapabilities(); caps.setCapability(MobileCapabilityType.PLATFORM_VERSION, "4.4"); caps.setCapability(MobileCapabilityType.PLATFORM_NAME, "Android"); caps.setCapability(MobileCapabilityType.DEVICE_NAME,"Moto X");//I am using Moto X as Real Device caps.setCapability(MobileCapabilityType.APP_PACKAGE, "com.android.dialer"); caps.setCapability(MobileCapabilityType.APP_ACTIVITY, "com.android.dialer.DailtactsActivity"); driver = new AndroidDriver (new URL("http://127.0.0.1:4723/wd/hub"), caps); driver.manage().timeouts().implicitlyWait(30,TimeUnit.SECONDS); } @Test public void testExample(){ WebElement dialPad=driver.findElementByAccessibilityId("dial pad"); dialPad.click(); for(int n=0;n<10;n++){ driver.findElement(By.name(""+n+"")).click(); } WebElement dial=driver.findElementByAccessibilityId("dial"); dial.click(); } @AfterClass public void tearDown(){ driver.closeApp(); } } 

Native iOS apps


Here we will work with the BMI calculator.


We will calculate the body mass index by weight and height. For this, do the following:

  1. Install the following desired capabilities to run the application.

     File app=new File("/Users/mhans/appium/ios/BmiCalc.app");//You can change it with your app address caps.setCapability(MobileCapabilityType.APP,app);//To set the app path 

  2. Now you need to find the input fields of height and weight. We will search by Xpath

     WebElement height=driver.findElement(By.xpath("(//UIATextField)[2]")); WebElement weight=driver.findElement(By.xpath("(//UIATextField)[4]")); 

  3. We are looking for the calculate button. Use name:

     WebElement calculateBMI=driver.findElement(By.name("Calculate BMI")); 

  4. Specify growth

     height.sendKeys("1.82"); 

  5. Specify weight

     weight.sendKeys("75"); 

  6. We consider the index

     calculateBMI.click(); 

  7. The whole script, using TestNG, will look like this:

     public class TestAppIication { IOSDriver driver; @BeforeClass public void setUp() throws MalformedURLException{ File app=new File("/Users/mhans/appium/ios/BmiCalc.app");//You can change it with your app address DesiredCapabilities caps = new DesiredCapabilities(); caps.setCapability(MobileCapabilityType.APP,app); caps.setCapability(MobileCapabilityType.PLATFORM_VERSION, "8.1"); caps.setCapability(MobileCapabilityType.PLATFORM_NAME, "iOS"); caps.setCapability(MobileCapabilityType.DEVICE_NAME,"iPad"); caps.setCapability("udid","Real Device Id "); driver = new IOSDriver (new URL("http://127.0.0.1:4723/wd/hub"), caps); driver.manage().timeouts().implicitlyWait(30,TimeUnit.SECONDS); } @Test public void testExample(){ WebElement height=driver.findElement(By.xpath("(//UIATextField)[2]")); height.sendKeys("1.82"); WebElement weight=driver.findElement(By.xpath("(//UIATextField)[4]")); weight.sendKeys("75"); WebElement calculateBMI=driver.findElement(By.name("Calculate BMI")); calculateBMI.click(); } @AfterClass public void tearDown(){ driver.closeApp(); } } 



Web Application Automation


Android Web Applications


For example, consider the Gmail login page. In this section, we will look at how to run Chrome on a real device, go to the desired address, transfer the login / password and click SignIn


  1. Install the following desired capabilities to run Chrome

     caps.setCapability(MobileCapabilityType.BROWSER_NAME, "Chrome"); 

  2. Now you need to specify the address

     driver.get("https://www.gmail.com"); 

  3. Need to find the username element. Use the name locator

     WebElement username=driver.findElement(By.name("Email")); 

  4. Pass value

     username.sendKeys("test"); 

  5. Now, we need to find the password element. Use the name locator

     WebElement password=driver.findElement(By.name("Passwd")); 

  6. Pass value

     password.sendKeys("test"); 

  7. We are looking for the SignIn button. Use the name locator

     WebElement signIn=driver.findElement(By.name("signIn")); 

  8. Cry

     signIn.click(); 

  9. The whole script, using TestNG, will look like this:

     public class TestAppIication { AndroidDriver driver; @BeforeClass public void setUp() throws MalformedURLException { DesiredCapabilities caps = new DesiredCapabilities(); caps.setCapability(MobileCapabilityType.BROWSER_NAME, "Chrome"); caps.setCapability(MobileCapabilityType.PLATFORM_VERSION, "4.4"); caps.setCapability(MobileCapabilityType.PLATFORM_NAME, "Android"); caps.setCapability(MobileCapabilityType.DEVICE_NAME,"Moto X"); driver = new AndroidDriver (new URL("http://127.0.0.1:4723/wd/hub"), caps); driver.manage().timeouts().implicitlyWait(30,TimeUnit.SECONDS); } @Test public void testExample(){ driver.get("https://www.gmail.com"); WebElement username=driver.findElement(By.name("Email")); username.sendKeys("test"); WebElement password=driver.findElement(By.name("Passwd")); password.sendKeys("test"); WebElement signIn=driver.findElement(By.name("signIn")); signIn.click(); } @AfterClass public void tearDown(){ driver.quit(); } } 

IOS web apps


For example, consider the Google search page. In this section, we will look at how to launch the browser on a real device, go to the desired address, pass the string to search for and click Search


  1. Install the following desired capabilities to launch Safari

     caps.setCapability(MobileCapabilityType.BROWSER_NAME, "Safari"); 

  2. Now you need to specify the address

     driver.get("https://www.google.com"); 

  3. You need to find the searchBox element. Use the name locator

     WebElement searchBox=driver.findElement(By.name("q")); 

  4. Pass value

     searchBox.sendKeys("Appium for mobile automation"); 

  5. Before running the script, we will need to enable the proxy with the command:

     ios_webkit_debug_proxy -c 2e5n6f615z66e98c1d07d22ee09658130d345443:27753 –d 

    Replace 2e5n6f615z66e98c1d07d22ee09658130d345443 with the UDID of your device. Port must be 27753

  6. Make sure Web Inspector is enabled (Settings | Safari | Advanced) and SafariLauncher is installed on your device.

  7. The whole script, using TestNG, will look like this:

     public class TestAppIication { IOSDriver driver; @BeforeClass public void setUp() throws MalformedURLException{ DesiredCapabilities caps = new DesiredCapabilities(); caps.setCapability(MobileCapabilityType.BROWSER_NAME, "Safari"); caps.setCapability(MobileCapabilityType.PLATFORM_VERSION, "8.1"); caps.setCapability(MobileCapabilityType.PLATFORM_NAME, "iOS"); caps.setCapability(MobileCapabilityType.DEVICE_NAME, "iPad"); caps.setCapability("udid","Real Device Identifier"); driver = new IOSDriver (new URL("http://127.0.0.1:4723/wd/hub"), caps); driver.manage().timeouts().implicitlyWait(30,TimeUnit.SECONDS); } @Test public void testExample(){ driver.get("https://www.google.com"); WebElement searchBox=driver.findElement(By.name("q")); searchBox.sendKeys("Appium for mobile automation"); } @AfterClass public void tearDown(){ driver.quit(); } } 


Hybrid Application Automation


Android hybrid applications


For example, we will work with Hybridtestapp
To work with hybrid applications, you need to make the changes listed here . In our application Hybridtestapp, they are already applied.


In the example we will try to fill in the form in the application:

  1. Install the following desired capabilities to run the hybrid application.

     File app=new File("C:\\Appium_test\\HybridtestApp.apk");// (On window platform) caps.setCapability(MobileCapabilityType.APP,app); caps.setCapability(MobileCapabilityType.APP_PACKAGE, "com.example.hybridtestapp"); caps.setCapability(MobileCapabilityType.APP_ACTIVITY, "com.example.hybridtestapp.MainActivity"); 

  2. Now you need to change the context to work with WebView.

     Set<String> contexts = driver.getContextHandles(); for (String context : contexts) { System.out.println(context); //   } 

  3. Switchable

     driver.context("WEBVIEW_com.example.hybridtestapp"); 

    or

     driver.context((String) contextNames.toArray()[1]); 

  4. After switching to WebView, we need to find the First Name input field. We will search by name locator

     WebElement firstName=driver.findElement(By.name("fname")); firstName.sendKeys("test"); 

  5. And Last Name

     WebElement lastName=driver.findElement(By.name("lname")); lastName.sendKeys("test"); 

  6. Thus we fill in the remaining fields.

     WebElement age=driver.findElement(By.name("age")); age.sendKeys("26"); WebElement username=driver.findElement(By.name("username")); username.sendKeys("appiumTester"); WebElement password=driver.findElement(By.id("psw")); password.sendKeys("appium@123"); 

  7. And click on the Register button.

     WebElement registerButton=driver.findElement(By.id("register")); registerButton.click(); 

  8. The whole script, using TestNG, will look like this:

     public class TestAppIication { AndroidDriver driver; @BeforeClass public void setUp() throws MalformedURLException{ File app=new File("C:\\Appium_test\\HybridtestApp.apk"); caps.setCapability(MobileCapabilityType.APP,app); DesiredCapabilities caps = new DesiredCapabilities(); caps.setCapability(MobileCapabilityType.PLATFORM_VERSION, "4.4"); caps.setCapability(MobileCapabilityType.PLATFORM_NAME, "Android"); caps.setCapability(MobileCapabilityType.DEVICE_NAME, "Moto X"); caps.setCapability(MobileCapabilityType.AUTOMATION_NAME, "Appium");// Selendroid   android  4.4 caps.setCapability(MobileCapabilityType.APP_PACKAGE, "com.example.hybridtestapp"); caps.setCapability(MobileCapabilityType.APP_ACTIVITY, "com.example.hybridtestapp.MainActivity"); driver = new AndroidDriver (new URL("http://127.0.0.1:4723/wd/hub"), caps); driver.manage().timeouts().implicitlyWait(30,TimeUnit.SECONDS); } @Test public void testExample(){ Set<String> contexts = driver.getContextHandles(); for (String context : contexts) { System.out.println(context); } driver.context((String) contexts.toArray()[1]); WebElement firstName=driver.findElement(By.name("fname")); firstName.sendKeys("test"); WebElement lastName=driver.findElement(By.name("lname")); lastName.sendKeys("test"); WebElement age=driver.findElement(By.name("age")); age.sendKeys("26"); WebElement username=driver.findElement(By.name("username")); username.sendKeys("appiumTester"); WebElement password=driver.findElement(By.id("psw")); password.sendKeys("appium@123"); WebElement registerButton=driver.findElement(By.id("register")); registerButton.click(); } @AfterClass public void tearDown(){ driver.closeApp(); } } 

IOS hybrid applications


An example will be considered on the application WebViewApp


We try to work with the application:

  1. Install the following desired capabilities to run the hybrid application.

     File app=new File("/Users/mhans/appium/ios/WebViewApp.app"); caps.setCapability(MobileCapabilityType.APP,app); 

  2. Now you need to find the input field and specify the desired address

     WebElement editBox=driver.findElement(By.className("UIATextField")); editBox.sendKeys("www.google.com"); 

  3. Search and click the Go button.

     WebElement goButton=driver.findElement(By.name("Go")); goButton.click(); 

  4. Now you need to change the context to work with WebView.

     Set<String> contexts = driver.getContextHandles(); for (String context : contexts) { System.out.println(context); } 

  5. Switchable

     driver.context("WEBVIEW_com.example.testapp"); 

    or

     driver.context((String) contextNames.toArray()[1]); 

  6. Everything. We can work with the google page. We try to open the Images stack:

     WebElement images=driver.findElement(By.linkText("Images")); images.click(); 

  7. Before running the entire test, you need to enable proxy

     ios_webkit_debug_proxy -c <UDID >:27753 –d 

    Port must be 27753

  8. The whole script, using TestNG, will look like this:

     public class TestAppIication { IOSDriver driver; @BeforeClass public void setUp() throws MalformedURLException{ DesiredCapabilities caps = new DesiredCapabilities(); File app=new File("/Users/mhans/appium/ios/WebViewApp.app"); caps.setCapability(MobileCapabilityType.APP,app); caps.setCapability(MobileCapabilityType.PLATFORM_VERSION, "8.1"); caps.setCapability(MobileCapabilityType.PLATFORM_NAME, "iOS"); caps.setCapability(MobileCapabilityType.DEVICE_NAME, "iPad"); caps.setCapability("udid","Real Device Identifier"); driver = new IOSDriver (new URL("http://127.0.0.1:4723/wd/hub"), caps); driver.manage().timeouts().implicitlyWait(30,TimeUnit.SECONDS); } @Test public void testExample(){ WebElement editBox=driver.findElement(By.className("UIATextField")); editBox.sendKeys("https://www.google.com"); WebElement goButton=driver.findElement(By.name("Go")); goButton.click(); Set<String> contexts = driver.getContextHandles(); for (String context : contexts) { System.out.println(context); } driver.context((String) contexts.toArray()[1]); WebElement images=driver.findElement(By.linkText("Images")); images.click(); } @AfterClass public void tearDown(){ driver.closeApp(); } } 



That's all. We have the last chapter left in which we will see how to imitate user-specific actions for a mobile platform.

Source: https://habr.com/ru/post/333546/


All Articles