📜 ⬆️ ⬇️

Translation of the Appium Essentials book. Chapter 5

Hey. We continue to translate the book Appium Essentials and the study of mobile automation. What has already been completed:


In this chapter, we turn to application automation:


There is a lot of code ahead. Go!



Before the start


Before launching Appium, let's make sure that all the necessary software is installed:
')
Requirements for Android:


Requirements for iOS:


When working with Appium, you must install the desired capabilities and initiate the Android / iOS driver.



Required desired capabilities for Android and launch of Android driver


There are two ways to set the desired capabilities: via Appium GUI and by initiating the object desired capabilities. The object desired capabilities preferred. Consider both options: Below are the Android settings in Appium GUI for native and hybrid applications:



How to setup:


Now about Android settings in Appium GUI for web applications:



How to configure:

  1. Open Android settings.
  2. Turn on Launch AVD and select the emulator created from the list [it is assumed that you have already created the emulator].
  3. Select PlatformVersion from the list.
  4. Turn on the Use Browser and select a browser from the list.
  5. Turn on the Device Name and write "Android emulator".
  6. Start the Appium server.

Now about the second method of forming DC.

Desired capabilities for native and hybrid applications


In the first chapter we have already discussed what the desired capabilities are and why they are needed, so here we will immediately plunge into the code [at the bottom !! 1!]. First of all, you need to import packages:

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

Then create the desired capabilities (for native and hybrid):

 DesiredCapabilities caps = new DesiredCapabilities();//  File app=new File("path of the apk");//  File,     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, "Android emulator");//,     caps.setCapability("avd","Name of the AVD to launch");//,      caps.setCapability(MobileCapabilityType.APP_PACKAGE, "package name of your app (you can get it from apk info app)");// package   caps.setCapability(MobileCapabilityType.APP_ACTIVITY, "Launch activity of your app (you can get it from apk info app)");//activity   

Desired capabilities for web applications


In Android web applications, some options are not needed. For example, APP, APP PACKAGE and APP ACTIVITY, because we are launching a browser.

First, you need to import the packages:

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

Then create the desired capabilities (for the web):

 DesiredCapabilities caps = new DesiredCapabilities();//  caps.setCapability(MobileCapabilityType.PLATFORM_VERSION, "4.4");//  Android caps.setCapability(MobileCapabilityType.PLATFORM_NAME, "Android");// OS caps.setCapability(MobileCapabilityType.DEVICE_NAME, "Android emulator");//,     caps.setCapability("avd","Name of the AVD to launch");//,      caps.setCapability(MobileCapabilityType.BROWSER_NAME, "Browser"); //   

As part of the desired capabilities, we did everything. Now you need to initiate the Android driver and connect it to the Appium-server.

Again, you need to import the packages:

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

Then, initialize the Android driver:

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

This line will launch the application on the emulator, using the settings that were specified in the desired capabilities.

Well, now you can create a blank for the test using TestNG:

 import io.appium.java_client.android.AndroidDriver; 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 { AndroidDriver driver; @BeforeClass public void setUp() throws MalformedURLException{ // desired capabilities DesiredCapabilities caps = new DesiredCapabilities(); File app=new File("path of the apk"); caps.setCapability(MobileCapabilityType.APP,app); caps.setCapability(MobileCapabilityType.PLATFORM_VERSION, "4.4"); caps.setCapability(MobileCapabilityType.PLATFORM_NAME, "Android"); caps.setCapability(MobileCapabilityType.DEVICE_NAME, "Android emulator"); caps.setCapability("avd","Name of the AVD to launch"); caps.setCapability(MobileCapabilityType.APP_PACKAGE, "package name of your app (you can get it from apk info app)"); caps.setCapability(MobileCapabilityType.APP_ACTIVITY, "Launch activity of your app (you can get it from apk info app)"); caps.setCapability(MobileCapabilityType.BROWSER_NAME, "Browser");//    - 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(){ //    } @AfterClass public void tearDown(){ driver.closeApp();//CloseApp()        ,  quit()  close() -   } } 



Required desired capabilities for iOS and launch of iOS drivers


Also, as on Android, you can set the desired capabilities in two ways.

Settings for native and hybrid applications:



  1. Open iOS Settings
  2. Specify the path to the application under test.
  3. Turn on Force Device and select a simulator from the list.
  4. Choose from the list Platform Version (or you can write for yourself, for example 8.1)
  5. Start the Appium server

Settings for web applications:



  1. Open iOS Settings
  2. Select "Use Mobile Safari"
  3. Turn on Force Device and select a simulator from the list.
  4. Choose from the list Platform Version (or you can write for yourself, for example 8.1)
  5. Start the Appium server

Desired capabilities for native and hybrid applications


What are DC and why, we discussed in Chapter 1, so let's go straight to the code. First, import packages:

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

Then create the desired capabilities (for native and hybrid):

 DesiredCapabilities caps = new DesiredCapabilities();//  File app=new File("path of the app");//  File,      caps.setCapability(MobileCapabilityType.APP,app);//    caps.setCapability(MobileCapabilityType.PLATFORM_VERSION, "8.1");// iOS caps.setCapability(MobileCapabilityType.PLATFORM_NAME, "iOS");// OS caps.setCapability(MobileCapabilityType.DEVICE_NAME, "iPhone 5");//     Appium   

Desired capabilities for web applications


Import packages:

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

Now install the desired capacities for the web application:

 DesiredCapabilities caps = new DesiredCapabilities();//  caps.setCapability(MobileCapabilityType.PLATFORM_VERSION, "8.1");// iOS caps.setCapability(MobileCapabilityType.PLATFORM_NAME, "iOS");// OS caps.setCapability(MobileCapabilityType.DEVICE_NAME, "iPhone 5");//     Appium   caps.setCapability(MobileCapabilityType.BROWSER_NAME, "Safari"); //  Safari 

It remains to initialize the driver iOS.

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

Now you can write a test using TestNG:

 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{ // desired capabilities 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, "iPhone 5"); 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


Android


Below is an example of Android calculator automation. In the example we are trying to add two numbers.



  1. Update the desired capabilities in the setup () method to run the calculator:

     caps.setCapability("avd","AVD_Nexus_4");// Mention the created AVD name caps.setCapability(MobileCapabilityType.APP_PACKAGE, "com.android.calculator2"); caps.setCapability(MobileCapabilityType.APP_ACTIVITY, "com.android.calculator2.Calculator"); 

  2. Now you need to find two numbers. We will search for them by name:

     WebElement five=driver.findElement(By.name("5")); WebElement four=driver.findElement(By.name("4")); 

  3. Also, you need to find the signs "+" and "="; we will search by AccessabilityID:

     WebElement plus=driver.findElement(By.name("+")); WebElement equalTo=driver.findElementByAccessibilityId("equals")); 

  4. Now, click:

     five.click(); plus.click(); four.click(); equalTo.click(); 

  5. Run your test using TestNG:

     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, "Android emulator"); caps.setCapability("avd","AVD_Nexus_4");// Mention the created AVD name caps.setCapability(MobileCapabilityType.APP_PACKAGE, "com.android.calculator2"); caps.setCapability(MobileCapabilityType.APP_ACTIVITY, "com.android.calculator2.Calculator"); 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 five=driver.findElement(By.name("5")); five.click(); WebElement plus=driver.findElement(By.name("+")); plus.click(); WebElement four=driver.findElement(By.name("4")); four.click(); WebElement equalTo=driver.findElementByAccessibilityId("equals")); equalTo.click(); } @AfterClass public void tearDown(){ driver.closeApp(); } } 


iOS


Below is an example of working with the TestApp application; You can download it from here . After downloading, unzip it.



In this example, let's try to add two numbers:

  1. Update the setup () method to start TestApp:

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

  2. Now you need to find the elements for entering numbers; we will search by name:

     WebElement editBox1=driver.findElement(By.name("TextField1")); WebElement editBox2=driver.findElement(By.name("TextField2")); 

  3. Then, find the compute button; use AccessibilityID:

     WebElement computeSumBtn=driver.findElementByAccessibilityId("Compute Sum")); 

  4. Enter a value in the first field:

     editBox1.sendKeys("10"); 
  5. Let's enter the value in the second:

     editBox2.sendKeys("20"); 
  6. Now, click on the Compute Sum button:

     computeSumBtn.click(); 
  7. Run your test using TestNG:

     public class TestAppIication { IOSDriver driver; @BeforeClass public void setUp() throws MalformedURLException{ File app=new File("/Users/mhans/appium/ios/TestApp.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, "iPhone 5"); 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 editBox1=driver.findElement(By.name("TextField1")); editBox1.sendKeys("10"); WebElement editBox2=driver.findElement(By.name("TextField2")); editBox2.sendKeys("20"); WebElement computeSumBtn=driver.findElementByAccessibilityId("Compute Sum")); computeSumBtn.click(); } @AfterClass public void tearDown(){ driver.closeApp(); } } 




Work with web applications


Android


As an example, take the Google search page. To work, we will use the Android native browser:



  1. Update the setup () method to launch the browser:

     caps.setCapability("avd","AVD_Nexus_4"); caps.setCapability(MobileCapabilityType.BROWSER_NAME, "Browser"); 

  2. Now you need to go to www.google.com :

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

  3. We need to find the element searchBox; we will search by name:

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

  4. Then, enter the search query:

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

  5. Run your test using TestNG:

     public class TestAppIication { AndroidDriver driver; @BeforeClass public void setUp() throws MalformedURLException{ DesiredCapabilities caps = new DesiredCapabilities(); caps.setCapability(MobileCapabilityType.BROWSER_NAME, "Browser"); caps.setCapability(MobileCapabilityType.PLATFORM_VERSION, "4.4"); caps.setCapability(MobileCapabilityType.PLATFORM_NAME, "Android"); caps.setCapability(MobileCapabilityType.DEVICE_NAME, "Android emulator"); caps.setCapability("avd","AVD_Nexus_4");// Mention the created AVD name 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.google.com"); WebElement searchBox=driver.findElement(By.name("q")); searchBox.sendKeys("Appium for mobile automation"); } @AfterClass public void tearDown(){ driver.quit(); } } 


iOS


As an example, take the Google search page. To work, we will use the native Safari browser:



  1. Update the setup () method to launch Safari:

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

  2. Go to www.google.com :

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

  3. Then, find the search string (by name):

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

  4. In the search box send a request:

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

  5. Run your test using TestNG:

     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, "iPhone 5"); 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


For study, we use testApp. You can download it from here . Before working with hybrid applications, you need to follow the instructions .


  1. Update the setup () method to start the application:

     File app=new File("C:\\Appium_test\\testApp.apk"); caps.setCapability(MobileCapabilityType.APP,app); caps.setCapability("avd","AVD_Nexus_4"); caps.setCapability(MobileCapabilityType.APP_PACKAGE, " com.example.testapp"); caps.setCapability(MobileCapabilityType.APP_ACTIVITY, " MainActivity"); 

  2. We need to find a field to enter our URL (https://www.google.com), find it by ID:

     WebElement editBox=driver.findElement(By.id("com.example.testapp:id/urlField")); editBox.sendKeys("https://www.google.com"); 

  3. Now find the Go button:

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

  4. Click on the button:

     goButton.click(); 

  5. Now you need to switch the context:

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

  6. Switch to web_view:

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

    or so:

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

  7. Now you can work with the Google webpage. We can click on the Images tab and find the item by linkText:

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

  8. Run your test using TestNG:

     public class TestAppIication { AndroidDriver 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, "4.4"); caps.setCapability(MobileCapabilityType.PLATFORM_NAME, "Android"); caps.setCapability(MobileCapabilityType.DEVICE_NAME, "Android emulator"); caps.setCapability("avd","AVD_Nexus_4");// Mention the created AVD name caps.setCapability(MobileCapabilityType.AUTOMATION_NAME, "Appium");//Use Selendroid in case of <4.4 android version caps.setCapability(MobileCapabilityType.APP_PACKAGE, "com.example.testapp"); caps.setCapability(MobileCapabilityType.APP_ACTIVITY, "com.example.testapp.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(){ WebElement editBox=driver.findElement(By.id("com.example.testapp:id/urlField")); 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(); } } 

iOS


Here we take, for example, WebViewApp. You can download from here . After downloading, unzip the application:



  1. Update the setup () method to start the application:

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

  2. You need to find a search string to pass the URL there (https://www.google.com). Let's find it by className:

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

  3. Now, by name find the Go button:

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

  4. Cry:

     goButton.click(); 

  5. Now we need to get a list of contexts:

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

  6. And switch to work with web_view:

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

    or

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

  7. Now you can work with the Google webpage. We can click on the Images tab and find the item by linkText:

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

  8. Run your test using TestNG:

     public class TestAppIication { IOSDriver driver; @BeforeClass public void setUp() throws MalformedURLException{ DesiredCapabilities caps = new DesiredCapabilities(); caps.setCapability(MobileCapabilityType.PLATFORM_VERSION, "8.1"); caps.setCapability(MobileCapabilityType.PLATFORM_NAME, "iOS"); caps.setCapability(MobileCapabilityType.DEVICE_NAME, "iPhone 5"); 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. In the next chapter, we will look at the features of working with real devices.

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


All Articles