add new testCases. prepare the test to have them into categories
[pub/Android/ownCloud.git] / automationTest / src / test / java / androidtest / tests / Common.java
1 package androidtest.tests;
2
3 import static org.junit.Assert.*;
4 import java.io.File;
5 import java.io.IOException;
6 import java.net.URL;
7 import java.text.SimpleDateFormat;
8 import java.util.Calendar;
9 import java.util.Date;
10 import java.util.concurrent.TimeUnit;
11 import org.apache.commons.io.FileUtils;
12 import org.openqa.selenium.By;
13 import org.openqa.selenium.NoSuchElementException;
14 import org.openqa.selenium.OutputType;
15 import org.openqa.selenium.TimeoutException;
16 import org.openqa.selenium.remote.DesiredCapabilities;
17 import org.openqa.selenium.remote.RemoteWebDriver;
18 import org.openqa.selenium.support.ui.WebDriverWait;
19 import io.appium.java_client.android.AndroidDriver;
20 import io.appium.java_client.android.AndroidElement;
21
22 public class Common{
23 AndroidDriver driver;
24 static int waitingTime = 30;
25
26 WebDriverWait wait;
27
28 protected AndroidDriver setUpCommonDriver () throws Exception {
29 File rootPath = new File(System.getProperty("user.dir"));
30 File appDir = new File(rootPath,"src/test/resources");
31 File app = new File(appDir,"ownCloud.apk");
32 DesiredCapabilities capabilities = new DesiredCapabilities();
33 capabilities.setCapability("platformName", "Android");
34 capabilities.setCapability("deviceName", "test");
35 capabilities.setCapability("app", app.getAbsolutePath());
36 capabilities.setCapability("appPackage", "com.owncloud.android");
37 capabilities.setCapability("appActivity", ".ui.activity.FileDisplayActivity");
38 capabilities.setCapability("appWaitActivity", ".authentication.AuthenticatorActivity");
39 driver = new AndroidDriver(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);
40 driver.manage().timeouts().implicitlyWait(waitingTime, TimeUnit.SECONDS);
41 wait = new WebDriverWait(driver, waitingTime, 50);
42 return driver;
43
44 }
45
46 protected boolean waitForTextPresent(String text, AndroidElement element) throws InterruptedException{
47 for (int second = 0;;second++){
48 if (second >= waitingTime)
49 return false;
50 try{
51 if (text.equals(element.getText()))
52 break;
53 } catch (Exception e){
54
55 }
56 Thread.sleep(1000);
57 }
58 return true;
59 }
60
61 protected boolean isElementPresent(AndroidElement element, By by) {
62 try {
63 element.findElement(by);
64 return true;
65 } catch (NoSuchElementException e) {
66 return false;
67 }
68 }
69
70 protected boolean isElementPresent(AndroidElement element) {
71 try{
72 element.isDisplayed();
73 } catch (NoSuchElementException e){
74 return false;
75 }
76 return true;
77 }
78
79 //pollingTime in milliseconds
80 public static void waitTillElementIsNotPresent (AndroidElement element, int pollingTime) throws Exception {
81 for (int time = 0;;time += pollingTime){
82 if (time >= waitingTime * 1000) //convert to milliseconds
83 break;
84 try{
85 element.isDisplayed();
86 } catch (NoSuchElementException e){
87 return;
88 }
89 Thread.sleep(pollingTime);
90 }
91 throw new TimeoutException();
92 }
93
94 protected void takeScreenShotOnFailed (String testName) throws IOException {
95 File file = ((RemoteWebDriver) driver).getScreenshotAs(OutputType.FILE);
96 SimpleDateFormat dt1 = new SimpleDateFormat("yyyy-MM-dd");
97 Date today = Calendar.getInstance().getTime();
98 String screenShotName = "ScreenShots/" + dt1.format(today) + "/" + testName + ".png";
99 FileUtils.copyFile(file, new File(screenShotName));
100 }
101
102 protected void assertIsInMainView() throws InterruptedException {
103 assertTrue(waitForTextPresent("ownCloud", (AndroidElement) driver.findElementByAndroidUIAutomator("new UiSelector().resourceId(\"android:id/action_bar_title\")")));
104 assertTrue(isElementPresent((AndroidElement) driver.findElementByAndroidUIAutomator("new UiSelector().description(\"Upload\")")));
105 }
106
107 protected void assertIsNotInMainView() throws InterruptedException {
108 AndroidElement fileElement;
109 assertTrue(waitForTextPresent("ownCloud", (AndroidElement) driver.findElementByAndroidUIAutomator("new UiSelector().resourceId(\"android:id/action_bar_title\")")));
110 try {
111 fileElement = (AndroidElement) driver.findElementByAndroidUIAutomator("new UiSelector().description(\"Upload\")");
112 } catch (NoSuchElementException e) {
113 fileElement = null;
114 }
115 assertNull(fileElement);
116 }
117
118 protected void assertIsPasscodeRequestView() throws InterruptedException {
119 assertTrue(waitForTextPresent("ownCloud", (AndroidElement) driver.findElementByAndroidUIAutomator("new UiSelector().resourceId(\"android:id/action_bar_title\")")));
120 assertTrue(((AndroidElement) driver.findElementByAndroidUIAutomator("new UiSelector().text(\"Please, insert your pass code\")")).isDisplayed());
121
122 }
123
124 protected void assertIsInSettingsView() throws InterruptedException {
125 assertTrue(waitForTextPresent("Settings", (AndroidElement) driver.findElementByAndroidUIAutomator("new UiSelector().resourceId(\"android:id/action_bar_title\")")));
126 }
127
128 }