/
PerfectoNativeSample.java
152 lines (124 loc) · 6 KB
/
PerfectoNativeSample.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
package ios;
import java.net.URL;
import java.util.List;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.Platform;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import com.perfecto.reportium.client.ReportiumClient;
import com.perfecto.reportium.client.ReportiumClientFactory;
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.perfecto.reportium.model.Job;
import io.appium.java_client.ios.IOSDriver;
import io.appium.java_client.ios.IOSElement;
public class PerfectoNativeSample {
public static void main(String[] args) throws Exception {
DesiredCapabilities capabilities = new DesiredCapabilities("", "", Platform.ANY);
// 1. Replace <<cloud name>> with your perfecto cloud name (e.g. demo is the
// cloud name of demo.perfectomobile.com).
String cloudName = "<<cloud name>>";
// 2. Replace <<security token>> with your perfecto security token.
String securityToken = "<<security token>>";
capabilities.setCapability("securityToken", securityToken);
// 3. Set device capabilities.
capabilities.setCapability("platformName", "iOS");
capabilities.setCapability("model", "iPhone.*");
// 4. Set Perfecto Media repository path of App under test.
capabilities.setCapability("app", "PUBLIC:ExpenseTracker/Native/iOS/InvoiceApp1.0.ipa");
// 5. Set the unique identifier of your app
capabilities.setCapability("bundleId", "io.perfecto.expense.tracker");
// Set other capabilities.
capabilities.setCapability("enableAppiumBehavior", true);
capabilities.setCapability("autoLaunch", true); // Whether to install and launch the app automatically.
capabilities.setCapability("iOSResign",true); // Resign with developer certificate
// capabilities.setCapability("fullReset", false); // Reset app state by uninstalling app.
capabilities.setCapability("takesScreenshot", false);
capabilities.setCapability("screenshotOnError", true);
capabilities.setCapability("openDeviceTimeout", 5);
// Initialize the IOSDriver driver
IOSDriver<IOSElement> driver = new IOSDriver<IOSElement>(
new URL("https://" + cloudName + ".perfectomobile.com/nexperience/perfectomobile/wd/hub"),
capabilities);
// Setting implicit wait
driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
PerfectoExecutionContext perfectoExecutionContext;
if (System.getProperty("jobName") != null) {
perfectoExecutionContext = new PerfectoExecutionContext.PerfectoExecutionContextBuilder()
.withProject(new Project("My Project", "1.0"))
.withJob(new Job(System.getProperty("jobName"),
Integer.parseInt(System.getProperty("jobNumber"))))
.withWebDriver(driver).build();
} else {
perfectoExecutionContext = new PerfectoExecutionContext.PerfectoExecutionContextBuilder()
.withProject(new Project("My Project", "1.0"))
.withWebDriver(driver).build();
}
ReportiumClient reportiumClient = new ReportiumClientFactory()
.createPerfectoReportiumClient(perfectoExecutionContext);
reportiumClient.testStart("Native Java iOS Sample", new TestContext("native", "ios"));
try {
// Sample test
reportiumClient.stepStart("Enter email");
WebDriverWait wait = new WebDriverWait(driver, 30);
IOSElement email = (IOSElement) wait
.until(ExpectedConditions.elementToBeClickable(driver.findElement(By.name("login_email"))));
email.sendKeys("test@perfecto.com");
reportiumClient.stepEnd();
reportiumClient.stepStart("Enter password");
IOSElement password = (IOSElement) wait
.until(ExpectedConditions.elementToBeClickable(driver.findElement(By.name("login_password"))));
password.sendKeys("test123");
reportiumClient.stepEnd();
reportiumClient.stepStart("Click login");
driver.hideKeyboard();
IOSElement login = (IOSElement) wait
.until(ExpectedConditions.elementToBeClickable(driver.findElement(By.name("login_login_btn"))));
login.click();
reportiumClient.stepEnd();
reportiumClient.stepStart("Add expense");
IOSElement add_expense = (IOSElement) wait
.until(ExpectedConditions.elementToBeClickable(driver.findElement(By.name("list_add_btn"))));
add_expense.click();
reportiumClient.stepEnd();
reportiumClient.stepStart("Select head");
IOSElement head = (IOSElement) wait
.until(ExpectedConditions.elementToBeClickable(driver.findElement(By.name("edit_head"))));
head.click();
List<WebElement> picker = wait
.until(ExpectedConditions.presenceOfAllElementsLocatedBy(By.xpath("//*[@value=\"- Select -\"]")));
picker.get(0).sendKeys("Flight");
reportiumClient.stepEnd();
reportiumClient.stepStart("Enter amount");
IOSElement amount = (IOSElement) wait
.until(ExpectedConditions.elementToBeClickable(driver.findElement(By.name("edit_amount"))));
amount.sendKeys("100");
reportiumClient.stepEnd();
reportiumClient.stepStart("Save expense");
IOSElement save_expense = (IOSElement) wait
.until(ExpectedConditions.elementToBeClickable(driver.findElement(By.name("add_save_btn"))));
save_expense.click();
reportiumClient.stepEnd();
reportiumClient.stepStart("Verify alert");
String expectedText = "Please enter valid category";
IOSElement alert = (IOSElement) wait
.until(ExpectedConditions.elementToBeClickable(driver.findElement(By.name(expectedText))));
reportiumClient.reportiumAssert(expectedText, alert.getText().equalsIgnoreCase(expectedText));
reportiumClient.stepEnd();
reportiumClient.testStop(TestResultFactory.createSuccess());
} catch (Exception e) {
reportiumClient.testStop(TestResultFactory.createFailure(e));
assert (false);
}
// Prints the Smart Reporting URL
String reportURL = reportiumClient.getReportUrl();
System.out.println("Report url - " + reportURL);
// Quits the driver
driver.quit();
}
}