Code Sample - First Mobile Test

  • 1
  • Idea
  • Updated 6 years ago
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import org.openqa.selenium.*;
import org.openqa.selenium.interactions.*;
import org.openqa.selenium.interactions.internal.Coordinates;
import org.openqa.selenium.WebDriver.*;

import com.perfectomobile.selenium.*;
import com.perfectomobile.selenium.api.*;
import com.perfectomobile.selenium.by.*;
import com.perfectomobile.selenium.output.*;
import com.perfectomobile.selenium.options.*;
import com.perfectomobile.selenium.options.rotate.*;
import com.perfectomobile.selenium.options.touch.*;
import com.perfectomobile.selenium.options.visual.*;
import com.perfectomobile.selenium.options.visual.image.*;
import com.perfectomobile.selenium.options.visual.text.*;
import com.perfectomobile.httpclient.MediaType;
import com.perfectomobile.httpclient.utils.FileUtils;

public class MobileGettingStartedSample {

public static void main(String[] args) {

System.out.println("Run started");

// The default empty constructor of MobileDriver should be used when running the code inside Eclipse.
// The user must have the ECLIPSE role in this case.
// Otherwise, use the constructor that receives the host, user and password. E.g.
// MobileDriver driver = new MobileDriver(host, user, password);

// Default MobileDriver constructor
MobileDriver driver = new MobileDriver();

try {
         // write your code here

//Select a single device according to device OS, iOS, instead of a specific device ID
MobileDeviceFindOptions options = new MobileDeviceFindOptions();
options.setOS(MobileDeviceOS.IOS);
options.setOSVersion("7.1");
IMobileDevice device = driver.findDevice(options);

//Open the selected device
device.open();

//Navigate device to home screen
device.home();

//Browse to "http://nxc.co.il/demoaut/index.php"; with OS native browser
IMobileWebDriver domDriver = device.getDOMDriver(MobileBrowserType.OS);
domDriver.get("http://nxc.co.il/demoaut/index.php");

//Login to web application - set user and password
WebElement nameElement = domDriver.findElement(By.name("username"));
            nameElement.sendKeys("John");

            WebElement passwordElement = domDriver.findElement(By.name("password"));
            passwordElement.sendKeys("Perfecto1");

//click "Sign in" button
            WebElement textElement = domDriver.findElement(By.linkText("Sign in"));
            textElement.click();
           
//Text checkpoint on "Welcome back John"
IMobileWebDriver visualDriver = device.getVisualDriver();
visualDriver.manage().timeouts().implicitlyWait(40, TimeUnit.SECONDS);
visualDriver.findElement(By.linkText("Welcome back John"));

//Close the device
device.close();

} catch (Exception e) {
e.printStackTrace();
} finally {
//quit the driver
driver.quit();
//download the report
downloadReport(driver);
}
System.out.println("Run ended");
}

/*
* Suspend script for a specified number of milliseconds.
*/
private static void sleep(long millis) {
try {
Thread.sleep(millis);
} catch (InterruptedException e) {
}
}

/*
* Download the report to a specified location.
* This method should be used after calling driver.quit().
*/
private static void downloadReport(IMobileDriver driver) {
InputStream reportStream = driver.downloadReport(MediaType.PDF);
if (reportStream != null) {
File reportFile = new File("C:\\test\\report.pdf");
FileUtils.write(reportStream, reportFile);
}
}
}
Photo of Adi

Adi, Official Rep

  • 7 Posts
  • 0 Reply Likes

Posted 6 years ago

  • 1

Be the first to post a reply!