Page tree
Skip to end of metadata
Go to start of metadata

Last updated: Jan 14, 2019 19:35

Prerequisite is please read this article. before implementing this. 

In order for your tests to generate HAR and other Network related files through Automation, you can use this example code below. 

  • Download this package and install on it on your computer. 
  • Open file '/HAR-File-Generation/config.properties' and update cloud name for 'moblieC.url' and your user token for the cloud and your user email, 
  • Run '/HAR-File-Generation/src/AppiumTest.java' as Java Application. Modify it as needed based on the test you want to run, right now it goes to a certain device on Demo cloud, launches Twitter app and does a certain text validation on its launch page. Here is the code below:


import java.io.*;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.*;
import java.util.*;
import java.util.concurrent.TimeUnit;

import org.openqa.selenium.*;
import org.openqa.selenium.remote.*;

import com.perfecto.reportium.client.ReportiumClient;
import com.perfecto.reportium.client.ReportiumClientFactory;
import com.perfecto.reportium.model.Job;
import com.perfecto.reportium.model.PerfectoExecutionContext;
import com.perfecto.reportium.model.Project;
import com.perfecto.reportium.test.TestContext;
import com.perfecto.reportium.test.result.TestResultFactory;
import com.perfectomobile.httpclient.ParameterValue;





/**
 * This template is for users that use DigitalZoom Reporting (ReportiumClient).
 * For any other use cases please see the basic template at https://github.com/PerfectoCode/Templates.
 * For more programming samples and updated templates refer to the Perfecto Documentation at: http://developers.perfectomobile.com/
 */
public class AppiumTest {

    static String env_endPoint = null;
    static String myToken = null;
    static String browser = null;
    static String myUser = null;
    static String password = null;
    static RemoteWebDriver driver;
	static List<ParameterValue> capabilityList = new LinkedList<>();
	static List<String> deviceAvail = new ArrayList<String>();
	private static String platform;
	private static String deviceId;
	
    @SuppressWarnings("rawtypes")
	public static void main(String[] args) throws MalformedURLException, IOException, NoSuchFieldException {
        System.out.println("---------------- Run started ----------------");

        Properties prop = new Properties();
        InputStream input = null;

        try {

            input = new FileInputStream("config.properties");

            // load a properties file
            prop.load(input);

            // get the property value and print it out

            env_endPoint = prop.getProperty("moblieC.url");
            browser = prop.getProperty("browserName");
            myToken = prop.getProperty("moblieC.token");
            myUser = prop.getProperty("user");
            password = prop.getProperty("password");



        }  catch (IOException ex) {
            ex.printStackTrace();
        } finally {
            if (input != null) {
                try {
                    input.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

        DesiredCapabilities capabilities = new DesiredCapabilities(browser, "", Platform.ANY);
        capabilities.setCapability("user", myUser);
        capabilities.setCapability("securityToken", myToken);
        
        //Enter the Platform name desired out of iOS or Android
        platform = "iOS";
        deviceId = "9C71D808D1C001F1B3DE289247CC4080B36C880D";
        DeviceSelectHelper.validatePlatformName (platform);
        
		capabilities.setCapability("platformName", platform);
        
      
//        String deviceId = DeviceSelectHelper.CheckIfDevicesAvailableReturnDevice (capabilities, platform, env_endPoint);
        System.out.println("---------------- device ID is #############:  ----------------:  " + deviceId);
     

  
        capabilities.setCapability("deviceName", deviceId);


        // Call this method if you want the script to share the devices with the Perfecto Lab plugin.
        PerfectoLabUtils.setExecutionIdCapability(capabilities, env_endPoint);

        driver = new RemoteWebDriver(new URL("https://" + env_endPoint + "/nexperience/perfectomobile/wd/hub") , capabilities);
        driver.manage().timeouts().implicitlyWait(15 , TimeUnit.SECONDS);

    	
    	Map<String, Object> params = new HashMap<>();
        params.put("generateHarFile", "true");
        driver.executeScript("mobile:vnetwork:start", params);


        // Open the Google search page
        HashMap<String, Object> params2 = new HashMap();
        //launch settings application
//        switchToContext(driver, "NATIVE_APP");
        params2.put("name", "Twitter");
        driver.executeScript("mobile:application:open", params2);
        
        Map<String, Object> params3 = new HashMap<>();
        params3.put("content", "Get started");
        driver.executeScript("mobile:text:find", params3);




    	//End the network virutalization
    	params.clear();
    	driver.executeScript("mobile:vnetwork:stop", params);

 
        // Reporting client. For more details, see http://developers.perfectomobile.com/display/PD/Reporting
        PerfectoExecutionContext perfectoExecutionContext = new PerfectoExecutionContext.PerfectoExecutionContextBuilder()
                .withProject(new Project("GoogleSearch", "1.0"))
                .withJob(new Job("Job1", 46))
                .withContextTags("GoogleSearch")
                .withWebDriver(driver)
                .build();
        ReportiumClient reportiumClient = new ReportiumClientFactory().createPerfectoReportiumClient(perfectoExecutionContext);

        try {
            reportiumClient.testStart("GoogleSearch_Basic-STS", new TestContext("GoogleSearch2", "GoogleSearch3"));

            reportiumClient.testStop(TestResultFactory.createSuccess());
        } catch (Exception e) {
            reportiumClient.testStop(TestResultFactory.createFailure(e.getMessage(), e));
            e.printStackTrace();
        } finally {
            try {
                driver.quit();

                // Retrieve the URL to the DigitalZoom Report (= Reportium Application) for an aggregated view over the execution
                String reportURL = reportiumClient.getReportUrl();

                // Retrieve the URL to the Execution Summary PDF Report
                String reportPdfUrl = (String)(driver.getCapabilities().getCapability("reportPdfUrl"));
                // For detailed documentation on how to export the Execution Summary PDF Report, the Single Test report and other attachments such as
                // video, images, device logs, vitals and network files - see http://developers.perfectomobile.com/display/PD/Exporting+the+Reports

            } catch (Exception e) {
                e.printStackTrace();
            }
        }

//        driver.close();
        System.out.println(env_endPoint);
        System.out.println(myUser);
        System.out.println(myToken);
        System.out.println(browser);
        System.out.println("---------------- Run ended ----------------");

        driver.quit();
    }

    private static void switchToContext(RemoteWebDriver driver, String context) {
        RemoteExecuteMethod executeMethod = new RemoteExecuteMethod(driver);
        Map<String,String> params = new HashMap<String,String>();
        params.put("name", context);
        executeMethod.execute(DriverCommand.SWITCH_TO_CONTEXT, params);
    }

    
    
}