6a40be29e09b967bdb678f488935364a530ea08d
[pub/Android/ownCloud.git] / automationTest / src / test / java / com / owncloud / android / test / ui / testSuites / Common.java
1 /**
2 * ownCloud Android client application
3 *
4 * @author purigarcia
5 * Copyright (C) 2015 ownCloud Inc.
6 *
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2,
9 * as published by the Free Software Foundation.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 *
19 */
20
21 package com.owncloud.android.test.ui.testSuites;
22
23 import static org.junit.Assert.*;
24 import java.io.File;
25 import java.io.IOException;
26 import java.net.URL;
27 import java.text.SimpleDateFormat;
28 import java.util.Calendar;
29 import java.util.Date;
30 import java.util.concurrent.TimeUnit;
31 import org.apache.commons.io.FileUtils;
32 import org.openqa.selenium.By;
33 import org.openqa.selenium.NoSuchElementException;
34 import org.openqa.selenium.OutputType;
35 import org.openqa.selenium.TimeoutException;
36 import org.openqa.selenium.remote.DesiredCapabilities;
37 import org.openqa.selenium.remote.RemoteWebDriver;
38 import org.openqa.selenium.support.ui.WebDriverWait;
39 import io.appium.java_client.android.AndroidDriver;
40 import io.appium.java_client.android.AndroidElement;
41
42 public class Common{
43 AndroidDriver driver;
44 static int waitingTime = 30;
45
46 WebDriverWait wait;
47
48 protected AndroidDriver setUpCommonDriver () throws Exception {
49 File rootPath = new File(System.getProperty("user.dir"));
50 File appDir = new File(rootPath,"src/test/resources");
51 File app = new File(appDir,"ownCloud.apk");
52 DesiredCapabilities capabilities = new DesiredCapabilities();
53 capabilities.setCapability("platformName", "Android");
54 capabilities.setCapability("deviceName", "test");
55 capabilities.setCapability("app", app.getAbsolutePath());
56 capabilities.setCapability("appPackage", "com.owncloud.android");
57 capabilities.setCapability("appActivity",
58 ".ui.activity.FileDisplayActivity");
59 capabilities.setCapability("appWaitActivity",
60 ".authentication.AuthenticatorActivity");
61 driver = new AndroidDriver(new URL("http://127.0.0.1:4723/wd/hub"),
62 capabilities);
63 driver.manage().timeouts().implicitlyWait(waitingTime,
64 TimeUnit.SECONDS);
65 wait = new WebDriverWait(driver, waitingTime, 50);
66 return driver;
67
68 }
69
70 protected boolean waitForTextPresent(String text, AndroidElement element)
71 throws InterruptedException{
72 for (int second = 0;;second++){
73 if (second >= waitingTime)
74 return false;
75 try{
76 if (text.equals(element.getText()))
77 break;
78 } catch (Exception e){
79
80 }
81 Thread.sleep(1000);
82 }
83 return true;
84 }
85
86 protected boolean isElementPresent(AndroidElement element, By by) {
87 try {
88 element.findElement(by);
89 return true;
90 } catch (NoSuchElementException e) {
91 return false;
92 }
93 }
94
95 public static boolean isElementPresent(AndroidElement element) {
96 try{
97 element.isDisplayed();
98 } catch (NoSuchElementException e){
99 return false;
100 }
101 return true;
102 }
103
104 //pollingTime in milliseconds
105 public static void waitTillElementIsNotPresent (AndroidElement element,
106 int pollingTime) throws Exception {
107 for (int time = 0;;time += pollingTime){
108 if (time >= waitingTime * 1000) //convert to milliseconds
109 break;
110 try{
111 element.isDisplayed();
112 } catch (NoSuchElementException e){
113 return;
114 }
115 Thread.sleep(pollingTime);
116 }
117 throw new TimeoutException();
118 }
119
120 protected void takeScreenShotOnFailed (String testName)
121 throws IOException {
122 File file = ((RemoteWebDriver) driver)
123 .getScreenshotAs(OutputType.FILE);
124 SimpleDateFormat dt1 = new SimpleDateFormat("yyyy-MM-dd");
125 Date today = Calendar.getInstance().getTime();
126 String screenShotName = "ScreenShots/" + dt1.format(today) + "/"
127 + testName + ".png";
128 FileUtils.copyFile(file, new File(screenShotName));
129 }
130
131 protected void assertIsInFileListView() throws InterruptedException {
132 assertTrue(waitForTextPresent("ownCloud", (AndroidElement) driver
133 .findElementByAndroidUIAutomator("new UiSelector()"
134 + ".resourceId(\"android:id/action_bar_title\")")));
135 assertTrue(isElementPresent((AndroidElement) driver
136 .findElementByAndroidUIAutomator("new UiSelector()"
137 + ".description(\"Upload\")")));
138 }
139
140 protected void assertIsNotInFileListView() throws InterruptedException {
141 AndroidElement fileElement;
142 assertTrue(waitForTextPresent("ownCloud", (AndroidElement) driver
143 .findElementByAndroidUIAutomator("new UiSelector()"
144 + ".resourceId(\"android:id/action_bar_title\")")));
145 try {
146 fileElement = (AndroidElement) driver
147 .findElementByAndroidUIAutomator("new UiSelector()"
148 + ".description(\"Upload\")");
149 } catch (NoSuchElementException e) {
150 fileElement = null;
151 }
152 assertNull(fileElement);
153 }
154
155 protected void assertIsPasscodeRequestView() throws InterruptedException {
156 assertTrue(waitForTextPresent("ownCloud", (AndroidElement) driver
157 .findElementByAndroidUIAutomator("new UiSelector()"
158 + ".resourceId(\"android:id/action_bar_title\")")));
159 assertTrue(((AndroidElement) driver.findElementByAndroidUIAutomator(
160 "new UiSelector().text(\"Please, insert your pass code\")"))
161 .isDisplayed());
162
163 }
164
165 protected void assertIsInSettingsView() throws InterruptedException {
166 assertTrue(waitForTextPresent("Settings", (AndroidElement) driver
167 .findElementByAndroidUIAutomator("new UiSelector()"
168 + ".resourceId(\"android:id/action_bar_title\")")));
169 }
170
171 }