Repository navigation
Expand file tree
/
Copy pathFirstTest.java
More file actions
104 lines (83 loc) · 3.56 KB
/
Copy pathFirstTest.java
File metadata and controls
104 lines (83 loc) · 3.56 KB
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
import io.appium.java_client.AppiumDriver;
import io.appium.java_client.android.AndroidDriver;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.By;
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 java.net.URL;
import java.util.List;
public class FirstTest {
private AppiumDriver driver;
@Before
public void setUp() throws Exception {
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability("platformName", "Android");
capabilities.setCapability("deviceName", "AndroidTestDevice");
capabilities.setCapability("platformVersion", "8.0");
capabilities.setCapability("automationName", "Appium");
capabilities.setCapability("appPackage", "org.wikipedia");
capabilities.setCapability("appActivity", ".main.MainActivity");
capabilities.setCapability("app",
"C:/Users/user/Documents/GitHub/JavaAppiumAutomation/apks/org.wikipedia.apk");
driver = new AndroidDriver(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);
}
@After
public void tearDown() {
driver.quit();
}
@Test
public void firstTest() {
waitForElementAndClick(
By.id("org.wikipedia:id/search_container"),
"Cannot find 'Search Wikipedia' input",
5
);
String search_line = "Java";
waitForElementAndSendKeys(
By.id("org.wikipedia:id/search_src_text"),
search_line,
"Cannot find search input",
5
);
waitForElementAndClick(
By.xpath("//*[@resource-id='org.wikipedia:id/page_list_item_container']//*[@text='Object-oriented programming language']"),
"Cannot find 'Object-oriented programming language' topic searching by " + search_line,
15
);
String article_title_locator = "org.wikipedia:id/view_page_title_text";
assertElementPresent(
By.id(article_title_locator),
"Cannot find element of title"
);
}
private WebElement waitForElementPresent(By by, String error_message, long timeoutInSeconds) {
WebDriverWait wait = new WebDriverWait(driver, timeoutInSeconds);
wait.withMessage(error_message + "\n");
return wait.until(ExpectedConditions.presenceOfElementLocated(by));
}
private WebElement waitForElementAndClick(By by, String error_message, long timeoutInSeconds) {
WebElement element = waitForElementPresent(by, error_message, timeoutInSeconds);
element.click();
return element;
}
private WebElement waitForElementAndSendKeys(By by, String value, String error_message, long timeoutInSeconds) {
WebElement element = waitForElementPresent(by, error_message, timeoutInSeconds);
element.sendKeys(value);
return element;
}
private int getAmountOfElements(By by) {
List elements = driver.findElements(by);
return elements.size();
}
private void assertElementPresent(By by, String error_message) {
int amount_of_elements = getAmountOfElements(by);
if (amount_of_elements == 0) {
String default_message = "An element '" + by.toString() + "' supposed to be not present.";
throw new AssertionError(default_message + " " + error_message);
}
}
}