Now that you have created your first project in android studio and added all the required selenium libraries, you will now see how to create your first test automation script to automate mobile app testing, you will also be introduced to the code that you need to write in selenium @ android studio, for this automation to work.
Steps to create your first test automation script – mobile app testing
- Expand the tree node in Project view to : {projectname] >app > src > test > java > {packagefolder}
- Right click on the package ( ex: com.appium.tt.mobiletestautomation , based on the names given during creation of andorid project in android studio article), select the menu options New > Java Class
- You will be presented with a “Create New Class” window, enter a class file name as per your interest, ex: TestCalc_MobileEmulator and click OK button, you can leave other fields to default values.
- Copy below code and paste it in the newly created java file
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364import java.net.MalformedURLException;import java.net.MalformedURLException;import java.net.URL;import java.util.concurrent.TimeUnit;import org.junit.After;import org.junit.Before;import org.openqa.selenium.By;import org.openqa.selenium.WebDriver;import org.openqa.selenium.remote.CapabilityType;import org.openqa.selenium.remote.DesiredCapabilities;import org.openqa.selenium.remote.RemoteWebDriver;import org.openqa.selenium.support.ui.ExpectedConditions;import org.openqa.selenium.support.ui.WebDriverWait;public class TestCalc_MobileEmulator {WebDriver driver;@Beforepublic void setUp() throws MalformedURLException {DesiredCapabilities capabilities = new DesiredCapabilities();// Created object of DesiredCapabilities class.capabilities.setCapability("deviceName", "Android Emulator");// Set android deviceName desired capability. Set your device name.capabilities.setCapability(CapabilityType.BROWSER_NAME, "Android");// Set BROWSER_NAME desired capability. As we are running on Android Emulator, It's Android.capabilities.setCapability(CapabilityType.VERSION, "7.0");// Set Android's OS VERSION @ desired capability.capabilities.setCapability("platformName", "Android");// Set platformName desired capability to Android.capabilities.setCapability("appPackage", "com.android.calculator2");// Set your application's package.capabilities.setCapability("appActivity", "com.android.calculator2.Calculator");// Set your application's MainActivity.driver = new RemoteWebDriver(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);// Create driver, set the Appium Server details.driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);}@Testpublic void testFirstCalculator() {driver.findElement(By.name("4")).click(); // Click on Number 4driver.findElement(By.name("+")).click(); // Click on Number +driver.findElement(By.name("7")).click(); // Click on Number 7driver.findElement(By.name("=")).click(); // Click on Number 11driver.manage().timeouts().implicitlyWait(25,TimeUnit.SECONDS);}@Afterpublic void End() {driver.quit();}} - You may have to change the values for capabilities based on the Mobile Emulator’s Android version, name, etc..
- Click Save, in case you see any error like below, i.e.e @Test or @Before or @After in red mark, you need to keep your cursor inside the text and press Alt+Enter, doing this will ask you to import packages from junit, go and ahead do the same and you are all set with the test script creation.
As you are using desired capabilities of Selenium, you need not include the appium client java jar file to the project, but if you want to work with mobile capabilities you will need to include the relevant jar files, similar to how you added selenium libraries, which is clearly mentioned in the article configure android project with selenium libraries.
In case you have any queries, we request you to ask them through comments and we would be more than happy to respond back to you with any solutions.
Now that you have created the test script, next steps would be to execute the script, but in order to execute the script you need to have the Appium server running and Mobile emulator up and running, you will be learning about this in the next articles.
Founder of TestingTools.co, constantly shares knowledge on different test automation tools. Has experience in building proof of concepts, solutions, frameworks, platforms & implementation of test automation projects.
In pursuit of building a platform with inbuilt framework and reusable components for Oracle Cloud applications ( Oracle HCM, CX, SCM, Financials clouds, Salesforce and other cloud applications )
Major accomplishments in his career:
Product architect and development manager for test automation platforms
Oracle Flow Builder @ Oracle
CloudTestr @ Suneratech
2 times to oracle open world
More than 20 successful POCs ( Proof of concepts )
100 demos
Designed pricing models.
Trained more than 50 members.
Built more than 100 re-usable functions.
Worked with tools:
OATS – Oracle applications testing suite
Selenium + Java / C#
Protractor
NightwatchJS
CodedUI
PyWinAuto