1
\$\begingroup\$

I am new to automation. I have automated few test cases for a mobile app using appium and wanted to know the areas where my code can be improved further.

package org.fltmobile.easychecklist;

import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.concurrent.TimeUnit;

import org.junit.Assert;
import org.openqa.selenium.WebElement;
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;

public class Tests 
{
    AndroidDriver driver;
    public void setUp() throws MalformedURLException
    {
        File app = new File("abcd.apk");
        DesiredCapabilities cap = new DesiredCapabilities();
        cap.setCapability(MobileCapabilityType.DEVICE_NAME, "emulator-5554");
        cap.setCapability(MobileCapabilityType.UDID, "emulator-5554");
        cap.setCapability(MobileCapabilityType.NEW_COMMAND_TIMEOUT, "100");
        cap.setCapability(MobileCapabilityType.APP, app.getAbsolutePath());

        driver = new AndroidDriver(new URL("http://127.0.0.1:4723/wd/hub"), cap);
        driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
    }

    @Test(priority=1, description="Create a new checklist and verify if it actually got created")
    public void createNewCheckList() throws MalformedURLException
    {
        System.out.println("Test Case 1 started execution..");
        setUp();
        System.out.println("App opened..");
        HelperMethods.createNewChecklist(driver, TestData.newChecklistName);
        System.out.println("Checklist created");
        if(!HelperMethods.verifyChecklistPresent(driver, TestData.newChecklistName))
            Assert.fail("Created checklist could not be found");
        System.out.println("Verified that the created checklist is actually created..");
    }

    @Test(priority=2, description="Add items to checklist and verify if all the items have actually got added to the checklist")
    public void addItemsToCheckList() throws Exception
    {
        System.out.println("Test Case 2 started execution..");
        createNewCheckList();
        System.out.println("Created new checklist and verified..");
        HelperMethods.openChecklist(driver, TestData.newChecklistName);
        System.out.println("Opened the created checklist..");
        String[] items = {TestData.itemName1, TestData.itemName2, TestData.itemName3};
        HelperMethods.addItemsToChecklist(driver, items);
        System.out.println("Added 3 items to created checlist..");
        if(!HelperMethods.verifyItemsInChecklist(driver, items))
            Assert.fail("The added items are not found in the checklist");
        System.out.println("Verified that the added items are actually added.");

    }

    @Test(priority=3, description="Remove an item from a checklist and verify that if the item has actually got removed from the checklist")
    public void removeItemsFromChecklist() throws Exception
    {
        addItemsToCheckList();
        System.out.println("Navigating back to the homepage");
        HelperMethods.navigateBackToHomePageFromChecklistPage(driver);
        String itemsToBeRemoved[] = {TestData.itemName2};
        String expectedItemsAfterRemoval[] = {TestData.itemName1, TestData.itemName3};
        System.out.println("Opening checklist");
        HelperMethods.openChecklist(driver, TestData.newChecklistName);
        System.out.println("Removing item from checkist");
        HelperMethods.removeItemsFromChecklist(driver, itemsToBeRemoved);
        System.out.println("Verifying if the item has actually got removed from the checklist");
        if(!HelperMethods.verifyItemsInChecklist(driver, expectedItemsAfterRemoval))
            Assert.fail("Looks like the item was not removed from the checklist");
    }

    @Test(priority=4, description="Delete a checklist and verify that the checklist is actually deleted")
    public void deleteChecklist() throws Exception
    {
        removeItemsFromChecklist();
        HelperMethods.navigateBackToHomePageFromChecklistPage(driver);
        HelperMethods.deleteCheckList(driver, TestData.newChecklistName);
        if(HelperMethods.verifyChecklistPresent(driver, TestData.newChecklistName))
            Assert.fail("Checklist "+TestData.newChecklistName+" does not get deleted");
    }
}

package org.fltmobile.easychecklist;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.testng.Assert;

import io.appium.java_client.android.AndroidDriver;

public class HelperMethods 
{
    public static void createNewChecklist(AndroidDriver driver, String checklistName)
    {
        driver.findElement(Locators.plusIcon).click();
        driver.findElement(Locators.newChecklistAlertNameTextField).sendKeys(checklistName);
        driver.findElement(Locators.newChecklistAlertOKButton).click();
        driver.navigate().back();
    }

    public static void openChecklist(AndroidDriver driver, String checklistName) throws Exception
    {
        List<WebElement> checklistElement = driver.findElements(Locators.listOfAllChecklists);
        int flag=0;
        for(WebElement ele: checklistElement)
        {
            if(ele.getText().equals(checklistName))
            {
                flag++;
                ele.click();
            }
        }
        if(flag==0)
            throw new Exception("Checklist "+checklistName+" is not found");
    }

    public static void addItemsToChecklist(AndroidDriver driver, String[] items) throws Exception
    {
        //Assuming that the checklist page is open
        if(items.length<1)
            throw new Exception("No items specified");
        for(int i=0; i<items.length; i++)
        {
            driver.findElement(Locators.addItemToChecklistTextBox).sendKeys(items[i]);
            driver.findElement(Locators.addItemToChecklistPlusButton).click();
        }
    }

    public static boolean verifyItemsInChecklist(AndroidDriver driver, String[] items)
    {
        //Assuming that the checklist page is open
        List<String> actualItems=new ArrayList<String>();
        List<WebElement> actualItemElements = driver.findElements(Locators.listOfAllItemsInChecklist);
        System.out.println("Actual Number of Items in checklist ="+actualItemElements.size());
        for(WebElement actualItemElement: actualItemElements)
        {
            System.out.println(actualItemElement.getText());
            actualItems.add(actualItemElement.getText());
        }
        if(Arrays.equals(items, actualItems.toArray()))
            return true;
        else    
            return false;
    }

    public static void removeItemsFromChecklist(AndroidDriver driver, String[] items) throws Exception
    {
        //Assuming that the checklist page is open
        List<WebElement> actualItemElements = driver.findElements(Locators.listOfAllItemsInChecklist);
        if(actualItemElements.isEmpty())
            throw new Exception("The checklist is empty. Cannot remove elements");
        if(items.length<1)
            throw new Exception("No item specified to remove from the checklist");
        for(int i=0; i<items.length; i++)
        {
            for(WebElement actualItemElement: actualItemElements)
            {
                if(actualItemElement.getText().equals(items[i]))
                    selectCheckbox(driver, items[i]);
            }
        }
        driver.findElement(Locators.deleteIcon).click();
    }

    public static void selectCheckbox(AndroidDriver driver, String itemName)
    {
        driver.findElementByXPath(Locators.item.replace("ITEM", itemName)+"/../android.widget.CheckBox").click();
    }

    public static void deleteCheckList(AndroidDriver driver, String checklistName)
    {
        //Assuming that the list of checklists page (which is the homepage) is open
        driver.findElement(By.xpath(Locators.XIconForChecklistDeletion.replace("CHECKLIST_NAME", checklistName))).click();
        driver.findElement(Locators.deleteChecklistAlertOKButton).click();
    }

    public static boolean verifyChecklistPresent(AndroidDriver driver, String checklistName)
    {
        List<WebElement> checkListElements = driver.findElements(Locators.listOfAllChecklists);
        if(checkListElements.size()==0)
            return false;
        for(WebElement checkListElement: checkListElements)
        {
            if(checkListElement.getText().equals(checklistName))
                return true;
        }
        return false;

    }

    public static void navigateBackToHomePageFromChecklistPage(AndroidDriver driver)
    {
        driver.navigate().back();
    }

}
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.